Cod sursa(job #2614127)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 11 mai 2020 11:51:01
Problema Energii Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.72 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cmath>

using namespace std;

const int INF = 2e9;
const int N = 1e3;
const int W = 5e3;

int dp[5 + W];

int main() {
    freopen("energii.in", "r", stdin);
    freopen("energii.out", "w", stdout);
    int n, w;
    scanf("%d%d", &n, &w);

    for(int i = 1; i <= n; i++) {
        int p, g;
        scanf("%d%d", &g, &p);


        for(int j = W; j >= 0; j--) {
            if(dp[j]) {
                if(dp[min(w, j + g)] > 0)
                    dp[min(w, j + g)] = min(dp[min(w, j + g)], dp[j] + p);
                else dp[min(w, j + g)] = dp[j] + p;
            }
        }

        if(dp[min(w, g)] > 0)
            dp[min(w, g)] = min(dp[min(w, g)], p);
        else dp[min(w, g)] = p;
    }

    if(dp[w]) printf("%d\n", dp[w]);
    else printf("-1\n");
    return 0;
}