Pagini recente » Cod sursa (job #976623) | Cod sursa (job #908916) | Cod sursa (job #1092486) | Cod sursa (job #2905985) | Cod sursa (job #1525257)
#include <bits/stdc++.h>
using namespace std;
int a[400009], n;
int main()
{
int i, m, n, s, smax, poz, x, st, dr;
ifstream fin("buline.in");
fin >> n;
for (i = 1; i <= n; ++i)
{
fin >> x >> s;
if (s == 0) a[i] = a[n + i] = -x;
else a[i] = a[n + i] = x;
}
fin.close();
s = smax = a[1];
st = dr = poz = 1;
if (s < 0) {s = 0; poz = 2;}
m = 2 * n;
for (i = 2; i < m; ++i)
{
if (poz <= i - n)
{
s -= a[i];
poz++;
}
s += a[i];
if (s > smax)
{
smax = s;
st = poz; dr = i;
}
if (s < 0)
{
s = 0; poz = i + 1;
}
}
ofstream fout("buline.out");
fout << smax << " " << st << " " << (dr - st + 1) << "\n";
fout.close();
return 0;
}