题目链接: http://codeforces.com/problemset/problem/264/C
#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e5+100;
const int ub=1e6;
const ll INF=1e18;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);
}
int n,m,c[maxn],v[maxn],id1,id2;
ll dp[maxn],ans,max1,max2;
ll solve(int x){if(id1==-1) return -INF;if(x==id1){if(id2!=-1) return max2;else return -INF;} else{return max1;}
}
int main(){scanf("%d%d",&n,&m);rep(i,1,n+1) scanf("%d",&v[i]);rep(i,1,n+1) scanf("%d",&c[i]);rep(i,0,m){int a,b;scanf("%d%d",&a,&b);rep(j,1,n+1) dp[j]=-INF;ans=-INF,max1=-INF,max2=-INF;id1=-1,id2=-1;rep(j,1,n+1){ll val=max(dp[c[j]]+1LL*a*v[j],1LL*b*v[j]+solve(c[j]));val=max(val,1LL*b*v[j]);dp[c[j]]=max(dp[c[j]],val);ans=max(ans,dp[c[j]]);if(val>=max1){if(id1==c[j]) max1=val;else {max2=max1,id2=id1;max1=val,id1=c[j];}}else if(val>max2){if(id1!=c[j])max2=val,id2=c[j];}}printf("%lld\n",max(0LL,ans));}return 0;
}