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>
using namespace std ;
#define int long long
#define maxn 200009
#define ll long long
#define pb push_back
#define fi first
#define se second
#define left id<<1
#define right id<<1|1
#define re exit(0);
#define _lower(x) lower_bound(v.begin(),v.end(),x)-v.begin()+1
const int mod = 1e9+7 ;
const int INF = 1e9 ;
typedef vector<int> vi ;
typedef pair<int,int> pii ;
typedef vector<pii> vii ;
template < typename T > void chkmin ( T &a , T b ) { if ( a > b ) a = b ; }
template < typename T > void chkmax ( T &a , T b ) { if ( a < b ) a = b ; }
void add ( int &a , int b )
{
a += b ;
if ( a >= mod ) a -= mod ;
if ( a < 0 ) a += mod ;
}
void rf ()
{
freopen ("bai1.inp","r",stdin) ;
}
int _pow ( int a , int n )
{
if ( n == 0 ) return 1 ;
int res = _pow (a,n/2) ;
if ( n % 2 ) return 1ll*res*res%mod*a%mod ;
else return 1ll*res*res%mod ;
}
int n , k ;
int h [maxn] , c [maxn] ;
ll dp [22][12][1<<11] ;
ll sum_high [1<<11] ;
int max_high [1<<11] , min_cost [1<<11] ;
void pre_calc ()
{
memset (min_cost,0x3f,sizeof min_cost) ;
for ( int mask = 0 ; mask < (1<<n) ; mask ++ )
{
for ( int i = 0 ; i < n ; i ++ )
{
if ( mask >> i & 1 ) sum_high [mask] += h [i+1] , chkmax(max_high[mask],h[i+1]) , chkmin (min_cost[mask],c[i+1]);
}
}
}
signed main ()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// rf () ;
cin >> n >> k ;
for ( int i = 1 ; i <= n ; i ++ ) cin >> h [i] >> c [i] , h [i] ++ ;
memset (dp,0x3f,sizeof dp) ;
pre_calc () ;
dp [0][0][0] = 0 ;
const int HIGH = n+*max_element(h+1,h+n+1) ;
for ( int i = 0 ; i < HIGH ; i ++ )
{
for ( int j = 0 ; j < n ; j ++ )
{
for ( int mask = 0 ; mask < (1<<n) ; mask ++ )
{
chkmin (dp[i+1][j][mask],dp[i][j][mask]) ;
int nmask = (1<<n) - 1 - mask ;
for ( int submask = nmask ; submask > 0 ; submask = (submask-1)&nmask )
{
if ( max_high [submask] <= i+1 && ( __builtin_popcount (submask) <= j || mask == 0 ))
{
int add_j = 0 ;
if ( mask == 0 && __builtin_popcount(submask) > 1 ) continue ;
if ( mask == 0 && __builtin_popcount(submask) == 1 ) add_j = 1 ;
chkmin (dp[i+1][j+add_j][mask|submask],dp[i][j][mask]+1ll*(__builtin_popcount (submask)*(i+1)-sum_high[submask])*k) ;
}
}
}
for ( int mask = 0 ; mask < (1<<n) ; mask ++ )
{
for ( int add_j = 0 ; add_j + j < n ; add_j ++ ) chkmin (dp[i+1][j+add_j][mask],dp[i+1][j][mask]+1ll*min_cost[mask]*add_j) ;
}
}
}
ll res = 1e18 ;
for ( int j = 0 ; j < n ; j ++ ) chkmin (res,dp[HIGH-1][j][(1<<n)-1]) ;
cout << res ;
}
Compilation message (stderr)
Main.cpp: In function 'void rf()':
Main.cpp:32:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen ("bai1.inp","r",stdin) ;
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |