Cod sursa(job #1727882)

Utilizator moise_alexandruMoise Alexandru moise_alexandru Data 11 iulie 2016 20:13:00
Problema Carnati Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 2005;
pair <int, int> v[maxn];
int n, c;
char T[35];
int poz = 0;
void citeste(int &numar)
{
     numar = 0;
     while (T[poz] < '0' || T[poz] > '9')
     {
          if (++poz == 30)
          {
               fread(T, 1, 30, stdin);
               poz = 0;
          }
     }
     while ('0' <= T[poz] && T[poz] <= '9')
     {
          numar = numar * 10 + T[poz] - '0';
          if (++poz == 30)
          {
               fread(T, 1, 30, stdin);
               poz=0;
          }
     }
}
int profit(int pret)
{
    int ret = 0;
    int s = 0;
    for(int i = 1; i <= n; i++)
    {
        int salariu = (v[i].first - v[i-1].first) * c;
        s = s - salariu;
        s = max(s, 0);
        if(pret <= v[i].second)
            s += pret;
        ret = max(ret, s - c);
    }
    return ret;
}
int main()
{
    freopen("carnati.in", "r", stdin);
    freopen("carnati.out", "w", stdout);
    citeste(n);
    citeste(c);
    for(int i = 1; i <= n; i++)
    {
        citeste(v[i].first);
        citeste(v[i].second);
    }
    sort(v + 1, v + n + 1);
    int mx = 0;
    for(int i = 1; i <= n; i++)
        mx = max(mx, profit(v[i].second));
    printf("%d\n", mx);
    return 0;
}