Problem:
Round Table of Sages
$N$ sages are sitting around a round table with $N$ seats. Each sage holds chopsticks with his dominant hand to eat his dinner. The following happens in this situation.
If sage $i$ is right-handed and a left-handed sage sits on his right, a level of frustration $w_i$ occurs to him. A right-handed sage on his right does not cause such frustration at all.
If sage $i$ is left-handed and a right-handed sage sits on his left, a level of frustration $w_i$ occurs to him. A left-handed sage on his left does not cause such frustration at all.
You wish you could minimize the total amount of frustration by clever sitting order arrangement.
Given the number of sages with his dominant hand information, make a program to evaluate the minimum frustration achievable.
Input
The input is given in the following format.
$N$
$a_1$ $a_2$ $...$ $a_N$
$w_1$ $w_2$ $...$ $w_N$
The first line provides the number of sages $N$ ($3 \leq N \leq 10$). The second line provides an array of integers $a_i$ (0 or 1) which indicate if the $i$-th sage is right-handed (0) or left-handed (1). The third line provides an array of integers $w_i$ ($1 \leq w_i \leq 1000$) which indicate the level of frustration the $i$-th sage bears.
Output
Output the minimum total frustration the sages bear.
Sample Input 1
5
1 0 0 1 0
2 3 5 1 2
Sample Output 1
3
Sample Input 2
3
0 0 0
1 2 3
Sample Output 2
0

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
int main(){
    int N;
    std::cin >> N;
    
    std::vector<int> a(N), w(N);
    for (int i = 0; i < N; i++) {
        std::cin >> a[i];
    }
    for (int i = 0; i < N; i++) {
        std::cin >> w[i];
    }
    
    std::vector<int> people(N);
    for (int i = 0; i < N; i++) {
        people[i] = i;
    }
    
    int ans=1000000;
    while (std::next_permutation(people.begin(), people.end())) {
        int cost = 0;
        for (int i = 0; i < N; i++) {
            if ((a[people[i]] ^ a[people[(i + 1) % N]]) == 1) {
                cost += w[people[i]];
            }
        }
        if (cost < ans) {
            ans = cost;
        }
    }
    
    std::cout << ans << std::endl;
    
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3584
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

int main() {
	cin.tie(nullptr);
	ios_base::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> p(N);
	rep(i, 0, N) p[i] = i;
	
	int ans = 1 << 30;
	do {
		int res = 0;
		
		rep(i, 0, N) {
			if ((A[p[i]] == 0 && A[p[(i + 1) % N]] == 1) || (A[p[i]] == 1 && A[p[(i + N - 1) % N]] == 0)) res += W[p[i]];
		}
		ans = min(ans, res);
	} while (next_permutation(p.begin(), p.end()));
	cout << ans << '\n';
}
accuracy: 1
time_ms: 100
memory_kb: 3432
final_rank: 8

code_snippet: #include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(){
    int N;cin>>N;
    vector<int> A(N), W(N), I(N);
    rep(i,N) cin >> A[i];
    rep(i,N) cin >> W[i];
    rep(i,N) I[i] = i;
    int ans = INT_MAX;
    
    do{
        int tmp = 0;
        rep(i, N){
            int p = I[i];
            int right = I[(i+1)%N];
            int left = I[(i-1+N)%N];
            
            if (A[p] == 0) {
                if (A[right] == 1) tmp += W[p];
            } else {
                if (A[left] == 0) tmp += W[p];
            }
        }
        ans = min(ans, tmp);
    } while(next_permutation(I.begin(), I.end()));
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3584
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <iomanip>
using namespace std;
using ll = long long;
using VVI = vector<vector<int>>;
using VVL = vector<vector<ll>>;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
using VC = vector<char>;
using VP = vector<pair<int, int>>;
using Graph0 = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define drep(i, a, b) for (int i = (int)(a);i >= (int)(b);i--)
#define urep(i, a, b) for (int i = (int)(a);i <= (int)(b);i++)
#define lrep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ldrep(i, a, b) for (ll i = (ll)(a);i >= (ll)(b);i--)
#define lurep(i, a, b) for (ll i = (ll)(a);i <= (ll)(b);i++)
#define arep(i, v) for (auto i : v)
#define all(a) (a).begin(), (a).end()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define eyes cout << "Yes" << endl;exit(0);
#define eno cout << "No" << endl;exit(0);
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b; 
    return true;
  }
  return false;
}
template<typename T>
void excout(T A) {
  cout << A << endl;
  exit(0);
}
constexpr long long INF = (1LL << 60); // INFにちゅういい！


struct Edge
{
  int to;
  int cost;
};
using Graph = std::vector<std::vector<Edge>>;
using Pair = std::pair<long long, int>;
void Dijkstra(const Graph& graph, std::vector<long long>& distances, int startIndex)
{
  std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> q;
  q.emplace((distances[startIndex] = 0), startIndex);

  while (!q.empty())
  {
    const long long distance = q.top().first;
    const int from = q.top().second;
    q.pop();
    if (distances[from] < distance)
    {
      continue;
    }
    for (const auto& edge : graph[from])
    {
      const long long d = (distances[from] + edge.cost);
      if (d < distances[edge.to])
      {
        q.emplace((distances[edge.to] = d), edge.to);
      }
    }
  }
}
template<typename T>
T MODS(T a, T mods) {
  return ((((((a + mods) % mods) + mods) % mods)));
}
VVL comb(int n, int r) {
  VVL v(n + 1, VL (n + 1, 0));
  for (int i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int j = 1; j < v.size(); j++) {
    for (int k = 1; k < j; k++) {
      v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
    }
  }
  return v;
}
vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long p = 2; p * p <= N; ++p) {
        if (N % p != 0) {
            continue;
        }
        int e = 0;
        while (N % p == 0) {
            ++e;
            N /= p;
        }
        res.emplace_back(p, e);
    }
    if (N != 1) {
        res.emplace_back(N, 1);
    }
    return res;
}

struct UnionFind {
    vector<int> par, siz;
    UnionFind(int n) : par(n, -1), siz(n, 1) {}
    int root(int x) {
        if (par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x);y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }
    int size(int x) {
        return siz[root(x)];
    }
};
template<class Abel> struct WUnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  WUnionFind(int n = 1, Abel SUM_UNITY = 0) {
    init(n, SUM_UNITY);
  }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n); rank.resize(n); diff_weight.resize(n);
    for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    }
    else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) {
    return root(x) == root(y);
  }

  bool merge(int x, int y, Abel w) { //W(y)=W(x)+w
    w += weight(x); w -= weight(y);
    x = root(x); y = root(y);
    if (x == y) return false;
    if (rank[x] < rank[y]) swap(x, y), w = -w;
    if (rank[x] == rank[y]) ++rank[x];
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y)  { // W(y) - W(x)
    return weight(y) - weight(x);
  }
};
VI topo_sort(Graph0& G) {
  int N = G.size();
  VI IND(N, 0);
  rep(v, N) {
    arep(nv, G[v]) {
      IND[nv]++;
    }
  }
  queue<int> que;
  rep(v, N) {
    if (IND[v] == 0) {
      que.push(v);
    }
  }
  VI ANS;
  while (!que.empty()) {
    int v = que.front();
    ANS.push_back(v);
    que.pop();
    arep(nv, G[v]) {
      IND[nv]--;
      if (IND[nv] == 0) {
        que.push(nv);
      }
    }
  }
  return ANS;
}
void ADD(int a, int b, Graph0& G) {
  G[a].push_back(b);
  G[b].push_back(a);
}
VP near(int i, int j, int H, int W) {
  VP ans;
  VP cand = {{i - 1, j}, {i + 1, j}, {i, j - 1}, {i, j + 1}};
  arep(v, cand) {
    if (v.first < 0 or v.first >= H) continue;
    if (v.second < 0 or v.second >= W) continue;
    ans.push_back(v);
  }
  return ans;
}
int cast(int i, int j, int H, int W) {
  return ((W * i) + j);
}
ll pows(ll x, ll n, ll mod) {
    if (!n) return 1;
    x %= mod;
    ll r = pows(x, n / 2, mod);
    (r *= r) %= mod;
    if (n % 2) (r *=x) %= mod;
    return r;
}
struct COMB_MOD {
  ll mod;
  int MAX;
  VL fac, finv, inv;
  COMB_MOD(int max, ll m) {
    fac.assign(max, 0);
    finv.assign(max, 0);
    inv.assign(max, 0);
    mod = m;
    MAX = max;
  }
  void solve() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % mod;

        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
    
  }
  ll comb(int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
  }
};

struct LCA {
    vector<vector<int>> parent;  
    vector<int> dist;           
    LCA(const Graph0 &G, int root = 0) { init(G, root); }
    void init(const Graph0 &G, int root = 0) {
        int V = G.size();
        int K = 1;
        while ((1 << K) < V) K++;
        parent.assign(K, vector<int>(V, -1));
        dist.assign(V, -1);
        dfs(G, root, -1, 0);
        for (int k = 0; k + 1 < K; k++) {
            for (int v = 0; v < V; v++) {
                if (parent[k][v] < 0) {
                    parent[k + 1][v] = -1;
                } else {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }
    void dfs(const Graph0 &G, int v, int p, int d) {
        parent[0][v] = p;
        dist[v] = d;
        for (auto e : G[v]) {
            if (e != p) dfs(G, e, v, d + 1);
        }
    }
    int query(int u, int v) {
        if (dist[u] < dist[v]) swap(u, v);  
        int K = parent.size();
        for (int k = 0; k < K; k++) {
            if ((dist[u] - dist[v]) >> k & 1) {
                u = parent[k][u];
            }
        }
        if (u == v) return u;
        for (int k = K - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    int get_dist(int u, int v) { return dist[u] + dist[v] - 2 * dist[query(u, v)]; }
};
int main(void) {
  int N;cin >> N;
  VI A(N), W(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> W[i];
  int res = 1e9;
  VI P;
  rep(i, N) P.push_back(i);
  do {
    int sc = 0;
    rep(i, N) {
      if (A[P[i]] == 0 and A[P[(i+N-1)%N]] == 1) {
        sc += W[P[i]];
      }
      if (A[P[i]] == 1 and A[P[(i+N+1)%N]] == 0) {
        sc += W[P[i]];
      }
      
    }
    chmin(res, sc);
  }while(next_permutation(all(P)));
  cout << res << endl;

}
accuracy: 1
time_ms: 50
memory_kb: 3584
final_rank: 16

code_snippet: #include "bits/stdc++.h"
using namespace std;

#define REP(i, n) for(int i = 0; i < n; i++)
#define ll long long 

int main(){
    ll n;
    cin >> n;
    ll ans = 1ll << 60;
    vector<ll> a(n), w(n);
    REP(i, n)cin >> a[i];
    REP(i, n) cin >> w[i];
    vector<ll> s;
    REP(i, n)s.push_back(i);
    do{
      ll now = 0;
      REP(j, n){
        if(a[s[j]] == 0 and a[s[(j + 1) % n]] == 1){
            now += w[s[j]];
            now += w[s[(j + 1) % n]];
          }
      }
      ans = min(ans ,now);
    }
    while(next_permutation(s.begin(), s.end()));
    cout << ans <<endl;
}
accuracy: 1
time_ms: 180
memory_kb: 3428
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N;
    cin >> N;
    vector<int> a(N),w(N);
    rep(i,N)cin >> a[i];
    rep(i,N) cin >> w[i];

    vector<int> o(N);
    rep(i,N) o[i] = i;

    int ans = 1e9;
    do{
        int h = 0;
        rep(i,N){
            if(a[o[i]]==0 && a[o[(i+1)%N]]==1){
                h += w[o[i]];
                h += w[o[(i+1)%N]];
            }
        }
        ans = min(ans, h);
    } while (next_permutation(o.begin(),o.end()));
    cout << ans << endl;

}
accuracy: 1
time_ms: 60
memory_kb: 3452
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,N) for(ll i=0;i<N;i++)
#define all(A) A.begin(),A.end()
const ll INF = 1LL<<60;

int main(){
	ll N;
	cin >> N;
	vector<ll> A(N),W(N);
	rep(i,N) cin >> A[i];
	rep(i,N) cin >> W[i];
	vector<ll> P(N);
	rep(i,N) P[i]=i;
	ll ans=INF;
	do{
		ll cost=0;
		rep(i,N){
			ll j=i+1;
			if(j==N) j=0;
			if(A[P[i]]==0&&A[P[j]]==1) cost+=W[P[i]]+W[P[j]];
		}
		ans=min(ans,cost);	
	}while(next_permutation(all(P)));
	cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3432
final_rank: 4

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  cout << (x);
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}
template <typename T>
void wt_integer128(T x) {
  char buf[64];
  char *d = end(buf);
  d--; *d = '\0';
  __uint128_t tmp = ((x < 0)? -x : x);
  do {
    d--; *d = char(tmp%10 + '0'); tmp /= 10;
  } while (tmp);
  if (x < 0) {
    d--; *d = '-';
  }
  cout << d;
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer128(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer128(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {break;}
  return 0;
}

int solve() {
  ll N;
  cin >> N;
  vi a(N), w(N);
  rep(i, N) cin >> a[i];
  rep(i, N) cin >> w[i];
  vi p(N);
  iota(all(p), 0);
  ll ans = inf;
  do {
    ll sum = 0;
    rep(i, N) {
      if (a[p[i]] == 0) {
        if (a[p[(i+1)%N]] == 1) sum += w[p[i]];
      } else {
        if (a[p[(i+N-1)%N]] == 0) sum += w[p[i]];
      }
    }
    chmin(ans, sum);
  } while (next_permutation(all(p)));
  print(ans);
  return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 3460
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int N, a[10], w[10];
bool used[10];

int soln(vector<int> order) {
    if (order.size() == N) {
        int sum = 0;
        for (int i = 0; i < N; i++) {
            int l = order[i], r = order[(i+1)%N];
            if (a[l] == 0 && a[r] == 1) {
                sum += w[l] + w[r];
            }
        }
        return sum;
    }

    int min_sum = 10000;
    for (int i = 0; i < N; i++) {
        if (used[i]) {continue;}
        order.push_back(i); used[i] = true;
        min_sum = min(min_sum, soln(order));
        order.pop_back(); used[i] = false;
    }
    return min_sum;
}

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

    vector<int> order;
    cout << soln(order) << endl;

    return 0;
}
accuracy: 1
time_ms: 450
memory_kb: 3416
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF INT64_MAX;
int main(void)
{
    int n,mn=1000000;
    cin>>n;
    vector< pair < int , int > >  ab;
    vector<int> a(n),b(n);
    for(int i=0;i<n;i++) cin>>a[i];
    for(int i=0;i<n;i++) cin>>b[i];
    for(int i=0;i<n;i++) ab.push_back({a[i],b[i]});
    sort(ab.begin(),ab.end());
    do{
        // for(int i=0;i<n;i++) cout<<ab[i].first;
        // cout<<endl;
        int sum=0;
        for(int i=0;i<n;i++){
            if(i==0){
                if(ab[i].first==1 && ab[i+1].first==0) sum+=ab[i].second;
                else if(ab[i].first==0 && ab[n-1].first==1) sum+=ab[i].second;
            }
            else if(i==n-1){
                if(ab[i].first==1 && ab[0].first==0) sum+=ab[i].second;
                else if(ab[i].first==0 && ab[i-1].first==1) sum+=ab[i].second;
            }
            else{
                if(ab[i].first==1 && ab[i+1].first==0) sum+=ab[i].second;
                else if(ab[i].first==0 && ab[i-1].first==1) sum+=ab[i].second;
            }
            
        }
        mn=min(sum,mn);
    }while(next_permutation(ab.begin(),ab.end()));
    cout<<mn<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3088
final_rank: 1

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

using ll = long long;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int N;
	cin >> N;
	vector<int> A(N), W(N);
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
	}
	for (int i = 0; i < N; ++i) {
		cin >> W[i];
	}

	vector<int> ord(N);
	iota(begin(ord), end(ord), 0);
	int ans = 1000000;
	do {
		int sum = 0;
		for (int i = 0; i < N; ++i) {
			const int k = ord[i];
			if (A[k] == 0 and A[ord[(i + 1) % N]] == 1) {
				sum += W[k];
			}
			if (A[k] == 1 and A[ord[(i + N - 1) % N]] == 0) {
				sum += W[k];
			}
		}
		ans = min(ans, sum);
	} while (next_permutation(begin(ord), end(ord)));
	cout << ans << '\n';

	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3460
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int n, ans = 1ll << 30;
    cin >> n;
    vector<pair<int,int>> a(n);
    for(auto &&p:a)cin >> p.first;
    for(auto &&p:a)cin >> p.second;
    sort(a.begin(), a.end());
    do{
        int s = 0;
        for(int i = 0; i < n; i++){
            if(a[i].first == 0){
                if(a[(i - 1 + n) % n].first == 1)s += a[(i - 1 + n) % n].second;
            }else{
                if(a[(i + 1 + n) % n].first == 0)s += a[(i + 1 + n) % n].second;
            }
        }
        ans = min(ans, s);
    }while(next_permutation(a.begin(), a.end()));
    cout << ans << '\n';
}
accuracy: 1
time_ms: 90
memory_kb: 3444
final_rank: 9

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

// {{{ Templates

// clang-format off

// Macros
#define over_load_(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) over_load_(__VA_ARGS__, rep4, rep3, rep2)(__VA_ARGS__)
#define rep2(i, r) for ( int i = 0; i < static_cast<int>(r); (i) += 1)
#define rep3(i, l, r) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += 1)
#define rep4(i, l, r, stride) for ( int i = static_cast<int>(l); i < static_cast<int>(r); (i) += (stride))
#define rrep(...) over_load_(__VA_ARGS__, rrep4, rrep3, rrep2)(__VA_ARGS__)
#define rrep2(i, r) for ( int i = static_cast<int>(r) - 1; i >= 0; (i) -= 1)
#define rrep3(i, l, r) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= 1)
#define rrep4(i, l, r, stride) for ( int i = static_cast<int>(r) - 1; i >= static_cast<int>(l); (i) -= (stride))
#define len(x) (static_cast<int>((x).size()))
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(...) debug_function(#__VA_ARGS__, __VA_ARGS__)

// Operators
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { bool is_first = true; for (auto x: v) { os << (is_first ? "" : " ") << x; is_first = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> v) { bool is_first = true; while (!v.empty()) { os << (is_first?"":" ")<<v.front(); v.pop(); is_first = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> v) { bool is_first = true; while (!v.empty()) { os << (is_first?"":" ") << v.top(); v.pop(); is_first=false; } return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? "" : " "); return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { for (const auto &vec: v) { os << vec << '\n'; } return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { rep (i, len(v)) os << v[i] << (i == len(v) - 1 ? "" : " "); return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { bool is_first = true; for (T x: v) { os << (is_first ? "" : " ") << x; is_first = false; } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in: v) { is >> in; } return is; }

// For debug macro
int find_comma_not_bracketed(string_view s){ stack<char> bs; string lbs = "({[", rbs = ")}]"; for (size_t i = 0; i < s.size(); i++) { if (lbs.find(s[i]) != string::npos) bs.push(s[i]); if (rbs.find(s[i]) != string::npos and !bs.empty()) bs.pop(); if (s[i] == ',' and bs.empty()) return i; } return s.size(); }
template <typename T, typename... Ts> void debug_function(string_view name, const T &a, Ts &&...rest) { int end = find_comma_not_bracketed(name); cerr << name.substr(0, end) << ":" << a; if constexpr (sizeof...(rest) == 0) { cerr << '\n'; } else { cerr << ' '; debug_function(name.substr(name.find_first_not_of(' ', end + 1)), forward<Ts>(rest)...); } }

// Functions
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b and (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b and (a = b, true); }

// Structs
struct IoSetup { IoSetup(int x = 15) { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

// Type aliases
using ull = unsigned long long;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int, int>;

// Literals
constexpr ll INF64 = INT64_MAX / 2;
constexpr int INF32 = INT32_MAX / 2;
constexpr int dy[] = { 0, 1, -1, 0, -1, 1, -1, 1 };
constexpr int dx[] = { 1, 0, 0, -1, -1, -1, 1, 1 };
constexpr int mod998244353 = 998244353;
constexpr int mod1000000007 = static_cast<int>(1e9) + 7;
constexpr char newl = '\n';

// clang-format on

// }}} Templates


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

  vector<int> as(n), ws(n);
  cin >> as >> ws;


  vector<int> idxs(n);
  whole(iota, idxs, 0);

  int ans = INF32;

  do {
    int sum_w = 0;

    rep(i, n) {
      int idx = idxs[i];
      int a = as[idx];
      int w = ws[idx];

      if (a == 0) {
        int right_side = i + 1;
        if (right_side >= n)
          right_side = 0;

        int right_side_idx = idxs[right_side];
        int right_a = as[right_side_idx];

        if (a == 0 and right_a == 1) {
          sum_w += w;
        }
      }

      if (a == 1) {
        int left_side = i - 1;
        if (left_side < 0)
          left_side = n - 1;

        int left_side_idx = idxs[left_side];
        int left_a = as[left_side_idx];

        if (a == 1 and left_a == 0) {
          sum_w += w;
        }
      }
    }

    chmin(ans, sum_w);
  } while (whole(next_permutation, idxs));


  cout << ans << newl;
}
accuracy: 1
time_ms: 60
memory_kb: 3452
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = (n) - 1; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n) - 1; (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int N;
    cin >> N;
    vc<int> a(N), w(N);
    rep (i, N) cin >> a[i];
    rep (i, N) cin >> w[i];

    vc<int> array(N);
    rep (i, N) array[i] = i;

    int ans = 100000;
    do {
        int val = 0;
        rep (i, N) {
            if (a[array[i]] == 0) {
                if (a[array[(i+1)%N]] == 1) val += w[array[i]];
            }
            else {
                if (a[array[(i-1+N)%N]] == 0) val += w[array[i]];
            }
        }
        chmin(ans, val);
    } while (next_permutation(all(array)));

    cout << ans << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 3464
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 10000;
int main(){
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++){
    cin >> a[i];
  }
  vector<int> w(N);
  for (int i = 0; i < N; i++){
    cin >> w[i];
  }
  vector<int> p(N);
  for (int i = 0; i < N; i++){
    p[i] = i;
  }
  int F = 1;
  for (int i = 1; i <= N; i++){
    F *= i;
  }
  int ans = INF;
  for (int i = 0; i < F; i++){
    int s = 0;
    for (int j = 0; j < N; j++){
      if (a[p[j]] == 0){
        int j2 = (j + 1) % N;
        if (a[p[j2]] == 1){
          s += w[p[j]];
        }
      }
      if (a[p[j]] == 1){
        int j2 = (j + N - 1) % N;
        if (a[p[j2]] == 0){
          s += w[p[j]];
        }
      }
    }
    ans = min(ans, s);
    next_permutation(p.begin(), p.end());
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 3484
final_rank: 14

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

using namespace std;
using ll = long long;
using pint = pair<int,int>;


int main()
{
	int N; cin >> N;
	vector<pint> A(N);
	for(int i = 0; i < N; i++) cin >> A[i].first;
	for(int i = 0; i < N; i++) cin >> A[i].second;
	sort(A.begin(), A.end());
	int ans = 1e9;
	do {

		vector<pint> B = A;
		B.push_back(A[0]);
		int sum = 0;
		for(int i = 0; i < N; i++) {
			if(B[i].first == 1 && B[i + 1].first == 0)sum += B[i].second + B[i + 1].second;
		}
		ans = min(ans, sum);
	}while(next_permutation(A.begin(), A.end()));
	cout << ans << endl;
}
accuracy: 1
time_ms: 160
memory_kb: 3416
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

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

    vector<int> order(n);
    for(int i = 0; i < n; i++) order[i] = i;

    int ans = 1001001001;
    do{
        int sum = 0;
        for(int i = 0; i < n; i++){
            if(a[order[i]] == 0 && a[order[(i+1)%n]] == 1){
                sum += w[order[i]];
            }
            if(a[order[i]] == 1 && a[order[(i+n-1)%n]] == 0){
                sum += w[order[i]];
            }
        }
        ans = min(ans, sum);
    }while(next_permutation(order.begin(), order.end()));

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3440
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)

#pragma define "Wall"

using ll = long long;

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

  vector<pair<int, int>> a(n);
  rep(i, n) {cin >> a[i].first; a[i].second = i;}

  vector<int> w(n);
  rep(i, n) cin >> w[i];

  sort(a.begin() + 1, a.end());
  int ans = 1001001001;
  do {
    int tmp = 0;
    for (size_t i = 0; i < n; i++) {
      int li = (i + n - 1) % n, ri = (i + 1) % n;

      int ai = a[i].first, l = a[li].first, r = a[ri].first;
      if ((ai == 0 && r == 1) || (l == 0 && ai == 1)) {
        tmp += w[a[i].second];
      }
    }

    ans = min(ans, tmp);
  } while (next_permutation(a.begin() + 1, a.end()));
  std::cout << ans << std::endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3448
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using std::string;

int main() {
  int n; std::cin >> n;
  std::vector<int> a(n), w(n);
  for (int i = 0; i < n; ++i) std::cin >> a[i];
  for (int i = 0; i < n; ++i) std::cin >> w[i];

  std::vector<int> perm;
  for (int i = 0; i < n; ++i) perm.push_back(i);
  int ans = 100000;
  do {
    int w_sum = 0;
    for (int i = 0; i < n; ++i) {
      int idx = perm[i];

      int right_side = perm[(i + 1) % n];
      int left_side = perm[(i + n - 1) % n];
      if (
        a[idx] == 0 && a[right_side] == 1 ||
        a[idx] == 1 && a[left_side] == 0
      ) {
        w_sum += w[idx];
      }
    }
    ans = std::min(ans, w_sum);
  } while (std::next_permutation(perm.begin() + 1, perm.end()));

  std::cout << ans << std::endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3448
final_rank: 3

code_snippet: #include<bits/stdc++.h>
#define newl endl
#define all(vec) vec.begin(),vec.end()
using namespace std;
template<class T>
using V=vector<T>;
using ll=long long;
const ll INF=1LL<<30;
int main(){
    int n;cin>>n;
    V<int> a(n),w(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<n;i++){
        cin>>w[i];
    }
    V<int> b(n);
    for(int i=0;i<n;i++)b[i]=i;
    int res=INF;
    do{
        int c=0;
        for(int i=0;i<n;i++){
            int s=b[i],t=b[(i+1)%n];
            if(a[s]==1&&a[t]==0){
                c+=w[s]+w[t];
            }
        }
        res=min(res,c);
    }while(next_permutation(all(b)));
    cout<<res<<newl;
}
accuracy: 1
time_ms: 90
memory_kb: 3100
final_rank: 2