-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cc
60 lines (52 loc) · 1.29 KB
/
Solution.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <list>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define VAR(V,init) __typeof(init) V=(init)
#define FOREACH(I,C) for(VAR(I,(C).begin());I!=(C).end();I++)
#define FOR(I, S, E) for(__typeof(S) I=(S); I < (E); ++I)
void readInt64(int64_t *v) {
scanf("%"SCNd64, v);
}
void printInt64(int64_t v) {
printf("%"PRId64, v);
}
#define MAX 1010
int main(int argc, char *argv[]) {
int n, m;
scanf("%d %d", &n, &m);
int w[MAX], b[MAX];
FOR (i, 0, n) { scanf("%d", &w[i]); }
FOR (i, 0, m) { scanf("%d", &b[i]); }
list<int> s;
set<int> books; // books that has been read
int ans = 0;
int sumSofar = 0;
FOR (i, 0, m) {
if (books.find(b[i]) == books.end()) {
ans += sumSofar;
books.insert(b[i]);
s.push_front(b[i]);
sumSofar += w[b[i]-1];
} else {
FOREACH (j, s) {
if (*j == b[i]) {
s.erase(j);
s.push_front(b[i]);
break;
}
ans += w[*j-1];
}
}
}
printf("%d\n", ans);
return 0;
}