/*************************************************************************************************************
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠛⠛⠛⠋⠉⠈⠉⠉⠉⠉⠛⠻⢿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⡿⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢿⣿⣿⣿⣿
⣿⣿⣿⣿⡏⣀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣤⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿
⣿⣿⣿⢏⣴⣿⣷⠀⠀⠀⠀⠀⢾⣿⣿⣿⣿⣿⣿⡆⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿
⣿⣿⣟⣾⣿⡟⠁⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⣷⢢⠀⠀⠀⠀⠀⠀⠀⢸⣿
⣿⣿⣿⣿⣟⠀⡴⠄⠀⠀⠀⠀⠀⠀⠙⠻⣿⣿⣿⣿⣷⣄⠀⠀⠀⠀⠀⠀⠀⣿
⣿⣿⣿⠟⠻⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠶⢴⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⣿
⣿⣁⡀⠀⠀⢰⢠⣦⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣿⡄⠀⣴⣶⣿⡄⣿
⣿⡋⠀⠀⠀⠎⢸⣿⡆⠀⠀⠀⠀⠀⠀⣴⣿⣿⣿⣿⣿⣿⣿⠗⢘⣿⣟⠛⠿⣼
⣿⣿⠋⢀⡌⢰⣿⡿⢿⡀⠀⠀⠀⠀⠀⠙⠿⣿⣿⣿⣿⣿⡇⠀⢸⣿⣿⣧⢀⣼
⣿⣿⣷⢻⠄⠘⠛⠋⠛⠃⠀⠀⠀⠀⠀⢿⣧⠈⠉⠙⠛⠋⠀⠀⠀⣿⣿⣿⣿⣿
⣿⣿⣧⠀⠈⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠟⠀⠀⠀⠀⢀⢃⠀⠀⢸⣿⣿⣿⣿
⣿⣿⡿⠀⠴⢗⣠⣤⣴⡶⠶⠖⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡸⠀⣿⣿⣿⣿
⣿⣿⣿⡀⢠⣾⣿⠏⠀⠠⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⠉⠀⣿⣿⣿⣿
⣿⣿⣿⣧⠈⢹⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⣿
⣿⣿⣿⣿⡄⠈⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣴⣾⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣦⣄⣀⣀⣀⣀⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⡄⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠙⣿⣿⡟⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠁⠀⠀⠹⣿⠃⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⣿⣿⣿⣿⡿⠛⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⢐⣿⣿⣿⣿⣿⣿⣿⣿⣿
⣿⣿⣿⣿⠿⠛⠉⠉⠁⠀⢻⣿⡇⠀⠀⠀⠀⠀⠀⢀⠈⣿⣿⡿⠉⠛⠛⠛⠉⠉
⣿⡿⠋⠁⠀⠀⢀⣀⣠⡴⣸⣿⣇⡄⠀⠀⠀⠀⢀⡿⠄⠙⠛⠀⣀⣠⣤⣤⠄
*************************************************************************************************************/
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <tuple>
#include <random>
#include <vector>
#include <bitset>
#include <chrono>
#include <iomanip>
#include <cassert>
#include <complex>
#include <fstream>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define UwU cin.tie(0);ios_base::sync_with_stdio(0);
#define test int test_num;cin>>test_num;while(test_num--)
#define prec(k) cout<<fixed<<setprecision(k);
#define cl(n,k) (((n)+(k)-1)/(k))
#define f first
#define s second
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using point = complex<double>;
ll gcd(ll a, ll b){return (b==0?a:gcd(b,a%b));}
ll lcm(ll a, ll b){return a/gcd(a,b)*b;}
const int M = 1e9 + 7;
const int O = 998244353;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323846;
const ll INF = (ll)1e18 + 5;
const int N = 2e3 + 5;
const int K = 50;
int n, m;
pair<pii,pii> w[N+N];
ll dp[2][N*K];
int main(){
UwU
cin >> n;
for(int i=0;i<n;i++){
cin >> w[i].f.s >> w[i].f.f >> w[i].s.f;
w[i].s.s = i;
}
cin >> m;
for(int i=0;i<m;i++){
cin >> w[i+n].f.s >> w[i+n].f.f >> w[i+n].s.f;
w[i+n].s.s = i+n;
}
sort(w,w+n+m, [] (pair<pii,pii> a, pair<pii,pii> b){
return make_pair(a.f.f, b.s.s) > make_pair(b.f.f, a.s.s);
});
for(int i=0;i<2;i++){
for(int j=0;j<=K*n;j++){
dp[i][j] = -INF;
}
}
dp[0][0] = 0;
for(int i=0;i<n+m;i++){
int b = i&1;
fill(dp[b^1],dp[b^1]+K*n+1,-INF);
for(int j=0;j<=K*n;j++){
if(w[i].s.s >= n && j >= w[i].f.s){
dp[b^1][j-w[i].f.s] = max(dp[b^1][j-w[i].f.s], dp[b][j] + w[i].s.f);
}
if(w[i].s.s < n && j+w[i].f.s <= K*n){
dp[b^1][j+w[i].f.s] = max(dp[b^1][j+w[i].f.s], dp[b][j] - w[i].s.f);
}
dp[b^1][j] = max(dp[b^1][j], dp[b][j]);
}
}
cout << *max_element(dp[(n+m)%2],dp[(n+m)%2]+K*n+1) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
508 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
69 ms |
1116 KB |
Output is correct |
6 |
Correct |
109 ms |
1172 KB |
Output is correct |
7 |
Correct |
345 ms |
1884 KB |
Output is correct |
8 |
Correct |
500 ms |
2040 KB |
Output is correct |
9 |
Correct |
349 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
600 KB |
Output is correct |
5 |
Correct |
12 ms |
604 KB |
Output is correct |
6 |
Correct |
12 ms |
604 KB |
Output is correct |
7 |
Correct |
15 ms |
668 KB |
Output is correct |
8 |
Correct |
16 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
273 ms |
1652 KB |
Output is correct |
4 |
Correct |
3 ms |
344 KB |
Output is correct |
5 |
Correct |
620 ms |
1880 KB |
Output is correct |
6 |
Correct |
776 ms |
2104 KB |
Output is correct |
7 |
Correct |
659 ms |
1880 KB |
Output is correct |
8 |
Correct |
657 ms |
1884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
41 ms |
856 KB |
Output is correct |
4 |
Correct |
165 ms |
1116 KB |
Output is correct |
5 |
Correct |
672 ms |
1884 KB |
Output is correct |
6 |
Correct |
727 ms |
1880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
508 KB |
Output is correct |
7 |
Correct |
3 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
69 ms |
1116 KB |
Output is correct |
14 |
Correct |
109 ms |
1172 KB |
Output is correct |
15 |
Correct |
345 ms |
1884 KB |
Output is correct |
16 |
Correct |
500 ms |
2040 KB |
Output is correct |
17 |
Correct |
349 ms |
1884 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
600 KB |
Output is correct |
22 |
Correct |
12 ms |
604 KB |
Output is correct |
23 |
Correct |
12 ms |
604 KB |
Output is correct |
24 |
Correct |
15 ms |
668 KB |
Output is correct |
25 |
Correct |
16 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
273 ms |
1652 KB |
Output is correct |
29 |
Correct |
3 ms |
344 KB |
Output is correct |
30 |
Correct |
620 ms |
1880 KB |
Output is correct |
31 |
Correct |
776 ms |
2104 KB |
Output is correct |
32 |
Correct |
659 ms |
1880 KB |
Output is correct |
33 |
Correct |
657 ms |
1884 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
41 ms |
856 KB |
Output is correct |
37 |
Correct |
165 ms |
1116 KB |
Output is correct |
38 |
Correct |
672 ms |
1884 KB |
Output is correct |
39 |
Correct |
727 ms |
1880 KB |
Output is correct |
40 |
Correct |
46 ms |
880 KB |
Output is correct |
41 |
Correct |
128 ms |
1116 KB |
Output is correct |
42 |
Correct |
507 ms |
1880 KB |
Output is correct |
43 |
Correct |
678 ms |
2116 KB |
Output is correct |
44 |
Correct |
702 ms |
2140 KB |
Output is correct |
45 |
Correct |
680 ms |
2008 KB |
Output is correct |
46 |
Correct |
227 ms |
1284 KB |
Output is correct |
47 |
Correct |
548 ms |
1716 KB |
Output is correct |
48 |
Correct |
518 ms |
1712 KB |
Output is correct |