#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Pk {
ll a, b, t;
bool operator<(const Pk other) const {
return a < other.a;
}
};
ll n, k, m, sz1, sz2, dp[105][2005], dp2[105][2005];
Pk v1[105],v2[105];
ll f(int idx, int T) {
if(idx>=sz1) return 0;
if(~dp[idx][T]) return dp[idx][T];
ll ret=0;
if(T<v1[idx].t) ret+=v1[idx].b;
if(idx!=sz1-1) {
ret+=f(idx+1,T+abs(v1[idx+1].a-v1[idx].a));
}
return dp[idx][T]=ret;
}
ll f2(int idx, int T) {
if(idx>=sz2) return 0;
if(~dp2[idx][T]) return dp2[idx][T];
ll ret=0;
if(T<v2[idx].t) ret+=v2[idx].b;
if(idx!=sz2-1) {
ret+=f2(idx+1,T+abs(v2[idx+1].a-v2[idx].a));
}
return dp2[idx][T]=ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
memset(dp,-1,sizeof dp);
memset(dp2,-1,sizeof dp2);
cin >> n >> k >> m;
for(int i = 0; i < m; i++) {
int a, b, t;
cin >> a >> b >> t;
if(a>k) v1[sz1++]={a,b,t};
else v2[sz2++]={a,b,t};
}
sort(v1, v1+sz1);
sort(v2, v2+sz2);
reverse(v2, v2+sz2);
ll ans = sz1 ? f(0,v1[0].a-k) : 0;
ll ans2 = sz2 ? f2(0, k-v2[0].a) : 0;
ll sum = 0;
for(int i = 0; i < sz2; i++) {
if(v2[i].t > k - v2[i].a) {
sum += v2[i].b;
}
ll r = sz1 ? f(0, k - v2[i].a + v1[0].a - v2[i].a) : 0ll;
ans = max(ans, sum + r);
}
sum = 0;
for(int i = 0; i < sz1; i++) {
if(v1[i].t > v1[i].a - k) {
sum += v1[i].b;
}
ll r = sz2 ? f2(0, v1[i].a - k + v1[i].a - v2[0].a) : 0ll;
ans2 = max(ans2, sum + r);
}
cout << max(ans, ans2);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3584 KB |
Output is correct |
2 |
Correct |
2 ms |
3584 KB |
Output is correct |
3 |
Correct |
2 ms |
3584 KB |
Output is correct |
4 |
Correct |
2 ms |
3584 KB |
Output is correct |
5 |
Correct |
3 ms |
3584 KB |
Output is correct |
6 |
Correct |
2 ms |
3584 KB |
Output is correct |
7 |
Incorrect |
2 ms |
3584 KB |
Output isn't correct |
8 |
Correct |
3 ms |
3584 KB |
Output is correct |
9 |
Correct |
2 ms |
3584 KB |
Output is correct |
10 |
Incorrect |
3 ms |
3584 KB |
Output isn't correct |