Pagini recente » Cod sursa (job #397978) | Cod sursa (job #340334) | Cod sursa (job #2548987) | Cod sursa (job #690547) | Cod sursa (job #1000640)
#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;
int bestStartIndex=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;
}
}
bestStartIndex = bestEndIndex;
int i=bestEndIndex;
int tempSum = bestSum;
while(i >= 0)
{
tempSum -= a[i];
if(tempSum==0)
{
if(i<bestStartIndex)
{
bestStartIndex=i;
}
}
i--;
}
fout << bestSum << " " << bestStartIndex+1 << " " << bestEndIndex+1 << "\n";
return 0;
}