func_code_string: #include <bits/stdc++.h>   using namespace std;   const int N = 5e5 + 10;   const int K = 23;   const long long INF = 1e18;   int id[N], a[N], b[N], w[N];   int n, m;   vector<int> pos[N];   long long dp[N];   int ls[N * K], rs[N * K], rootcnt = 0;   long long vs[N * K];   void copy(int root, int newroot) {     ls[newroot] = ls[root];     rs[newroot] = rs[root];     vs[newroot] = vs[root];   }   void updates(int root, int& newroot, int l, int r, int pos, int v) {     newroot = ++rootcnt;     copy(root, newroot);     vs[newroot] += v;     if (l == r || v == 0) return;     int m = (l + r) >> 1;     if (pos <= m)       updates(ls[root], ls[newroot], l, m, pos, v);     else       updates(rs[root], rs[newroot], m + 1, r, pos, v);   }   long long querys(int lt, int rt, int l, int r, int a, int b) {     if (l == a && r == b) {       return vs[rt] - vs[lt];     }     int m = (l + r) >> 1;     if (b <= m)       return querys(ls[lt], ls[rt], l, m, a, b);     else if (a > m)       return querys(rs[lt], rs[rt], m + 1, r, a, b);     else {       return querys(ls[lt], ls[rt], l, m, a, m) +              querys(rs[lt], rs[rt], m + 1, r, m + 1, b);     }   }   long long val[N << 2];   long long tag[N << 2];   void build(int root, int l, int r) {     val[root] = -INF;     tag[root] = 0;     if (l == r) return;     int m = (l + r) >> 1;     build((root << 1), l, m);     build((root << 1 | 1), m + 1, r);   }   void pushdown(int root) {     long long c = tag[root];     tag[root] = 0;     val[(root << 1)] += c;     tag[(root << 1)] += c;     val[(root << 1 | 1)] += c;     tag[(root << 1 | 1)] += c;   }   void setv(int root, int l, int r, int pos, long long v) {     if (l == r) {       val[root] = v;       return;     }     pushdown(root);     int m = (l + r) >> 1;     if (pos <= m)       setv((root << 1), l, m, pos, v);     else       setv((root << 1 | 1), m + 1, r, pos, v);     val[root] = max(val[(root << 1)], val[(root << 1 | 1)]);   }   void update(int root, int l, int r, int a, int b, long long v) {     if (v == 0) return;     if (a > b) return;     if (l == a && r == b) {       val[root] += v;       tag[root] += v;       return;     }     pushdown(root);     int m = (l + r) >> 1;     if (b <= m)       update((root << 1), l, m, a, b, v);     else if (a > m)       update((root << 1 | 1), m + 1, r, a, b, v);     else {       update((root << 1), l, m, a, m, v);       update((root << 1 | 1), m + 1, r, m + 1, b, v);     }     val[root] = max(val[(root << 1)], val[(root << 1 | 1)]);   }   long long query(int root, int l, int r, int a, int b) {     if (a > b) return -INF;     if (l == a && r == b) return val[root];     pushdown(root);     int m = (l + r) >> 1;     if (b <= m)       return query((root << 1), l, m, a, b);     else if (a > m)       return query((root << 1 | 1), m + 1, r, a, b);     else       return max(query((root << 1), l, m, a, m),                  query((root << 1 | 1), m + 1, r, m + 1, b));   }   bool hasAns() {     int len = 0;     for (int i = 1; i <= n && len < m; i++) {       if (a[i] == b[len + 1]) len++;     }     return len == m;   }   int root[N];   long long lessSum(int l, int r, int v) {     if (l > r || v <= 0) return 0;     return querys(root[l - 1], root[r], 1, n, 1, v);   }   void solve() {     scanf( %d , &n);     root[0] = 0;     for (int i = 1; i <= n; i++) scanf( %d , a + i);     for (int i = 1; i <= n; i++) {       scanf( %d , w + i);       updates(root[i - 1], root[i], 1, n, a[i], max(0, w[i]));     }     scanf( %d , &m);     for (int i = 1; i <= m; i++) scanf( %d , b + i), id[b[i]] = i;     for (int i = 1; i <= n; i++) {       if (id[a[i]]) pos[id[a[i]]].push_back(i);     }     if (!hasAns()) {       printf( NO n );       return;     }     printf( YES n );     for (int i = 1; i <= n; i++) dp[i] = -INF;     build(1, 1, n);     for (auto p : pos[m]) {       dp[p] = lessSum(p + 1, n, b[m]) + w[p];       setv(1, 1, n, p, dp[p]);     }     for (int i = m - 1; i >= 1; i--) {       vector<int> vec;       for (auto j : pos[i + 1]) vec.push_back(j);       for (auto j : pos[i]) vec.push_back(j);       sort(vec.begin(), vec.end());       for (int j = (int)vec.size() - 2; j >= 0; j--) {         update(1, 1, n, vec[j + 1], n, lessSum(vec[j] + 1, vec[j + 1] - 1, b[i]));         if (a[vec[j]] == b[i]) {           dp[vec[j]] = query(1, 1, n, vec[j + 1], n) + w[vec[j]];           if (w[vec[j]] > 0) {             update(1, 1, n, vec[j + 1], n, w[vec[j]]);           }         }       }       for (int j = (int)vec.size() - 2; j >= 0; j--) {         update(1, 1, n, vec[j + 1], n,                -lessSum(vec[j] + 1, vec[j + 1] - 1, b[i]));         if (a[vec[j]] == b[i]) {           if (w[vec[j]] > 0) {             update(1, 1, n, vec[j + 1], n, -w[vec[j]]);           }         }       }       for (auto j : pos[i + 1]) {         setv(1, 1, n, j, -INF);       }       assert(val[1] == -INF);       for (auto j : pos[i]) {         setv(1, 1, n, j, dp[j]);       }     }     long long ans = 0;     for (int i = 1; i <= n; i++) ans += w[i];     ans -= val[1];     printf( %lld n , ans);   }   int main() {     solve();     return 0;   }   