Cod sursa(job #2607486)

Utilizator avtobusAvtobus avtobus Data 29 aprilie 2020 20:52:13
Problema Subsecventa de suma maxima Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <stdio.h>
#include <bits/stdc++.h>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int INF = 0x3f3f3f3f;

ifstream fin ("ssm.in");
ofstream fout ("ssm.out");

int main(void) {
  // freopen("ssm.in", "r", stdin);
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  int n, a, smx = -INF, lmx = -INF, rmx = -INF, s = -INF, l = -1;
  // s is the largest sum of a subsequence that ends in i.
  // l is the corresponding start position. (0-based)s

  fin >> n;
  rep(i, n) {
    fin >> a;
    s += a;
    if (s < a) {
      s = a;
      l = i;
    }
    if (s > smx) {
      smx = s;
      lmx = l;
      rmx = i;
    }
  }
  fout << smx << " " << lmx+1 << " " << rmx+1 << endl;

  return 0;
}