Source Code
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stdlib.h>
#include <time.h>
#include <stack>
#include <map>
#include <math.h>
#include <cmath>
#include <string.h>
#include <numeric>
using namespace std;
typedef vector<int> vi ;
typedef long long ll;
#define all(x) (x).begin() , (x).end()
#define pb push_back
const int N = 1e6+5, M=1e6+1 , MX = 1e6+1;
const ll MXL=1e18 , MOD = 1e9 + 7 ;
int a[N], b[N], cost[N], dp[N], indx[N], neg;
int main(){
// 	freopen("input.txt", "r", stdin);
	int n, m;
	cin >> n >> m;
	for(int i=1 ; i<=n ; i++ ){
		scanf("%d" , a+i );
	}
	for(int i=1 ; i<=n ; i++ ){
		scanf("%d" , cost+i );
		if( cost[i] <= 0 ) 
			neg+=cost[i];
		else
			cost[i]=0;
	}
	for(int i=1 ; i<=m ; i++ ){
		scanf("%d" , b+i );
		indx[b[i]]=i;
		// cout << indx[b[i]] <<" "<<b[i]<<"\n";
		dp[i]=-1*MX;
	}
	for(int i=1 ; i<=n ; i++ ){
		if( indx[a[i]] ){
			int in = indx[a[i]];
			// cout<<"*"<< in <<" ";
			dp[in] = max( dp[in] , cost[i]+ dp[in-1]);
			// cout << dp[in] <<"\n";
		}
	}
	// for(int i=1 ; i<=m ; i++ )
	cout<< neg - dp[m]<<" ";
	
	
  return 0 ;
}
Copy
Always with Me, Always with You Rand()
GNU G++17
151 ms
12.6 MB
Wrong Answer