Cod sursa(job #3296810)

Utilizator tudorhTudor Horobeanu tudorh Data 17 mai 2025 12:50:45
Problema Lapte Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("lapte.in");
ofstream fout("lapte.out");
pair<int,int>v[101];
int dp[101][101],n,l;
vector<pair<int,int>>rr;
bool check(int timp)
{
    for(int i=0;i<=n;i++)
        fill(dp[i],dp[i]+l+1,-1);
    dp[0][0]=0;
    for(int p=1;p<=n;p++)
    {
        for(int i1=0;i1*v[p].first<=timp;i1++)
        {
            int i2=(timp-i1*v[p].first)/v[p].second;
            for(int i=i1;i<=l;i++)
            {
                if(dp[p-1][i-i1]!=-1)
                    dp[p][i]=max(dp[p][i],dp[p-1][i-i1]+i2);
            }
        }
    }
    return dp[n][l]>=l;
}
int main()
{
    fin>>n>>l;
    for(int i=1;i<=n;i++)
        fin>>v[i].first>>v[i].second;
    int st=1,dr=1e6,mid,r=-1,goal;
    while(st<=dr)
    {
        mid=(st+dr)/2;
        if(check(mid))
        {
            r=mid;
            goal=dp[n][l];
            dr=mid-1;
        }
        else st=mid+1;
    }
    fout<<r<<'\n';
    check(r);
    for(int p=n;p>=1;p--)
    {
        for(int i1=0;i1*v[p].first<=r && i1<=l;i1++)
        {
            int i2=(r-i1*v[p].first)/v[p].second;
            if(dp[p-1][l-i1]!=-1 && dp[p-1][l-i1]+i2==goal && (p!=1 || i1==l))
            {
                rr.push_back({i1,i2});
                l-=i1;
                goal=dp[p-1][l];
                break;
            }
        }
    }
    reverse(rr.begin(),rr.end());
    for(auto i:rr)
        fout<<i.first<<' '<<i.second<<'\n';
    return 0;
}