Cod sursa(job #2047388)

Utilizator TherevengerkingSurani Adrian Therevengerking Data 24 octombrie 2017 19:54:40
Problema Energii Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("energii.in");
ofstream fout("energii.out");
#define pii pair<int, int>
#define x first
#define y second
const int Nmax = 1000 + 5;
const int Dmax = 10001 + 5001 + 5;
int n, pg, p, c, ans;
int dp[Dmax];
pii a[Nmax];
bool cmp(pii a1, pii a2)
{
    if(a1.x == a2.x)return a1.y < a2.y;
    return a1.x < a2.x;
}
int main()
{
    fin >> n >> pg;
    dp[0] = 0; ans = 1 << 30;
    for(int i = 1; i <= pg + 10002; ++i)
        dp[i] = 1 << 30;
    for(int i = 1; i <= n; ++i)
        fin >> a[i].x >> a[i].y;
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1; i <= n; ++i)
    {
        p = a[i].x; c = a[i].y;
        for(int j = 0; j < pg; ++j)
            if(dp[j] != 1 << 30)
            {
                dp[j + p] = min(dp[j + p], dp[j] + c);
                if(j + p >= pg)
                    ans = min(dp[j + p], ans);
            }
    }
    if(ans == 1 << 30)ans = -1;
    fout << ans;
    return 0;
}