Cod sursa(job #2719381)

Utilizator tomaionutIDorando tomaionut Data 9 martie 2021 20:05:13
Problema Buline Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("buline.in");
ofstream fout("buline.out");
int n, a[200005],s,smax,smin,stmin,stmax,drmin,drmax,stotal;
int main()
{   
    int i,x, semn,st;
    fin >> n;
    for (i = 1; i <= n; i++)
    {
        fin >> x >> semn;
        if (semn == 0) a[i] = -x;
        else a[i] = x;
        stotal += a[i];
    }
    st = 1;
    s = smax = a[1];
    if (smax < 0) { smax = 0; st = 2; }
    for (i = 2; i <= n; i++)
    {
        s += a[i];
        if (s > smax)
        {
            smax = s;
            stmax = st;
            drmax = i;
        }
        if (s < 0)
        {
            s = 0;
            st = i + 1;
        }
    }

    s = smin = a[1];
    if (smin > 0) { smin = 0; st = 2; }
    for (i = 2; i <= n; i++)
    {
        s += a[i];
        if (s < smin)
        {
            smin = s;
            stmin = st;
            drmin = i;
        }
        if (s > 0)
        {
            s = 0;
            st = i + 1;
        }
    }
    
    stotal -= smin;
    if (stotal > smax)
    {
        fout << stotal << " " << drmin + 1 << " " << n - drmin + stmin - 1;
    }
    else if (stotal <= smax)
    {
        fout << smax << " " << stmax << " " << drmax - stmax + 1;
    }
   
    return 0;
}