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);
   set<int> s;
   map<int,int> nb; map<int,int> nb1;
   int num[n];
   for (int i = 0; i < n; i++) {
      cin >> num[i];
      nb[num[i]]++;
      s.insert(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;
      nb1[x]++;
   }
   ll cost = 0;
   for (int i : s) {
      while (nb[i] != nb1[i]) {
         if (v[i][0] < 0) {
            cost+= v[i][0];
            v[i].pop_front();
            nb[i]--;
         }
         else
            break;
      }
   }
   cout << cost;
}
Copy
Always with Me, Always with You chaffar107
GNU G++17
145 ms
262.1 MB
Memory Limit Exceeded