Pagini recente » Cod sursa (job #2809810) | Cod sursa (job #2558827) | Cod sursa (job #2036522) | Cod sursa (job #1689135) | Cod sursa (job #2806975)
#include <iostream>
using namespace std;
int main() {
int n, value, max_sum = 0, current_sum = 0, start_pos = 0, end_pos = 0;
// current_sum - stores the maximum value between the current value and the sum of previously computed sum and current value
// max_sum - computes the maximum value between 0 and the largest value out of all current sums
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> value;
if (value > current_sum + value) {
start_pos = i;
}
current_sum = max(current_sum + value, value);
if (current_sum > max_sum) {
end_pos = i;
}
max_sum = max(current_sum, max_sum);
}
cout << max_sum << ' ' << start_pos << ' ' << end_pos;
return 0;
}