Pagini recente » Cod sursa (job #2491950) | Cod sursa (job #2139674) | Cod sursa (job #410359) | Cod sursa (job #410665) | Cod sursa (job #3275262)
#include <bits/stdc++.h>
using namespace std;
ifstream f("ssm.in");
ofstream g("ssm.out");
int main()
{
int sc_i = 0, st = 1, st_max = 1, dr_max = 1, s_max = INT_MIN;
/*
sc_i = suma curentă până la elementul i
st = începutul secvenței curente
st_max, dr_max = începutul și sfârșitul celei mai bune secvențe găsite
s_max = suma maximă găsită, inițializată cu cel mai mic număr posibil (INT_MIN
*/
int n, x;
f >> n;
for(int i = 1; i <= n; i++) {
f >> x;
if (x > sc_i + x) {
sc_i = x;
st = i;
} else
sc_i += x;
if (sc_i > s_max) {
s_max = sc_i;
st_max = st;
dr_max = i;
}
}
g << s_max << " " << st_max << " " << dr_max;
return 0;
}