Cod sursa(job #2926550)

Utilizator Vlad_NistorNIstor Vlad Vlad_Nistor Data 17 octombrie 2022 23:03:35
Problema Carnati Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <bits/stdc++.h>
using namespace std;

pair<long long int,int > a[2001], dp[2001];
int main(void){
    ofstream cout("carnati.out");
    ifstream cin("carnati.in");
    int n,c;
    cin >> n >> c;
    for(int i = 1;i<=n;i++){
        cin >> a[i].first >> a[i].second;
    }
    long long int maxmax = -1;
    sort(a+1,a+n+1);
    for(int i = 1;i<=n;i++){
        /// vom lua fiecare pret in parte
        long long int max1 = -1;
        for(int j = 1;j<=n;j++){
            dp[j].first = 0, dp[j].second = 0;
        }
        bool ok = 1;
            for(int j = 1;j<=n;j++){
                if(a[j].second >= a[i].second){ /// daca cumparatorul poate cumpara carnatul
                    if(ok){
                        dp[j].first = a[i].second;
                        dp[j].second = a[j].first;
                    }else{
                        if(dp[j-1].first + a[i].second - c * ((a[j].first - dp[j-1].second)+1) > a[i].second - c){
                            dp[j].first = dp[j-1].first + a[i].second;
                            dp[j].second = dp[j-1].second;
                            max1 = max(max1,dp[j].first - c * (a[j].first - dp[j].second + 1));
                        }else{
                            dp[j].first = a[i].second;
                            dp[j].second = a[j].first;
                            max1 = max(max1, dp[j].first - c);
                        }
                    }
                    ok = 0;
                }else{
                    dp[j].first = dp[j-1].first;
                    dp[j].second = dp[j-1].second;

                }
                maxmax = max(maxmax, max1);
            }
    }
    cout << maxmax;
}