Cod sursa(job #997581)

Utilizator poptibiPop Tiberiu poptibi Data 14 septembrie 2013 16:16:08
Problema Lapte Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.41 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int NMAX = 110;

int N, L, A[NMAX], B[NMAX], DP[NMAX][NMAX], P[NMAX][NMAX];

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

    scanf("%i %i", &N, &L);
    for(int i = 1; i <= N; ++ i)
        scanf("%i %i", &A[i], &B[i]);


    for(int T = 0; T <= 100; ++ T)
    {
        memset(DP, -1, sizeof(DP));
        DP[0][0] = 0;

        for(int i = 1; i <= N; ++ i)
            for(int j = 0; j <= L; ++ j)
                for(int k = 0; k <= j; ++ k)
                {
                    if(k * A[i] > T) break;

                    if(DP[i - 1][j - k] != -1 && DP[i - 1][j - k] + (T - k * A[i]) / B[i] > DP[i][j])
                    {
                        DP[i][j] = DP[i - 1][j - k] + (T - k * A[i]) / B[i];
                        P[i][j] = k;
                    }
                }

        if(DP[N][L] < L) continue;

        printf("%i\n", T);
        vector<pair<int, int> > Ans;
        for(int i = N; i; -- i)
        {
            Ans.push_back(make_pair(P[i][L], (T - P[i][L] * A[i]) / B[i]));
            L -= P[i][L];
        }

        reverse(Ans.begin(), Ans.end());
        for(int i = 0; i < N; ++ i)
            printf("%i %i\n", Ans[i].first, Ans[i].second);

        break;
    }

    return 0;
}