Pagini recente » Cod sursa (job #263462) | Cod sursa (job #1958157) | Cod sursa (job #709915) | Cod sursa (job #2568144) | Cod sursa (job #2719381)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("buline.in");
ofstream fout("buline.out");
int n, a[200005],s,smax,smin,stmin,stmax,drmin,drmax,stotal;
int main()
{
int i,x, semn,st;
fin >> n;
for (i = 1; i <= n; i++)
{
fin >> x >> semn;
if (semn == 0) a[i] = -x;
else a[i] = x;
stotal += a[i];
}
st = 1;
s = smax = a[1];
if (smax < 0) { smax = 0; st = 2; }
for (i = 2; i <= n; i++)
{
s += a[i];
if (s > smax)
{
smax = s;
stmax = st;
drmax = i;
}
if (s < 0)
{
s = 0;
st = i + 1;
}
}
s = smin = a[1];
if (smin > 0) { smin = 0; st = 2; }
for (i = 2; i <= n; i++)
{
s += a[i];
if (s < smin)
{
smin = s;
stmin = st;
drmin = i;
}
if (s > 0)
{
s = 0;
st = i + 1;
}
}
stotal -= smin;
if (stotal > smax)
{
fout << stotal << " " << drmin + 1 << " " << n - drmin + stmin - 1;
}
else if (stotal <= smax)
{
fout << smax << " " << stmax << " " << drmax - stmax + 1;
}
return 0;
}