Pagini recente » Cod sursa (job #2639346) | Cod sursa (job #941542) | Cod sursa (job #1107452) | Cod sursa (job #2282863) | Cod sursa (job #2371693)
#include <fstream>
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++;
max_negatives=max(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";
}