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 53 54 55 56 57 58 59 60
| #include "iostream" #include "algorithm" #include "cstring" #include "cstdio" #include "cmath" #include "vector" #include "map" #include "set" #include "queue" using namespace std; #define MAXN 56
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) typedef long long ll; int n , k; ll L[MAXN] , R[MAXN] , C[MAXN]; ll dp[MAXN][MAXN][MAXN][2][2][2][2];
inline void ckm( ll& a , ll b ) { a = min( a , b ); } ll wkr( int c , int l , int r , int l1 , int l2 , int r1 , int r2 ) { if( c == k ) return l > r ? 0 : 1e18; ll& s = dp[c][l][r][l1][l2][r1][r2]; if( ~s ) return s; ll lt = ( ( l1 ? R[l - 1] : L[l - 1] ) >> c ) ^ l2; ll rt = ( ( r1 ? R[r + 1] : L[r + 1] ) >> c ) ^ r2; s = 1e18; ckm( s , wkr( c + 1 , l , r , l1 , 0 , r1 , 0 ) + ( l != 1 && r != n ? ( ( lt & 1 ) ^ ( rt & 1 ) ) * C[c] : 0 ) ); rep( k , l , r ) rep( t , 0 , 1 ) { if( !c ) ckm( s , wkr( c , l , k - 1 , l1 , l2 , t , 0 ) + wkr( c , k + 1 , r , t , 0 , r1 , r2 ) ); ll w = ( t ? R[k] : L[k] ); if( L[k] <= ( ( w ^ ( 1ll << c ) ) & ( ~( ( 1ll << c ) - 1 ) ) ) && ( ( w ^ ( 1ll << c ) ) | ( ( 1ll << c ) - 1 ) ) <= R[k] ) ckm( s , wkr( c , l , k - 1 , l1 , l2 , t , 1 ) + wkr( c , k + 1 , r , t , 1 , r1 , r2 ) ); } return s; }
void solve() { cin >> n >> k; rep( i , 1 , n ) scanf("%lld%lld",L + i,R + i); rep( i , 0 , k - 1 ) scanf("%lld",C + i); memset( dp , -1 , sizeof dp ); cout << wkr( 0 , 1 , n , 0 , 0 , 0 , 0 ) << endl; }
signed main() {
solve(); }
|