#include <bits/stdc++.h>
using namespace std;
struct pos
{
long long nr, val, cost;
bool tip;
pos(long long nr, long long val, long long cost, bool tip)
{
this->nr = nr;
this->val = val;
this->cost = cost;
this->tip = tip;
}
bool operator<(pos b) const
{
if(val != b.val)
return val > b.val;
return tip < b.tip;
}
};
long long n,m,ans;
vector<pos> v;
long long dp[100005];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
long long nr, val, cost;
cin>>nr>>val>>cost;
v.push_back(pos(nr, val, cost, 0));
}
cin>>m;
for(int i=1;i<=m;i++)
{
int nr, val, cost;
cin>>nr>>val>>cost;
v.push_back(pos(nr, val, cost, 1));
}
sort(v.begin(), v.end());
for(int nr=0; nr<= 50*n; nr++)
dp[nr] = -1e18;
dp[0]=0;
for(auto it : v)
{
if(it.tip == 0)
{
for(int nr=50*n; nr>=0; nr--)
if(nr - it.nr >= 0)
dp[nr] = max(dp[nr], dp[nr-it.nr] - it.cost);
}
else
{
for(int nr=0; nr<= 50*n; nr++)
if(nr + it.nr <= 50*n)
dp[nr] = max(dp[nr], dp[nr + it.nr] + it.cost);
}
}
long long ans=0;
for(int nr=0; nr<= 50*n; nr++)
ans = max(ans, dp[nr]);
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
40 ms |
704 KB |
Output is correct |
6 |
Correct |
40 ms |
596 KB |
Output is correct |
7 |
Correct |
187 ms |
1196 KB |
Output is correct |
8 |
Correct |
180 ms |
1188 KB |
Output is correct |
9 |
Correct |
170 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
6 ms |
468 KB |
Output is correct |
6 |
Correct |
5 ms |
344 KB |
Output is correct |
7 |
Correct |
7 ms |
432 KB |
Output is correct |
8 |
Correct |
8 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
107 ms |
992 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
360 ms |
1236 KB |
Output is correct |
6 |
Correct |
367 ms |
1364 KB |
Output is correct |
7 |
Correct |
375 ms |
1376 KB |
Output is correct |
8 |
Correct |
352 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
23 ms |
596 KB |
Output is correct |
4 |
Correct |
80 ms |
724 KB |
Output is correct |
5 |
Correct |
387 ms |
1236 KB |
Output is correct |
6 |
Correct |
363 ms |
1348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
4 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
4 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
40 ms |
704 KB |
Output is correct |
14 |
Correct |
40 ms |
596 KB |
Output is correct |
15 |
Correct |
187 ms |
1196 KB |
Output is correct |
16 |
Correct |
180 ms |
1188 KB |
Output is correct |
17 |
Correct |
170 ms |
1108 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
6 ms |
468 KB |
Output is correct |
23 |
Correct |
5 ms |
344 KB |
Output is correct |
24 |
Correct |
7 ms |
432 KB |
Output is correct |
25 |
Correct |
8 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
107 ms |
992 KB |
Output is correct |
29 |
Correct |
3 ms |
340 KB |
Output is correct |
30 |
Correct |
360 ms |
1236 KB |
Output is correct |
31 |
Correct |
367 ms |
1364 KB |
Output is correct |
32 |
Correct |
375 ms |
1376 KB |
Output is correct |
33 |
Correct |
352 ms |
1364 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
23 ms |
596 KB |
Output is correct |
37 |
Correct |
80 ms |
724 KB |
Output is correct |
38 |
Correct |
387 ms |
1236 KB |
Output is correct |
39 |
Correct |
363 ms |
1348 KB |
Output is correct |
40 |
Correct |
25 ms |
468 KB |
Output is correct |
41 |
Correct |
75 ms |
736 KB |
Output is correct |
42 |
Correct |
232 ms |
1108 KB |
Output is correct |
43 |
Correct |
374 ms |
1364 KB |
Output is correct |
44 |
Correct |
372 ms |
1364 KB |
Output is correct |
45 |
Correct |
370 ms |
1364 KB |
Output is correct |
46 |
Correct |
97 ms |
852 KB |
Output is correct |
47 |
Correct |
211 ms |
1108 KB |
Output is correct |
48 |
Correct |
203 ms |
1108 KB |
Output is correct |