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 all(s) s.begin(),s.end()
#define F first
#define S second
using namespace std;
typedef int ll;
struct node
{
ll c,f,v;
bool tp=0;
} a[40009];
bool cmp(const node&xo,const node&yo)
{
if(xo.f==yo.f)
return xo.tp<yo.tp;
return xo.f>yo.f;
}
ll n,m;
long long dp[2][100001],ans=-1e15;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
for(ll i=0; i<n; i++)
cin>>a[i].c>>a[i].f>>a[i].v;
cin>>m;
for(ll i=n; i<n+m; i++)
{
cin>>a[i].c>>a[i].f>>a[i].v;
a[i].tp=1;
}
sort(a,a+n+m,cmp);
for(ll i=1; i<=1e5; i++)
dp[1][i]=-1e15;
for(ll i=0; i<n+m; i++)
{
bool b=i&1;
for(ll j=0; j<=1e5; j++)
{
long long &r=dp[b][j];
r=dp[b^1][j];
if(a[i].tp&&j+a[i].c<=1e5)
r=max(r,dp[b^1][j+a[i].c]+a[i].v);
else if(a[i].tp==0&&j-a[i].c>=0)
r=max(r,dp[b^1][j-a[i].c]-a[i].v);
if(i==n+m-1)
ans=max(ans,r);
}
}
cout<<ans;
return 0;
}
# | 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... |