# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
547008 | fvogel499 | Roller Coaster Railroad (IOI16_railroad) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
* File created on 04/08/2022 at 21:58:56.
* Link to problem:
* Description:
* Time complexity: O()
* Space complexity: O()
* Status: ---
* Copyright: Ⓒ 2022 Francois Vogel
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace std;
using namespace __gnu_pbds;
#define pii pair<int, int>
#define f first
#define s second
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define size(x) (int)((x).size())
#define pb push_back
#define ins insert
#define cls clear
#define int ll
#define ll long long
#define ld long double
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int inf = 1e18;
int plan_roller_coaster_act(vi from, vi to) {
int n = size(from);
int dp [1<<n][n];
for (int i = 0; i < n; i++) dp[(1<<n)-1][i] = 0;
for (int i = (1<<n)-2; i >= 0; i--) for (int last = 0; last < n; last++) {
dp[i][last] = inf;
for (int j = 0; j < n; j++) if (!((i>>j)&1)) {
dp[i][last] = min(dp[i][last], dp[i|(1<<j)][j]+max(0LL, to[last]-from[j]));
}
}
int res = inf;
for (int i = 0; i < n; i++) res = min(res, dp[1<<i][i]);
return res;
}
int plan_roller_coaster(vector<signed> from, vector<signed> to) {
vi ifrom, ito;
for (int i : from) ifrom.pb(i);
for (int i : to) ito.pb(i);
return plan_roller_coaster_act(ifrom, ito);
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<signed> from(n), to(n);
for (int i = 0; i < n; i++) cin >> from[i];
for (int i = 0; i < n; i++) cin >> to[i];
cout << plan_roller_coaster(from, to) << endl;
cout.flush();
int d = 0;
d++;
}