Cod sursa(job #2360001)

Utilizator popicabogdanPopica Bogdan popicabogdan Data 1 martie 2019 11:21:45
Problema Energii Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>

using namespace std;

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

struct ob
{
    int p, c;
};

ob a[1005];
int n, w, dp[2][10005];

void Citire()
{
    fin >> n >> w;
    for(int i = 1; i <= n; i++)
        fin >> a[i].p >> a[i].c;
}

void Rezolvare()
{
    int i, j, mx, L0, L1;
    L0 = 0;
    L1 = 1;
    for(i = 1; i <= w; i++)
        dp[L0][i] = 1e9;
    for(i = 2; i <= n; i++)
    {
        for(j = 2; j <= w; j++)
            if(j >= a[i].p)
                dp[L1][j] = min(dp[L0][j], dp[L0][j-a[i].p] + a[i].c);
            else dp[L1][j]= min(dp[L0][j], a[i].c);
        L0 = 1 - L0;
        L1 = 1 - L1;
    }
    if(dp[L0][w] == 1000000)
        fout << "-1\n";
    else fout << dp[L0][w] << "\n";
}

int main()
{
    Citire();
    Rezolvare();
    return 0;
}