Cod sursa(job #2651243)

Utilizator tudorbuhniaTudor Buhnia tudorbuhnia Data 21 septembrie 2020 19:59:34
Problema Carnati Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
using namespace std;
int v[2005],a[2005],capat=0,c;
int maxSubArraySum(int sum)
{
    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,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]));
    cout << output;
    return 0;
}