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(c[i] > 0){
                     ad += c[i] ; 
                     c[i] = 0 ; 
              }
              if(pl[a[i]]){
                     a[i] = pl[a[i]] ; 
              }else{
                     a[i] = -1; 
              }
       }

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