Pagini recente » Cod sursa (job #1219064) | Cod sursa (job #1850314) | Cod sursa (job #2472391) | Cod sursa (job #1303003) | Cod sursa (job #1364980)
#include <fstream>
#include <climits>
using namespace std;
ifstream fin("ssm.in");
ofstream fout("ssm.out");
int n, a[6000004];
int smax = -INT_MAX, imax, jmax;
int s, i, j;
int main()
{
fin >> n;
for ( i = 1; i <= n; i++ )
{
fin >> a[i];
if ( a[i] > smax ) smax = a[i], imax = jmax = i;
}
// for ( int i = 1; i <= n; i++ )
// fout << a[i] << ' ';
//fout << '\n';
i = 1;
for ( int j = 1; j <= n; j++ )
{
if ( s + a[j] <= 0 )
{
s = 0, i = j + 1;
}
else
{
s += a[j];
if ( smax < s )
{
smax = s;
imax = i;
jmax = j;
}
else
if ( smax == s && imax > i )
{
imax = i;
jmax = j;
}
else
if ( smax == s && imax == i && jmax > j )
jmax = j;
}
}
fout << smax << ' ' << imax << ' ' << jmax;
fin.close();
fout.close();
return 0;
}