Cod sursa(job #2669642)

Utilizator David861Retegan David David861 Data 7 noiembrie 2020 14:06:48
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("ssm.in");
ofstream g("ssm.out");

int n, x, i1, i2, s;
int nmx, smx, imx, i1mx, i2mx;

 int main() {
    f >> n;
    i1 = 1;
    i2 = 1;
    nmx = -200100;
    s = -1;
    
    for (int i = 1; i <= n; i++) {
        f >> x;
        i2 = i;
        if (x > nmx) {
            nmx = x;
            imx = i;
        }
        if (s < 0) {
           s = 0;
           s += x;
           i1 = i;
         if (s > smx) {
             smx = s;
             i1mx = i1;
             i2mx = i2;
           }
       } else {
           s += x;
         if (s > smx) {
             smx = s;
             i1mx = i1;
             i2mx = i2;
          }
        }
      }
    
    if (nmx < 0) {
         g << nmx << " " << imx << " " << imx;
      } else {
         g << smx << " " << i1mx << " " << i2mx;
     }

    return 0;
}