#include<bits/stdc++.h>
using namespace std;
#define lalala ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
#define ll long long
#define pb push_back
#define N 200005
int n,k,m;
vector<tuple<int,int,int>> pokemon;
ll int dp[2][2005][105][105];
ll int hesap(int nerden, int sn,int left,int right){
if(sn>2000)return 0;
int yedek=nerden;
if(left<0||right>=m)return 0;
if(dp[nerden][sn][left][right]!=-1)return dp[nerden][sn][left][right];
if(nerden==0)nerden=left;
else nerden=right;
int f,d,e,x,y,z;
tie(f,d,e)=pokemon[nerden];
ll int a=0,b=0;
if(left>0){
tie(x,y,z)=pokemon[left-1];
a=hesap(0,sn+abs(f-x),left-1,right);
if(sn+abs(f-x)<z)a+=y;
}
if(right<m-1){
tie(x,y,z)=pokemon[right+1];
b=hesap(1,sn+abs(f-x),left,right+1);
if(sn+abs(f-x)<z)b+=y;
}
return dp[yedek][sn][left][right]=max(a,b);
}
int main(){
lalala;
memset(dp,-1,sizeof(dp));
cin>>n>>k>>m;
int le, rr;
le=rr=-1;
for(int i=0;i<m;i++){
ll int a,b,c;cin>>a>>b>>c;
pokemon.pb({a,b,c});
if(a>=k&&rr==-1)rr=i;
else if(a<k)le=i;
}
ll int a=0,b=0;
if(le!=-1){
int x,y,z;
tie(x,y,z)=pokemon[le];
a=hesap(0,abs(x-k),le,le);
if(abs(x-k)<z)a+=y;
}
memset(dp,-1,sizeof(dp));
if(rr!=-1){
int x,y,z;
tie(x,y,z)=pokemon[rr];
b=hesap(1,abs(x-k),rr,rr);
if(abs(x-k)<z)b+=y;
}
cout<<max(a,b)<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
346248 KB |
Output is correct |
2 |
Correct |
172 ms |
346372 KB |
Output is correct |
3 |
Correct |
183 ms |
346364 KB |
Output is correct |
4 |
Correct |
174 ms |
346372 KB |
Output is correct |
5 |
Correct |
248 ms |
346368 KB |
Output is correct |
6 |
Correct |
225 ms |
346324 KB |
Output is correct |
7 |
Correct |
315 ms |
346316 KB |
Output is correct |
8 |
Correct |
352 ms |
346372 KB |
Output is correct |
9 |
Correct |
393 ms |
346368 KB |
Output is correct |
10 |
Correct |
324 ms |
346372 KB |
Output is correct |