Pagini recente » Cod sursa (job #3344782) | Cod sursa (job #3265370) | Cod sursa (job #1446091) | Cod sursa (job #2551490) | Cod sursa (job #3311865)
#include <bits/stdc++.h>
using namespace std;
ifstream f("buline.in");
ofstream g("buline.out");
int n, s1, l1, p1, s2, l2, p2, x, y, a[200005], sum = 0;
void subs_max (int& s1, int& l1, int& p1) {
s1 = INT_MIN; l1 = 1; p1 = 1;
int l = 1, p = 1;
int s = a[1];
for (int i=2; i<=n; ++i) {
if (a[i] > s + a[i]) {
s = a[i];
p = i; l = 1;
if (s > s1) {
s1 = s; p1 = p; l1 = l;
}
}
else {
s += a[i]; l++;
if (s > s1) {
s1 = s; p1 = p; l1 = l;
}
}
}
}
void subs_min (int& s2, int& l2, int& p2) {
s2 = INT_MAX; l2 = 1; p2 = 1;
int l = 1, p = 1;
int s = a[1];
for (int i=2; i<=n; ++i) {
if (a[i] < s + a[i]) {
s = a[i];
p = i; l = 1;
if (s < s2) {
s2 = s; l2 = l; p2 = p;
}
}
else {
s += a[i]; l++;
if (s < s2) {
s2 = s; l2 = l; p2 = p;
}
}
}
}
int main()
{
f >> n;
for (int i=1; i<=n; ++i) {
f >> x >> y;
if (y)
a[i] = x;
else a[i] = -x;
sum += a[i];
}
subs_max (s1, l1, p1);
subs_min (s2, l2, p2);
if (s1 >= sum - s2)
g << s1 << ' ' << p1 << ' ' << l1;
else
g << sum - s2 << ' ' << p2 + 1 << ' ' << n - l2;
return 0;
}