Pagini recente » Cod sursa (job #209844) | Rating Popoviciu Diana (didiia) | Cod sursa (job #217968) | Cod sursa (job #571475) | Cod sursa (job #682713)
Cod sursa(job #682713)
#include <iostream>
#include <fstream>
#include <limits.h>
using namespace std;
ifstream f("ssm.in");
ofstream g("ssm.out");
int n, v[6000005], best[6000005], lung[6000005];
void citire() {
int i;
f>>n;
for(i=1; i<=n; i++) f>>v[i];
}
void rezolva() {
int i,j;
best[1]=v[1];
lung[1]=1;
for(i=2; i<=n; i++) {
if(best[i-1]>0) { best[i]=best[i-1]+v[i]; }
else { best[i]=v[i]; }
}
int max=-INT_MAX;
//secventa se termina unde ii maximu, incepe unde ii primul best negativ dinainte maximului
for(i=1; i<=n; i++) if(best[i]>max) max=best[i];
i=n;
int start, stop;
int startfin=1, stopfin=n;
while(i>=1) {
if(best[i]==max) {
stop=i;
j=i-1;
while(best[j]>=0 && j>=1) {
j--;
}
start=j+1;
if(stop-start<stopfin-startfin) { stopfin=stop; startfin=start; }
if(stop-start==stopfin-startfin && start<startfin) { startfin=start; stopfin=stop; }
}
i--;
}
g<<max<<" "<<startfin<<" "<<stopfin<<"\n";
// for(i=1; i<=n; i++) g<<best[i]<<" "; g<<"\n";
}
int main() {
citire();
rezolva();
f.close();
g.close();
return 0;
}