Cod sursa(job #1773904)

Utilizator silkMarin Dragos silk Data 8 octombrie 2016 12:48:57
Problema Shop Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <cstdio>
#include <algorithm>
#define NMax 30
#define ll long long
#define mp make_pair
#define MIN(a,b)((a)<(b)?(a):(b))
using namespace std;

pair<int, int> e[NMax+1];
ll pwr[NMax+1];
ll sol[NMax+1];
ll ans;
int o[NMax+1];
int N,C;

int Solve(ll L, int v)
{
    int i;
    ll x,t;

    x = L/C;
    for(i = N; i >= 2-v; --i)
    {
        t = x / pwr[ e[i].first - 1 ];
        t = MIN(t, e[i].second);

        sol[i] = t;
        x = x - t * pwr[ e[i].first - 1 ];
        ans = ans + t;
    }

    return x;
}

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

    int i,j,x,y;
    ll L,t;

    scanf("%d %d %lld",&N,&C,&L);
    for(i = 1; i <= N; ++i)
    {
        scanf("%d %d",&x,&y);
        e[i] = mp(x,y);
        o[i] = x;
    }

    sort(e+1,e+N+1);
    pwr[0] = 1;
    for(t = C, i = 1; t <= L; ++i, t = t * C ) pwr[i] = t;

    if( e[1].first > 0 ) Solve(L,1);
    else
    {
        for(i = 0; i <= e[1].second; ++i)
        if( ( L - i ) % C == 0 )
        {
            ans = sol[1] = i;
            if( !Solve(L-i,0) ) break;
        }
    }

    printf("%lld\n", ans);
    for(i = 1; i <= N; ++i)
    {
        for(j = 1; j <= N; ++j)
        if( o[i] == e[j].first ) printf("%lld ",sol[j]);
    }
    printf("\n");



return 0;
}