#include "railroad.h"
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
int n = (int) s.size();
ll ans=0;
vector<vector<pair<int,ll>>> g(n+1);
s.push_back(0);t.push_back(1);
for(int i=0;i<=n;i++){
for(int j=0;j<n;j++){
if(i!=j)g[(i+1)%(n+1)].push_back({(j+1)%(n+1),max(t[i]-s[j],0)});
}
}
vector<vector<ll>> dp(1<<(n+1),vector<ll>(n+1,1e18));
dp[1][0]=0;
for(int i=0;i<(1<<(n+1));i++){
for(int j=0;j<=n;j++){
if(dp[i][j]<1e18){
for(pair<int,ll> u:g[j]){
if(!((1<<u.first)&i)){
dp[i+(1<<u.first)][u.first]=min(dp[i][j]+u.second,dp[i+(1<<u.first)][u.first]);
}
}
}
}
}
ans=1e18;
for(int i=1;i<=n;i++)ans=min(ans,dp[(1<<(n+1))-1][i]);
return ans;;
}