Source Code
/*
	Failure is not the reason for you to stop trying
	   it is actually a remainder that you should
			KEEP GOING
*/
#include<bits/stdc++.h>
#define ll long long
#define SaveTime ios_base::sync_with_stdio(false), cin.tie(0);
using namespace std;

int main()
{
   SaveTime
   int n,m; cin >> n >> m;
   vector<deque<int>> v(n+1);
   bool visited[n+1];
   int nb[n+1];
   memset(nb, 0, sizeof nb);
   int num[n];
   for (int i = 0; i < n; i++) {
      cin >> num[i];
      nb[num[i]]++;
   }
   for (int i = 0; i < n; i++) {
      int x; cin >> x;
      v[num[i]].push_back(x);
   }
   for (int i = 0; i <= n; i++) {
      sort(v[i].begin(), v[i].end());
   }
   for (int i = 0; i < m; i++) {
      int x; cin >> x;
      nb[x]--;
   }
   //cout << nb[1] << endl;
   ll cost = 0;
   memset(visited, false, sizeof visited);
   for (int j = 0; j < n; j++) {
      int i = num[j];
      if (visited[i])
         continue;
      visited[i] = true;
      while (nb[i] > 0) {
         if (v[i][0] < 0) {
            cost+= v[i][0];
            //cout << cost << ' ' << i << endl;
            v[i].pop_front();
            nb[i]--;
         }
         else
            break;
      }
   }
   cout << cost;

}
Copy
Always with Me, Always with You chaffar107
GNU G++17
144 ms
262.1 MB
Memory Limit Exceeded