Cod sursa(job #2078033)

Utilizator dragos.galeteanu2001Dragos Iulian dragos.galeteanu2001 Data 28 noiembrie 2017 20:18:27
Problema Buline Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <fstream>

using namespace std;

ifstream f("buline.in");
ofstream g("buline.out");

int main()
{
    long int x, y, n, S_max, Suma, P, poz, i, j, L;
    f>>n;
    long int S[2*n], v[2*n];

    for (i=0;i<n;i++) { f>>x>>y;
        if (!y) { v[i]=-x; v[i+n]=-x; }
        else { v[i]=x; v[i+n]=x; }
    }

    S[0]=v[0];
    for (S_max=Suma=v[0], x=P=poz=0, i=L=1; i<2*n-1; i++) {
        if (Suma<0 || x==n) { Suma=v[i]; poz=i; x=1; }
        else { Suma+=v[i]; x++;
            if (S_max<Suma) { S_max=Suma; P=poz; L=i-poz+1; }
        }
        S[i]=Suma;
    }

    g<<S_max<<' '<<(P+1)<<' '<<L;

    f.close();
    g.close();
    return 0;
}