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
using namespace std;
const int INF = 1000000000000000000;
int n,m;
int a[200005], s[200005], p[200005];
int b[200005], t[200005], q[200005];
int pa[200005];
int pb[200005];
int pp[200005], pq[200005];
int mem[2005][2005];
int dp(int i, int j){
if (i == n+1 && j == m+1) return 0;
if (mem[i][j] != -1) return mem[i][j];
int T = pa[i-1] + pb[j-1];
int ret = -INF;
if (i != n+1){
ret = max(ret, dp(i+1, j) + ((T+a[i] <= s[i])?p[i]:0));
}
if (j != m+1){
ret = max(ret, dp(i, j+1) + ((T+b[j] <= t[j])?q[j]:0));
}
//printf("%d %d %d %d\n",i,j,T,ret);
return mem[i][j] = ret;
}
main(){
scanf("%lld%lld",&n,&m);
for (int i = 1; i <= n; i++){
scanf("%lld%lld%lld",&a[i],&s[i],&p[i]);
pa[i] = pa[i-1] + a[i];
pp[i] = pp[i-1] + p[i];
}
for (int i = 1; i <= m; i++){
scanf("%lld%lld%lld",&b[i],&t[i],&q[i]);
pb[i] = pb[i-1] + b[i];
pq[i] = pq[i-1] + q[i];
}
if (n <= 2000 && m <= 2000){
memset(mem,-1,sizeof(mem));
printf("%lld",dp(1,1));
}
else{
if (s[0] > t[0]){
swap(n,m);
swap(a,b);
swap(s,t);
swap(p,q);
swap(pp,pq);
swap(pa,pb);
}
int ans = -INF;
int R = m;
int curT = 0;
int stopT = 0;
for (int i = 1; i <= n; i++){
if (pa[i] <= s[1]) pp[i] = max(pp[i-1], pp[i]);
else pp[i] = pp[i-1];
}
for (int i = 0; i <= n; i++){
while (pa[i] + pb[R] > t[1]){
R--;
}
ans = max(ans, pp[i]+pq[R]);
}
printf("%lld",ans);
}
}
Compilation message (stderr)
dishes.cpp:26:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
26 | main(){
| ^~~~
dishes.cpp: In function 'int main()':
dishes.cpp:53:13: warning: unused variable 'curT' [-Wunused-variable]
53 | int curT = 0;
| ^~~~
dishes.cpp:54:13: warning: unused variable 'stopT' [-Wunused-variable]
54 | int stopT = 0;
| ^~~~~
dishes.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf("%lld%lld",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~~~~
dishes.cpp:29:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | scanf("%lld%lld%lld",&a[i],&s[i],&p[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
dishes.cpp:34:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | scanf("%lld%lld%lld",&b[i],&t[i],&q[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |