Cod sursa(job #2343510)

Utilizator hristacheruxiRuxandra Hristache hristacheruxi Data 14 februarie 2019 08:02:14
Problema Carnati Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <bits/stdc++.h>
#define N 2001
using namespace std;
struct carnat
{
    int timp, pret;
} v[N];

bool cmp(carnat a, carnat b)
{
    if(a.timp < b.timp)
        return true;
    else
        return false;
}

int main()
{
    freopen("carnati.in", "r", stdin);
    freopen("carnati.out", "w", stdout);
    int n, c, i, smax, s, x, j, suma_final;

    scanf("%d%d", &n, &c);
    for (i = 1; i <= n; i++)
        scanf("%d%d", &v[i].timp, &v[i].pret);

    std::sort(v + 1, v + n + 1, cmp);

    smax = -1;
    v[0] = v[1];
    for (i = 1; i <= n; i++)
    {
        suma_final = -INFINITY;
        s = 0;
        for (j = 1; j <= n; j++)
        {
            if (v[i].pret > v[j].pret)
                x = 0;
            else
                x = v[i].pret;

            s = max((s - (v[j].timp - v[j - 1].timp) * c), 0) + x;
            suma_final = max(suma_final, s - c);
            smax = max(suma_final, smax);
        }

    }
    printf("%d", smax);
    return 0;
}