Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define F first
- #define S second
- #define pb push_back
- using namespace std;
- typedef long long ll;
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace __gnu_pbds;
- #define ordered_set tree<pair<ll,int>, null_type,less<pair<ll,int>>, rb_tree_tag,tree_order_statistics_node_update>
- const ll INF = 1e16;
- const int mxN = 1e5 + 9;
- const int mxK = 1e2 + 9;
- const int MOD = 1000000007;
- vector <pair <pair <int, int>, int> > options[mxN];
- ll dp[mxN][mxK], A[mxN];
- void solve () {
- int n, m;
- cin >> n >> m;
- for (int i = 1; i <= n; i++)
- cin >> A[i];
- for (int i = 1; i <= m; i++) {
- int e, t, p;
- cin >> e >> t >> p;
- options[e].push_back ({{t, p}, i});
- }
- vector <int> res;
- int lastDone = 0;
- for (int i = 1; i <= n; i++) {
- int len = options[i].size();
- for (int j = 0; j <= len; j++)
- for (int k = 0; k <= 200; k++)
- dp[j][k] = INF;
- dp[0][0] = 0;
- int j = 1;
- for (auto [x, y]: options[i]) {
- int t = x.F, p = x.S;
- for (int k = 0; k <= 200; k++) {
- dp[j][k] = dp[j - 1][k];
- if (k >= p)
- dp[j][k] = min (dp[j][k], dp[j - 1][k - p] + t);
- }
- j++;
- }
- ll toCompare = INF;
- for (int k = 100; k <= 200; k++)
- toCompare = min (toCompare, dp[len][k]);
- //cout << toCompare << "\n";
- if (toCompare + lastDone > A[i]) {
- puts ("-1");
- return;
- } else {
- int x, y;
- for (int j = 100; j <= 200; j++)
- if (toCompare == dp[len][j])
- x = len, y = j;
- pair <int, int> curState = {x, y};
- while (curState.F > 0) {
- int j = curState.F, k = curState.S;
- if (dp[j][k] == dp[j - 1][k]) {
- curState = {j - 1, k};
- } else {
- res.pb (options[i][j - 1].S);
- curState = {j - 1, k - options[i][j - 1].F.S};
- }
- }
- lastDone += toCompare;
- }
- }
- cout << res.size() << "\n";
- for (auto u: res)
- cout << u << ' ';
- puts ("");
- for (int i = 1; i <= n; i++)
- options[i].clear();
- return;
- }
- int main () {
- int t = 1;
- cin >> t;
- //preCalc ();
- while (t--) {
- solve ();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement