Cod sursa(job #3323564)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 18 noiembrie 2025 18:33:04
Problema Lapte Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.86 kb
#include <fstream>

#define ll long long
#define cin fin
#define cout fout

using namespace std;

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

const int NMAX = 100;

int n, l;
pair<int, int> a[NMAX + 1];
int dp[NMAX + 1][NMAX + 1];
pair<int, int> secv[NMAX + 1];
pair<int, int> parent[NMAX + 1][NMAX + 1];

bool check(int t) {
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= l; j++) {
            dp[i][j] = -1;
        }
    }
    dp[0][0] = 0;

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= l; j++) {
            if (dp[i - 1][j] == -1) {
                continue;
            }
            for (int a_here = 0; a[i].first * a_here <= t; a_here++) {
                int next_j = min(l, j + a_here);
                int rem_b = (t - a[i].first * a_here) / a[i].second;
                int cost_here = dp[i - 1][j] + rem_b;
                if (cost_here > dp[i][next_j]) {
                    dp[i][next_j] = cost_here;
                    parent[i][next_j] = { j, a_here };
                }
            }
        }
    }
    return dp[n][l] >= l;
}

int main() {
    cin >> n >> l;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].first >> a[i].second;
    }

    int left = 1, right = NMAX, answer = -1;
    while (left <= right) {
        int mid = (left + right) / 2;
        if (check(mid)) {
            answer = mid;
            right = mid - 1;
        }
        else {
            left = mid + 1;
        }
    }
    check(answer);

    cout << answer << '\n';
    for (int i = n; i >= 1; i--) {
        auto [last_j, a_here] = parent[i][l];
        secv[i] = { a_here, (answer - a[i].first * a_here) / a[i].second };
        l -= a_here;
    }
    
    for (int i = 1; i <= n; i++) {
        cout << secv[i].first << ' ' << secv[i].second << '\n';
    }
    return 0;
}