Cod sursa(job #2689736)

Utilizator YusyBossFares Yusuf YusyBoss Data 21 decembrie 2020 23:15:03
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <stdio.h>

using namespace std;

int main() {
  FILE *fin, *fout;
  int n, i, last, left, x, smax, leftmax, rightmax;

  fin = fopen("ssm.in", "r");
  fscanf(fin, "%d", &n);

  fscanf(fin, "%d", &x);
  last = x;
  left = leftmax = rightmax = 0;
  smax = x;

  for (i = 1; i < n; i++) {
    fscanf(fin, "%d", &x);
    if (last >= 0)
      last += x;
    else {
      last = x;
      left = i;
    }

    if (last > smax) {
      smax = last;
      leftmax = left;
      rightmax = i;
    }
  }
  fclose( fin );

  fout = fopen("ssm.out", "w");
  fprintf(fout, "%d %d %d", smax, leftmax + 1, rightmax + 1);
  fclose( fout );
  return 0;
}