#include <bits/stdc++.h>
using namespace std;
#define all(a) begin(a), end(a)
using ll = long long;
bool cmp(pair<int, int> x, pair<int, int> y) {
if (x.first != y.first)
return x.first < y.first;
return x.second > y.second;
}
void solve() {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (auto &i : a)
cin >> i;
vector<int> solution(n, -1);
for (int i = n - 1; i >= 0; i--) {
int needed = (n - 1 - i) / 2;
vector<pair<int, int>> cost;
for (int j = i + 1; j < n; j++) {
if (solution[j] == -1)
--needed;
else
cost.push_back({solution[j] + a[j], j});
}
sort(all(cost), cmp);
ll c = 0;
for (int j = 0; j < needed; j++)
c += cost[j].first;
if (c > m) {
solution[i] = -1;
continue;
}
for (int j = i + 1; j < n; j++)
solution[j] = 0;
solution[i] = m - c;
for (int j = 0; j < needed; j++) {
solution[cost[j].second] = cost[j].first;
}
}
for (auto i : solution)
cout << i << " ";
cout << "\n";
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int tests = 1;
// cin >> tests;
while (tests--)
solve();
}
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 66 67 | #include <bits/stdc++.h> using namespace std; #define all(a) begin(a), end(a) using ll = long long; bool cmp(pair<int, int> x, pair<int, int> y) { if (x.first != y.first) return x.first < y.first; return x.second > y.second; } void solve() { int n, m; cin >> n >> m; vector<int> a(n); for (auto &i : a) cin >> i; vector<int> solution(n, -1); for (int i = n - 1; i >= 0; i--) { int needed = (n - 1 - i) / 2; vector<pair<int, int>> cost; for (int j = i + 1; j < n; j++) { if (solution[j] == -1) --needed; else cost.push_back({solution[j] + a[j], j}); } sort(all(cost), cmp); ll c = 0; for (int j = 0; j < needed; j++) c += cost[j].first; if (c > m) { solution[i] = -1; continue; } for (int j = i + 1; j < n; j++) solution[j] = 0; solution[i] = m - c; for (int j = 0; j < needed; j++) { solution[cost[j].second] = cost[j].first; } } for (auto i : solution) cout << i << " "; cout << "\n"; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int tests = 1; // cin >> tests; while (tests--) solve(); } |
English