Cod sursa(job #2404020)

Utilizator popashtefan10Popa Stefan popashtefan10 Data 12 aprilie 2019 11:01:22
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.71 kb
#include <iostream>
#include <cstdio>

using namespace std;

int main() {
  FILE *fin = fopen("ssm.in", "r");
  FILE *fout = fopen("ssm.out", "w");
  int n, i, x, maxim = -1, s = 0, st, dr, stmax, drmax;

  fscanf(fin, "%d", &n);
  st = dr = 0;
  stmax = drmax = 0;
  for(i = 0; i < n; i++) {
    fscanf(fin, "%d", &x);
    s += x;
    if(s > maxim) {
      maxim = s;
      dr = i;
      stmax = st;
      drmax = dr;
    }
    else if(x > maxim) {
      s = x;
      maxim = x;
      st = i;
      dr = i;
      stmax = drmax = i;
    }
    else if(s < 0) {
      s = 0;
      st = i + 1;
      dr = i + 1;
    }
  }
  fprintf(fout, "%d %d %d", maxim, stmax + 1, drmax + 1);

  return 0;
}