Cod sursa(job #2514075)

Utilizator flaviu_2001Craciun Ioan-Flaviu flaviu_2001 Data 24 decembrie 2019 14:45:03
Problema Energii Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <bits/stdc++.h>
#define ff first
#define ss second

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;

const string file = "energii";
const ll INF = 9223372036854775807ll;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, inf = 2147483647-20005, nmax = 10005;

int n, G, pd[2][nmax];

int main()
{
    ifstream fin (file+".in");
    ofstream fout (file+".out");
    fin >> n >> G;
    for (int i = 0; i < nmax; ++i)
        pd[0][i] = inf;
    pd[0][0] = 0;
    for (int i = 1; i <= n; ++i){
        int x, c;
        fin >> x >> c;
        x = min(x, G);
        for (int j = 0; j <= G; ++j){
            pd[i%2][j] = pd[1-(i%2)][j];
            if (x <= j)
                pd[i%2][j] = min(pd[i%2][j], pd[1-(i%2)][j-x]+c);
            else pd[i%2][j] = min(pd[i%2][j], pd[1-(i%2)][0]+c);
        }
    }
    if (pd[n%2][G] != pd[n%2][G])
        fout << pd[n%2][G] << "\n";
    else fout << "-1\n";
    return 0;
}