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>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pb push_back
#define ff first
#define ss second
const ll INF = 1e9 + 5;
pii a[501];
ll dp[501][501][501][2];
void solve(){
int n, L;
cin>>n;
cin>>L;
a[n + 1] = {0, 0};
for(int i=1; i<=n; i++){
cin>>a[i+n + 1].ff;
}
for(int i=1; i<=n; i++){
cin>>a[i+n+1].ss;
}
for(int i=n; i>=1; i--){
a[i] = {a[i + n + 1].ff - L, a[i+n+1].ss};
}
for(int i=1; i<=n + n + 1; i++){
for(int r = n + n + 1; r>=i; r--){
for(int k=0; k<=n; k++){
dp[i][r][k][0] = dp[i][r][k][1] = INF;
}
}
}
dp[n+1][n+1][0][0] = dp[n+1][n+1][0][1] = 0;
int ans = 0;
for(int d=2; d<=n+1; d++){
for(int l=1; l<=2 * n + 1 - d + 1; l++){
int r = l + d - 1;
for(int k=0; k<=n; k++){
ll g;
g = dp[l][r-1][k][0] + a[r].ff - a[l].ff;
if(g <= a[r].ss){
dp[l][r][k+1][1] = min(dp[l][r][k+1][1], g);
}
dp[l][r][k][1] = min(dp[l][r][k][1], g);
g = dp[l][r-1][k][1] + a[r].ff - a[r-1].ff;
if(g <= a[r].ss){
dp[l][r][k+1][1] = min(dp[l][r][k+1][1], g);
}
dp[l][r][k][1] = min(dp[l][r][k][1], g);
g = dp[l+1][r][k][0] + a[l+1].ff - a[l].ff;
if(g <= a[l].ss){
dp[l][r][k+1][0] = min(dp[l][r][k+1][0], g);
}
dp[l][r][k][0] = min(dp[l][r][k][0], g);
g = dp[l+1][r][k][1] + a[r].ff - a[l].ff;
if(g <= a[l].ss){
dp[l][r][k+1][0] = min(dp[l][r][k+1][0], g);
}
dp[l][r][k][0] = min(dp[l][r][k][0], g);
}
}
}
for(int i=1; i<=n + n + 1; i++){
for(int r = n + n + 1; r>=i; r--){
for(int k=0; k<=n; k++){
if(dp[i][r][k][0] < INF or dp[i][r][k][1] < INF){
//cout<<i<<' '<<r<<' '<<k<<' '<<min(dp[i][r][k][1], dp[i][r][k][0])<<'\n';
ans = max(ans, k);
}
}
}
}
cout<<ans;
}
signed main(){
solve();
}
# | 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... |