Cod sursa(job #2651241)

Utilizator tudorbuhniaTudor Buhnia tudorbuhnia Data 21 septembrie 2020 19:56:34
Problema Carnati Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>
using namespace std;
int v[2005],a[2005],capat=0;
int maxSubArraySum(int sum,int c)
{
    int max_so_far = -2*c, max_ending_here = 0;
    for (int i = 0; i <=capat; i++)
    {
        max_ending_here-=c;
        if(a[i]>=sum)
            max_ending_here = max_ending_here + sum;
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;

        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}
int main()
{
    ifstream cin("carnati.in");
    ofstream cout("carnati.out");
    int n,c,x,y,output=0;
    cin >> n >> c;
    for(int i=0;i<n;i++)
    {
        cin >> x >> y;
        v[i]=y;
        a[x]=y;
        capat=max(capat,x);
    }
    for(int i=0;i<n;i++)
        output=max(output,maxSubArraySum(v[i],c));
    cout << output;
    return 0;
}