#include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u = (s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define f first #define s second #define vec vector #define pb push_back #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) using namespace std; const int N = 5e4; int n, m; int a[N]; int profit[N]; int profit_[N]; void step(int pirate) { vec<pair<int, int>> to_sort; foru(i, pirate) { if(profit[i] == -1) to_sort.pb({0, i}); else to_sort.pb({profit[i] + a[i], i}); } sort(to_sort.begin(), to_sort.end()); foru(i, to_sort.size()) { //to_sort[i].s *= -1; auto p = to_sort[i]; if(2*(i+1) > to_sort.size()) profit_[p.s] = 0; else profit_[p.s] = p.f; } int leftover = m; foru(i, pirate) leftover -= profit_[i]; if (leftover < 0){ profit[pirate] = -1; return; } else { profit_[pirate] = leftover; swap(profit, profit_); return; } } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; for(int i = n-1; i>=0; i--) cin >> a[i]; profit[0] = m; fors(i, n, 1) step(i); for(int i = n-1; i>=0; i--) cout << profit[i] << " "; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u = (s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define f first #define s second #define vec vector #define pb push_back #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) using namespace std; const int N = 5e4; int n, m; int a[N]; int profit[N]; int profit_[N]; void step(int pirate) { vec<pair<int, int>> to_sort; foru(i, pirate) { if(profit[i] == -1) to_sort.pb({0, i}); else to_sort.pb({profit[i] + a[i], i}); } sort(to_sort.begin(), to_sort.end()); foru(i, to_sort.size()) { //to_sort[i].s *= -1; auto p = to_sort[i]; if(2*(i+1) > to_sort.size()) profit_[p.s] = 0; else profit_[p.s] = p.f; } int leftover = m; foru(i, pirate) leftover -= profit_[i]; if (leftover < 0){ profit[pirate] = -1; return; } else { profit_[pirate] = leftover; swap(profit, profit_); return; } } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; for(int i = n-1; i>=0; i--) cin >> a[i]; profit[0] = m; fors(i, n, 1) step(i); for(int i = n-1; i>=0; i--) cout << profit[i] << " "; return 0; } |