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
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n,L;
cin>>n>>L;
vector <int> a(n),b(n);
for(int i=0;i<n;i++)cin>>a[i];
map <int,int> dp[(1<<n)][n];
for(int i=0;i<n;i++){
cin>>b[i];
if(min(a[i],L-a[i])<b[i])dp[(1<<i)][i][min(a[i],L-a[i])]=1;
}
for(int i=0;i<(1<<n);i++){
for(int j=0;j<n;j++){
if((i&(1<<j))){
int mask=i^(1<<j);
for(int l=0;l<n;l++){
if((mask&(1<<l))){
for(auto x : dp[mask][l]){
int mn=min(abs(a[j]-a[l]),L-max(a[j],a[l])+min(a[j],a[l]));
if(x.ff+mn<=b[j])dp[i][j][x.ff+mn]=max(dp[i][j][x.ff+mn],x.ss+1);
}
}
}
}
}
}
int mx=0;
for(int i=0;i<(1<<n);i++){
for(int j=0;j<n;j++){
for(auto x : dp[i][j])mx=max(mx,x.ss);
}
}
cout<<mx<<"\n";
}
/*
4 10
3 6 2 9
8 35
3 7 1 5 10 2 11 6
6 25
3 4 7 17 21 23
11 7 17 10 8 10
5 20
4 5 8 13 17
18 23 17 7 10
4 19
3 7 12 14
2 0 5 4
10 87
9 23 33 38 42 44 45 62 67 78
15 91 7 27 31 53 12 91 89 46
*/
# | 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... |