#include <bits/stdc++.h>
#define int long long int
#define ll long long
using namespace std;
const int N = 1e5 + 5;
int dp[N];
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
cin>>n;
vector<tuple<int,int,int>> v;
for(int i=0;i<n;i++)
{
int c,f,cost;
cin>>c>>f>>cost;
v.push_back(make_tuple(f,cost,c));
}
int m;
cin>>m;
for(int i=0;i<m;i++)
{
int c,f,cost;
cin>>c>>f>>cost;
v.push_back(make_tuple(f,-cost,c));
}
sort(v.begin(),v.end());
const int inf = 1e15 + 5;
for(int i=0;i<N;i++)
dp[i] = -inf;
dp[0] = 0;
int ans = 0;
for(int i=v.size()-1;i>=0;i--)
{
int c,f,cost;
tie(f,cost,c) = v[i];
cost = -cost;
if(cost < 0) //buying
{
for(int j=N-1;j>=0;j--)
{
if(j + c < N)
dp[j + c] = max(dp[j + c],dp[j] + cost);
}
}
else
{
for(int j=0;j<N;j++)
{
if(j - c >= 0)
dp[j - c] = max(dp[j - c],dp[j] + cost);
}
}
}
for(int i=0;i<N;i++)
ans = max(ans,dp[i]);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
13 ms |
1104 KB |
Output is correct |
4 |
Correct |
24 ms |
1100 KB |
Output is correct |
5 |
Correct |
211 ms |
1172 KB |
Output is correct |
6 |
Correct |
213 ms |
1100 KB |
Output is correct |
7 |
Correct |
226 ms |
1184 KB |
Output is correct |
8 |
Correct |
228 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
2 ms |
972 KB |
Output is correct |
3 |
Correct |
13 ms |
1100 KB |
Output is correct |
4 |
Correct |
13 ms |
1100 KB |
Output is correct |
5 |
Correct |
108 ms |
1128 KB |
Output is correct |
6 |
Correct |
108 ms |
1124 KB |
Output is correct |
7 |
Correct |
222 ms |
1100 KB |
Output is correct |
8 |
Correct |
222 ms |
1100 KB |
Output is correct |
9 |
Correct |
268 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
972 KB |
Output is correct |
2 |
Correct |
4 ms |
972 KB |
Output is correct |
3 |
Correct |
20 ms |
1112 KB |
Output is correct |
4 |
Correct |
21 ms |
1100 KB |
Output is correct |
5 |
Correct |
40 ms |
1100 KB |
Output is correct |
6 |
Correct |
44 ms |
1100 KB |
Output is correct |
7 |
Correct |
55 ms |
1100 KB |
Output is correct |
8 |
Correct |
61 ms |
1120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1100 KB |
Output is correct |
2 |
Correct |
3 ms |
1100 KB |
Output is correct |
3 |
Correct |
166 ms |
1184 KB |
Output is correct |
4 |
Correct |
209 ms |
1196 KB |
Output is correct |
5 |
Correct |
429 ms |
1240 KB |
Output is correct |
6 |
Correct |
475 ms |
1320 KB |
Output is correct |
7 |
Correct |
480 ms |
1320 KB |
Output is correct |
8 |
Correct |
451 ms |
1312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1100 KB |
Output is correct |
2 |
Correct |
19 ms |
1100 KB |
Output is correct |
3 |
Correct |
121 ms |
1124 KB |
Output is correct |
4 |
Correct |
219 ms |
1176 KB |
Output is correct |
5 |
Correct |
451 ms |
1268 KB |
Output is correct |
6 |
Correct |
504 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
13 ms |
1104 KB |
Output is correct |
4 |
Correct |
24 ms |
1100 KB |
Output is correct |
5 |
Correct |
211 ms |
1172 KB |
Output is correct |
6 |
Correct |
213 ms |
1100 KB |
Output is correct |
7 |
Correct |
226 ms |
1184 KB |
Output is correct |
8 |
Correct |
228 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
2 ms |
972 KB |
Output is correct |
11 |
Correct |
13 ms |
1100 KB |
Output is correct |
12 |
Correct |
13 ms |
1100 KB |
Output is correct |
13 |
Correct |
108 ms |
1128 KB |
Output is correct |
14 |
Correct |
108 ms |
1124 KB |
Output is correct |
15 |
Correct |
222 ms |
1100 KB |
Output is correct |
16 |
Correct |
222 ms |
1100 KB |
Output is correct |
17 |
Correct |
268 ms |
1100 KB |
Output is correct |
18 |
Correct |
4 ms |
972 KB |
Output is correct |
19 |
Correct |
4 ms |
972 KB |
Output is correct |
20 |
Correct |
20 ms |
1112 KB |
Output is correct |
21 |
Correct |
21 ms |
1100 KB |
Output is correct |
22 |
Correct |
40 ms |
1100 KB |
Output is correct |
23 |
Correct |
44 ms |
1100 KB |
Output is correct |
24 |
Correct |
55 ms |
1100 KB |
Output is correct |
25 |
Correct |
61 ms |
1120 KB |
Output is correct |
26 |
Correct |
3 ms |
1100 KB |
Output is correct |
27 |
Correct |
3 ms |
1100 KB |
Output is correct |
28 |
Correct |
166 ms |
1184 KB |
Output is correct |
29 |
Correct |
209 ms |
1196 KB |
Output is correct |
30 |
Correct |
429 ms |
1240 KB |
Output is correct |
31 |
Correct |
475 ms |
1320 KB |
Output is correct |
32 |
Correct |
480 ms |
1320 KB |
Output is correct |
33 |
Correct |
451 ms |
1312 KB |
Output is correct |
34 |
Correct |
2 ms |
1100 KB |
Output is correct |
35 |
Correct |
19 ms |
1100 KB |
Output is correct |
36 |
Correct |
121 ms |
1124 KB |
Output is correct |
37 |
Correct |
219 ms |
1176 KB |
Output is correct |
38 |
Correct |
451 ms |
1268 KB |
Output is correct |
39 |
Correct |
504 ms |
1272 KB |
Output is correct |
40 |
Correct |
116 ms |
1144 KB |
Output is correct |
41 |
Correct |
246 ms |
1196 KB |
Output is correct |
42 |
Correct |
332 ms |
1292 KB |
Output is correct |
43 |
Correct |
462 ms |
1220 KB |
Output is correct |
44 |
Correct |
457 ms |
1328 KB |
Output is correct |
45 |
Correct |
466 ms |
1368 KB |
Output is correct |
46 |
Correct |
237 ms |
1200 KB |
Output is correct |
47 |
Correct |
340 ms |
1308 KB |
Output is correct |
48 |
Correct |
339 ms |
1292 KB |
Output is correct |