Cod sursa(job #3234816)

Utilizator iusty64Iustin Epanu iusty64 Data 11 iunie 2024 20:36:13
Problema Lapte Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <bits/stdc++.h>
#include <climits>

using namespace std;

const int Vmax = 101;

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

int dp[Vmax][Vmax], A[Vmax], B[Vmax];
int bestX[Vmax][Vmax];
int n, l;

int calcul(int T){
	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= l; j++){
			dp[i][j] = INT_MIN;
			bestX[i][j] = 0;
		}
	}
	dp[0][0] = 0;
	for(int i = 1; i <= n; i++){
		for(int j = 0; j <= l; j++){
			for(int x = 0; x <= min(j, T / A[i]); x++){
				int y = (T - x * A[i]) / B[i];
				if(dp[i - 1][j - x] + y > dp[i][j]){
					dp[i][j] = dp[i - 1][j - x] + y;
					bestX[i][j] = x;
				}
			}
		}
	}
	return dp[n][l] >= l;
}

void reconst(int i, int j){
	if(i == 0)
		return;
	int x = bestX[i][j];
	int y = dp[i][j] - dp[i - 1][j - x];
	reconst(i - 1, j - x);
	fout << x << " " << y << '\n'; 
}

int main(){
	fin >> n >> l;
	for(int i = 1; i <= n; i++){
		fin >> A[i] >> B[i];
	}
	int st = 0;
	int dr = 20000;
	int finalSol = 0;
	while(st <= dr){
		int mid = (st + dr) / 2;
		bool maiBun = calcul(mid);
		if(maiBun){
			finalSol = mid;
			dr = mid - 1;
		}
		else{
			st = mid + 1;
		}
	}
	fout << finalSol << '\n';
	calcul(finalSol); // mai calculam odata finalSol fiindca e posibil sa se fi stricat matricea bestX
	reconst(n, l);
	return 0;
}