1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include<iostream> #include<cstdio> #include<cstring> #define ll long long using namespace std; const int N=50+10; const ll INF=1e18; template<class T> inline void read(T &x) { x=0;int f=0; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); if(f)x=~x+1; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } int n,k; ll L[N],R[N],c[N]; ll f[N][N][N][2][2][2][2]; inline void ckmin(ll &x,ll y){x=x<y?x:y;} ll solve(int x,int l,int r,bool l1,bool l2,bool r1,bool r2) { if(x>=k)return l>r?0:INF; ll &res=f[x][l][r][l1][l2][r1][r2]; if(~res)return res; res=INF; bool tl=((l1?R[l-1]:L[l-1])>>x&1)^l2,tr=((r1?R[r+1]:L[r+1])>>x&1)^r2; ckmin(res,solve(x+1,l,r,l1,0,r1,0)+(l!=1&&r!=n&&(tl^tr)?c[x]:0)); for(int mid=l;mid<=r;mid++) for(int t=0;t<2;t++) { if(!x)ckmin(res,solve(x,l,mid-1,l1,l2,t,0)+solve(x,mid+1,r,t,0,r1,r2)); ll lim=t?R[mid]:L[mid]; if(L[mid]<=((lim^(1ll<<x))&(~((1ll<<x)-1)))&&((lim^(1ll<<x))|((1ll<<x)-1))<=R[mid]) ckmin(res,solve(x,l,mid-1,l1,l2,t,1)+solve(x,mid+1,r,t,1,r1,r2)); } return res; } int main() { read(n,k); for(int i=1;i<=n;i++)read(L[i],R[i]); for(int i=0;i<k;i++)read(c[i]); memset(f,-1,sizeof(f)); printf("%lld\n",solve(0,1,n,0,0,0,0)); return 0; }
|