Pagini recente » Cod sursa (job #2581481) | Cod sursa (job #2705485) | Cod sursa (job #1116323) | Cod sursa (job #2536361) | Cod sursa (job #2371713)
#include <fstream>
#include <climits>
using namespace std;
ifstream in("ssm.in");
ofstream out("ssm.out");
int n,i,x,max_so_far,max_ending_here,inceput,inceput2,sfarsit,max_negatives=INT_MIN,poz,negative;
int main()
{
in>>n;
for (i=1; i<=n; ++i)
{
in>>x;
if (x<0)
{
negative++;
if (x>max_negatives)
{
max_negatives=x;
poz=i;
}
}
max_ending_here+=x;
if (max_ending_here<=0)
{
max_ending_here=0;
inceput=i+1;
}
if (max_so_far<max_ending_here)
{
max_so_far=max_ending_here;
inceput2=inceput;
sfarsit=i;
}
else if (max_so_far==max_ending_here)
{
if (i-inceput+1<sfarsit-inceput2+1)
{
inceput2=inceput;
sfarsit=i;
}
}
}
if (negative==n) out<<max_negatives<<" "<<poz<<" "<<poz<<"\n";
else out<<max_so_far<<" "<<inceput2<<" "<<sfarsit<<"\n";
}