Cod sursa(job #1772227)

Utilizator ionutpop118Pop Ioan Cristian ionutpop118 Data 6 octombrie 2016 16:21:26
Problema Energii Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <cstdio>
using namespace std;

const int Dmax = 10000;
int d[Dmax + 5];

int main()
{
    freopen("energii.in", "r", stdin);
    freopen("energii.out", "w", stdout);

    int n, k, ans, s = 0, x, c;

    scanf("%d %d", &n, &k);
    d[0] = 1; ans = 2000000000;
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d %d", &x, &c);
        for (int j = s; j >= 0; --j)
            if (d[j] != 0)
            {
                if (j + x >= k && d[j] + c < ans)
                    ans = d[j] + c;
                else if (d[j] + c > d[j + x] || d[j + x] == 0)
                    d[j + x] = d[j] + c;
            }

        s += x;
        if (s > k - 1)
            s = k - 1;
    }
    printf("%d\n", ans - 1);
    return 0;
}