#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <unordered_map>
#include <stack>
#include <bit>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
#define ll long long
#define ld long double
#define inf (ll)(2*1e18)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define endl "\n"
using namespace std;
void solve(){
ll n, l, i, x, y, val, pos, res=0, time, cur;
cin>>n>>l;
vector<ll> a(2*n+1);
vector<ll> b(2*n+1);
a[n] = l;
vector<vector<vector<vector<ll>>>> dp(2*n+1, vector<vector<vector<ll>>>(2*n+1, vector<vector<ll>>(n+1, vector<ll>(2, inf))));
set<pair<ll, pair<pair<ll, ll>, pair<ll, ll>>>> s;
for(i=n+1;i<=2*n;++i){
cin>>x;
a[i] = x+l;
a[i-n-1] = x;
}
for(i=n+1;i<=2*n;++i){
cin>>x;
b[i] = x;
b[i-n-1] = x;
}
s.insert({0, {{n, n}, {0, 0}}});
dp[n][n][0][0] = 1;
while(!s.empty()){
time = s.begin()->first;
x = s.begin()->second.first.first;
y = s.begin()->second.first.second;
val = s.begin()->second.second.first;
pos = s.begin()->second.second.second;
s.erase(s.begin());
if(y - x == n){
res = max(res, val);
continue;
}
if(pos){
cur = y;
}else{
cur = x;
}
if(time + a[cur] - a[x-1] <= b[x-1]){
if(dp[x-1][y][val+1][0] > time + a[cur] - a[x-1]){
s.erase({dp[x-1][y][val+1][0], {{x-1, y}, {val+1, 0}}});
dp[x-1][y][val+1][0] = time + a[cur] - a[x-1];
s.insert({dp[x-1][y][val+1][0], {{x-1, y}, {val+1, 0}}});
}
}else{
if(dp[x-1][y][val][0] > time + a[cur] - a[x-1]){
s.erase({dp[x-1][y][val][0], {{x-1, y}, {val, 0}}});
dp[x-1][y][val][0] = time + a[cur] - a[x-1];
s.insert({dp[x-1][y][val][0], {{x-1, y}, {val, 0}}});
}
}
if(time + a[y+1] - a[cur] <= b[y+1]){
if(dp[x][y+1][val+1][1] > time + a[y+1] - a[cur]){
s.erase({dp[x][y+1][val+1][1], {{x, y+1}, {val+1, 1}}});
dp[x][y+1][val+1][1] = time + a[y+1] - a[cur];
s.insert({dp[x][y+1][val+1][1], {{x, y+1}, {val+1, 1}}});
}
}else{
if(dp[x][y+1][val][1] > time + a[y+1] - a[cur]){
s.erase({dp[x][y+1][val][1], {{x, y+1}, {val, 1}}});
dp[x][y+1][val][1] = time + a[y+1] - a[cur];
s.insert({dp[x][y+1][val][1], {{x, y+1}, {val, 1}}});
}
}
}
cout<<res<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
srand(time(nullptr));
ll t=1;
// cin>>t;
for(;t>0;--t){
solve();
}
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... |