#include "bits/stdc++.h"

using namespace std;

#define ll long long
#define ld long double

#define f first
#define s second
#define pb push_back

#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define sz(li) (int) (li).size()
#define sum(a) (accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vvl vector<vl>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>

#define endl '\n'

#define rep(i, a, b) for(int i = (a); i < (b); i++)

template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) { cout << "[ "; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << " ]"; }
template<typename A> ostream& operator<<(ostream &cout, vector<vector<A>> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) { cout << " "; } cout << v[i]; if (i < v.size() - 1) { cout << endl; } } return cout << "]"; }
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; }
template<typename T, typename U> void cmin(T& a, U b){ if (a > b) a = b; }
template<typename T, typename U> void cmax(T& a, U b){ if (a < b) a = b; }

void solve() {
  int n; cin >> n;
  vi t(n);

  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }

  int mx = accumulate(all(t), 0);

  vector<bool> possible(mx + 1);
  possible[0] = true;

  vi can;

  for (auto& T : t) {
    for (int i = sz(possible) - 1; i >= T; i--) {
      if (possible[i - T] && !possible[i]) {
        can.pb(i);
        possible[i] = true;
      }
    }
  }

  sort(all(can), greater<int>());

  int mn = INT_MAX;
  for (auto& s : can) {
    mn = min(mn, max(s, mx - s));
  }

  cout << mn << endl;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

  int t = 1;
  // cin >> t;

  while (t--) {
    solve();
  }

  cerr << "Time: " << 1000.0*clock()/CLOCKS_PER_SEC << " ms\n";
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(void){
    int N;
    cin >> N;
    vector<int> A(N);
    vector<int> B(N);
    vector<int> C(N);
    vector<int> cnt(N);

    
    for(int i=0;i<N;i++){
        int t;
        cin >> t;
        A[i] = t-1;
    }
    for(int i=0;i<N;i++){
        int t;
        cin >> t;
        B[i] = t-1;
    }
    for(int i=0;i<N;i++){
        int t;
        cin >> t;
        C[i] = t-1;
    }


    long long Ans = 0;
    


    for(int i=0;i<N;i++){
        cnt[B[C[i]]]+=1;
    }
    for(int i=0;i<N;i++){
        Ans+=cnt[A[i]];
    }
    cout << Ans << endl;
    return 0;
    
}