Cod sursa(job #2661666)

Utilizator martinmiere133Cranga Antonio martinmiere133 Data 22 octombrie 2020 15:14:36
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.64 kb
#include <iostream>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
ifstream f("energii.in");
ofstream g("energii.out");
int n , gmax , dp[50001] , gc , vc;
int main() {
    f>>n>>gmax;
    int mmax =0 ;
    for(int i=1;i<=n;i++)
    {
        f>>gc>>vc;
        mmax = max(mmax, vc);
        for(int j= gmax ; j>=gc ; j--)
        {dp[j] = max(dp[j] , dp[j-gc] + vc);
        }
    }
    auto it = upper_bound(dp+1, dp+vc,gmax);
    if(*it < gmax)g<<-1;
    else g<<*it;
    return 0;
}