#include "railroad.h"
//Segment Tree is a State of Mind
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr<<#x<<" is "<<x<<endl;
#define debugp(x,y) cerr<<#x<<' '<<#y<<" is "<<x<<' '<<y<<endl;
#define debugl(x) cerr<<#x<<" is ";for(auto p:x)cerr<<p<<" ";cerr<<endl;
#define debugpl(x) cerr<<#x<<" is ";for(auto p:x)cerr<<p.first<<" "<<p.second<<" , ";cerr<<endl;
#define int long long
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int,pii> ipii;
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define rdint(x,y) rnd()%(y-x+1)+x
const int inf=4e18+5;
const int mod=1e9+7;
const int mod2=998244353;
const int mod3=1e9+9;
const int maxn=2e5+5;
long long plan_roller_coaster(std::vector<signed> s, std::vector<signed> t) {
int n = (int) s.size();
vector<pii> dp[(1<<n)];//espeed, cost
dp[0]={{1,0}};
for(int i=1;i<(1<<n);i++){
for(int m=0;m<n;m++){
if((1<<m)&i){
for(pii p:dp[i-(1<<m)]){
pii np={t[m],p.se+max(p.fi-s[m],0LL)};
dp[i].pb(np);
}
}
}
}
/*for(auto i:dp){
debugpl(i)
}*/
int ans=inf;
for(auto [e,c]:dp[(1<<n)-1]){
ans=min(ans,c);
}
return ans;
}