Cod sursa(job #709339)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 7 martie 2012 23:47:37
Problema Shop Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <cstdio>
#include <algorithm>

#define LL long long
#define NMax 32

using namespace std;

int N, C, A[NMax], B[NMax], Index[NMax], S[NMax];
LL Sum, NC;

inline bool Compare (const int &a, const int &b)
{
    return A[a]<A[b];
}

inline LL Pow (int X, int P)
{
    LL R=1;
    for (; P>0; --P) R*=X;
    return R;
}

void Solve ()
{
    sort (Index+1, Index+N+1, Compare);
    for (int i=N; i>0; NC+=S[Index[i]], --i)
    {
        S[Index[i]]=(int)min (1LL*B[Index[i]], Sum/Pow (C, A[Index[i]]));
        Sum-=(S[Index[i]]*Pow (C, A[Index[i]]));
    }
}

void Read ()
{
    freopen ("shop.in", "r", stdin);
    scanf ("%d %d %lld", &N, &C, &Sum);
    for (int i=1; i<=N; ++i)
    {
        scanf ("%d %d", &A[i], &B[i]); Index[i]=i;
    }
}

void Print ()
{
    freopen ("shop.out", "w", stdout);
    printf ("%lld\n", NC);
    for (int i=1; i<=N; ++i) printf ("%d ", S[i]);
    printf ("\n");
}

int main()
{
    Read ();
    Solve ();
    Print ();
    return 0;
}