This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |