Cod sursa(job #2333080)

Utilizator hristacheruxiRuxandra Hristache hristacheruxi Data 31 ianuarie 2019 18:00:28
Problema Carnati Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <stdio.h>
#include <math.h>
#include <algorithm>
#define N 2001
struct carnat
{
    int t, p;
}v[N];

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

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

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

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

    s = 0;
    smax = -INFINITY;
    for (i = 1; i <= n; i++)
    {
        if (s < 0)
            s = 0;
        else
        {
            if (s > smax)
                smax = s;
            s = s + v[i].p + (v[i].t - v[i - 1].t) * c;
        }
    }

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