Cod sursa(job #1895367)

Utilizator jason2013Andronache Riccardo jason2013 Data 27 februarie 2017 22:05:04
Problema Energii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include<bits/stdc++.h>
#define oo 0x3f3f3f3f
using namespace std;

int n, w, D[5001][5001], Val[1001], Cost[1001], st;

int main()
{
    ifstream f("energii.in");
    ofstream g("energii.out");

    f >> n >> w;
    for(int i = 1; i <= n; i++)
        f >> Cost[i] >> Val[i];

    //border
    for(int i = 0; i <= n; i ++)
        D[i][0] = oo;
    for(int i = 0; i <= w; i ++)
        D[0][i] = oo;

    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= w; j ++)
        {
            if( Cost[j] <= j )
            {
                if( D[i-1][j - Cost[i]] == oo ){
                    D[i][j] = min( D[i-1][j], Val[j] );
                    st += D[i][j];
                }
                else{
                        D[i][j] = min(D[i-1][j], D[i-1][j - Cost[i]] + Val[j]);
                        st += D[i][j];
                    }
            }else D[i][j] = D[i-1][j];
        }
    //printf(out, "%d", D[n][w]);

    if( st >= w ) g << st;
    else g << -1;

    return 0;
}