Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cassert>
- #define MAXN 100005
- #define VAL(x) (x.first)
- #define COLOR(x) (x.second)
- using namespace std;
- typedef long long int ll;
- int N, Q, C[MAXN];
- ll best[MAXN],V[MAXN], A, B, ans;
- bool visit[MAXN];
- pair<ll,int> t1,t2,cur;
- ////USE COUT FOR LONG LONG!!!
- /*
- same A
- different B
- first B
- */
- int main(){
- // freopen("C.in","r",stdin);
- // freopen("C.out","w",stdout);
- scanf("%d%d",&N, &Q);
- for(int i=1;i<=N;++i) cin >> V[i];
- for(int i=1;i<=N;++i) scanf("%d",&C[i]);
- for(int z=0;z<Q;++z){
- cin >> A >> B;
- if(A <= 0 && B <= 0){
- printf("0\n");
- continue;
- }
- //Init
- t1 = pair<ll,int>(0,0);
- t2 = pair<ll,int>(0,-1);
- ans = 0;
- memset(visit,0,sizeof(visit));
- for(int i=1;i<=N;++i){
- //Compute
- cur = pair<ll,int>(B * V[i],C[i]); //first
- //get different colour
- if(COLOR(t1) != COLOR(cur) && VAL(t1) > 0) VAL(cur) += VAL(t1);
- else if(COLOR(t2) != COLOR(cur) && VAL(t2) > 0) VAL(cur) += VAL(t2);
- //get same color
- if(visit[C[i]]) VAL(cur) = max(VAL(cur), best[C[i]] + A * V[i]);
- ans = max(ans,VAL(cur));
- //Update
- if(!visit[C[i]]) best[C[i]] = VAL(cur);
- else best[C[i]] = max(VAL(cur),best[C[i]]);
- visit[C[i]] = 1;
- //cur has different color from both t1 and t2
- if(COLOR(cur) != COLOR(t1) && COLOR(cur) != COLOR(t2)){
- if(VAL(cur) >= VAL(t1)){
- t2 = t1;
- t1 = cur;
- }
- else if(VAL(cur) > VAL(t2)) t2 = cur;
- }
- //cur has same color with EXACTLY one of them
- else{
- if(COLOR(cur) == COLOR(t1)) VAL(t1) = max(VAL(t1),VAL(cur));
- else VAL(t2) = max(VAL(t2),VAL(cur));
- if(VAL(t1) < VAL(t2)) swap(t1,t2);
- }
- assert(VAL(t1) >= VAL(t2));
- assert(COLOR(t1) != COLOR(t2));
- }
- cout << ans << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement