Cod sursa(job #2495952)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 20 noiembrie 2019 00:20:50
Problema Shop Scor 100
Compilator cpp-64 Status done
Runda ad_hoc Marime 1.76 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 35;

long long ans[N];

struct solve
{
    long long price, has, root, ind;
} a[N];

bool cmp(solve x, solve y)
{
    return x.price < y.price;
}
bool cmp2(solve x, solve y)
{
    return x.ind < y.ind;
}
void usain_bolt()
{
    ios::sync_with_stdio(false);
    fin.tie(0);
}

int main()
{
    usain_bolt();

    long long sum, n, base;



    fin >> n >> base >> sum;

    for(int i = 1; i <= n; ++i) {
        long long x, y;
        fin >> x >> y;
        long long nr = 1;
        for(int j = 1; j <= x; ++j) nr *= base;
        a[i].price = nr, a[i].has = y, a[i].root = x, a[i].ind = i;
    }

    sort(a + 1, a + 1 + n, cmp);

    long long m = min(a[n].has, sum / a[n].price);
    int cntt = 1;
    for(long long i = m; i >= 0; --i) {
        long long maybe = sum;
        long long cnt = i * a[n - cntt + 1].price;
        if(cnt > 0) ans[a[n].root] = i;
        maybe -= cnt;
        for(int j = n - cntt; j >= 1 && maybe > 0; --j) {
            long long cnt = min(a[j].has, maybe / a[j].price) * a[j].price;
            if(cnt > 0) ans[a[j].root] = cnt / a[j].price;
            maybe -= cnt;
        }
        if(maybe == 0LL) {
            sort(a + 1, a + 1 + n, cmp2);
            long long sol = 0;
            for(int j = 1; j <= n; ++j) sol += ans[a[j].root];
            fout << sol << "\n";
            for(int j = 1; j <= n; ++j) fout << ans[a[j].root] << " ";
            return 0;
        }
        if((i == 1 || i == 0) && cntt <= n) ++cntt, m = min(a[n - cntt + 1].has , sum / a[n - cntt + 1].price);
    }
    //for(int i = 1; i <= n; ++i) fout << a[i].price << " ";
    return 0;
}