Cod sursa(job #1412129)

Utilizator alexndru.enacheiumei fhd alexndru.enache Data 1 aprilie 2015 09:48:47
Problema Buline Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <cstdio>

using namespace std;
int v[400005];
int main()
{
    freopen("buline.in", "r", stdin);
    freopen("buline.out", "w", stdout);
    int n, i, x, y, lung, st, stm, drm, s, smax;
    scanf("%d", &n);
    for (i = 1; i <= n; i ++){
        scanf("%d%d", &x, &y);
        if (y == 0)
            v[i] = -x;
        else
            v[i] = x;
    }
    for (i = 1; i <= n; i ++)
        v[n + i] = v[i];
    s = smax = v[1];
    st = stm = drm = lung = 1;
    for (i = 2; i <= 2 * n; i ++){
        if (s + v[i] > v[i] && i - st + 1 <= n)
            s += v[i];
        else
            s = v[i], st = i;
        if (s > smax){
            stm = st;
            drm = i;
            smax = s;
            lung = drm - stm + 1;
        }
        else if (s == smax && i - st < drm - stm)
            stm = st, drm = i, lung = drm - stm + 1;
    }
    printf("%d %d %d", smax, stm, lung);
    return 0;
}