#include <stdio.h>
#include <iostream>
#include <set>

using namespace std;

struct MaxTracker
{
	long long val;
	std::set<int> colors;
};

const int MAX_N = 100002;
const long long MAX_S = 100000000000000000;			//Do ket qua co the toi 10^15
long long V[MAX_N], C[MAX_N];
long long S[MAX_N];					//Ket qua tot nhat co duoc khi chon vi tri i lam duoi cua 1 mang
long long maxStoC[MAX_N];			//Ket qua tot nhat cua mang "co duoi loai i" o thoi diem dang xet
MaxTracker MT1, MT2;			//MT1. val != MT2. val && MT1.colors != MT2.colors (khac nhau tung doi mot)
int n, q, i;
long long a, b;
long long ans[501], ans_tmp;			//chua danh sach cac ket qua cua query

//Doc du lieu ban dau
void input()
{
	cin >> n >> q;
	for (i = 0; i < n; i++)
		cin >> V[i];
	for (i = 0; i < n; i++)
		cin >> C[i];
}

//Khoi tao
void init()
{
	MT1.val = -MAX_S;
	MT2.val = -MAX_S;
	MT1.colors.clear();
	MT2.colors.clear();

	/*memset(S, -MAX_S, MAX_N);
	memset(maxStoC, -MAX_S, MAX_N);*/
	for (int ii = 0; ii < n; ii++)
	{
		S[ii] = -MAX_S;
		maxStoC[ii] = -MAX_S;
	}

	ans_tmp = -MAX_S;
}

//=======XU LI========
void calculateSu(int u)
{
	S[u] = V[u] * b;			//Truong hop dau tien, chuoi chi co 1 phan tu la chinh no
	S[u] = fmax(S[u], maxStoC[C[u]] + V[u] * a);

	if (MT1.colors.size() > 1)				//neu colors co so luong > 2
	{
		S[u] = fmax(S[u], MT1.val + V[u] * b);		//chung to rang ton tai 1 mang duoi "khac loai" va co S = val, ta se noi duoi voi mang do.
	}
	else if (MT1.colors.size() == 1)
	{
		std::set<int>::iterator it = MT1.colors.find(C[u]);

		//tim ra Ci && chi co duy nhat Ci co S=val
		//=> Khong ton tai mang duoi "khac loai" nao co S = val, ta phai xet toi MT2 (vi cac colors MT2 != color MT1)
		if (it != MT1.colors.end())
			S[u] = fmax(S[u], MT2.val + V[u] * b);
		else
			S[u] = fmax(S[u], MT1.val + V[u] * b);		//neu khong tim ra Ci thi chac chan co 1 mang "khac loai" co S = val.
	}
}

//=====CAP NHAT
void update(int u)
{
	maxStoC[C[u]] = fmax(maxStoC[C[u]], S[u]);
	if (MT1.val < S[u])
	{
		MT2 = MT1;
		std::set<int>::iterator it2 = MT2.colors.find(C[u]);			//tim xem co Ci trong tap cu khong, co thi xoa di
		if (it2 != MT2.colors.end())
			MT2.colors.erase(it2);

		MT1.val = S[u];
		MT1.colors.clear();				//xoa sach danh sach cu
		MT1.colors.insert(C[u]);
	}
	else if (MT1.val == S[u])
	{
		MT1.colors.insert(C[u]);		//set co kha nang tu kiem tra phan tu trung, nen minh khong can check
	}
	else if (MT2.val < S[u])
	{
		MT2.val = S[u];
		MT2.colors.clear();
		MT2.colors.insert(C[u]);
	}
	else if (MT2.val == S[u])
	{
		MT2.colors.insert(C[u]);
	}
}

//Xu li
void process()
{
	for (i = 0; i < n; i++)
	{
		calculateSu(i);
		update(i);
	}

	ans_tmp = MT1.val;
	//Neu ket qua nhan duoc la am => Lay so 0
	if (ans_tmp < 0)
		ans_tmp = 0;
}

//Xuat
void output()
{
	for (i = 0; i < q; i++)
		cout << ans[i] << endl;
}

void main()
{
	input();
	for (int q0 = 0; q0 < q; q0++)
	{
		cin >> a >> b;
		init();
		process();
		ans[q0] = ans_tmp;
	}
	output();
}


#include <stdio.h>
#include <iostream>
#include <set>

using namespace std;

struct MaxTracker
{
	long long val;
	//std::set<int> colors;
	int color;
};

const int MAX_N = 100003;
const long long MAX_S = 1000000000000000000;			//Do ket qua co the toi 10^15
long long V[MAX_N], C[MAX_N];
long long S[MAX_N];					//Ket qua tot nhat co duoc khi chon vi tri i lam duoi cua 1 mang
long long maxStoC[MAX_N];			//Ket qua tot nhat cua mang "co duoi loai i" o thoi diem dang xet
MaxTracker MT1, MT2;			//MT1. val != MT2. val && MT1.color != MT2.color
int n, q, i;
long long a, b;
long long ans[501], ans_tmp;			//chua danh sach cac ket qua cua query

//Doc du lieu ban dau
void input()
{
	cin >> n >> q;
	for (i = 0; i < n; i++)
		cin >> V[i];
	for (i = 0; i < n; i++)
		cin >> C[i];
}

//Khoi tao
void init()
{
	MT1.val = -MAX_S;
	MT2.val = -MAX_S;

	/*memset(S, -MAX_S, MAX_N);
	memset(maxStoC, -MAX_S, MAX_N);*/
	for (int ii = 0; ii <= n; ii++)			//ii = n la vi Ci co the bang n.
	{
		S[ii] = -MAX_S;
		maxStoC[ii] = -MAX_S;
	}

	ans_tmp = -MAX_S;
}

//=======XU LI========
void calculateSu(int u)
{
	//if (MT1.colors.size() > 1)				//neu colors co so luong > 1
	//{
	//	S[u] = fmax(S[u], MT1.val + V[u] * b);		//chung to rang ton tai 1 mang duoi "khac loai" va co S = val, ta se noi duoi voi mang do.
	//}
	//else if (MT1.colors.size() == 1)
	//{
	//	std::set<int>::iterator it = MT1.colors.find(C[u]);
	//	//tim ra Ci && chi co duy nhat Ci co S=val
	//	//=> Khong ton tai mang duoi "khac loai" nao co S = val, ta phai xet toi MT2 (vi cac colors MT2 != color MT1)
	//	if (it != MT1.colors.end())
	//		S[u] = fmax(S[u], val2 + V[u] * b);
	//	else
	//		S[u] = fmax(S[u], MT1.val + V[u] * b);		//neu khong tim ra Ci thi chac chan co 1 mang "khac loai" co S = val.
	//}

	long long tmpS = 0;
	if (MT1.color == C[i])			//cai 1 chinh la Ci, thi lay cai thu 2
		tmpS = MT2.val;
	else tmpS = MT1.val;
	//Truong hop dau tien, chuoi chi co 1 phan tu la chinh no = V[u] * b
	S[u] = fmax(V[u] * b, fmax(maxStoC[C[u]] + V[u] * a, tmpS + V[u] * b));
}

//=====CAP NHAT
void update(int u)
{
	//if (maxStoC[C[u]] >= S[u])
	//	return;
	//else if (maxStoC[C[u]] < S[u])
	//{
	//	maxStoC[C[u]] = S[u];
	//	//Color Ci da duoc cap nhat gia tri tong lon nhat. Vi vay phai cap nhat ca MT1, MT2
	//	//Luu y rang : luc nay, S[u] = maxStoC[C[u]]
	//	if (MT1.val < S[u])
	//	{
	//		std::set<int>::iterator it = MT1.colors.find(C[u]);
	//		//MT1.colors chi co 1 class duy nhat va do chinh la Ci => Chi cap nhat val, khong thay doi gi them.
	//		if (MT1.colors.size() == 1 && it != MT1.colors.end())
	//		{
	//			MT1.val = S[u];
	//		}
	//		else {
	//			val2 = MT1.val;
	//			MT1.val = S[u];
	//			MT1.colors.clear();				//xoa sach danh sach cu
	//			MT1.colors.insert(C[u]);
	//		}
	//	}
	//	else if (MT1.val == S[u])
	//	{
	//		MT1.colors.insert(C[u]);		//set co kha nang tu kiem tra phan tu trung, nen minh khong can check
	//	}
	//}
	if (maxStoC[C[u]] < S[u])
		maxStoC[C[u]] = S[u];

	if (MT1.val <= S[u])
	{
		MT1.val = S[u];
		if (MT1.color != C[u])			//neu la chuoi loai khac
		{
			MT2 = MT1;
			MT1.color = C[u];
		}
	}
	else if (MT2.val < S[u])
	{
		MT2.val = S[u];
		MT2.color = C[u];
	}
}

//Xu li
void process()
{
	for (i = 0; i < n; i++)
	{
		calculateSu(i);
		update(i);
	}

	ans_tmp = MT1.val;
	//Neu ket qua nhan duoc la am => Lay so 0
	if (ans_tmp < 0)
		ans_tmp = 0;
}

//Xuat
void output()
{
	for (i = 0; i < q; i++)
		cout << ans[i] << endl;
}

void main()
{
	input();
	for (int q0 = 0; q0 < q; q0++)
	{
		cin >> a >> b;
		init();
		process();
		ans[q0] = ans_tmp;
	}
	output();

	system("Pause");
}

/*
10 3
-10 62 100 231 40 176 80 -10305 464 1000
1 2 1 3 6 10 2 2 3 1
-10000 10000
150 -200
33665 28879
*/

/*
6 3
1 -2 3 4 0 -1
1 2 1 2 1 1
5 1
-2 1
1 0
*/

/*
4 1
-3 6 -1 2
1 2 3 1
1 -1
*/


#include <stdio.h>
#include <iostream>
#include <set>

using namespace std;

struct MaxTracker
{
	long long val;
	std::set<int> colors;
};

const int MAX_N = 100003;
const long long MAX_S = 1000000000000000000;			//Do ket qua co the toi 10^15
long long V[MAX_N], C[MAX_N];
long long S[MAX_N];					//Ket qua tot nhat co duoc khi chon vi tri i lam duoi cua 1 mang
long long maxStoC[MAX_N];			//Ket qua tot nhat cua mang "co duoi loai i" o thoi diem dang xet
MaxTracker MT1, MT2;			//MT1. val != MT2. val && MT1.colors != MT2.colors (khac nhau tung doi mot)
int n, q, i;
long long a, b;
long long ans[501], ans_tmp;			//chua danh sach cac ket qua cua query

//Doc du lieu ban dau
void input()
{
	cin >> n >> q;
	for (i = 0; i < n; i++)
		cin >> V[i];
	for (i = 0; i < n; i++)
		cin >> C[i];
}

//Khoi tao
void init()
{
	MT1.val = -MAX_S;
	MT2.val = -MAX_S;
	MT1.colors.clear();
	MT2.colors.clear();

	/*memset(S, -MAX_S, MAX_N);
	memset(maxStoC, -MAX_S, MAX_N);*/
	for (int ii = 0; ii <= n; ii++)			//ii = n la vi Ci co the bang n.
	{
		S[ii] = -MAX_S;
		maxStoC[ii] = -MAX_S;
	}

	ans_tmp = -MAX_S;
}

//=======XU LI========
void calculateSu(int u)
{
	S[u] = V[u] * b;			//Truong hop dau tien, chuoi chi co 1 phan tu la chinh no
	S[u] = fmax(S[u], maxStoC[C[u]] + V[u] * a);

	if (MT1.colors.size() > 1)				//neu colors co so luong > 1
	{
		S[u] = fmax(S[u], MT1.val + V[u] * b);		//chung to rang ton tai 1 mang duoi "khac loai" va co S = val, ta se noi duoi voi mang do.
	}
	else if (MT1.colors.size() == 1)
	{
		std::set<int>::iterator it = MT1.colors.find(C[u]);

		//tim ra Ci && chi co duy nhat Ci co S=val
		//=> Khong ton tai mang duoi "khac loai" nao co S = val, ta phai xet toi MT2 (vi cac colors MT2 != color MT1)
		if (it != MT1.colors.end())
			S[u] = fmax(S[u], MT2.val + V[u] * b);
		else
			S[u] = fmax(S[u], MT1.val + V[u] * b);		//neu khong tim ra Ci thi chac chan co 1 mang "khac loai" co S = val.
	}
}

//=====CAP NHAT
void update(int u)
{
	if (maxStoC[C[u]] >= S[u])
		return;
	else if (maxStoC[C[u]] < S[u])
	{
		maxStoC[C[u]] = S[u];

		//Color Ci da duoc cap nhat gia tri tong lon nhat. Vi vay phai cap nhat ca MT1, MT2
		//Luu y rang : luc nay, S[u] = maxStoC[C[u]]
		if (MT1.val < S[u])
		{
			std::set<int>::iterator it = MT1.colors.find(C[u]);

			//MT1.colors chi co 1 class duy nhat va do chinh la Ci => Chi cap nhat val, khong thay doi gi them.
			if (MT1.colors.size() == 1 && it != MT1.colors.end())
			{
				MT1.val = S[u];
			}
			else {
				MT2 = MT1;
				std::set<int>::iterator it2 = MT2.colors.find(C[u]);			//tim xem co Ci trong tap cu khong, co thi xoa di
				if (it2 != MT2.colors.end())
					MT2.colors.erase(it2);

				MT1.val = S[u];
				MT1.colors.clear();				//xoa sach danh sach cu
				MT1.colors.insert(C[u]);
			}
		}
		//Neu S = val, ta phai kiem tra xem tai MT2 co Ci khong?
		//Neu co, xoa Ci khoi MT2, roi moi them vao
		else if (MT1.val == S[u])
		{
			std::set<int>::iterator it2 = MT2.colors.find(C[u]);
			if (it2 != MT2.colors.end())
				MT2.colors.erase(it2);

			MT1.colors.insert(C[u]);		//set co kha nang tu kiem tra phan tu trung, nen minh khong can check
		}
		else if (MT2.val < S[u])
		{
			MT2.val = S[u];
			MT2.colors.clear();
			MT2.colors.insert(C[u]);
		}
		else if (MT2.val == S[u])
		{
			MT2.colors.insert(C[u]);
		}
	}
}

//Xu li
void process()
{
	for (i = 0; i < n; i++)
	{
		calculateSu(i);
		update(i);
	}

	ans_tmp = MT1.val;
	//Neu ket qua nhan duoc la am => Lay so 0
	if (ans_tmp < 0)
		ans_tmp = 0;
}

//Xuat
void output()
{
	for (i = 0; i < q; i++)
		cout << ans[i] << endl;
}

void main()
{
	input();
	for (int q0 = 0; q0 < q; q0++)
	{
		cin >> a >> b;
		init();
		process();
		ans[q0] = ans_tmp;
	}
	output();

	//system("Pause");
}

/*
10 3
-10 62 100 231 40 176 80 -10305 464 1000
1 2 1 3 6 10 2 2 3 1
-10000 10000
150 -200
33665 28879
*/

/*
6 1
1 -2 3 4 0 -1
1 2 1 2 1 1
1 0
*/


#include <stdio.h>
#include <iostream>
#include <set>

using namespace std;

struct MaxTracker
{
	long long val;
	//std::set<int> colors;
	int color;
};

const int MAX_N = 100003;
const long long MAX_S = 1000000000000000000;			//Do ket qua co the toi 10^15
long long V[MAX_N], C[MAX_N];
long long S[MAX_N];					//Ket qua tot nhat co duoc khi chon vi tri i lam duoi cua 1 mang
long long maxStoC[MAX_N];			//Ket qua tot nhat cua mang "co duoi loai i" o thoi diem dang xet
MaxTracker MT1, MT2;			//MT1. val != MT2. val && MT1.color != MT2.color
int n, q, i;
long long a, b;
long long ans[501], ans_tmp;			//chua danh sach cac ket qua cua query

//Doc du lieu ban dau
void input()
{
	cin >> n >> q;
	for (i = 0; i < n; i++)
		cin >> V[i];
	for (i = 0; i < n; i++)
		cin >> C[i];
}

//Khoi tao
void init()
{
	MT1.val = -MAX_S;
	MT2.val = -MAX_S;

	/*memset(S, -MAX_S, MAX_N);
	memset(maxStoC, -MAX_S, MAX_N);*/
	for (int ii = 0; ii <= n; ii++)			//ii = n la vi Ci co the bang n.
	{
		//S[ii] = -MAX_S;
		maxStoC[ii] = -MAX_S;
	}

	ans_tmp = -MAX_S;
}

//=======XU LI========
void calculateSu(int u)
{
	long long tmpS = 0;
	if (MT1.color == C[u])			//cai 1 chinh la Ci, thi lay cai thu 2
		tmpS = MT2.val;
	else tmpS = MT1.val;
	//Truong hop dau tien, chuoi chi co 1 phan tu la chinh no = V[u] * b
	//S[u] = fmax(V[u] * b, fmax(maxStoC[C[u]] + V[u] * a, tmpS + V[u] * b));
	S[u] = fmax(fmax(tmpS, 0) + V[u] * b, maxStoC[C[u]] + V[u] * a);
}

//=====CAP NHAT
void update(int u)
{
	if (maxStoC[C[u]] < S[u])
	{
		maxStoC[C[u]] = S[u];

		if (MT1.val <= S[u])
		{
			if (MT1.color != C[u])			//neu la chuoi loai khac
			{
				MT2 = MT1;
				MT1.color = C[u];
			}
			MT1.val = S[u];
		}
		else if (MT2.val < S[u])
		{
			MT2.val = S[u];
			MT2.color = C[u];
		}
	}
}

//Xu li
void process()
{
	for (i = 0; i < n; i++)
	{
		calculateSu(i);
		update(i);
	}

	ans_tmp = MT1.val;
	//Neu ket qua nhan duoc la am => Lay so 0
	if (ans_tmp < 0)
		ans_tmp = 0;
}

//Xuat
void output()
{
	for (i = 0; i < q; i++)
		cout << ans[i] << endl;
}

void main()
{
	input();
	for (int q0 = 0; q0 < q; q0++)
	{
		cin >> a >> b;
		init();
		process();
		cout << ans_tmp << endl;
	}
	//output();

	//system("Pause");
}


#include <stdio.h>
#include <iostream>
#include <set>

using namespace std;

struct MaxTracker
{
	long val;
	std::set<int> colors;
};

const int MAX_N = 100002;
const long MAX_S = 10000000000000000;			//Do ket qua co the toi 10^15
long V[MAX_N], C[MAX_N];
long S[MAX_N];					//Ket qua tot nhat co duoc khi chon vi tri i lam duoi cua 1 mang
long maxStoC[MAX_N];			//Ket qua tot nhat cua mang "co duoi loai i" o thoi diem dang xet
MaxTracker MT1, MT2;			//MT1. val != MT2. val && MT1.colors != MT2.colors (khac nhau tung doi mot)
int n, q, i;
long a, b;
long ans[501], ans_tmp;			//chua danh sach cac ket qua cua query

//Doc du lieu ban dau
void input()
{
	cin >> n >> q;
	for (i = 0; i < n; i++)
		cin >> V[i];
	for (i = 0; i < n; i++)
		cin >> C[i];
}

//Khoi tao
void init()
{
	MT1.val = -MAX_S;
	MT2.val = -MAX_S;
	MT1.colors.clear();
	MT2.colors.clear();

	/*memset(S, -MAX_S, MAX_N);
	memset(maxStoC, -MAX_S, MAX_N);*/
	for (int ii = 0; ii < n; ii++)
	{
		S[ii] = -MAX_S;
		maxStoC[ii] = -MAX_S;
	}

	ans_tmp = -MAX_S;
}

//=======XU LI========
void calculateSu(int u)
{
	S[u] = V[u] * b;			//Truong hop dau tien, chuoi chi co 1 phan tu la chinh no
	S[u] = fmax(S[u], maxStoC[C[u]] + V[u] * a);

	if (MT1.colors.size() > 1)				//neu colors co so luong > 2
	{
		S[u] = fmax(S[u], MT1.val + V[u] * b);		//chung to rang ton tai 1 mang duoi "khac loai" va co S = val, ta se noi duoi voi mang do.
	}
	else if (MT1.colors.size() == 1)
	{
		std::set<int>::iterator it = MT1.colors.find(C[u]);

		//tim ra Ci && chi co duy nhat Ci co S=val
		//=> Khong ton tai mang duoi "khac loai" nao co S = val, ta phai xet toi MT2 (vi cac colors MT2 != color MT1)
		if (it != MT1.colors.end())
			S[u] = fmax(S[u], MT2.val + V[u] * b);
		else
			S[u] = fmax(S[u], MT1.val + V[u] * b);		//neu khong tim ra Ci thi chac chan co 1 mang "khac loai" co S = val.
	}
}

//=====CAP NHAT
void update(int u)
{
	maxStoC[C[u]] = fmax(maxStoC[C[u]], S[u]);
	if (MT1.val < S[u])
	{
		MT2 = MT1;
		std::set<int>::iterator it2 = MT2.colors.find(C[u]);			//tim xem co Ci trong tap cu khong, co thi xoa di
		if (it2 != MT2.colors.end())
			MT2.colors.erase(it2);

		MT1.val = S[u];
		MT1.colors.clear();				//xoa sach danh sach cu
		MT1.colors.insert(C[u]);
	}
	else if (MT1.val == S[u])
	{
		MT1.colors.insert(C[u]);		//set co kha nang tu kiem tra phan tu trung, nen minh khong can check
	}
	else if (MT2.val < S[u])
	{
		MT2.val = S[u];
		MT2.colors.clear();
		MT2.colors.insert(C[u]);
	}
	else if (MT2.val == S[u])
	{
		MT2.colors.insert(C[u]);
	}
}

//Xu li
void process()
{
	for (i = 0; i < n; i++)
	{
		calculateSu(i);
		update(i);
	}

	ans_tmp = MT1.val;
	//Neu ket qua nhan duoc la am => Lay so 0
	if (ans_tmp < 0)
		ans_tmp = 0;
}

//Xuat
void output()
{
	for (i = 0; i < q; i++)
		cout << ans[i] << endl;
}

void main()
{
	input();
	for (int q0 = 0; q0 < q; q0++)
	{
		cin >> a >> b;
		init();
		process();
		ans[q0] = ans_tmp;
	}
	output();
}


#include <stdio.h>
#include <iostream>
#include <set>

using namespace std;

struct MaxTracker
{
	long long val;
	//std::set<int> colors;
	int color;
};

const int MAX_N = 100003;
const long long MAX_S = 1000000000000000000;			//Do ket qua co the toi 10^15
long long V[MAX_N], C[MAX_N];
long long S[MAX_N];					//Ket qua tot nhat co duoc khi chon vi tri i lam duoi cua 1 mang
long long maxStoC[MAX_N];			//Ket qua tot nhat cua mang "co duoi loai i" o thoi diem dang xet
MaxTracker MT1, MT2;			//MT1. val != MT2. val && MT1.color != MT2.color
int n, q, i;
long long a, b;
long long ans[501], ans_tmp;			//chua danh sach cac ket qua cua query

//Doc du lieu ban dau
void input()
{
	cin >> n >> q;
	for (i = 0; i < n; i++)
		cin >> V[i];
	for (i = 0; i < n; i++)
		cin >> C[i];
}

//Khoi tao
void init()
{
	MT1.val = -MAX_S;
	MT2.val = -MAX_S;

	/*memset(S, -MAX_S, MAX_N);
	memset(maxStoC, -MAX_S, MAX_N);*/
	for (int ii = 0; ii <= n; ii++)			//ii = n la vi Ci co the bang n.
	{
		S[ii] = -MAX_S;
		maxStoC[ii] = -MAX_S;
	}

	ans_tmp = -MAX_S;
}

//=======XU LI========
void calculateSu(int u)
{
	//if (MT1.colors.size() > 1)				//neu colors co so luong > 1
	//{
	//	S[u] = fmax(S[u], MT1.val + V[u] * b);		//chung to rang ton tai 1 mang duoi "khac loai" va co S = val, ta se noi duoi voi mang do.
	//}
	//else if (MT1.colors.size() == 1)
	//{
	//	std::set<int>::iterator it = MT1.colors.find(C[u]);
	//	//tim ra Ci && chi co duy nhat Ci co S=val
	//	//=> Khong ton tai mang duoi "khac loai" nao co S = val, ta phai xet toi MT2 (vi cac colors MT2 != color MT1)
	//	if (it != MT1.colors.end())
	//		S[u] = fmax(S[u], val2 + V[u] * b);
	//	else
	//		S[u] = fmax(S[u], MT1.val + V[u] * b);		//neu khong tim ra Ci thi chac chan co 1 mang "khac loai" co S = val.
	//}

	long long tmpS = 0;
	if (MT1.color == C[i])			//cai 1 chinh la Ci, thi lay cai thu 2
		tmpS = MT2.val;
	else tmpS = MT1.val;
	//Truong hop dau tien, chuoi chi co 1 phan tu la chinh no = V[u] * b
	S[u] = fmax(V[u] * b, fmax(maxStoC[C[u]] + V[u] * a, tmpS + V[u] * b));
}

//=====CAP NHAT
void update(int u)
{
	//if (maxStoC[C[u]] >= S[u])
	//	return;
	//else if (maxStoC[C[u]] < S[u])
	//{
	//	maxStoC[C[u]] = S[u];
	//	//Color Ci da duoc cap nhat gia tri tong lon nhat. Vi vay phai cap nhat ca MT1, MT2
	//	//Luu y rang : luc nay, S[u] = maxStoC[C[u]]
	//	if (MT1.val < S[u])
	//	{
	//		std::set<int>::iterator it = MT1.colors.find(C[u]);
	//		//MT1.colors chi co 1 class duy nhat va do chinh la Ci => Chi cap nhat val, khong thay doi gi them.
	//		if (MT1.colors.size() == 1 && it != MT1.colors.end())
	//		{
	//			MT1.val = S[u];
	//		}
	//		else {
	//			val2 = MT1.val;
	//			MT1.val = S[u];
	//			MT1.colors.clear();				//xoa sach danh sach cu
	//			MT1.colors.insert(C[u]);
	//		}
	//	}
	//	else if (MT1.val == S[u])
	//	{
	//		MT1.colors.insert(C[u]);		//set co kha nang tu kiem tra phan tu trung, nen minh khong can check
	//	}
	//}
	if (maxStoC[C[u]] < S[u])
		maxStoC[C[u]] = S[u];

	if (MT1.val <= S[u])
	{
		MT1.val = S[u];
		if (MT1.color != C[u])			//neu la chuoi loai khac
		{
			MT2 = MT1;
			MT1.color = C[u];
		}
	}
	else if (MT2.val < S[u])
	{
		MT2.val = S[u];
		MT2.color = C[u];
	}
}

//Xu li
void process()
{
	for (i = 0; i < n; i++)
	{
		calculateSu(i);
		update(i);
	}

	ans_tmp = MT1.val;
	//Neu ket qua nhan duoc la am => Lay so 0
	if (ans_tmp < 0)
		ans_tmp = 0;
}

//Xuat
void output()
{
	for (i = 0; i < q; i++)
		cout << ans[i] << endl;
}

void main()
{
	input();
	for (int q0 = 0; q0 < q; q0++)
	{
		cin >> a >> b;
		init();
		process();
		ans[q0] = ans_tmp;
	}
	output();

	//system("Pause");
}

/*
10 3
-10 62 100 231 40 176 80 -10305 464 1000
1 2 1 3 6 10 2 2 3 1
-10000 10000
150 -200
33665 28879
*/

/*
6 3
1 -2 3 4 0 -1
1 2 1 2 1 1
5 1
-2 1
1 0
*/

/*
4 1
-3 6 -1 2
1 2 3 1
1 -1
*/


#include <stdio.h>
#include <iostream>
#include <set>

using namespace std;

struct MaxTracker
{
	long long val;
	std::set<int> colors;
};

const int MAX_N = 100002;
const long long MAX_S = 1000000000000000000;			//Do ket qua co the toi 10^15
long long V[MAX_N], C[MAX_N];
long long S[MAX_N];					//Ket qua tot nhat co duoc khi chon vi tri i lam duoi cua 1 mang
long long maxStoC[MAX_N];			//Ket qua tot nhat cua mang "co duoi loai i" o thoi diem dang xet
MaxTracker MT1, MT2;			//MT1. val != MT2. val && MT1.colors != MT2.colors (khac nhau tung doi mot)
int n, q, i;
long long a, b;
long long ans[501], ans_tmp;			//chua danh sach cac ket qua cua query

//Doc du lieu ban dau
void input()
{
	cin >> n >> q;
	for (i = 0; i < n; i++)
		cin >> V[i];
	for (i = 0; i < n; i++)
		cin >> C[i];
}

//Khoi tao
void init()
{
	MT1.val = -MAX_S;
	MT2.val = -MAX_S;
	MT1.colors.clear();
	MT2.colors.clear();

	/*memset(S, -MAX_S, MAX_N);
	memset(maxStoC, -MAX_S, MAX_N);*/
	for (int ii = 0; ii < n; ii++)
	{
		S[ii] = -MAX_S;
		maxStoC[ii] = -MAX_S;
	}

	ans_tmp = -MAX_S;
}

//=======XU LI========
void calculateSu(int u)
{
	S[u] = V[u] * b;			//Truong hop dau tien, chuoi chi co 1 phan tu la chinh no
	S[u] = fmax(S[u], maxStoC[C[u]] + V[u] * a);

	if (MT1.colors.size() > 1)				//neu colors co so luong > 2
	{
		S[u] = fmax(S[u], MT1.val + V[u] * b);		//chung to rang ton tai 1 mang duoi "khac loai" va co S = val, ta se noi duoi voi mang do.
	}
	else if (MT1.colors.size() == 1)
	{
		std::set<int>::iterator it = MT1.colors.find(C[u]);

		//tim ra Ci && chi co duy nhat Ci co S=val
		//=> Khong ton tai mang duoi "khac loai" nao co S = val, ta phai xet toi MT2 (vi cac colors MT2 != color MT1)
		if (it != MT1.colors.end())
			S[u] = fmax(S[u], MT2.val + V[u] * b);
		else
			S[u] = fmax(S[u], MT1.val + V[u] * b);		//neu khong tim ra Ci thi chac chan co 1 mang "khac loai" co S = val.
	}
}

//=====CAP NHAT
void update(int u)
{
	maxStoC[C[u]] = fmax(maxStoC[C[u]], S[u]);
	if (MT1.val < S[u])
	{
		MT2 = MT1;
		std::set<int>::iterator it2 = MT2.colors.find(C[u]);			//tim xem co Ci trong tap cu khong, co thi xoa di
		if (it2 != MT2.colors.end())
			MT2.colors.erase(it2);

		MT1.val = S[u];
		MT1.colors.clear();				//xoa sach danh sach cu
		MT1.colors.insert(C[u]);
	}
	else if (MT1.val == S[u])
	{
		MT1.colors.insert(C[u]);		//set co kha nang tu kiem tra phan tu trung, nen minh khong can check
	}
	else if (MT2.val < S[u])
	{
		MT2.val = S[u];
		MT2.colors.clear();
		MT2.colors.insert(C[u]);
	}
	else if (MT2.val == S[u])
	{
		MT2.colors.insert(C[u]);
	}
}

//Xu li
void process()
{
	for (i = 0; i < n; i++)
	{
		calculateSu(i);
		update(i);
	}

	ans_tmp = MT1.val;
	//Neu ket qua nhan duoc la am => Lay so 0
	if (ans_tmp < 0)
		ans_tmp = 0;
}

//Xuat
void output()
{
	for (i = 0; i < q; i++)
		cout << ans[i] << endl;
}

void main()
{
	input();
	for (int q0 = 0; q0 < q; q0++)
	{
		cin >> a >> b;
		init();
		process();
		ans[q0] = ans_tmp;
	}
	output();
}


#include <stdio.h>
#include <iostream>
#include <set>

using namespace std;

struct MaxTracker
{
	long long val;
	//std::set<int> colors;
	int color;
};

const int MAX_N = 100003;
const long long MAX_S = 1000000000000000000;			//Do ket qua co the toi 10^15
long long V[MAX_N], C[MAX_N];
long long S[MAX_N];					//Ket qua tot nhat co duoc khi chon vi tri i lam duoi cua 1 mang
long long maxStoC[MAX_N];			//Ket qua tot nhat cua mang "co duoi loai i" o thoi diem dang xet
MaxTracker MT1, MT2;			//MT1. val != MT2. val && MT1.color != MT2.color
int n, q, i;
long long a, b;
long long ans[501], ans_tmp;			//chua danh sach cac ket qua cua query

//Doc du lieu ban dau
void input()
{
	cin >> n >> q;
	for (i = 0; i < n; i++)
		cin >> V[i];
	for (i = 0; i < n; i++)
		cin >> C[i];
}

//Khoi tao
void init()
{
	MT1.val = -MAX_S;
	MT2.val = -MAX_S;

	/*memset(S, -MAX_S, MAX_N);
	memset(maxStoC, -MAX_S, MAX_N);*/
	for (int ii = 0; ii <= n; ii++)			//ii = n la vi Ci co the bang n.
	{
		S[ii] = -MAX_S;
		maxStoC[ii] = -MAX_S;
	}

	ans_tmp = -MAX_S;
}

//=======XU LI========
void calculateSu(int u)
{
	//if (MT1.colors.size() > 1)				//neu colors co so luong > 1
	//{
	//	S[u] = fmax(S[u], MT1.val + V[u] * b);		//chung to rang ton tai 1 mang duoi "khac loai" va co S = val, ta se noi duoi voi mang do.
	//}
	//else if (MT1.colors.size() == 1)
	//{
	//	std::set<int>::iterator it = MT1.colors.find(C[u]);
	//	//tim ra Ci && chi co duy nhat Ci co S=val
	//	//=> Khong ton tai mang duoi "khac loai" nao co S = val, ta phai xet toi MT2 (vi cac colors MT2 != color MT1)
	//	if (it != MT1.colors.end())
	//		S[u] = fmax(S[u], val2 + V[u] * b);
	//	else
	//		S[u] = fmax(S[u], MT1.val + V[u] * b);		//neu khong tim ra Ci thi chac chan co 1 mang "khac loai" co S = val.
	//}

	long long tmpS = 0;
	if (MT1.color == C[i])			//cai 1 chinh la Ci, thi lay cai thu 2
		tmpS = MT2.val;
	else tmpS = MT1.val;
	//Truong hop dau tien, chuoi chi co 1 phan tu la chinh no = V[u] * b
	S[u] = fmax(V[u] * b, fmax(maxStoC[C[u]] + V[u] * a, tmpS + V[u] * b));
}

//=====CAP NHAT
void update(int u)
{
	//if (maxStoC[C[u]] >= S[u])
	//	return;
	//else if (maxStoC[C[u]] < S[u])
	//{
	//	maxStoC[C[u]] = S[u];
	//	//Color Ci da duoc cap nhat gia tri tong lon nhat. Vi vay phai cap nhat ca MT1, MT2
	//	//Luu y rang : luc nay, S[u] = maxStoC[C[u]]
	//	if (MT1.val < S[u])
	//	{
	//		std::set<int>::iterator it = MT1.colors.find(C[u]);
	//		//MT1.colors chi co 1 class duy nhat va do chinh la Ci => Chi cap nhat val, khong thay doi gi them.
	//		if (MT1.colors.size() == 1 && it != MT1.colors.end())
	//		{
	//			MT1.val = S[u];
	//		}
	//		else {
	//			val2 = MT1.val;
	//			MT1.val = S[u];
	//			MT1.colors.clear();				//xoa sach danh sach cu
	//			MT1.colors.insert(C[u]);
	//		}
	//	}
	//	else if (MT1.val == S[u])
	//	{
	//		MT1.colors.insert(C[u]);		//set co kha nang tu kiem tra phan tu trung, nen minh khong can check
	//	}
	//}
	if (maxStoC[C[u]] < S[u])
		maxStoC[C[u]] = S[u];

	if (MT1.val <= S[u])
	{
		if (MT1.color != C[u])			//neu la chuoi loai khac
		{
			MT2 = MT1;
			MT1.color = C[u];
		}
		MT1.val = S[u];
	}
	else if (MT2.val < S[u])
	{
		MT2.val = S[u];
		MT2.color = C[u];
	}
}

//Xu li
void process()
{
	for (i = 0; i < n; i++)
	{
		calculateSu(i);
		update(i);
	}

	ans_tmp = MT1.val;
	//Neu ket qua nhan duoc la am => Lay so 0
	if (ans_tmp < 0)
		ans_tmp = 0;
}

//Xuat
void output()
{
	for (i = 0; i < q; i++)
		cout << ans[i] << endl;
}

void main()
{
	input();
	for (int q0 = 0; q0 < q; q0++)
	{
		cin >> a >> b;
		init();
		process();
		ans[q0] = ans_tmp;
	}
	output();

	//system("Pause");
}

/*
10 3
-10 62 100 231 40 176 80 -10305 464 1000
1 2 1 3 6 10 2 2 3 1
-10000 10000
150 -200
33665 28879
*/

/*
6 3
1 -2 3 4 0 -1
1 2 1 2 1 1
5 1
-2 1
1 0
*/

/*
4 1
-3 6 -1 2
1 2 3 1
1 -1
*/


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <cstdlib>
#include <queue>
#include <set>
#include <iomanip>

using namespace std;

int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int n,q;
	cin >>n >>q;
	vector <int> v(n),c(n);
	for (int i=0; i<n; ++i)
		cin >>v[i];
	for (int i=0; i<n; ++i)
		cin >>c[i];
	for (int j=0; j<q; ++j){
		int a,b;
		cin >>a >>b;
		vector <long long> maxcolor(n);
		long long max1=0,max2=0;
		int pos1=-1,pos2=-1;
		vector <bool> used(n,false);
		for (int i=0; i<n; ++i){
			long long prev=maxcolor[c[i]-1];
			if (used[c[i]-1])
				maxcolor[c[i]-1]=max(maxcolor[c[i]-1],(long long)v[i]*b);
			else
				maxcolor[c[i]-1]=(long long)v[i]*b;
			if (used[c[i]-1])
				maxcolor[c[i]-1]=max(maxcolor[c[i]-1],prev+(long long)v[i]*a);
			if (pos1!=-1 && pos1!=c[i]-1)
				maxcolor[c[i]-1]=max(maxcolor[c[i]-1],max1+(long long)v[i]*b);
			else
				if (pos2!=-1 && pos2!=c[i]-1)
					maxcolor[c[i]-1]=max(maxcolor[c[i]-1],max2+(long long)v[i]*b);
			long long upd=maxcolor[c[i]-1];
			if (upd>max1){
				if (c[i]-1!=pos1){
					pos2=pos1;
					max2=max1;
				}
				max1=maxcolor[c[i]-1];
				pos1=c[i]-1;
			}
			else
				if (upd>max2 && c[i]-1!=pos1){
					max2=maxcolor[c[i]-1];
					pos2=c[i]-1;
				}
			used[c[i]-1]=true;
		}
		long long ans=0;
		for (int i=0; i<n; ++i)
			ans=max(ans,maxcolor[i]);
		cout <<ans <<endl;
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <cstdlib>
#include <queue>
#include <set>
#include <iomanip>

using namespace std;

int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int n,q;
	cin >>n >>q;
	vector <int> v(n),c(n);
	for (int i=0; i<n; ++i)
		cin >>v[i];
	for (int i=0; i<n; ++i)
		cin >>c[i];
	for (int j=0; j<q; ++j){
		int a,b;
		cin >>a >>b;
		vector <long long> maxcolor(n);
		long long max1=0,max2=0;
		int pos1=0,pos2=0;
		vector <bool> used(n,false);
		for (int i=0; i<n; ++i){
			if (v[i]*a>=0 && used[c[i]-1]){
				maxcolor[c[i]-1]+=(long long)v[i]*a;
				if (maxcolor[c[i]-1]>max1){
					if (pos1!=c[i]-1){
						max2=max1;
						pos2=pos1;
					}
					max1=maxcolor[c[i]-1];
					pos1=c[i]-1;
				}
				else
					if (maxcolor[c[i]-1]>max2 && c[i]-1!=pos1){
						max2=maxcolor[c[i]-1];
						pos2=c[i]-1;
					}
			}
			if (v[i]*b>=0){
				long long add;
				if (pos1==c[i]-1)
					add=max2;
				else
					add=max1;
				maxcolor[c[i]-1]=max(maxcolor[c[i]-1],add+v[i]*b);
				if (maxcolor[c[i]-1]>max1){
					if (pos1!=c[i]-1){
						max2=max1;
						pos2=pos1;
					}
					max1=maxcolor[c[i]-1];
					pos1=c[i]-1;
				}
				else
					if (maxcolor[c[i]-1]>max2 && c[i]-1!=pos1){
						max2=maxcolor[c[i]-1];
						pos2=c[i]-1;
					}
				used[c[i]-1]=true;
			}
		}
		long long ans=0;
		for (int i=0; i<n; ++i)
			ans=max(ans,maxcolor[i]);
		cout <<ans <<endl;
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <cstdlib>
#include <queue>
#include <set>
#include <iomanip>

using namespace std;

int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int n,q;
	cin >>n >>q;
	vector <int> v(n),c(n);
	for (int i=0; i<n; ++i)
		cin >>v[i];
	for (int i=0; i<n; ++i)
		cin >>c[i];
	for (int j=0; j<q; ++j){
		int a,b;
		cin >>a >>b;
		vector <long long> maxcolor(n);
		long long max1=0,max2=0;
		int pos1=-1,pos2=-1;
		vector <bool> used(n,false);
		for (int i=0; i<n; ++i){
			long long prev=maxcolor[c[i]-1];
			long long add;
			if (pos1==c[i]-1)
				add=max2;
			else
				add=max1;
			if (used[c[i]-1])
				maxcolor[c[i]-1]=max(maxcolor[c[i]-1],max(add,0ll)+v[i]*b);
			else
				maxcolor[c[i]-1]=(long long)v[i]*b;
			if (used[c[i]-1]){
				maxcolor[c[i]-1]=max(prev+(long long)v[i]*a,maxcolor[c[i]-1]);
			}
			if (pos1==-1){
				max1=maxcolor[c[i]-1];
				pos1=c[i]-1;
			}
			else
				if (maxcolor[c[i]-1]>max1){
					if (pos1!=c[i]-1){
						max2=max1;
						pos2=pos1;
					}
					max1=maxcolor[c[i]-1];
					pos1=c[i]-1;
				}
				else
					if (pos2==-1 && pos1!=c[i]-1){
						max2=maxcolor[c[i]-1];
						pos2=c[i]-1;
					}
					else
						if (pos1!=c[i]-1 && max2<maxcolor[c[i]-1]){
							max2=maxcolor[c[i]-1];
							pos2=c[i]-1;
						}
			used[c[i]-1]=true;
		}
		long long ans=0;
		for (int i=0; i<n; ++i)
			ans=max(ans,maxcolor[i]);
		cout <<ans <<endl;
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <cstdlib>
#include <queue>
#include <set>
#include <iomanip>

using namespace std;

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n,q;
    cin >>n >>q;
    vector <long long> v(n),c(n);
    for (int i=0; i<n; ++i)
        cin >>v[i];
    for (int i=0; i<n; ++i)
        cin >>c[i];
    for (int j=0; j<q; ++j){
        int a,b;
        cin >>a >>b;
        vector <long long> maxcolor(n);
        long long max1=0,max2=0;
        int pos1=-1,pos2=-1;
        vector <bool> used(n,false);
        for (int i=0; i<n; ++i){
            long long prev=maxcolor[c[i]-1];
            long long add;
            if (pos1==c[i]-1)
                add=max2;
            else
                add=max1;
            if (used[c[i]-1])
                maxcolor[c[i]-1]=max(maxcolor[c[i]-1],max(add,0ll)+v[i]*b);
            else
                maxcolor[c[i]-1]=(long long)v[i]*b;
            if (used[c[i]-1]){
                maxcolor[c[i]-1]=max(prev+(long long)v[i]*a,maxcolor[c[i]-1]);
            }
            if (pos1==-1){
                max1=maxcolor[c[i]-1];
                pos1=c[i]-1;
            }
            else
                if (maxcolor[c[i]-1]>max1){
                    if (pos1!=c[i]-1){
                        max2=max1;
                        pos2=pos1;
                    }
                    max1=maxcolor[c[i]-1];
                    pos1=c[i]-1;
                }
                else
                    if (pos2==-1 && pos1!=c[i]-1){
                        max2=maxcolor[c[i]-1];
                        pos2=c[i]-1;
                    }
                    else
                        if (pos1!=c[i]-1 && max2<maxcolor[c[i]-1]){
                            max2=maxcolor[c[i]-1];
                            pos2=c[i]-1;
                        }
            used[c[i]-1]=true;
        }
        long long ans=0;
        for (int i=0; i<n; ++i)
            ans=max(ans,maxcolor[i]);
        cout <<ans <<endl;
    }
    return 0;
}


#include<stdio.h>
#include<stack>
#include<math.h>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<set>
#include<memory.h>
#include<vector>
#include<map>
#include<queue>
#include<deque>
#include<cassert>
#include<time.h>
#include<bitset>
#include<ctime>
#include<unordered_set>

using namespace std;

const int INF = 1e9;
const double pi = 3.14159265358979323846;

typedef long long li;
typedef long double ld;

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define pb push_back
#define mp make_pair

int n, q;
int v[100000];
int c[100000];

bool read()
{
    if (!(cin >> n >> q))
        return false;
    forn (i, n)
        cin >> v[i];
    forn (i, n)
        cin >> c[i];
    return true;
}

void solve()
{
    vector<li> dp(100000, -1e15);
    forn (i, q)
    {
        int firstMaxPos = 0, secondMaxPos = 1;
        forn (j, 1e5)
            dp[j] = -1e15;
        li a, b;
        cin >> a >> b;
        forn (i, n)
        {
            li val1 = firstMaxPos == c[i] - 1 ? dp[secondMaxPos] : dp[firstMaxPos];
            li val2 = dp[c[i] - 1];
            val1 += v[i] * b;
            val1 = max(val1, v[i] * b);
            if (val2 == -1e15)
                val2 = v[i] * b;
            else
                val2 += v[i] * a;
            dp[c[i] - 1] = max(dp[c[i] - 1], max(val1, val2));
            if (dp[c[i] - 1] >= dp[firstMaxPos] && c[i] - 1 != firstMaxPos)
                secondMaxPos = firstMaxPos, firstMaxPos =  c[i] - 1;
            else
                if (dp[c[i] - 1] > dp[secondMaxPos] && c[i] - 1 != firstMaxPos)
                    secondMaxPos = c[i] - 1;
        }
        cout << max(0LL, dp[firstMaxPos]) << endl;
    }
}

int main()
{
#ifdef Baster
    freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
#endif
    while(read())
        solve();
    return 0;
}


#pragma comment(linker, "/STACK:167177216")

#include<stdio.h>
#include<stack>
#include<math.h>
#include<time.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<set>
#include<memory.h>
#include<vector>
#include<map>
#include<queue>

using namespace std;

typedef long long li;

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define pb push_back
#define mp make_pair

int main()
{
    int n, q;
    li v[100000];
    int c[100000];
    cin >> n >> q;
    forn (i, n)
        cin >> v[i];
    forn (i, n)
        cin >> c[i];
    forn (i, q)
    {
        li a, b;
        cin >> a >> b;
        vector<li> dp(n, 0);
        li ans = 0;
        forn (j, n)
        {
            dp[j] = max(0, (int)v[j]);
            for (int k = 1; k < 10 && j - k >= 0; ++k)
            {
                if(c[j] == c[j - k])
                    dp[j] = max(dp[j], dp[j - k] + v[j] * a);
                else
                    dp[j] = max(dp[j], dp[j - k] + v[j] * b);
            }
            ans = max(ans, dp[j]);
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <memory>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

typedef long long Int;
typedef pair<int,int> PII;
typedef vector<int> VInt;

#define FOR(i, a, b) for(i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define SIZE(a) int((a).size())
#define ALL(a) (a).begin(),(a).end()
#define PB push_back
#define MP make_pair

#define INF (1LL << 60)

int C[1 << 20];
int V[1 << 20];
Int R[1 << 20];

int main()
{
	int n, q;
	scanf("%d%d", &n, &q);

	int i, j;
	FOR(i, 0, n)
		scanf("%d", &V[i]);

	FOR(i, 0, n)
		scanf("%d", &C[i]);

	FOR(i, 0, q)
	{
		int a, b;
		scanf("%d%d", &a, &b);

		FOR(j, 0, n + 1)
			R[j] = -INF;

		R[0] = 0;
		Int x = 0;
		Int y = 1;

		FOR(j, 0, n)
		{
			int c = C[j];
			Int v = V[j];
			Int r = max(R[c] + a*v, R[ x == c ? y : x ] + b*v);

			if(r > R[c])
			{
				R[c] = r;
				if(c == x || c == y)
				{
					if(R[x] < R[y])
						swap(x, y);
				}
				else
				{
					if(r > R[x])
					{
						y = x;
						x = c;
					}
					else if(r > R[y])
					{
						y = c;
					}
				}
			}
		}

		printf("%lld\n", R[x]);
	}

	return 0;
};


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0
#pragma comment(linker, "/STACK:200000000")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <fstream>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <list>
#include <string>
#include <sstream>
#include <vector>
#include <utility>
#include <cmath>
using namespace std;

#define pb push_back
#define mp make_pair
#define mset(mas,val) memset(mas,val,sizeof(mas))
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

#define forn(i,n) for (int i=0; i<int(n); ++i)
#define fornd(i,n) for (int i=int(n)-1; i>=0; --i)
#define forab(i,a,b) for (int i=int(a); i<int(b); ++i)

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int INF = (int) 1e9;
const long long INF64 = (long long) 1e18;
const long double eps = 1e-9;
const long double pi = 3.14159265358979323846;
const int N = 1e5 + 10;
ll v[N], c[N];
int n;
ll z[N];
pair<ll,ll> tmp;
ll solve(ll a, ll b) {
	forn(i,n)
		z[i] = -INF64;
	vector < pair<ll,ll> > maxc(2);
	maxc[0] = mp(-INF64, 0);
	maxc[1] = mp(0, n);
	ll answer = 0;
	forn(i, n) {
		ll value = max(z[c[i]], z[c[i]] + a*v[i]);
		forn(j, sz(maxc)){
			if (c[i] != maxc[j].second)
				value = max(value, maxc[j].first + b*v[i]);
		}
		z[c[i]] = value;
		answer = max(answer, value);
		tmp = mp(z[c[i]], c[i]);
		if (maxc[0].second == tmp.second) {
			maxc[0] = tmp;
			continue;
		}
		if (maxc[1].second == tmp.second || maxc[1].first < tmp.first)
			maxc[1] = tmp;
		if (maxc[1].first > maxc[0].first)
			swap(maxc[1],maxc[0]);
	}
	return answer;
}
int main(){
#ifdef gridnevvvit
	freopen("input.txt","rt",stdin);
	freopen("output.txt","wt",stdout);
#endif
	int T;
	scanf("%d %d", &n, &T);
	forn(i,n)
		scanf("%I64d",&v[i]);
	forn(i, n)
		scanf("%I64d",&c[i]), c[i]--;
	while (T--) {
		ll a, b;
		scanf("%I64d%I64d", &a, &b);
		printf("%I64d\n", solve(a,b));
	}
}


#define _CRT_SECURE_NO_DEPRECATE
#define _SECURE_SCL 0
#pragma comment(linker, "/STACK:200000000")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <fstream>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <list>
#include <string>
#include <sstream>
#include <vector>
#include <utility>
#include <cmath>
using namespace std;

#define pb push_back
#define mp make_pair
#define mset(mas,val) memset(mas,val,sizeof(mas))
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

#define forn(i,n) for (int i=0; i<int(n); ++i)
#define fornd(i,n) for (int i=int(n)-1; i>=0; --i)
#define forab(i,a,b) for (int i=int(a); i<int(b); ++i)

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int INF = (int) 1e9;
const long long INF64 = (long long) 1e18;
const long double eps = 1e-9;
const long double pi = 3.14159265358979323846;
const int N = 1e5 + 10;
ll v[N], c[N];
int n;
ll z[N];
pair<ll,ll> tmp;
ll solve(ll a, ll b) {
	forn(i,n)
		z[i] = -INF64;
	vector < pair<ll,ll> > maxc(2);
	maxc[0] = mp(0, n);
	maxc[1] = mp(-INF64, 0);
	ll answer = 0;
	forn(i, n) {
		ll value = max(z[c[i]], z[c[i]] + a*v[i]);
		forn(j, sz(maxc)){
			if (c[i] != maxc[j].second)
				value = max(value, maxc[j].first + b*v[i]);
		}
		z[c[i]] = value;
		answer = max(answer, value);
		tmp = mp(z[c[i]], c[i]);
		if (maxc[0].second == tmp.second) {
			maxc[0] = tmp;
			continue;
		}
		if (maxc[1].second == tmp.second || maxc[1].first < tmp.first)
			maxc[1] = tmp;
		if (maxc[1].first > maxc[0].first)
			swap(maxc[1],maxc[0]);
	}
	return answer;
}
int main(){
#ifdef gridnevvvit
	freopen("input.txt","rt",stdin);
	freopen("output.txt","wt",stdout);
#endif
	int T;
	scanf("%d %d", &n, &T);
	forn(i,n)
		cin >> v[i];
	forn(i, n)
		cin >> c[i], c[i]--;
	while (T--) {
		ll a, b;
		cin >> a >> b;
		cout << solve(a,b) << endl;
	}
}


#pragma comment(linker, "/STACK:500000000")
#include <functional>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <ctype.h>
#include <stdio.h>
#include <bitset>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define Int long long
#define INF 0x3F3F3F3F
#define eps 1e-9
using namespace std;

#define N 100009

Int C[N], V[N];
Int dp[N];

int main()
{
    int n, q, i, j, a, b;
    cin >> n >> q;
    for(i = 0; i < n; i++)
        scanf("%I64d", &V[i]);
    for(i = 0; i < n; i++)
        scanf("%I64d", &C[i]);
    while(q--)
    {
        scanf("%d %d", &a, &b);
        Int ans = 0;
        Int m1 = -1000000000ll * 1000000000, m2 = -1000000000ll * 1000000000;
        fill(dp, dp + n + 1, m1);
        for(i = 0; i < n; i++)
        {
            Int res = max(dp[C[i]] + V[i] * a, b * V[i]);
            Int cur = dp[C[i]];
            if (m1 == cur)
            {
                res = max(res, b * V[i] + m2);
                dp[C[i]] = max(dp[C[i]], res);
                m2 = dp[C[i]];
                if (m2 > m1)
                    swap(m1, m2);
            }
            else
            {
                res = max(res, b * V[i] + m1);
                dp[C[i]] = max(dp[C[i]], res);
                m2 = dp[C[i]];
                if (m2 > m1)
                    swap(m1, m2);
            }
            ans = max(ans, res);
        }
        cout << ans << endl;
    }
}


#pragma comment(linker, "/STACK:500000000")
#include <functional>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <ctype.h>
#include <stdio.h>
#include <bitset>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define Int long long
#define INF 0x3F3F3F3F
#define eps 1e-9
using namespace std;

#define OPER(a, b) max(a, b)
const int ppow = 1 << 18;
const Int default_value = -1000000000ll * 1000000000;
struct SegmentTree
{
    Int t[ppow * 2];

    SegmentTree()
    {
        clear();
    }

    Int F(int L, int R)
    {
        L += ppow;
        R += ppow;
        Int res = default_value;
        while(L <= R)
        {
            if (L & 1)
                res = OPER(res, t[L++]);
            if (~R & 1)
                res = OPER(res, t[R--]);
            L /= 2;
            R /= 2;
        }
        return res;
    }

    void Add(int pos, Int value)
    {
        pos += ppow;
        t[pos] += value;
        pos /= 2;
        while(pos > 0)
        {
            t[pos] = OPER(t[pos * 2], t[pos * 2 + 1]);
            pos /= 2;
        }
    }

    void Set(int pos, Int value)
    {
        pos += ppow;
        t[pos] = value;
        pos /= 2;
        while(pos > 0)
        {
            t[pos] = OPER(t[pos * 2], t[pos * 2 + 1]);
            pos /= 2;
        }
    }

    void clear()
    {
        for(int i = ppow * 2 - 1; i >= 0; i--)
            t[i] = default_value;
    }

    Int operator [](int i)
    {
        return t[i + ppow];
    }
};

#define N 100009

Int C[N], V[N];

SegmentTree dp;

int main()
{
    int n, q, i, j, a, b;
    cin >> n >> q;
    for(i = 0; i < n; i++)
        scanf("%I64d", &V[i]);
    for(i = 0; i < n; i++)
        scanf("%I64d", &C[i]);

    while(q--)
    {
        scanf("%d %d", &a, &b);
        dp.clear();
        Int ans = 0;
        for(i = 0; i < n; i++)
        {
            Int res = max(dp[C[i]] + V[i] * a, b * V[i]);
            res = max(res, b * V[i] + max(dp.F(1, C[i] - 1), dp.F(C[i] + 1, n)));
            dp.Set(C[i], max(dp[C[i]], res));
            ans = max(ans, res);
        }
        cout << ans << endl;
    }
}


#pragma comment(linker, "/STACK:500000000")
#include <functional>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <ctype.h>
#include <stdio.h>
#include <bitset>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define Int long long
#define INF 0x3F3F3F3F
#define eps 1e-9
using namespace std;

#define N 100009

Int C[N], V[N];
Int dp[N];

int main()
{
    int n, q, i, j, a, b;
    cin >> n >> q;
    for(i = 0; i < n; i++)
        scanf("%I64d", &V[i]);
    for(i = 0; i < n; i++)
        scanf("%I64d", &C[i]);
    while(q--)
    {
        scanf("%d %d", &a, &b);
        Int ans = 0;
        Int m1 = -1000000000ll * 1000000000, m2 = -1000000000ll * 1000000000;
        fill(dp, dp + n + 1, m1);
        for(i = 0; i < n; i++)
        {
            Int res = max(dp[C[i]] + V[i] * a, b * V[i]);
            Int cur = dp[C[i]];
            if (m1 == cur)
            {
                res = max(res, b * V[i] + m2);
                dp[C[i]] = max(dp[C[i]], res);
                m1 = dp[C[i]];
            }
            else
            {
                res = max(res, b * V[i] + m1);
                dp[C[i]] = max(dp[C[i]], res);
                m2 = max(m2, dp[C[i]]);
                if (m2 > m1)
                    swap(m1, m2);
            }
            ans = max(ans, res);
        }
        cout << ans << endl;
    }
}


#pragma comment(linker, "/STACK:500000000")
#include <functional>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <ctype.h>
#include <stdio.h>
#include <bitset>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define Int long long
#define INF 0x3F3F3F3F
#define eps 1e-9
using namespace std;

#define N 100009

Int C[N], V[N];
Int dp[N];

int main()
{
    int n, q, i, j, a, b;
    cin >> n >> q;
    for(i = 0; i < n; i++)
        scanf("%I64d", &V[i]);
    for(i = 0; i < n; i++)
        scanf("%I64d", &C[i]);
    while(q--)
    {
        scanf("%d %d", &a, &b);
        Int ans = 0;
        Int m1 = -1000000000ll * 1000000000, m2 = -1000000000ll * 1000000000;
        fill(dp, dp + n + 1, m1);
        for(i = 0; i < n; i++)
        {
            Int res = max(dp[C[i]] + V[i] * a, b * V[i]);
            Int cur = dp[C[i]];
            if (m1 == cur)
            {
                res = max(res, b * V[i] + m2);
                dp[C[i]] = max(dp[C[i]], res);
                m2 = dp[C[i]];
                if (m2 > m1)
                    swap(m1, m2);
            }
            else
            {
                res = max(res, b * V[i] + m1);
                dp[C[i]] = max(dp[C[i]], res);
                m2 = max(m2, dp[C[i]]);
                if (m2 > m1)
                    swap(m1, m2);
            }
            ans = max(ans, res);
        }
        cout << ans << endl;
    }
}


#pragma comment(linker, "/STACK:500000000")
#include <functional>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <sstream>
#include <ctype.h>
#include <stdio.h>
#include <bitset>
#include <vector>
#include <string>
#include <math.h>
#include <time.h>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#define Int long long
#define INF 0x3F3F3F3F
#define eps 1e-9
using namespace std;

#define OPER(a, b) max(a, b)
const int ppow = 1 << 17;
const Int default_value = -1000000000ll * 1000000000;
struct SegmentTree
{
    Int t[ppow * 2];

    SegmentTree()
    {
        clear();
    }

    Int F(int L, int R)
    {
        L += ppow;
        R += ppow;
        Int res = default_value;
        while(L <= R)
        {
            if (L & 1)
                res = OPER(res, t[L++]);
            if (~R & 1)
                res = OPER(res, t[R--]);
            L /= 2;
            R /= 2;
        }
        return res;
    }

    void Set(int pos, Int value)
    {
        pos += ppow;
        t[pos] = value;
        pos /= 2;
        while(pos > 0)
        {
            t[pos] = OPER(t[pos * 2], t[pos * 2 + 1]);
            pos /= 2;
        }
    }

    void clear()
    {
        for(int i = ppow * 2 - 1; i >= 0; i--)
            t[i] = default_value;
    }

    Int operator [](int i)
    {
        return t[i + ppow];
    }
};

#define N 100009

Int C[N], V[N];

SegmentTree dp;

int main()
{
    int n, q, i, j, a, b;
    cin >> n >> q;
    for(i = 0; i < n; i++)
        scanf("%I64d", &V[i]);
    for(i = 0; i < n; i++)
        scanf("%I64d", &C[i]);
    while(q--)
    {
        scanf("%d %d", &a, &b);
        dp.clear();
        Int ans = 0;
        for(i = 0; i < n; i++)
        {
            Int res = max(dp[C[i]] + V[i] * a, b * V[i]);
            Int mg = dp.t[1];
            Int cur = dp[C[i]];
            if (cur == mg)
                res = max(res, b * V[i] + max(dp.F(1, C[i] - 1), dp.F(C[i] + 1, n)));
            else
                res = max(res, b * V[i] + mg);
            dp.Set(C[i], max(dp[C[i]], res));
            ans = max(ans, res);
        }
        cout << ans << endl;
    }
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/stack:16777216")
#include <string>
#include <vector>
#include <map>
#include <list>
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility>
#include <complex>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define RREP(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define PI acos(-1.0)

typedef long long Int;
typedef unsigned long long UInt;
typedef vector <int> VI;
typedef pair <int, int> PII;

const int INF = 1000000000;
const int MAX = 1 << 17;
const Int MINF = -10000000000000000LL;

int n, q;
int V[MAX];
int C[MAX];
Int R[MAX];
Int RC[MAX];
Int a, b;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif

    cin >> n >> q;
    FOR (i,0,n)
        scanf("%d", &V[i]);
    FOR (i,0,n)
    {
        scanf("%d", &C[i]);
        --C[i];
    }

    FOR (i,0,q)
    {
        cin >> a >> b;
        FOR (i,0,n)
            R[i] = RC[i] = MINF;

        Int m0 = MINF, m1 = MINF, m2;
        int c0 = -1, c1 = -1, c2;
        Int res = 0;

        FOR (i,0,n)
        {
            R[i] = b * V[i];
            if (RC[C[i]] > MINF)
                R[i] = max(R[i], RC[C[i]] + a * V[i]);
            if (m0 > MINF && c0 != C[i])
                R[i] = max(R[i], m0 + b * V[i]);
            if (m1 > MINF && c1 != C[i])
                R[i] = max(R[i], m1 + b * V[i]);
            RC[C[i]] = max(RC[C[i]], R[i]);

            m2 = RC[C[i]];
            c2 = C[i];
            if (m2 > m1)
            {
                swap(m2, m1);
                swap(c2, c1);
            }
            if (m1 > m0)
            {
                swap(m1, m0);
                swap(c1, c0);
            }
            if (c1 == c0)
            {
                swap(m1, m2);
                swap(c1, c2);
            }
            res = max(res, R[i]);
        }
        cout << res << endl;
    }

}


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define lx(x) (x<<1)
#define rx(x) (x<<1|1)
#define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)

/******** program ********************/

const int MAXN = 1e5+5;
const ll INF = 1e18;

ll dp[MAXN];
int val[MAXN],c[MAXN],n,m;

ll max3(ll x,ll y,ll z){
    return max(x,max(y,z));
}

void solve(){
    ll x,y;
    cin>>x>>y;

    fill( dp+1,dp+n+1,-INF );

    int fir = 0 , sec = 0;
    ll m1 = -INF , m2 = -INF;

    rep1(i,n){
        dp[c[i]] = max3( dp[c[i]],y*val[i],dp[c[i]]+x*val[i] );
        if(fir!=c[i])
            dp[c[i]] = max( dp[c[i]],m1+y*val[i] );
        else
            dp[c[i]] = max( dp[c[i]],m2+y*val[i] );

        if(dp[c[i]]>m1){
            if(c[i]!=fir){
                m2 = m1;
                m1 = dp[c[i]];
                sec = c[i];
                swap(fir,sec);
            }else
                m1 = dp[c[i]];
        }else if(dp[c[i]]>m2){
            sec = c[i];
            m2 = dp[c[i]];
        }
    }

    ll ans = 0;
    ans = max( ans,*max_element(dp+1,dp+n+1) );
    cout<<ans<<endl;
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("sum.in","r",stdin);
	//freopen("sum.out","w",stdout);
#endif

    while(cin>>n>>m){
        rep1(i,n)
            RD(val[i]);
        rep1(i,n)
            RD(c[i]);
        while(m--)
            solve();
    }

	return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define lx(x) (x<<1)
#define rx(x) (x<<1|1)
#define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)

/******** program ********************/

const int MAXN = 1e5+5;
const ll INF = 1e18;

ll dp[MAXN];
int c[MAXN],val[MAXN],n,m;

void solve(){
    ll x,y;
    cin>>x>>y;
    rep1(i,n)
        dp[i] = -INF;

    dp[0] = 0;
    ll ans = 0;

    int now = 1;

    rep1(i,n){
        if(i==1){
            dp[ c[i] ] = max(0LL,y*val[i]);
            continue;
        }
        if(dp[c[i]]!=-INF)
            dp[ c[i] ] = max(dp[c[i]],dp[ c[i] ] + x*val[i]);
        else
            dp[c[i]] = y*val[i];

        if(c[now]!=c[i]){
            ll t = ll(y)*val[i]+max(0LL,dp[now]);
            //cout<<"ttttttttttttttttt = "<<now<<" "<<dp[now]<<" "<<t<<endl;
            if(t>=dp[c[i]]){
                dp[ c[i] ] = t;
                now = c[i];
            }else if(t<=dp[c[i]])
                now = c[i];
            else if(dp[now]<=dp[c[i]])
                now = c[i];
        }
        //cout<<i<<" "<<now<<" "<<dp[now]<<endl;
        ans = max( ans,dp[c[i]] );
    }

    cout<<max(ans,ll(0))<<endl;
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("sum.in","r",stdin);
	//freopen("sum.out","w",stdout);
#endif

    while(~RD2(n,m)){
        rep1(i,n)
            RD(val[i]);
        rep1(i,n)
            RD(c[i]);
        while(m--)
            solve();
    }

	return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define lx(x) (x<<1)
#define rx(x) (x<<1|1)
#define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)

/******** program ********************/

const int MAXN = 1e5+5;
const ll INF = 1e18;

ll dp[MAXN];
int val[MAXN],c[MAXN],n,m;

ll max3(ll x,ll y,ll z){
    return max(x,max(y,z));
}

void solve(){
    ll x,y;
    cin>>x>>y;

    fill( dp+1,dp+n+1,-INF );

    int fir = c[1] , sec = 0;

    rep1(i,n){
        dp[c[i]] = max3( dp[c[i]],y*val[i],dp[c[i]]+x*val[i] );
        if( fir==c[i] ){
            if(sec&&dp[sec]+y*val[i]>=dp[c[i]])
                dp[c[i]] = dp[sec]+y*val[i];
        }else if( sec==c[i] ){
            if(dp[fir]+y*val[i]>=dp[c[i]]){
                dp[c[i]] = dp[fir]+y*val[i];
                swap(fir,sec);
            }else if(dp[c[i]]>dp[fir])
                swap(fir,sec);
        }else{
            if(dp[fir]+y*val[i]>=dp[c[i]]){
                dp[c[i]] = dp[fir]+y*val[i];
                sec = c[i];
                swap(fir,sec);
            }else if(dp[fir]<=dp[c[i]]){
                sec = c[i];
                swap(fir,sec);
            }
        }
    }

    ll ans = 0;
    ans = max( ans,*max_element(dp+1,dp+n+1) );
    cout<<ans<<endl;
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("sum.in","r",stdin);
	//freopen("sum.out","w",stdout);
#endif

    while(cin>>n>>m){
        rep1(i,n)
            RD(val[i]);
        rep1(i,n)
            RD(c[i]);
        while(m--)
            solve();
    }

	return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define lx(x) (x<<1)
#define rx(x) (x<<1|1)
#define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)

/******** program ********************/

const int MAXN = 1e5+5;
const ll INF = 1e18;

int val[MAXN],c[MAXN],n,m;
ll dp[MAXN];

void solve(){
    fill(dp+1,dp+n+1,-INF);
    ll x,y;
    scanf("%I64d%I64d",&x,&y);

    int id1 = 0 , id2 = 0;
    rep1(i,n){
        dp[c[i]] = max( dp[c[i]],max(dp[c[i]]+x*val[i] , y*val[i]) );
        if(id1==c[i])
            dp[c[i]] = max(dp[c[i]],dp[id2]+y*val[i]);
        else
            dp[c[i]] = max(dp[c[i]],dp[id1]+y*val[i]);
        if(dp[c[i]]>dp[id1]){
            if(id1!=c[i]){
                id2 = id1;
                id1 = c[i];
            }
        }else if(dp[c[i]]>dp[id2]&&id1!=c[i])
            id2 = c[i];
    }

    ll ans = 0;
    ans = max(ans,*max_element(dp+1,dp+n+1));
    cout<<ans<<endl;
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("sum.in","r",stdin);
	//freopen("sum.out","w",stdout);
#endif

    while(cin>>n>>m){
        rep1(i,n)
            scanf("%d",&val[i]);
        rep1(i,n)
            scanf("%d",&c[i]);
        while(m--)
            solve();
    }

	return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define lx(x) (x<<1)
#define rx(x) (x<<1|1)
#define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)

/******** program ********************/

const int MAXN = 1e5+5;
const ll INF = 1e18;

ll dp[MAXN];
int c[MAXN],val[MAXN],n,m;

void solve(){
    int x,y;
    RD2(x,y);
    rep1(i,n)
        dp[i] = -INF;

    dp[0] = 0;
    ll ans = 0;

    int now = 1;

    rep1(i,n){
        if(i==1){
            dp[ c[i] ] = max(0LL,ll(y)*val[i]);
            continue;
        }
        if(dp[c[i]]!=INF)
            dp[ c[i] ] = max(dp[c[i]],dp[ c[i] ] + ll(x)*val[i]);
        else
            dp[c[i]] = ll(y)*val[i];

        if(c[now]!=c[i]){
            ll t = ll(y)*val[i]+max(0LL,dp[now]);
            //cout<<"ttttttttttttttttt = "<<now<<" "<<dp[now]<<" "<<t<<endl;
            if(t>=dp[c[i]]){
                dp[ c[i] ] = t;
                now = c[i];
            }else if(t<=dp[c[i]])
                now = c[i];
            else if(dp[now]<=dp[c[i]])
                now = c[i];
        }
        //cout<<i<<" "<<now<<" "<<dp[now]<<endl;
        ans = max( ans,dp[c[i]] );
    }

    cout<<max(ans,ll(0))<<endl;
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("sum.in","r",stdin);
	//freopen("sum.out","w",stdout);
#endif

    while(~RD2(n,m)){
        rep1(i,n)
            RD(val[i]);
        rep1(i,n)
            RD(c[i]);
        while(m--)
            solve();
    }

	return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define lx(x) (x<<1)
#define rx(x) (x<<1|1)
#define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)

/******** program ********************/

const int MAXN = 1e5+5;
const ll INF = 1e18;

ll dp[MAXN];
int val[MAXN],c[MAXN],n,m;

ll max3(ll x,ll y,ll z){
    return max(x,max(y,z));
}

void solve(){
    ll x,y;
    cin>>x>>y;

    fill( dp+1,dp+n+1,-INF );

    int fir = 0 , sec = 0;
    ll m1 = -INF , m2 = -INF;

    rep1(i,n){
        dp[c[i]] = max3( dp[c[i]],y*val[i],dp[c[i]]+x*val[i] );

        if(fir!=c[i])
            dp[c[i]] = max( dp[c[i]],m1+y*val[i] );
        else
            dp[c[i]] = max( dp[c[i]],m2+y*val[i] );

        if(dp[c[i]]>m1){
            if(c[i]==fir){
                m1 = dp[c[i]];
            }else{
                sec = fir;
                m2 = m1;
                m1 = dp[c[i]];
                fir = c[i];
            }
        }else if(dp[c[i]]>m2){
            sec = c[i];
            m2 = dp[c[i]];
        }
    }

    ll ans = 0;
    ans = max( ans,*max_element(dp+1,dp+n+1) );
    cout<<ans<<endl;
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("sum.in","r",stdin);
	//freopen("sum.out","w",stdout);
#endif

    while(cin>>n>>m){
        rep1(i,n)
            RD(val[i]);
        rep1(i,n)
            RD(c[i]);
        while(m--)
            solve();
    }

	return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define lx(x) (x<<1)
#define rx(x) (x<<1|1)
#define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)

/******** program ********************/

const int MAXN = 1e5+5;
const ll INF = 1e18;

ll dp[MAXN];
int val[MAXN],c[MAXN],n,m;

ll max3(ll x,ll y,ll z){
    return max(x,max(y,z));
}

void solve(){
    ll x,y;
    cin>>x>>y;

    fill( dp+1,dp+n+1,-INF );

    int fir = c[1] , sec = 0;

    rep1(i,n){
        dp[c[i]] = max3( dp[c[i]],y*val[i],dp[c[i]]+x*val[i] );
        if( fir==c[i] ){
            if(sec&&dp[sec]+y*val[i]>=dp[c[i]])
                dp[c[i]] = dp[sec]+y*val[i];
        }else if( sec==c[i] ){
            if(dp[fir]+y*val[i]>=dp[c[i]]){
                dp[c[i]] = dp[fir]+y*val[i];
                swap(fir,sec);
            }
        }else{
            if(dp[fir]+y*val[i]>=dp[c[i]]){
                dp[c[i]] = dp[fir]+y*val[i];
                sec = c[i];
                swap(fir,sec);
            }
        }
    }

    ll ans = 0;
    ans = max( ans,*max_element(dp+1,dp+n+1) );
    cout<<ans<<endl;
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("sum.in","r",stdin);
	//freopen("sum.out","w",stdout);
#endif

    while(cin>>n>>m){
        rep1(i,n)
            RD(val[i]);
        rep1(i,n)
            RD(c[i]);
        while(m--)
            solve();
    }

	return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define lx(x) (x<<1)
#define rx(x) (x<<1|1)
#define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)

/******** program ********************/

const int MAXN = 1e5+5;
const ll INF = 1e18;

ll dp[MAXN];
int val[MAXN],c[MAXN],n,m;

ll max3(ll x,ll y,ll z){
    return max(x,max(y,z));
}

void solve(){
    ll x,y;
    cin>>x>>y;

    fill( dp+1,dp+n+1,-INF );

    int best = c[1];
    rep1(i,n){
        dp[c[i]] = max3( dp[c[i]],y*val[i],dp[c[i]]+x*val[i] );

        if(best==c[i])continue;
        ll t = dp[best]+y*val[i];
        if(t>=dp[c[i]]){
            dp[c[i]] = t;
            best = c[i];
        }else if(dp[best]<=dp[c[i]])
            best = c[i];
    }

    ll ans = 0;
    ans = max( ans,*max_element(dp+1,dp+n+1) );
    cout<<ans<<endl;
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("sum.in","r",stdin);
	//freopen("sum.out","w",stdout);
#endif

    while(cin>>n>>m){
        rep1(i,n)
            RD(val[i]);
        rep1(i,n)
            RD(c[i]);
        while(m--)
            solve();
    }

	return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define lx(x) (x<<1)
#define rx(x) (x<<1|1)
#define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)

/******** program ********************/

const int MAXN = 1e5+5;
const ll INF = 1e18;

int val[MAXN],c[MAXN],n,m;
ll dp[MAXN];

void solve(){
    fill(dp+1,dp+n+1,-INF);
    ll x,y;
    scanf("%I64d%I64d",&x,&y);

    int id1 = 0 , id2 = 0;
    rep1(i,n){
        dp[c[i]] = max( dp[c[i]],max(dp[c[i]]+x*val[i] , y*val[i]) );
        if(id1==c[i])
            dp[c[i]] = max(dp[c[i]],dp[id2]+y*val[i]);
        else
            dp[c[i]] = max(dp[c[i]],dp[id1]+y*val[i]);
        if(dp[c[i]]>dp[id1]){
            if(id1!=c[i]){
                id2 = id1;
                id1 = c[i];
            }
        }else if(dp[c[i]]>dp[id2]&&id2!=c[i])
            id2 = c[i];
    }

    ll ans = 0;
    ans = max(ans,*max_element(dp+1,dp+n+1));
    cout<<ans<<endl;
}

int main(){

#ifndef ONLINE_JUDGE
	freopen("sum.in","r",stdin);
	//freopen("sum.out","w",stdout);
#endif

    while(cin>>n>>m){
        rep1(i,n)
            scanf("%d",&val[i]);
        rep1(i,n)
            scanf("%d",&c[i]);
        while(m--)
            solve();
    }

	return 0;
}


#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
const double pi = acos(-1.0);
const int size = 200 * 1000;
const long long inf = (long long) 1e15;

int n, q;
int v[size], c[size];
int a, b;
long long best[size];

int main() {
	//freopen("problem_c.in", "r", stdin);
	//freopen("problem_c.out", "w", stdout);

	scanf("%d%d", &n, &q);
	for (int i = 0; i < n; i++)
		scanf("%d", &v[i]);
	for (int i = 0; i < n; i++) {
		scanf("%d", &c[i]);
		c[i]--;
	}

	for (int i = 0; i < q; i++) {
		scanf("%d%d", &a, &b);
		for (int j = 0; j < n; j++)
			best[j] = -inf;
		pair <long long, int> m1, m2, m3;
		m1 = mp(0, -1);
		m2 = mp(-inf, 1);
		long long bans = 0, mans = 0;
		for (int j = 0; j < n; j++) {
			mans = best[c[j]] + v[j] * 1ll * a;
			if (m1.sc == c[j])
				mans = max(mans, m2.fs + v[j] * 1ll * b);
			else
				mans = max(mans, m1.fs + v[j] * 1ll * b);
			bans = max(bans, mans);
			best[c[j]] = max(best[c[j]], mans);
			m3 = mp(best[c[j]], c[j]);
			if (m3 > m2)
				swap(m2, m3);
			if (m2 > m1)
				swap(m2, m1);
		}

		cout << bans << endl;
	}

	return 0;
}


#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
const double pi = acos(-1.0);
const int size = 200 * 1000;
const long long inf = (long long) 1e18;

int n, q;
int v[size], c[size];
int a, b;
long long best[size];

int main() {
	//freopen("problem_c.in", "r", stdin);
	//freopen("problem_c.out", "w", stdout);

	scanf("%d%d", &n, &q);
	for (int i = 0; i < n; i++)
		scanf("%d", &v[i]);
	for (int i = 0; i < n; i++) {
		scanf("%d", &c[i]);
		c[i]--;
	}

	for (int i = 0; i < q; i++) {
		scanf("%d%d", &a, &b);
		for (int j = 0; j < n; j++)
			best[j] = -inf;
		pair <long long, int> m1, m2, m3;
		m1 = mp(0, -1);
		m2 = mp(-inf, 0);
		long long bans = 0, mans = 0;
		for (int j = 0; j < n; j++) {
			mans = best[c[j]] + v[j] * 1ll * a;
			if (m1.sc == c[j])
				mans = max(mans, m2.fs + v[j] * 1ll * b);
			else
				mans = max(mans, m1.fs + v[j] * 1ll * b);
			bans = max(bans, mans);
			best[c[j]] = max(best[c[j]], mans);
			m3 = mp(best[c[j]], c[j]);
			if (m3.sc == m1.sc)
				m1 = mp(-inf, 0);
			if (m3.sc == m2.sc)
				m2 = mp(-inf, 0);
			if (m3 > m2)
				swap(m2, m3);
			if (m2 > m1)
				swap(m2, m1);
			if (m3 > m2)
				swap(m2, m3);
		}

		cout << bans << endl;
	}

	return 0;
}


#pragma comment(linker, "/stack:100000000")

#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <cassert>
#include <queue>

using namespace std;

#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()

const int N = 100100;
const long long INF = -(1LL << 60);

int v[N], c[N], id[N];
long long dp[N];

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	long long start = clock();
	srand(time(NULL));
#endif
	int n, q;
	scanf("%d %d", &n, &q);
	REP(i, n) scanf("%d", &v[i]);
	REP(i, n) scanf("%d", &c[i]);
	//n = 100000;
	//q = 500;
	//REP(i, n) v[i] = rand() * rand() % 100000 + 1;
	//REP(i, n) c[i] = rand() * rand() % 100000 + 1;
	REP(_, q) {
		long long a, b;
		scanf("%I64d %I64d", &a, &b);
		//a = rand() * rand() % 100000;
		//b = rand() * rand() % 100000;
		memset(id, -1, sizeof(id));
		fill(dp, dp + n, INF);
		//priority_queue <pair <long long, int> > q;
		pair <long long, int> mx1(INF, -1), mx2(INF, -1);
		REP(i, n) {
			dp[i] = max(dp[i], b * v[i]);
			if (id[c[i]] != -1) {
				if (a * v[i] > 0) {
					dp[i] = max(dp[i], dp[id[c[i]]] + a * v[i]);
				} else {
					dp[i] = max(dp[i], dp[id[c[i]]]);
				}
			}
			pair <long long, int> p(INF, -1);
			//while (!q.empty()) {
			//	p = q.top();
			//	if (p.second != c[i]) {
			//		break;
			//	}
			//	q.pop();
			//}
			if (mx1.second != c[i]) {
				p = mx1;
			} else {
				p = mx2;
			}
			if (p.second != -1 && p.second != c[i]) {
				dp[i] = max(dp[i], p.first + b * v[i]);
			}
			id[c[i]] = i;
			pair <long long, int> np(dp[i], c[i]);
			if (mx1.second == c[i]) mx1 = make_pair(INF, -1);
			if (mx2.second == c[i]) mx2 = make_pair(INF, -1);
			if (mx1.second == -1) mx1 = np;
			else if (mx2.second == -1) mx2 = np;
			else {
				if (np > mx1) {
					mx2 = mx1;
					mx1 = np;
				} else if (np > mx2) {
					mx2 = np;
				}
			}
			if (mx1 < mx2) swap(mx1, mx2);
			//q.push(make_pair(dp[i], c[i]));
		}
		printf("%I64d\n", max(0LL, *max_element(dp, dp + n)));
	}
#ifdef LOCAL
	fprintf(stderr, "\n\nTime: %.3lf\n\n", (clock() - start) / 1000.);
#endif
	return 0;
}


#pragma comment(linker, "/stack:100000000")

#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
#include <cassert>
#include <queue>

using namespace std;

#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()

const int N = 100100;

int v[N], c[N], id[N];
long long dp[N];

int main() {
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	long long start = clock();
#endif
	int n, q;
	scanf("%d %d", &n, &q);
	REP(i, n) scanf("%d", &v[i]);
	REP(i, n) scanf("%d", &c[i]);
	REP(_, q) {
		long long a, b;
		scanf("%I64d %I64d", &a, &b);
		memset(id, -1, sizeof(id));
		fill(dp, dp + n, -(1LL << 60));
		priority_queue <pair <long long, int> > q;
		REP(i, n) {
			dp[i] = max(dp[i], b * v[i]);
			if (id[c[i]] != -1) {
				if (a * v[i] > 0) {
					dp[i] = max(dp[i], dp[id[c[i]]] + a * v[i]);
				} else {
					dp[i] = max(dp[i], dp[id[c[i]]]);
				}
			}
			pair <long long, int> p(-1, -1);
			while (!q.empty()) {
				p = q.top();
				if (p.second != c[i]) {
					break;
				}
				q.pop();
			}
			if (p.second != c[i]) {
				dp[i] = max(dp[i], p.first + b * v[i]);
			}
			id[c[i]] = i;
			q.push(make_pair(dp[i], c[i]));
		}
		printf("%I64d\n", max(0LL, *max_element(dp, dp + n)));
	}
#ifdef LOCAL
	fprintf(stderr, "\n\nTime: %.3lf\n\n", (clock() - start) / 1000.);
#endif
	return 0;
}


#include <cstdio>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>

typedef long long LL;
typedef long double LD;

using namespace std;

const int MAXN = 1e5+5;
const LL INF = 1e18;

int n, q;
LL v[MAXN];
int c[MAXN];
LL best[MAXN];

int m;
int top[3];

LL getBestDifftentFrom(int c) {
	for(int i = 0; i < m; i++) if(top[i] != c) return best[top[i]];
	return 0;
}

void setValue(int c, LL v) {
	if(best[c] >= v) return;

	best[c] = v;
	bool ok = false;
	for(int i = 0; i < m; i++) {
		if(top[i] == c) {
			ok = true;
		}
	}
	if(!ok) {
		top[min(m, 2)] = c;
		if(m < 3) m++;
	}

	for(int i = 0; i < m; i++) {
		for(int j = i+1; j < m; j++) {
			if(best[top[i]] < best[top[j]]) swap(top[i], top[j]);
		}
	}

}

LL solve(LL a, LL b) {
	LL res = 0;
	m = 0;
	for(int i = 1; i <= n; i++) best[i] = -INF;

	for(int i = 0; i < n; i++) {
		LL curr = v[i]*b;
		curr = max(curr, v[i]*a + best[c[i]]);
		curr = max(curr, v[i]*b + getBestDifftentFrom(c[i]));

		setValue(c[i], curr);
		res = max(res, curr);
	}
	return res;

}

int main() {
	scanf("%d %d", &n, &q);
	for(int i = 0; i < n; i++) scanf("%I64d ", &v[i]);
	for(int i = 0; i < n; i++) scanf("%d ", &c[i]);
	for(int i = 0; i < q; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		printf("%I64d \n", solve(a, b));
	}
}


#include <cstdio>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>

typedef long long LL;
typedef long double LD;

using namespace std;

const int MAXN = 1e5+5;
const LL INF = 1e18;

int n, q;
LL v[MAXN];
int c[MAXN];
LL best[MAXN];

int m;
int top[3];

LL getBestDifftentFrom(int c) {
	LL res = 0;
	for(int j = 1; j <= n; j++)  if(j != c) res = max(res, best[j]);
	return res;
	for(int i = 0; i < m; i++) if(top[i] != c) return best[top[i]];
	return -INF;
}

void setValue(int c, LL v) {
	if(best[c] >= v) return;

	best[c] = v;
	bool ok = false;
	for(int i = 0; i < m; i++) {
		if(top[i] == c) {
			ok = true;
		}
	}
	if(!ok) {
		top[min(m, 2)] = c;
		if(m < 3) m++;
	}

	for(int i = 0; i < m; i++) {
		for(int j = 0; j < m; j++) {
			if(best[top[i]] < best[top[j]]) swap(top[i], top[j]);
		}
	}

}

LL solve(LL a, LL b) {
	LL res = 0;
	m = 0;
	for(int i = 1; i <= n; i++) best[i] = -INF;

	for(int i = 0; i < n; i++) {
		LL curr = v[i]*b;
		curr = max(curr, v[i]*a + best[c[i]]);
		curr = max(curr, v[i]*b + getBestDifftentFrom(c[i]));

		setValue(c[i], curr);
		res = max(res, curr);
	}
	return res;

}

int main() {
	scanf("%d %d", &n, &q);
	for(int i = 0; i < n; i++) scanf("%I64d ", &v[i]);
	for(int i = 0; i < n; i++) scanf("%d ", &c[i]);
	for(int i = 0; i < q; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		printf("%I64d \n", solve(a, b));
	}
}


#include <cstdio>
#include <set>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>

typedef long long LL;
typedef long double LD;

using namespace std;

const int MAXN = 1e5+5;
const LL INF = 1e18;

int n, q;
LL v[MAXN];
int c[MAXN];
LL best[MAXN];

int m;
int top[3];

LL getBestDifftentFrom(int c) {
	for(int i = 0; i < m; i++) if(top[i] != c) return best[top[i]];
	return -INF;
}

void setBest(int c, LL v) {
	best[c] = v;
	bool ok = false;
	for(int i = 0; i < m; i++) {
		if(top[i] == c) {
			ok = true;
		}
	}
	if(!ok) {
		top[min(m, 2)] = c;
		if(m < 3) m++;
	}

	for(int i = 0; i < m; i++) {
		for(int j = 0; j < m; j++) {
			if(best[top[i]] < best[top[j]]) swap(top[i], top[j]);
		}
	}

}

LL solve(LL a, LL b) {
	LL res = 0;
	m = 0;
	for(int i = 1; i <= n+1; i++) {
		best[i] = -INF;
	}

	for(int i = 0; i < n; i++) {
		LL curr = v[i]*b;
		curr = max(curr, v[i]*a+best[c[i]]);
		curr = max(curr, v[i]*b + getBestDifftentFrom(c[i]));

		res = max(res, curr);
		setBest(c[i], max(best[c[i]], curr));

	}
	return res;

}

int main() {
	scanf("%d %d", &n, &q);
	for(int i = 0; i < n; i++) scanf("%I64d ", &v[i]);
	for(int i = 0; i < n; i++) scanf("%d ", &c[i]);
	for(int i = 0; i < q; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		printf("%I64d \n", solve(a, b));
	}
}


#include<stdio.h>
#include<algorithm>
using namespace std;

#define MAX(A, B) ((A)>(B)?(A):(B))

typedef __int64 LL;
LL v[100005];
int c[100005];
LL ans;
int n;
LL a, b;
LL dp[100005];

LL max1, max2;
int col1, col2;

inline void CHECK(int color, LL val)
{
	if(dp[color] >= val) return;

	dp[color] = val;

	if(ans < val) ans = val;

	if(color==col1)
	{
		max1 = val;
		return;
	}

	if(color==col2)
	{
		max2 = val;

		if(max1 < max2)
		{
			swap(max1, max2);
			swap(col1, col2);
		}

		return;
	}

	if(val > max1)
	{
		max2 = max1;
		col2 = col1;

		max1 = val;
		col1 = color;
	}
	else if(val > max2)
	{
		max2 = val;
		col2 = color;
	}
}

void solve()
{
	int i;
	LL now;

	LL aa = -10000000;
	aa*=-aa;

	col1 = -1, col2 = -1;
	max1 = aa, max2 = aa;

	for(i = 0; i <= n; i++)
		dp[i] = aa;

	for(i = 0; i < n; i++)
	{
		if( c[i] == col1 )
		{
			if(max2!=-1)
			{
				now = MAX(dp[ c[i] ] + v[i]*a, max2 + v[i]*b);
				now = MAX(now, v[i]*b);
				CHECK(c[i], now);
			}
			else
			{
				now = dp[ c[i] ] + v[i]*a;
				now = MAX(now, v[i]*b);
				CHECK(c[i], now);
			}
		}
		else if( c[i] == col2 )
		{
			now = MAX(max1 + v[i]*b, dp[ c[i] ] + v[i]*a);
			now = MAX(now, v[i]*b);
			CHECK(c[i], now);
		}
		else
		{
			if(dp[ c[i] ]!=aa)
			{
				now = MAX(max1 + v[i]*b, dp[ c[i] ] + v[i]*a);
				now = MAX(now, v[i]*b);
				CHECK(c[i], now);
			}
			else
			{
				now = MAX(0,max1) + v[i]*b;
				now = MAX(now, v[i]*b);
				CHECK(c[i], now);
			}
		}
	}
}

int main()
{
	int q, i;

	scanf("%d%d", &n, &q);

	for(i = 0; i < n; i++)
		scanf("%I64d", &v[i]);

	for(i = 0; i < n; i++)
		scanf("%d", &c[i]);

	while(q--)
	{
		scanf("%I64d%I64d", &a, &b);

		ans = 0;
		solve();

		printf("%I64d\n", ans);
	}

	return 0;
}


#pragma warning( disable : 4786 )

#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <string>
#include <math.h>
#include <iostream>
#include <sstream>
#include <utility>
#include <limits>
#include <numeric>
#include <iomanip>
#include <fstream>
#include <memory.h>
#include <algorithm>

using namespace std;

const int M = 100000;
int c[M + 5];
int v[M + 5];
long long last[M + 5];
int was[M + 5];

int main()
{
	int n, q;
	scanf("%d%d", &n, &q);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &v[i]);
	}
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &c[i]);
	}

	for (int test = 0; test < q; ++test)
	{
		int a1, b1;
		scanf("%d%d", &a1, &b1);
		long long a = a1, b = b1;
		long long besta = 0, bestb = 0;
		const int E = -1;
		int numa = E, numb = E;
		for (int i = 0; i < n; ++i)
		{
			long long d = v[i]*b;
			if (was[c[i]] == test+1)
			{
				d = max(d, last[c[i]] + v[i]*a);
			}
			if (numa != E && numa != c[i])
			{
				d = max(d, besta + v[i]*b);
			}
			else if (numb != E)
			{
				d = max(d, bestb + v[i]*b);
			}

			if (was[c[i]] != test+1)
			{
				last[c[i]] = d;
			}
			else
			{
				last[c[i]] = max(last[c[i]], d);
			}
			was[c[i]] = test+1;
			if (d > besta)
			{
				if (c[i] != numa) { bestb = besta; numb = numa;}
				besta = d; numa = c[i];
			}
			else if (d == besta && d > bestb)
			{
				bestb = d; numb = c[i];
			}
		}
		cout << besta << endl;
	}
	return 0;
}


#pragma warning( disable : 4786 )

#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <string>
#include <math.h>
#include <iostream>
#include <sstream>
#include <utility>
#include <limits>
#include <numeric>
#include <iomanip>
#include <fstream>
#include <memory.h>
#include <algorithm>

using namespace std;

const int M = 100000;
int c[M + 5];
int v[M + 5];
long long last[M + 5];
int was[M + 5];

int main()
{
	int n, q;
	scanf("%d%d", &n, &q);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &v[i]);
	}
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &c[i]);
	}

	for (int test = 0; test < q; ++test)
	{
		int a1, b1;
		scanf("%d%d", &a1, &b1);
		long long a = a1, b = b1;
		long long besta = -1000000000000000LL, bestb = -1000000000000000LL;
		const int E = -1;
		int numa = E, numb = E;
		for (int i = 0; i < n; ++i)
		{
			long long d = v[i]*b;
			if (was[c[i]] == test+1)
			{
				d = max(d, last[c[i]] + v[i]*a);
			}
			if (numa != E && numa != c[i])
			{
				d = max(d, besta + v[i]*b);
			}
			else if (numb != E)
			{
				d = max(d, bestb + v[i]*b);
			}

			if (was[c[i]] != test+1)
			{
				last[c[i]] = d;
			}
			else
			{
				last[c[i]] = max(last[c[i]], d);
			}
			was[c[i]] = test+1;
			if (d > besta)
			{
				if (c[i] != numa) { bestb = besta; numb = numa;}
				besta = d; numa = c[i];
			}
			else if (d > bestb)
			{
				bestb = d; numb = c[i];
			}
		}
		cout << max(0LL, besta) << endl;
	}
	return 0;
}


#pragma warning( disable : 4786 )

#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <string>
#include <math.h>
#include <iostream>
#include <sstream>
#include <utility>
#include <limits>
#include <numeric>
#include <iomanip>
#include <fstream>
#include <memory.h>
#include <algorithm>

using namespace std;

const int M = 100000;
int c[M + 5];
int v[M + 5];
long long last[M + 5];
int was[M + 5] = {0};

int main()
{
	int n, q;
	scanf("%d%d", &n, &q);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &v[i]);
	}
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &c[i]);
	}

	for (int test = 0; test < q; ++test)
	{
		int a1, b1;
		scanf("%d%d", &a1, &b1);
		long long a = a1, b = b1;
		long long besta = -1000000000000000LL, bestb = -1000000000000000LL;
		const int E = -1;
		int numa = E, numb = E;
		for (int i = 0; i < n; ++i)
		{
			long long d = v[i]*b;
			if (was[c[i]] == test+1)
			{
				d = max(d, last[c[i]] + v[i]*a);
			}
			if (numa != E && numa != c[i])
			{
				d = max(d, besta + v[i]*b);
			}
			else if (numa == c[i] && numb != E)
			{
				d = max(d, bestb + v[i]*b);
			}

			if (was[c[i]] != test+1)
			{
				last[c[i]] = d;
			}
			else
			{
				last[c[i]] = max(last[c[i]], d);
			}
			was[c[i]] = test+1;
			if (d > besta || d == besta && numa == c[i])
			{
				if (c[i] != numa) { bestb = besta; numb = numa;}
				besta = d; numa = c[i];
			}
			else if (d > bestb)
			{
				bestb = d; numb = c[i];
			}
		}
		cout << max(0LL, besta) << endl;
	}
	return 0;
}


#pragma warning( disable : 4786 )

#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <string>
#include <math.h>
#include <iostream>
#include <sstream>
#include <utility>
#include <limits>
#include <numeric>
#include <iomanip>
#include <fstream>
#include <memory.h>
#include <algorithm>

using namespace std;

const int M = 100000;
int c[M + 5];
int v[M + 5];
long long last[M + 5];
int was[M + 5] = {0};

int main()
{
	int n, q;
	scanf("%d%d", &n, &q);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &v[i]);
	}
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &c[i]);
	}

	for (int test = 0; test < q; ++test)
	{
		int a1, b1;
		scanf("%d%d", &a1, &b1);
		long long a = a1, b = b1;
		long long besta = -1000000000000000LL, bestb = -1000000000000000LL;
		const int E = -1;
		int numa = E, numb = E;
		for (int i = 0; i < n; ++i)
		{
			long long d = v[i]*b;
			if (was[c[i]] == test+1)
			{
				d = max(d, last[c[i]] + v[i]*a);
			}
			if (numa != E && numa != c[i])
			{
				d = max(d, besta + v[i]*b);
			}
			else if (numa == c[i] && numb != E)
			{
				d = max(d, bestb + v[i]*b);
			}

			if (was[c[i]] != test+1)
			{
				last[c[i]] = d;
			}
			else
			{
				last[c[i]] = max(last[c[i]], d);
			}
			was[c[i]] = test+1;
			if (d > besta || d == besta && numa == c[i])
			{
				if (c[i] != numa) { bestb = besta; numb = numa;}
				besta = d; numa = c[i];
			}
			else if (d > bestb && c[i] != numa)
			{
				bestb = d; numb = c[i];
			}
		}
		cout << max(0LL, besta) << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

//#define debug

using namespace std;

const double EPS = 1e-9;
const int MXN = 1e6 + 10;

int a, b;

bool check(double x){
    return (x - floor(x) > EPS);
}

double calc(pair <int, int> a, pair <int, int> b){
    double tmp1 = (a.first - b.first) * 1.0;
    double tmp2 = (a.second - b.second) * 1.0;
    double dist = sqrt(tmp1 * tmp1 + tmp2 + tmp2);
    return dist;
}

bool boo[MXN];
pair <int, int> c[MXN];

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    for(int i = 3; i <= 1000; ++i){
        for(int j = 4; j <= 1000; ++j){
            double z = sqrt(i * i * 1.0 + j * j * 1.0);
            if(z < 10000.0 && !check(z) && !boo[int(z)]){
                boo[int(z)] = true;
                c[int(z)] = make_pair(i, j);
            }
        }
    }
    #ifdef debug
    for(int i = 1; i <= 100; ++i)
        printf("%d ", i);
    printf("\n");
    for(int i = 1; i <= 100; ++i)
        printf("%d ", boo[i]);
    #endif // debug
    scanf("%d%d", &a, &b);
    swap(c[a].first, c[a].second);
    if(!boo[a] || !boo[b] || check(calc(c[a], c[b])))
        printf("NO");
    else
        printf("YES\n0 0\n%d %d\n%d %d\n", c[a].second, c[a].first, -c[b].first, c[b].second);
    return 0;
}


#include <bits/stdc++.h>

//#define debug

using namespace std;

const double EPS = 1e-9;
const int MXN = 1e6 + 10;

int a, b;

bool check(double x){
    return (x - floor(x) > EPS);
}

double calc(pair <int, int> a, pair <int, int> b){
    double tmp1 = (a.first - b.first) * 1.0;
    double tmp2 = (a.second - b.second) * 1.0;
    double dist = sqrt(tmp1 * tmp1 + tmp2 + tmp2);
    return dist;
}

bool boo[MXN];
pair <int, int> c[MXN];

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    for(int i = 3; i <= 1000; ++i){
        for(int j = 4; j <= 1000; ++j){
            double z = sqrt(i * i * 1.0 + j * j * 1.0);
            if(z < 10000.0 && !check(z) && !boo[int(z)]){
                boo[int(z)] = true;
                c[int(z)] = make_pair(i, j);
            }
        }
    }
    #ifdef debug
    for(int i = 1; i <= 100; ++i)
        printf("%d ", i);
    printf("\n");
    for(int i = 1; i <= 100; ++i)
        printf("%d ", boo[i]);
    #endif // debug
    scanf("%d%d", &a, &b);
    if(!boo[a] || !boo[b] || check(calc(c[a], c[b])))
        printf("NO");
    else
        printf("YES\n0 0\n%d %d\n%d %d\n", c[a].second, c[a].first, -c[b].first, c[b].second);
    return 0;
}


#include <bits/stdc++.h>

//#define debug

using namespace std;

const double EPS = 1e-9;
const int MXN = 1e6 + 10;

int a, b;

bool check(double x){
    double fpart, intpart;
    fpart = modf(x, &intpart);
    return fpart > EPS;
}

bool boo[MXN];
pair <int, int> c[MXN];

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    for(int i = 3; i <= 2000; ++i){
        for(int j = 4; j <= 2000; ++j){
            double z = sqrt(i * i * 1.0 + j * j * 1.0);
            if(z < 10000.0 && !check(z)){
                boo[int(z)] = true;
                c[int(z)] = make_pair(i, j);
            }
        }
    }
    #ifdef debug
    for(int i = 1; i <= 1000; ++i)
        if(boo[i])
            printf("%d ", i);
    #endif // debug
    scanf("%d%d", &a, &b);
    if(!boo[a] || !boo[b])
        printf("NO");
    else
        printf("YES\n0 0\n%d %d\n%d %d\n", c[a].first, c[a].second, c[b].second, -c[b].first);
    return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const double EPS = 1e-9;

int a, b;
bool ok;
pair <int, int> c;

int cross_product(pair <int, int> a, pair<int, int> b){
    return a.F * b.S - a.S * b.F;
}

bool check(double x){
    double asd;
    x = modf(x, &asd);
    return x <= EPS;
}

int main(){
    scanf("%d%d", &a, &b);
    for(int i = 3; i <= 1000; ++i){
        double tmp = sqrt(a * a * 1.0 - i * i * 1.0);
        if(check(tmp)){
            ok = true;
            c = make_pair(i, int(tmp));
            break;
        }
    }
    pair <int, int> d = make_pair(c.S, c.F);
    if(!ok){
        puts("NO");
        return 0;
    }
    int g = __gcd(d.F, d.S);
    d = make_pair(d.F / g, d.S / g);
    int dist = hypot(d.F, d.S);
    if(b %  dist != 0){
        puts("NO");
        return 0;
    }
    d = make_pair(-d.F * b / dist, d.S * b / dist);
    if(!cross_product(make_pair(d.F - c.F, d.S - c.S), make_pair(100000, 0)) ||
        !cross_product(make_pair(d.F - c.F, d.S - c.S), make_pair(100000, 0)))
        puts("NO");
    printf("YES\n0 0\n%d %d\n%d %d\n", c.F, c.S, d.F, d.S);
    return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const double EPS = 1e-9;

int a, b;
bool ok;
pair <int, int> c;

int cross_product(pair <int, int> a, pair<int, int> b){
    return a.F * b.S - a.S * b.F;
}

bool check(double x){
    double asd;
    x = modf(x, &asd);
    return x <= EPS;
}

int main(){
    scanf("%d%d", &a, &b);
    for(int i = 1; i <= 1000; ++i){
        double tmp = sqrt(a * a * 1.0 - i * i * 1.0);
        if(check(tmp)){
            ok = true;
            c = make_pair(i, int(tmp));
            break;
        }
    }
    pair <int, int> d = make_pair(c.S, c.F);
    if(!ok){
        puts("NO");
        return 0;
    }
    int g = __gcd(d.F, d.S);
    d = make_pair(d.F / g, d.S / g);
    int dist = hypot(d.F, d.S);
    if(b %  dist != 0){
        puts("NO");
        return 0;
    }
    d = make_pair(-d.F * b / dist, d.S * b / dist);
    if(!cross_product(make_pair(d.F - c.F, d.S - c.S), make_pair(100000, 0)) ||
        !cross_product(make_pair(d.F - c.F, d.S - c.S), make_pair(100000, 0)))
        puts("NO");
    printf("YES\n0 0\n%d %d\n%d %d\n", c.F, c.S, d.F, d.S);
    return 0;
}


#include <bits/stdc++.h>

//#define debug

using namespace std;

const double EPS = 1e-6;
const int MXN = 1e6 + 10;

int a, b;

bool check(double x){
    double fpart, intpart;
    fpart = modf(x, &intpart);
    return fpart > EPS;
}

bool boo[MXN];
pair <int, int> c[MXN];

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    for(int i = 3; i <= 2000; ++i){
        for(int j = 4; j <= 2000; ++j){
            double z = sqrt(i * i * 1.0 + j * j * 1.0);
            if(z < 10000.0 && !check(z) && !boo[int(z)]){
                boo[int(z)] = true;
                c[int(z)] = make_pair(i, j);
            }
        }
    }
    #ifdef debug
    for(int i = 1; i <= 1000; ++i)
        if(boo[i])
            printf("%d ", i);
    #endif // debug
    scanf("%d%d", &a, &b);
    if(!boo[a] || !boo[b])
        printf("NO");
    else
        printf("YES\n0 0\n%d %d\n%d %d\n", c[a].first, c[a].second, c[b].second, -c[b].first);
    return 0;
}


#include <bits/stdc++.h>

//#define debug

using namespace std;

const double EPS = 1e-15;
const int MXN = 1e6 + 10;

int a, b;

bool check(double x){
    double fpart, intpart;
    fpart = modf(x, &intpart);
    return fpart > EPS;
}

bool boo[MXN];
pair <int, int> c[MXN];

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    for(int i = 3; i <= 2000; ++i){
        for(int j = 4; j <= 2000; ++j){
            double z = sqrt(i * i * 1.0 + j * j * 1.0);
            if(z < 10000.0 && !check(z) && !boo[int(z)]){
                boo[int(z)] = true;
                c[int(z)] = make_pair(i, j);
            }
        }
    }
    #ifdef debug
    for(int i = 1; i <= 1000; ++i)
        if(boo[i])
            printf("%d ", i);
    #endif // debug
    scanf("%d%d", &a, &b);
    if(!boo[a] || !boo[b])
        printf("NO");
    else
        printf("YES\n0 0\n%d %d\n%d %d\n", c[a].first, c[a].second, c[b].second, -c[b].first);
    return 0;
}


#include <bits/stdc++.h>

//#define debug

using namespace std;

const double EPS = 1e-9;
const int MXN = 1e6 + 10;

int a, b;

bool check(double x){
    double fpart, intpart;
    fpart = modf(x, &intpart);
    return fpart > EPS;
}

bool boo[MXN];
pair <int, int> c[MXN];

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    for(int i = 3; i <= 2000; ++i){
        for(int j = 4; j <= 2000; ++j){
            double z = sqrt(i * i * 1.0 + j * j * 1.0);
            if(z < 10000.0 && !check(z) && !boo[int(z)]){
                boo[int(z)] = true;
                c[int(z)] = make_pair(i, j);
            }
        }
    }
    #ifdef debug
    for(int i = 1; i <= 1000; ++i)
        if(boo[i])
            printf("%d ", i);
    #endif // debug
    scanf("%d%d", &a, &b);
    if(!boo[a] || !boo[b])
        printf("NO");
    else
        printf("YES\n0 0\n%d %d\n%d %d\n", c[a].first, c[a].second, c[b].second, -c[b].first);
    return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second

using namespace std;

const double EPS = 1e-9;

int a, b;
bool ok;
pair <int, int> c;

int cross_product(pair <int, int> a, pair<int, int> b){
    return a.F * b.S - a.S * b.F;
}

bool check(double x){
    double asd;
    x = modf(x, &asd);
    return x <= EPS;
}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // ONLINE_JUDGE
    scanf("%d%d", &a, &b);
    for(int i = 3; i <= 1000; ++i){
        double tmp = sqrt(a * a * 1.0 - i * i * 1.0);
        if(check(tmp)){
            ok = true;
            c = make_pair(i, int(tmp));
            break;
        }
    }
    pair <int, int> d = make_pair(c.S, c.F);
    if(!ok){
        puts("NO");
        return 0;
    }
    int g = __gcd(d.F, d.S);
    d = make_pair(d.F / g, d.S / g);
    int dist = hypot(d.F, d.S);
    if(b % dist != 0){
        puts("NO");
        return 0;
    }
    d = make_pair(-d.F * b / dist, d.S * b / dist);
    if(!cross_product(make_pair(d.F - c.F, d.S - c.S), make_pair(100, 0)) ||
        !cross_product(make_pair(d.F - c.F, d.S - c.S), make_pair(0, 100))){
        swap(c.F, c.S);
        d = make_pair(-d.S, -d.F);
    }
    if(!cross_product(make_pair(d.F - c.F, d.S - c.S), make_pair(100, 0)) ||
        !cross_product(make_pair(d.F - c.F, d.S - c.S), make_pair(0, 100)))
        puts("NO");
    else
        printf("YES\n0 0\n%d %d\n%d %d\n", c.F, c.S, d.F, d.S);
    return 0;
}