func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const long double PI = 4 * atan((long double)1);   const long long int INF = 1e18;   const long long int NINF = -1e18;   long long int get_hash(string s) {     long long int N = 1000001;     long long int base[N], A = 11, MD = 1110111110111;     base[0] = 1;     for (long long int i = (1); i < (N); ++i) base[i] = (base[i - 1] * A) % MD;     long long int hs = 0;     for (long long int i = (0); i < (s.size()); ++i) {       hs += (s[i] * base[i]);       hs %= MD;     }     return hs;   }   long long power(long long a, long long n) {     long long res = 1;     while (n) {       if (n % 2) res *= a;       a *= a;       n /= 2;     }     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     int n, m;     cin >> n >> m;     map<int, int> mpc;     long long int a[n], b[m];     for (long long int i = (0); i < (n); ++i) cin >> a[i];     int arr[n];     map<int, int> mpa, mpb;     int k = 0;     for (long long int i = (0); i < (m); ++i) {       cin >> b[i];       if (mpa[b[i]] != 1) {         arr[k] = b[i];         k++;         mpa[b[i]] = 1;       }     }     if (k != n) {       for (long long int i = (1); i < (n + 1); ++i) {         if (mpa[i] != 1) {           mpa[i] = 1;           arr[k] = i;           k++;         }       }     }     stack<int> st, st1;     for (int i = n - 1; i >= 0; i--) st.push(arr[i]);     long long int ans = 0;     for (long long int i = (0); i < (m); ++i) {       int x = b[i];       while (st.top() != x) {         st1.push(st.top());         ans += a[st.top() - 1];         st.pop();       }       st.pop();       while (!st1.empty()) {         st.push(st1.top());         st1.pop();       }       st.push(x);     }     cout << ans;     return 0;   }   