Cod sursa(job #2733265)

Utilizator TudorNicorescuNicorescu Tudor TudorNicorescu Data 30 martie 2021 10:26:33
Problema Buline Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("buline.in");
ofstream fout("buline.out");
vector <int> v;
vector <int> sp;
vector <int> p;
int main()
{
    int n, i, x, semn, total = 0, st, stmax, drmax, s, smax, stmin, drmin, smin;
    fin >> n;
    for(i = 1; i <= n; i++)
    {
        fin >> x >> semn;
        if(semn == 1)
        {
            v.push_back(x);
        }
        else
        {
            v.push_back(x * (-1));
        }
        total += v[i-1];
    }
    s = smin = v[0];
    st = stmin = drmin = 0;
    for(i = 1; i < n; i++)
    {
        if(s + v[i] > v[i])
        {
            s = v[i];
            st =i;
        }
        else s += v[i];
        if(smin > s)
        {
            smin = s;
            stmin = st;
            drmin = i;
        }
    }
    s = smax = v[0];
    st = stmax = drmax = 0;
    for(i = 1; i < n; i++)
    {
        if(s+v[i]<v[i])
        {
            s = v[i];
            st = i;
        }
        else s += v[i];
        if(smax < s and i - st < n)
        {
            smax = s;
            stmax = st;
            drmax = i;
        }
    }
    if(smax > total - smin)
        fout << smax << " " << stmax + 1 << " " << drmax - stmax + 1 << '\n';
    else
        fout << total - smin << " " << drmin + 2 << " " << n - (drmin - stmin + 1);
    return 0;
}