# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1090995 | elotelo966 | Wiring (IOI17_wiring) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define OYY LLONG_MAX
#define mod 1000000007
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR for(int i=1;i<=n;i++)
#define mid (start+end)/2
#define lim 17
#define fi first
#define se second
int a[lim],b[lim];
int n,m;
int dp[lim][(1<<lim)];
inline int f(int sira,int mask){
//cout<<sira<<" "<<mask<<" "<<(1<<m)-1<<endl;
if(sira==n){
if(mask==(1<<m)-1)return 0;
return OYY;
}
if(~dp[sira][mask])return dp[sira][mask];
int cev=LLONG_MAX;
for(int i=0;i<m;i++){
int tut=f(sira+1,mask|(1<<i));
if(tut==LLONG_MAX)continue;
cev=min(cev,tut+abs(a[sira]-b[i]));
}
return dp[sira][mask]=cev;
}
int32_t main(){
faster
memset(dp,-1,sizeof(dp));
cin>>n>>m;
FOR{
cin>>a[i-1];
}
for(int i=0;i<m;i++){
cin>>b[i];
}
if(n<m){
swap(n,m);
swap(a,b);
}
int cev=f(0,0);
cout<<cev<<'\n';
return 0;
}