Cod sursa(job #2336029)

Utilizator ezioconnorVlad - Gabriel Iftimescu ezioconnor Data 4 februarie 2019 18:43:21
Problema Buline Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <climits>

using namespace std;

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

long long v[400002];

int main()
{
    int n, a, b, poz, st, dr;
    long long s = 0, smax = LLONG_MIN;
    in >> n;
    for (int i = 1; i <= n; ++i)
    {
        in >> a >> b;
        if (b == 0)
        {
            v[i + n] = -1 * a;
            v[i] = -1 * a;
        }
        else
        {
            v[i + n] = a;
            v[i] = a;
        }
    }
    for (int i = 1; i <= 2 * n - 1; ++i)
    {
        if (s < 0)
        {
            s = v[i];
            poz = i;
        }
        else
            s += v[i];
        if (s > smax && i - poz + 1 <= n)
        {
            smax = s;
            st = poz;
            dr = i;
        }
    }
    out << smax << " " << st << " " << dr - st + 1;
    return 0;
}