Cod sursa(job #2320498)

Utilizator cristicioteiCiotei Cristian cristiciotei Data 14 ianuarie 2019 20:25:10
Problema Carnati Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n,C;
struct client {
int timp;
int bani;
};
bool cmp (client a , client b)
{
    if (a.timp<b.timp)
        return 1;
    return 0;
}
client c[2001];
long long Profit (int val)
{
    int p=0;
    int P=0;
    int G;
    if (c[1].bani>=val)
        G=val;
    else
        G=0;
    p=G-C;
    for (int i=2 ; i<=n ; i++)
    {
        if (c[i].bani>=val)
            G=val;
        else
            G=0;
        p=max(p+G-C*(c[i].timp-c[i-1].timp),G-C);
        if (p>P)
            P=p;
    }
    return P;
}
long long profit,sol;
int main()
{
    freopen("carnati.in","r",stdin);
    freopen("carnati.out","w",stdout);
    cin>>n>>C;
    for (int i=1 ; i<=n ; i++)
    {
        cin>>c[i].timp>>c[i].bani;
    }
    sort (c+1,c+n+1,cmp);
    for (int i=1 ; i<=n ; i++)
    {
        profit = Profit (c[i].bani);
        if (profit>sol)
            sol=profit;
    }
    cout<<sol;
    return 0;
}