# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
477821 | BackNoob | Roller Coaster Railroad (IOI16_railroad) | C++14 | 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.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define mask(i) (1LL << (i))
#define task "name"
#define ull unsigned long long
using namespace std;
const ll mxN = 220797 + 7;
const ll inf = 1e9 + 277;
const ll mod = 2147483648;
const ll infll = 1e18 + 7;
const ll base = 307;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
int s[mxN] , t[mxN];
ll dp[(1 << 16)][20];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("railroad.inp" , "r" , stdin);
//freopen("railroad.out" , "w" , stdout);
int n;
cin >> n;
if(n > 16) return 0;
for(int i = 0 ; i < n ; i++) cin >> s[i] >> t[i];
memset(dp , 0x3f , sizeof(dp));
for(int mask = 0 ; mask < (1 << n) ; mask++) {
if(mask == 0) {
for(int i = 0 ; i < n ; i++) {
if(!(mask & (1 << i)) && s[i] <= 1) {
dp[mask | (1 << i)][i] = 0;
}
}
}
else {
for(int i = 0 ; i < n ; i++) {
if((mask & (1 << i)) && dp[mask][i] != dp[0][0]) {
for(int j = 0 ; j < n ; j++) {
if(!(mask & (1 << j))) {
minimize(dp[mask | (1 << j)][j] , dp[mask][i] + max(0 , t[i] - s[j]));
}
}
}
}
}
}
ll res = dp[0][0];
for(int i = 0 ; i < n ; i++) {
minimize(res , dp[(1 << n) - 1][i]);
}
cout << res;
return 0;
}