Pagini recente » Cod sursa (job #361473) | Cod sursa (job #1575957) | Cod sursa (job #3231851) | Cod sursa (job #3134868) | Cod sursa (job #2563986)
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{
ifstream fin ("ssm.in");
ofstream fout ("ssm.out");
int n, bestSum = -2000000000, bestLeft = -1, bestRight = -1, currentSum = 0, currentLeft = 1;
fin >> n;
for (int i = 1; i <= n; ++i) {
int currentValue;
fin >> currentValue;
if (currentSum >= 0) {
currentSum += currentValue;
} else {
currentSum = currentValue;
currentLeft = i;
}
if (bestSum < currentSum or bestLeft == -1) {
bestSum = currentSum;
bestLeft = currentLeft;
bestRight = i;
} else if (bestSum == currentSum and currentLeft < bestLeft) {
bestLeft = currentLeft;
bestRight = i;
} else if (bestSum == currentSum and currentLeft == bestLeft and bestRight < i) {
bestRight = i;
}
}
fout << bestSum << ' ' << bestLeft << ' ' << bestRight;
return 0;
}