Pagini recente » Cod sursa (job #1807783) | Cod sursa (job #1956794) | Cod sursa (job #2560486) | tema | Cod sursa (job #1000635)
#include <iostream>
#include <fstream>
using namespace std;
const int maxLength = 6000010;
int a[maxLength];
int best[maxLength];
int main()
{
ifstream fin("ssm.in");
ofstream fout("ssm.out");
if (!fin)
{
cout << "There was a problem opening file";
return 0;
}
int arrLength=0;
int counter=0;
fin >> arrLength;
for(int i=0; i < arrLength; i++)
{
fin>>a[counter] ;
counter++;
}
int bestSum = a[0];
int bestEndIndex=0;
for (int i = 0; i < arrLength; ++ i)
{
best[i] = a[i];
if (best[i] < best[i-1] + a[i])
best[i] = best[i-1] + a[i];
if (bestSum < best[i])
{
bestSum = best[i];
bestEndIndex = i;
}
}
int i=bestEndIndex;
int tempSum = bestSum;
while(tempSum >= 0 && i >= 0){
tempSum -= a[i];
i--;
}
if(bestSum >= 0){
++i;
}
fout << bestSum << " " << i+1 << " " << bestEndIndex+1 << "\n";
return 0;
}