首页 > 代码库 > Tea Party

Tea Party

#include <bits/stdc++.h>
#define _xx ios_base::sync_with_stdio(0);cin.tie(0);

using namespace std;
struct Node {
    int value, id;
} a[10005];
bool cmp(const Node& t1, const Node& t2) {
    return t1.value < t2.value;
}
int ans[10005];
int main() {
    int n, w;
    while(cin >> n >> w) {
        int now = 0;
        for(int i = 1; i <= n; i++)
        {
            cin >> a[i].value;
            a[i].id = i;
            ans[i] = ceil(a[i].value*1.0/2);
            now += ans[i];
        }
        if(now > w) {
            cout << -1 << endl;
            continue;
        }
        sort(a + 1, a + n + 1, cmp);
        for(int i = n; i >= 1 && now < w; i--) {
            int t = min(w - now, a[i].value - ans[a[i].id]);
            now += t;
            ans[a[i].id] += t;
        }
        if(now < w) {
            cout << -1 << endl;
            continue;
        }
        for(int i = 1; i <= n; i++) cout << ans[i] << " ";
        cout << endl;
    }
    return 0;
}

  

Tea Party