Cod sursa(job #2704173)

Utilizator isa_edi_cristeaEdi Cristea isa_edi_cristea Data 9 februarie 2021 20:57:16
Problema Carnati Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream fin("carnati.in");
ofstream fout("carnati.out");

struct carnaciori{
    long long int time,price;
}v[2010];

bool cmp(carnaciori a,carnaciori b){
    if(a.time<=b.time)
        return true;
    return false;
}
int main()
{
    long long int i,n,sellerPrice,totalMax=0,currentIncome=0,currentPrice,waitingTime=0,added;
    fin>>n>>sellerPrice;
    for(i=1;i<=n;i++){
        fin>>v[i].time>>v[i].price;
    }
    sort(v+1,v+n+1,cmp);
    for(int k=1;k<=n;k++){
        currentPrice=v[k].price;
        currentIncome=0;
        waitingTime=v[1].time;
        added=0;
        for(i=1;i<=n;i++){
            if(currentPrice<=v[i].price){
                currentIncome+=currentPrice;
                added=1;
            }
            if(currentIncome-(v[i].time-waitingTime)*sellerPrice<=0){
                if(added==1){
                    currentIncome-=currentPrice;
                    added=0;
                }
                if(currentIncome>totalMax)
                    totalMax=currentIncome;
                currentIncome=0;
                waitingTime=v[i+1].time;
            }
            else{
                currentIncome-=(v[i].time-waitingTime)*sellerPrice;
                waitingTime=v[i].time;
            }
            if(currentIncome>totalMax)
                totalMax=currentIncome;
        }
        if(currentIncome>totalMax)
                totalMax=currentIncome;
    }
    if(totalMax-sellerPrice<=0)
        fout<<"0";
    else
        fout<<totalMax-sellerPrice<<endl;
    return 0;
}