Cod sursa(job #2430536)

Utilizator pickleVictor Andrei pickle Data 15 iunie 2019 12:24:44
Problema Subsecventa de suma maxima Scor 85
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <stdio.h>
#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < n; i++)
#define repa(i, l, r) for (int i = l; i < r; i++)
#define repd(i, r, l) for (int i = r; i > l; i--)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

ifstream fin ("ssm.in");
ofstream fout ("ssm.out");
const int Nmax = 6000666;
int N, a[Nmax];

int main(void) {
  fin >> N;
  rep(i,N) {
    fin >> a[i];
  }
  int pre = 0, l = -1, cur = 0, r, mx = a[0];
  rep(i,N) {
    cur += a[i];
    if (pre > cur) { // minimum prefix of the sequence
      pre = cur;
      l = i;
    }
    if (cur - pre > mx) {
      mx = cur - pre;
      r = i;
    }
  }
  fout << mx << ' ' << (l+2) << ' ' << r+1 << '\n';

  return 0;
}