Source Code
#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 7;
int n , m ; 
vector<int> a;
int pl[N] ;  
long long c[N] ; 

int main()
{
       ios_base::sync_with_stdio(0);
       cin.tie(0);
#ifndef ONLINE_JUDGE
#endif
       
       cin >> n >> m; 

       a.resize(n) ; 
       for(auto &u : a)cin >> u ; 
       for(int i = 0 ;i < n; ++ i)cin >> c[i] ; 
       for(int i = 0 ;i < m ;++ i){
              int x; cin >> x; 
              pl[x] = i + 1; 
       }
       long long ad = 0 ; 
       for(int i = 0 ;i < n;++ i){
              if(pl[a[i]]){
                     a[i] = pl[a[i]] ; 
              }else{
                     if( c[i] > 0 ){
                            ad += c[i] ; 
                     }
                     a[i] = - 1; 
              }
       }

       long long ans = 0 ; 
       vector<long long > dp(m + 2 , -1e18) ; 
       dp[0] = 0 ; 
       for(int i = 0 ;i < n;++ i){
              if(a[i] == -1)continue ; 
              dp[ a[i] ] = max(dp[a[i]] , dp[a[i] - 1] + c[i]) ; 
       }
       cout << accumulate(c , c + n , 0ll) - dp[m] - ad; 
       return 0;
}
Copy
Always with Me, Always with You Mohamed.Sobhy
GNU G++17
133 ms
15.8 MB
Wrong Answer