Cod sursa(job #2655442)
| Utilizator | Data | 4 octombrie 2020 13:41:08 | |
|---|---|---|---|
| Problema | Subsecventa de suma maxima | Scor | 0 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.66 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ssm.in");
ofstream fout("ssm.out");
int main() {
int n, x, s, inc, sf, s_max = 0;
vector<int> v;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
v.push_back(x);
}
/// solutie de complexitate O(n^2)
for (int i = 0; i < n; i++) {
s = 0;
for (int j = i; j < n; j++) {
s += v[j];
if (s > s_max) {
inc = i;
sf = j;
s_max = s;
}
}
}
cout << s_max << ' ' << inc + 1 << ' ' << sf + 1;
}