Cod sursa(job #1636809)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 7 martie 2016 12:29:58
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <stdio.h>
#include <algorithm>

using namespace std;

#define ll long long unsigned
#define pb push_back
#define mp make_pair

int EG[1005],CG[1005],dp[10000005];
// dp[i] = pretul minim pt a produce i energie

int main(){
    int G,W,i,j;
    freopen("energii.in", "r", stdin);
    freopen("energii.out", "w", stdout);
    scanf("%d",&G);
    scanf("%d",&W);
    int sum = 0;
    for(i = 1;i <= G;i++){
        scanf("%d %d",&EG[i],&CG[i]);
    }
    for(i = 0;i <= 10000005;i++){
        dp[i] = 1e4;
    }
    for(i = 1;i <= G;i++){
        sum += EG[i];
        for(j = sum;j >= 0;j--){
            dp[j] = min(min(CG[i],dp[j+1]), (dp[j-EG[i]] == 10000 ? 0 : dp[j-EG[i]])+CG[i]);
        }
    }
    if(sum < W){
        printf("-1");
        return 0;
    }
    int mn = 1e7;
    for(i = W;i <= 10000000;i++){
        mn = min(mn, dp[i]);
    }
    printf("%d",mn);
    return 0;
}