Cod sursa(job #2650641)

Utilizator Darius1414Dobre Darius Adrian Darius1414 Data 19 septembrie 2020 16:28:46
Problema Carnati Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#define nmax 2005
using namespace std;
pair <int,int> v[nmax];
int n,c,i,j,s,r,q,st,mx;
int main()
{
    ifstream f ("carnati.in");
    ofstream g ("carnati.out");
    f>>n>>c;
    for (i=1;i<=n;i++) f>>v[i].first>>v[i].second;
    sort (v+1,v+n+1);
    for (j=1;j<=n;j++)
    {
        r=0;
        q=0;
        s=v[j].second;
        for (i=1;i<=n;i++) {
            if (v[i].second>=s) {
                if (q==0) {
                    r+=s;
                    st=v[i].first;
                    q=1;
                }
                else {
                    r+=s;

                    if (r<=0) {
                        q=0;
                        r=0;
                    }
                }
                int r_val = r - (v[i].first - st + 1) * c;
                if (r_val > mx)
                    mx = r_val;
            }
        }
    }
    g<<mx;
}