Go: package main

import "fmt"

const MAX = 1001

func main() {
	var n int
	fmt.Scanln(&n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	w := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&w[i])
	}
	l := MAX
	r := MAX
	for i := 0; i < n; i++ {
		if a[i] == 0 {
			if w[i] < r {
				r = w[i]
			}
		} else {
			if w[i] < l {
				l = w[i]
			}
		}
	}
	ans := 0
	if l < MAX && r < MAX {
		ans = l + r
	}
	fmt.Println(ans)
}

C++: //yukicoder@cpp14
//author:luckYrat(twitter:@luckYrat_)
//<ここに一言>

//せんげん！
#include <iostream>

#include <cmath>
#include <algorithm>
#include <iomanip>

#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <bitset>

#include <cctype>
#include <utility>
#include <climits>


//なまえくーかん！
using namespace std;
using ll = long long;
using P = pair<ll,ll>;

//てーすう！
const int mod = 1000000007;
const int inf = (1<<30)-1;
const ll linf = (1LL<<62LL)-1;
const double EPS = (1e-10);

//でふぁいん！
#define anyfill(n,s) setw(n) << setfill(s)
#define loop(s) for(int i = 0; s > i; i++)
#define rep(i,q) for(int i = 0; (q) > i; i++)
#define repp(i,n,q) for(int i = n; (q) > i; i++)
#define dep(i,q) for(int i = (q); 0 < i; i--)

//みじかく！
#define pb push_back
#define fir first
#define scn second
#define ednl endl

//いぇすのー！
#define YesNo(a) (a?"Yes":"No")
#define YESNO(a) (a?"YES":"NO")
#define yesno(a) (a?"yes":"no")

//きんぼーnほーこー！！
P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる

計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/
template <typename T>
T gcd(T a,T b){
  if(a%b==0)return b;
  else return gcd(b,a%b);
}
template <typename T>
T lcm(T a,T b){
  return a/gcd(a,b)*b;
}


__attribute__((constructor))
void initial() {
 cin.tie(0);
 ios::sync_with_stdio(false);
}
int main(){
	int n;cin>>n;
	vector<P> A(n);
	for(int i = 0; n > i; i++){
		cin>>A[i].first;
	}
	for(int i = 0; n > i; i++){
		cin>>A[i].second;
	}
	sort(A.begin(),A.end());
	int ans = 1000000000;
	do{
		int t = 0;
		for(int i = 0; n > i; i++){
			if(A[i].first==1){
				//左利き
				if(A[(i-1<0?i-1+n:i-1)].first==0)t+=A[i].second;
			}else{
				if(A[i+1==n?0:i+1].first==1)t+=A[i].second;
			}
		}
		ans = min(ans,t);
	}while(next_permutation(A.begin(),A.end()));
	cout << ans << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
using namespace std;
#define int long long
int a[20];
int a_ = -1, b_ = -1;
signed main() {
	int n; cin >> n;
	for (int i = 0; i < n; i++)cin >> a[i];
	for (int i = 0; i < n; i++) {
		int k; cin >> k;
		if (a[i]) {
			if (a_ < 0)a_ = k;
			else a_ = min(a_, k);
		}
		else{
			if (b_ < 0)b_ = k;
			else b_ = min(b_, k);
		}
	}
	if (a_ < 0 || b_ < 0)cout << "0\n";
	else cout << a_ + b_ << endl;
}

C++: // 2018/12/10 Tazoe

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	int N;
	cin >> N;

	int a[10];
	for(int i=0; i<N; i++){
		cin >> a[i];
	}

	int w[10];
	for(int i=0; i<N; i++){
		cin >> w[i];
	}

	int wmin0 = 1001;
	int wmin1 = 1001;

	for(int i=0; i<N; i++){
		if(a[i]==0){
			wmin0 = min(wmin0, w[i]);
		}
		else{
			wmin1 = min(wmin1, w[i]);
		}
	}

	if(wmin0==1001 || wmin1==1001){
		cout << 0 << endl;
	}
	else{
		cout << wmin0+wmin1 << endl;
	}

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using P = pair<llong, llong>;

#define BE(x) x.begin(), x.end()

const llong inf = llong(1e18)+7;
const llong mod = 1e9+7;

int main(){
	llong N;
	vector<llong> a,w[2];

	cin >> N;
	a.resize(N);
	for(int i = 0; i < N; i++)
		cin >> a[i];
	llong in;
	for(int i = 0; i < N; i++){
		cin >> in;
		w[a[i]].push_back(in);
	}

	sort(BE(w[0]));
	sort(BE(w[1]));

	llong ans = 0;
	if(w[0].size() && w[1].size())
		ans += w[0][0] + w[1][0];

	cout << ans << endl;

	return 0;
}

C++: #include "bits/stdc++.h"
#define rep(var,cnt) for(int (var)=0; (var)<(cnt); ++(var))

using namespace std;

using pi=pair<int,int>;

int main() {
	int N; cin>>N;
	vector<pi> aw(N);
	vector<int> aa(N),ww(N);
	rep(i,N) cin>>aa[i];
	rep(i,N) cin>>ww[i];
	rep(i,N) aw[i]=pi(aa[i],ww[i]);
	vector<int> index(N);
	iota(index.begin(),index.end(),0);
	
	int ans=INT_MAX;
	do{
		int tmp=0;
		rep(i,N){
			if(aw[index[i]].first==0){
				if(aw[index[(i+1)%N]].first==1){
					tmp+=aw[index[i]].second;
					//cout<<"  "<<index[i]<<" fuman "<<aw[index[i]].second<<endl;
				}
			}
			else{
				if(aw[index[(i-1+N)%N]].first==0){
					tmp+=aw[index[i]].second;
					//cout<<"  "<<index[i]<<" fuman "<<aw[index[i]].second<<endl;
				}
			}
		}
		if(tmp<ans){
			ans=tmp;
			/*rep(i,N){
				cout<<index[i];
			}
			cout<<endl;*/
		}
	}while(next_permutation(index.begin(),index.end()));
	cout<<ans<<endl;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
typedef long long int ll;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T>
bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false;}
template<typename T>
bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false;}
// clang-format on

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> a(n), w(n);
    REP(i, 0, n) {
        cin >> a[i];
    }
    REP (i, 0, n) {
        cin >> w[i];
    }
    vector<int> perm(n - 1);
    iota(perm.begin(), perm.end(), 1);
    ll ans = 1LL << 60;
    do {
        vector<int> p(n);
        p[0] = 0;
        REP (i, 1, n) {
            p[i] = perm[i - 1];
        }
        ll s = 0;
        REP (i, 0, n) {
            int j = (i + 1) % n;
            int k = (i - 1 + n) % n;
            if (a[p[i]] == 0 && a[p[j]] == 1) {
                s += w[p[i]];
            }
            if (a[p[i]] == 1 && a[p[k]] == 0) {
                s += w[p[i]];
            }
        }
        chmin(ans, s);
    } while (next_permutation(perm.begin(), perm.end()));
    cout << ans << endl;

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
    int n; cin >> n;
    vector<int> a(n), w(n);
    for(auto& x : a) cin >> x;
    for(auto& x : w) cin >> x;

    vector<int> ord(n);
    iota(begin(ord), end(ord), 0);
    int ans = 10000000;
    do {
        int tans = 0;
        for(int i = 0; i < n; ++i) {
            if(a[ord[i]] == 0) {
                if(a[ord[(i + n - 1) % n]] == 1) {
                    tans += w[ord[i]];
                }
            } else {
                if(a[ord[(i + 1) % n]] == 0) {
                    tans += w[ord[i]];
                }
            }
        }
        ans = min(ans, tans);
    } while(next_permutation(begin(ord) + 1, end(ord)));

    cout << ans << endl;
}