#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; void Main() { int N,M,X; cin >> N >> M >> X; vector A(N),B(N); vector> G(M); for(int i = 0;i < N;i++) { cin >> A[i] >> B[i]; B[i]--; G[B[i]].push_back(A[i]); } int K; cin >> K; vector C(N + 1); for(int i = 0;i < K;i++) { int c; cin >> c; C[c]++; } multiset> S,T; for(int i = 0;i < N;i++) { S.insert(make_pair(A[i],B[i])); } vector Q; Q.reserve(N); vector nw(N); for(int i = 0;i < N;i++) { if(T.empty()) { pair P = *S.rbegin(); nw[i] = 1; Q.push_back(P.first); for(int a : G[P.second]) { auto it = S.find(make_pair(a,P.second)); S.erase(it); T.insert(make_pair(a,P.second)); } T.erase(T.find(P)); } else if(S.empty()) { pair P = *T.rbegin(); Q.push_back(P.first); T.erase(T.find(P)); } else { pair L = *S.rbegin(),R = *T.rbegin(); if(L.first + X > R.first) { nw[i] = 1; Q.push_back(L.first); for(int a : G[L.second]) { auto it = S.find(make_pair(a,L.second)); S.erase(it); T.insert(make_pair(a,L.second)); } T.erase(T.find(L)); } else { Q.push_back(R.first); T.erase(T.find(R)); } } } long long ans = 0; int cnt = 0; long long sum = 0; for(int i = 0;i < N;i++) { if(nw[i]) { cnt++; } sum += Q[i]; ans += (sum + (long long) X * cnt) * C[i + 1]; } cout << ans << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tt = 1; /* cin >> tt; */ while(tt--) Main(); }