#include <bits/stdc++.h>   using namespace std;   int w[505], p[1005], t[5001], was[105];   int main() {     int a, b, c, d, f = 0, n, m, x, y, i, j;     memset(was, 0, sizeof was);     memset(t, 0, sizeof t);     scanf( %d %d , &n, &m);     for (a = 1; a <= n; a++) scanf( %d , &w[a]);     for (a = 1; a <= m; a++) scanf( %d , &p[a]);     t[1] = p[1];     d = 2;     for (a = 2; a <= m; a++) {       y = 0;       for (i = 1; i < d; i++) {         if (t[i] == p[a]) {           for (b = i; b > 1; b--) {             t[b] = t[b - 1];             f += w[t[b]];           }           t[1] = p[a];           y = 1;           break;         }       }       if (y == 0) {         for (b = d; b > 1; b--) {           t[b] = t[b - 1];           f += w[t[b]];         }         t[1] = p[a];         d++;       }     }     printf( %d n , f);     return 0;   }   