#include <bits/stdc++.h>
using namespace std;
#define ll long long

void solve() {
    int n;
    cin >> n;

    int a, b;

    while(n--) {
        int x;
        cin >> x;
        if(x == 1) a++;
        else b++;
    }

    if(a > 0) {
        a--;
        cout << 1 << " ";
    }

    while(b--) {
        cout << 2 <<  " ";
    }

    while(a--) {
        cout << 1 << " ";
    }

    cout << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin>>t;

    while(t--)
        solve();
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

void solve() {
    int n;
    cin >> n;

    int a=0, b=0;

    while(n--) {
        int x;
        cin >> x;
        if(x == 1) a++;
        else b++;
    }

    if(b > 0) {
        b--;
        cout << 2 << " ";
    }
    if(a > 0) {
        a--;
        cout << 1 << " ";
    }
    while(b--) {
        cout <<  2 << " ";
    }
    while(a--) {
        cout << 1 << " ";
    }
    cout << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    //int t;
    //cin>>t;

    //while(t--)
        solve();
}


#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define ll		long long
#define fi		first
#define se		second
#define pb		push_back
#define all(a)		(a).begin(), (a).end()
#define sz(a)		((int)(a.size()))
#define REP(i, n)	for (int i=0; i<(n); ++i)
#define FOR(i, a, b)	for (int i=(a); i<=(b); ++i)
#define FORR(i, a, b)	for (int i=(a); i>=(b); --i)

typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef tuple<int, int, int> ti3;
typedef tuple<int, int, int, int> ti4;
typedef stack<int> si;
typedef queue<int> qi;
typedef priority_queue<int> pqi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef tuple<ll, ll, ll> tl3;
typedef tuple<ll, ll, ll, ll> tl4;
typedef stack<ll> sl;
typedef queue<ll> ql;
typedef priority_queue<ll> pql;

const int mod = 1e9+7;
const ll inf = 1ll<<55;
const ld pi = 3.1415926535897932384626433;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
const int ddy[8] = {1, -1, 1, 0, -1, 1, 0, -1};

ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }
ll LCM(ll a, ll b) { return (a*b)/GCD(a, b); }

#define MAX_VAL 200001
int num[200001];
bool prime[200001];
int n, t, one, two, idx, sum;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

#ifdef DEBUG_MODE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	vi vec;

	cin >> n;
	for (int i=0; i<n; ++i) {
		scanf("%d", &t);
		if (t == 1)
			one++;
		else
			two++;
	}

	fill(prime+2, prime+MAX_VAL, true);
	for (int i=2; i<=MAX_VAL; ++i) {
		if (prime[i])
			vec.push_back(i);
		for (int j=i; j<MAX_VAL; j+=i)
			prime[j] = false;
	}

	while (one + two > 0) {
		if (idx >= vec.size())
			break;

		int cnt = vec[idx++] - sum;

		while (cnt >= 2 && two != 0) {
			cout << "2 ";
			cnt -= 2;
			sum += 2;
			two--;
		}

		while (cnt >= 1 && one != 0) {
			cout << "1 ";
			cnt -= 1;
			sum += 1;
			one--;
		}
	}

	return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define ll		long long
#define fi		first
#define se		second
#define pb		push_back
#define all(a)		(a).begin(), (a).end()
#define sz(a)		((int)(a.size()))
#define REP(i, n)	for (int i=0; i<(n); ++i)
#define FOR(i, a, b)	for (int i=(a); i<=(b); ++i)
#define FORR(i, a, b)	for (int i=(a); i>=(b); --i)

typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef tuple<int, int, int> ti3;
typedef tuple<int, int, int, int> ti4;
typedef stack<int> si;
typedef queue<int> qi;
typedef priority_queue<int> pqi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef tuple<ll, ll, ll> tl3;
typedef tuple<ll, ll, ll, ll> tl4;
typedef stack<ll> sl;
typedef queue<ll> ql;
typedef priority_queue<ll> pql;

const int mod = 1e9+7;
const ll inf = 1ll<<55;
const ld pi = 3.1415926535897932384626433;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
const int ddy[8] = {1, -1, 1, 0, -1, 1, 0, -1};

ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }
ll LCM(ll a, ll b) { return (a*b)/GCD(a, b); }

#define MAX_VAL 200001

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

#ifdef DEBUG_MODE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int num[MAX_VAL];
	int n, one = 0, two = 0;

	cin >> n;
	for (int i=0; i<n; ++i) {
		scanf("%d", &num[i]);
		if (num[i] == 1)
			one++;
		else
			two++;
	}

	if (one == 0 || two == 0) {
		for (int i=0; i<n; ++i)
			cout << num[i] << " ";
	} else {
		one--;
		two--;
		cout << "2 1";
		for (int i=0; i<two; ++i)
			cout<< " 2";
		for (int i=0; i<one; ++i)
			cout << " 1";
	}

	return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define ll		long long
#define fi		first
#define se		second
#define pb		push_back
#define all(a)		(a).begin(), (a).end()
#define sz(a)		((int)(a.size()))
#define REP(i, n)	for (int i=0; i<(n); ++i)
#define FOR(i, a, b)	for (int i=(a); i<=(b); ++i)
#define FORR(i, a, b)	for (int i=(a); i>=(b); --i)

typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef tuple<int, int, int> ti3;
typedef tuple<int, int, int, int> ti4;
typedef stack<int> si;
typedef queue<int> qi;
typedef priority_queue<int> pqi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef tuple<ll, ll, ll> tl3;
typedef tuple<ll, ll, ll, ll> tl4;
typedef stack<ll> sl;
typedef queue<ll> ql;
typedef priority_queue<ll> pql;

const int mod = 1e9+7;
const ll inf = 1ll<<55;
const ld pi = 3.1415926535897932384626433;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
const int ddy[8] = {1, -1, 1, 0, -1, 1, 0, -1};

ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }
ll LCM(ll a, ll b) { return (a*b)/GCD(a, b); }

#define MAX_VAL 200001

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

#ifdef DEBUG_MODE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int num[MAX_VAL];
	int n, one, two;

	cin >> n;
	for (int i=0; i<n; ++i) {
		scanf("%d", &num[i]);
		if (num[i] == 1)
			one++;
		else
			two++;
	}

	if (one == 0 || two == 0) {
		for (int i=0; i<n; ++i)
			cout << num[i] << " ";
	} else {
		one--;
		two--;
		cout << "2 1";
		for (int i=0; i<two; ++i)
			cout<< " 2";
		for (int i=0; i<one; ++i)
			cout << " 1";
	}

	return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define ll		long long
#define fi		first
#define se		second
#define pb		push_back
#define all(a)		(a).begin(), (a).end()
#define sz(a)		((int)(a.size()))
#define REP(i, n)	for (int i=0; i<(n); ++i)
#define FOR(i, a, b)	for (int i=(a); i<=(b); ++i)
#define FORR(i, a, b)	for (int i=(a); i>=(b); --i)

typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef tuple<int, int, int> ti3;
typedef tuple<int, int, int, int> ti4;
typedef stack<int> si;
typedef queue<int> qi;
typedef priority_queue<int> pqi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef tuple<ll, ll, ll> tl3;
typedef tuple<ll, ll, ll, ll> tl4;
typedef stack<ll> sl;
typedef queue<ll> ql;
typedef priority_queue<ll> pql;

const int mod = 1e9+7;
const ll inf = 1ll<<55;
const ld pi = 3.1415926535897932384626433;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
const int ddy[8] = {1, -1, 1, 0, -1, 1, 0, -1};

ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }
ll LCM(ll a, ll b) { return (a*b)/GCD(a, b); }

#define MAX_VAL 200001

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

#ifdef DEBUG_MODE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int num[MAX_VAL];
	int n, one, two;

	cin >> n;
	for (int i=0; i<n; ++i) {
		scanf("%d", &num[i]);
		if (num[i] == 1)
			one++;
		else
			two++;
	}

	if (one == 0 || two == 0) {
		for (int i=1; i<=n; ++i)
			cout << num[i] << " ";
	} else {
		one--;
		two--;
		cout << "2 1";
		for (int i=1; i<=two; ++i)
			cout<< " 2";
		for (int i=1; i<=one; ++i)
			cout << " 1";
	}

	return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define ll		long long
#define fi		first
#define se		second
#define pb		push_back
#define all(a)		(a).begin(), (a).end()
#define sz(a)		((int)(a.size()))
#define REP(i, n)	for (int i=0; i<(n); ++i)
#define FOR(i, a, b)	for (int i=(a); i<=(b); ++i)
#define FORR(i, a, b)	for (int i=(a); i>=(b); --i)

typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef tuple<int, int, int> ti3;
typedef tuple<int, int, int, int> ti4;
typedef stack<int> si;
typedef queue<int> qi;
typedef priority_queue<int> pqi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef tuple<ll, ll, ll> tl3;
typedef tuple<ll, ll, ll, ll> tl4;
typedef stack<ll> sl;
typedef queue<ll> ql;
typedef priority_queue<ll> pql;

const int mod = 1e9+7;
const ll inf = 1ll<<55;
const ld pi = 3.1415926535897932384626433;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
const int ddy[8] = {1, -1, 1, 0, -1, 1, 0, -1};

ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }
ll LCM(ll a, ll b) { return (a*b)/GCD(a, b); }

#define MAX_VAL 200001

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

#ifdef DEBUG_MODE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int num[MAX_VAL];
	int n, one = 0, two = 0;

	cin >> n;
	for (int i=0; i<n; ++i) {
		scanf("%d", &num[i]);
		if (num[i] == 1)
			one++;
		else
			two++;
	}

	if (one == 0 || two == 0) {
		for (int i=1; i<=n; ++i)
			cout << num[i] << " ";
	} else {
		one--;
		two--;
		cout << "2 1";
		for (int i=1; i<=two; ++i)
			cout<< " 2";
		for (int i=1; i<=one; ++i)
			cout << " 1";
	}

	return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define ll		long long
#define fi		first
#define se		second
#define pb		push_back
#define all(a)		(a).begin(), (a).end()
#define sz(a)		((int)(a.size()))
#define REP(i, n)	for (int i=0; i<(n); ++i)
#define FOR(i, a, b)	for (int i=(a); i<=(b); ++i)
#define FORR(i, a, b)	for (int i=(a); i>=(b); --i)

typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef tuple<int, int, int> ti3;
typedef tuple<int, int, int, int> ti4;
typedef stack<int> si;
typedef queue<int> qi;
typedef priority_queue<int> pqi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef tuple<ll, ll, ll> tl3;
typedef tuple<ll, ll, ll, ll> tl4;
typedef stack<ll> sl;
typedef queue<ll> ql;
typedef priority_queue<ll> pql;

const int mod = 1e9+7;
const ll inf = 1ll<<55;
const ld pi = 3.1415926535897932384626433;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
const int ddy[8] = {1, -1, 1, 0, -1, 1, 0, -1};

ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }
ll LCM(ll a, ll b) { return (a*b)/GCD(a, b); }

#define MAX_VAL 200001

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

#ifdef DEBUG_MODE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int num[MAX_VAL];
	int n, one = 0, two = 0;

	cin >> n;
	for (int i=0; i<n; ++i) {
		cin >> num[i];
		if (num[i] == 1)
			one++;
		else
			two++;
	}

	if (one == 0 || two == 0) {
		for (int i=0; i<n; ++i)
			cout << num[i] << " ";
	} else {
		one--;
		two--;
		cout << "2 1";
		for (int i=0; i<two; ++i)
			cout<< " 2";
		for (int i=0; i<one; ++i)
			cout << " 1";
	}

	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

const int N = 3e5 + 10;
const int INF = 1e9 + 7;

main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int _1, _2;
    _1 = _2 = 0;
    for (int i = 1, q;i <= n;++i)
    {
        cin >> q;
        _1 += (q == 1);
        _2 += (q == 2);
    }
    if (!_1 || !_2)
    {
        for (int i = 1;i < _1;++i)
            cout << 1 << " ";
        for (int j = 1;j < _2;++j)
            cout << 2 << " ";
    }
    if (_2 == n)
        for (int i = 1;i <= n;++i)
            cout << 2 << " ";
    if (_1 == n)
        for (int i = 1;i <= n;++i)
            cout << 1 << " ";
    else
    {
        cout << 2 << " " << 1 << " ";
        _2--;
        _1--;
        for (int i = 1;i <= _2;++i)
        {
            cout << 2 << " ";
        }
        for (int i = 1;i <= _1;++i)
        {
            cout << 1 << " ";
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

const int N = 3e5 + 10;
const int INF = 1e9 + 7;

main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int _1, _2;
    _1 = _2 = 0;
    for (int i = 1, q;i <= n;++i)
    {
        cin >> q;
        _1 += (q == 1);
        _2 += (q == 2);
    }
    if (!_1 || !_2)
    {
        for (int i = 1;i <= _1;++i)
            cout << 1 << " ";
        for (int j = 1;j <= _2;++j)
            cout << 2 << " ";
    }
    if (_2 == n)
        for (int i = 1;i <= n;++i)
            cout << 2 << " ";
    if (_1 == n)
        for (int i = 1;i <= n;++i)
            cout << 1 << " ";
    else
    {
        cout << 2 << " " << 1 << " ";
        _2--;
        _1--;
        for (int i = 1;i <= _2;++i)
        {
            cout << 2 << " ";
        }
        for (int i = 1;i <= _1;++i)
        {
            cout << 1 << " ";
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

const int N = 3e5 + 10;
const int INF = 1e9 + 7;

main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int _1, _2;
    _1 = _2 = 0;
    for (int i = 1, q;i <= n;++i)
    {
        cin >> q;
        _1 += (q == 1);
        _2 += (q == 2);
    }
    if (_2 == n)
        for (int i = 1;i <= n;++i)
            cout << 2 << " ";
    if (_1 == n)
        for (int i = 1;i <= n;++i)
            cout << 1 << " ";
    else
    {
        cout << 2 << " " << 1 << " ";
        _2--;
        _1--;
        for (int i = 1;i <= _2;++i)
        {
            cout << 2 << " ";
        }
        for (int i = 1;i <= _1;++i)
        {
            cout << 1 << " ";
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

const int N = 3e5 + 10;
const int INF = 1e9 + 7;

main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int _1, _2;
    _1 = _2 = 0;
    for (int i = 1, q;i <= n;++i)
    {
        cin >> q;
        _1 += (q == 1);
        _2 += (q == 2);
    }
    if (!_1 || !_2)
    {
        for (int i = 1;i <= _1;++i)
            cout << 1 << " ";
        for (int j = 1;j <= _2;++j)
            cout << 2 << " ";
        return 0;
    }
    if (_2 == n)
        for (int i = 1;i <= n;++i)
            cout << 2 << " ";
    if (_1 == n)
        for (int i = 1;i <= n;++i)
            cout << 1 << " ";
    else
    {
        cout << 2 << " " << 1 << " ";
        _2--;
        _1--;
        for (int i = 1;i <= _2;++i)
        {
            cout << 2 << " ";
        }
        for (int i = 1;i <= _1;++i)
        {
            cout << 1 << " ";
        }
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

const int N = 3e5 + 10;
const int INF = 1e9 + 7;

main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int _1, _2;
    _1 = _2 = 0;
    for (int i = 1, q;i <= n;++i)
    {
        cin >> q;
        _1 += (q == 1);
        _2 += (q == 2);
    }
    if (n == 1)
    {
        if (_1)
            cout << 1;
        else
            cout << 2;
        return 0;
    }
    if (_2 == n)
        for (int i = 1;i <= n;++i)
            cout << 2 << " ";
    if (_1 == n)
        for (int i = 1;i <= n;++i)
            cout << 1 << " ";
    else
    {
        cout << 2 << " " << 1 << " ";
        _2--;
        _1--;
        for (int i = 1;i <= _2;++i)
        {
            cout << 2 << " ";
        }
        for (int i = 1;i <= _1;++i)
        {
            cout << 1 << " ";
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

const int N = 1e5 + 10;

int cnt[2];
vector <int> prime;
map <int, int> dp[N], par[N];

bool is_prime(int n) {
	for (int i = 0; i < (int)prime.size() && prime[i] * prime[i] <= n; i++) {
		if(n % prime[i] == 0)
			return false;
	}
	return true;
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	int sum = 0;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		cnt[x - 1]++;
		sum += x;
	}
	for (int i = 2; i <= sum; i++) {
		if(is_prime(i))
			prime.push_back(i);
	}
	dp[0][0] = 0;
	for (int i = 0; i <= cnt[0]; i++) {
		for (int j = 0; j <= cnt[1]; j++) {
			if(i == 0 && j == 0)
				continue;
			if(i) {
				par[i][j] = 1;
				sum = (cnt[0] - i) + (cnt[1] - j) * 2;
				dp[i][j] = dp[i - 1][j] + binary_search(prime.begin(), prime.end(), sum + 1);
			}
			if(j) {
				sum = (cnt[0] - i) + (cnt[1] - j) * 2;
				ll temp = dp[i][j - 1] + binary_search(prime.begin(), prime.end(), sum + 2);
				if(!par[i][j])
					par[i][j] = 2, dp[i][j] = temp;
				if(temp > dp[i][j]) {
					dp[i][j] = temp;
					par[i][j] = 2;
				}
			}
		}
	}
	vector <int> ans;
	int x = cnt[0], y = cnt[1];
	while(par[x][y] != 0) {
		if(par[x][y] == 1) {
			ans.push_back(1);
			x--;
		}
		else {
			ans.push_back(2);
			y--;
		}
	}
//	reverse(ans.begin(), ans.end());
	for (auto u : ans)
		cout << u << " ";
	cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

const int N = 4e5 + 10;

int cnt[2];
vector <int> prime;
bitset <N> mark;

void prep(int limit) {
	for (int i = 2; i <= limit; i++) {
		if(!mark[i]) {
			prime.push_back(i);
			for (int j = 2 * i; j <= limit; j += i) {
				mark[j] = true;
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	int sum = 0;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		cnt[x - 1]++;
		sum += x;
	}
	int dp[cnt[0] + 1][cnt[1] + 1], par[cnt[0] + 1][cnt[1] + 1];
	memset(dp, 0, sizeof dp);
	memset(par, 0, sizeof par);
	prep(sum);
	for (int i = 0; i <= cnt[0]; i++) {
		for (int j = 0; j <= cnt[1]; j++) {
			if(i == 0 && j == 0)
				continue;
			sum = (cnt[0] - i) + (cnt[1] - j) * 2;
			ll t1 = -1, t2 = -1;
			if(i)
				t1 = dp[i - 1][j] + (mark[sum + 1] == false);
			sum = (cnt[0] - i) + (cnt[1] - j) * 2;
			if(j)
				t2 = dp[i][j - 1] + (mark[sum + 2] == false);
			if(t1 > t2) {
				dp[i][j] = t1;
				par[i][j] = 1;
			}
			else {
				dp[i][j] = t2;
				par[i][j] = 2;
			}
		}
	}
	int x = cnt[0], y = cnt[1];
	while(par[x][y] != 0) {
		if(par[x][y] == 1) {
			cout << 1 << " ";
			x--;
		}
		else {
			cout << 2 << " ";
			y--;
		}
	}
	cout << "\n";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

const int N = 4e5 + 10;

int cnt[2];
bitset <N> mark;

void prep(int limit) {
	for (int i = 2; i <= limit; i++) {
		if(!mark[i]) {
			for (int j = 2 * i; j <= limit; j += i) {
				mark[j] = true;
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	int sum = 0;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		cnt[x - 1]++;
		sum += x;
	}
	prep(sum);
	sum = 0;
	for (int i = 0; i < n; i++) {
		if(!cnt[0])
			sum += 2, cnt[1]--, cout << 2 << " ";
		else if(!cnt[1])
			sum++, cnt[0]--, cout << 1 << " ";
		else {
			if(mark[sum + 2] == false)
				sum += 2, cnt[1]--, cout << 2 << " ";
			else
				sum++, cnt[0]--, cout << 1 << " ";
		}
	}
	cout << "\n";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

const int N = 4e5 + 10;

int cnt[2];
bitset <N> mark;

void prep(int limit) {
	for (int i = 2; i <= limit; i++) {
		if(!mark[i]) {
			for (int j = 2 * i; j <= limit; j += i) {
				mark[j] = true;
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	int sum = 0;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		cnt[x - 1]++;
		sum += x;
	}
	prep(sum);
	sum = 0;
	for (int i = 0; i < n; i++) {
		if(!cnt[0])
			sum += 2, cnt[1]--, cout << 2 << " ";
		else if(!cnt[1])
			sum++, cnt[0]--, cout << 1 << " ";
		else {
			if(mark[sum + 1] == false)
				sum++, cnt[0]--, cout << 1 << " ";
			else
				sum += 2, cnt[1]--, cout << 2 << " ";
		}
	}
	cout << "\n";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

const int N = 4e5 + 10;

int cnt[2];
bitset <N> mark;

void prep(int limit) {
	for (int i = 2; i <= limit; i++) {
		if(!mark[i]) {
			for (int j = 2 * i; j <= limit; j += i) {
				mark[j] = true;
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	int sum = 0;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		cnt[x - 1]++;
		sum += x;
	}
	int dp[cnt[0] + 1][cnt[1] + 1], par[cnt[0] + 1][cnt[1] + 1];
	memset(dp, 0, sizeof dp);
	memset(par, 0, sizeof par);
	prep(sum);
	for (int i = 0; i <= cnt[0]; i++) {
		for (int j = 0; j <= cnt[1]; j++) {
			if(i == 0 && j == 0)
				continue;
			sum = (cnt[0] - i) + (cnt[1] - j) * 2;
			ll t1 = -1, t2 = -1;
			if(i)
				t1 = dp[i - 1][j] + (mark[sum + 1] == false);
			sum = (cnt[0] - i) + (cnt[1] - j) * 2;
			if(j)
				t2 = dp[i][j - 1] + (mark[sum + 2] == false);
			if(t1 > t2) {
				dp[i][j] = t1;
				par[i][j] = 1;
			}
			else {
				dp[i][j] = t2;
				par[i][j] = 2;
			}
		}
	}
	int x = cnt[0], y = cnt[1];
	while(par[x][y] != 0) {
		if(par[x][y] == 1) {
			cout << 1 << " ";
			x--;
		}
		else {
			cout << 2 << " ";
			y--;
		}
	}
	cout << "\n";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

const int N = 4e5 + 10;

int cnt[2];
bitset <N> mark;

void prep(int limit) {
	mark[1] = true;
	for (int i = 2; i <= limit; i++) {
		if(!mark[i]) {
			for (int j = 2 * i; j <= limit; j += i) {
				mark[j] = true;
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	int sum = 0;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		cnt[x - 1]++;
		sum += x;
	}
	prep(sum);
	sum = 0;
	for (int i = 0; i < n; i++) {
		if(!cnt[0])
			sum += 2, cnt[1]--, cout << 2 << " ";
		else if(!cnt[1])
			sum++, cnt[0]--, cout << 1 << " ";
		else {
			if(mark[sum + 1] == true)
				sum += 2, cnt[1]--, cout << 2 << " ";
			else
				sum++, cnt[0]--, cout << 1 << " ";
		}
	}
	cout << "\n";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;

const int N = 4e5 + 10;

int cnt[2];
bitset <N> mark;

void prep(int limit) {
	for (int i = 2; i <= limit; i++) {
		if(!mark[i]) {
			for (int j = 2 * i; j <= limit; j += i) {
				mark[j] = true;
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	int sum = 0;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		cnt[x - 1]++;
		sum += x;
	}
	map <int, int> dp[N], par[N];
	prep(sum);
	for (int i = 0; i <= cnt[0]; i++) {
		for (int j = 0; j <= cnt[1]; j++) {
			if(i == 0 && j == 0)
				continue;
			sum = (cnt[0] - i) + (cnt[1] - j) * 2;
			ll t1 = -1, t2 = -1;
			if(i)
				t1 = dp[i - 1][j] + (mark[sum + 1] == false);
			sum = (cnt[0] - i) + (cnt[1] - j) * 2;
			if(j)
				t2 = dp[i][j - 1] + (mark[sum + 2] == false);
			if(t1 > t2) {
				dp[i][j] = t1;
				par[i][j] = 1;
			}
			else {
				dp[i][j] = t2;
				par[i][j] = 2;
			}
		}
	}
	int x = cnt[0], y = cnt[1];
	while(par[x][y] != 0) {
		if(par[x][y] == 1) {
			cout << 1 << " ";
			x--;
		}
		else {
			cout << 2 << " ";
			y--;
		}
	}
	cout << "\n";
}


/***
	chauhan176's code
***/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define fi first
#define se second
#define maxn 300005
#define stringio cin.ignore(256, '\n')  //to take string input after taking integer inputs
#define go_t int t;cin>>t;while(t--)
#define go_pt int t;cin>>t;for(int tt=1;tt<=t;tt++)
#define set_ar(arr,v) memset(arr,v,sizeof(arr))

typedef pair<int,int>pii[maxn];
int coun[3];
int num[maxn];
vector<int> prim;
void SieveOfEratosthenes(int n)
{
	bool prime[n+1];
	memset(prime, true, sizeof(prime));

	for (int p=2; p*p<=n; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=n; i += p)
				prime[i] = false;
		}
	}
    int k=1;
	for (int p=2; p<=n; p++)
	if (prime[p])
		prim.pb(p);
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    //freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    //freopen("output1.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);

    int n;
    cin>>n;
    set_ar(coun,0);SieveOfEratosthenes(2*n+10);
    ll minn=10e9 + 10;
    for(int i=1;i<=n;i++)
    {
        cin>>num[i];
        coun[num[i]]++;
    }
    ll last=0;
    for(int p : prim)
    {
        while(p-last>=2)
        {
            if(coun[2]==0)
                break;
            else
            {
                cout<<2<<" ";
                last+=2;
                p-=2;
                coun[2]-=1;
            }
        }
        while(p-last>=1)
        {
            if(coun[1]==0)
                break;
            else{
                cout<<1<<" ";
                last+=1;
                p-=1;
                coun[1]-=1;
            }
        }
        if(coun[1]==0 && coun[2]==0)
            break;
    }
    return 0;
}


/***
	chauhan176's code
***/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define fi first
#define se second
#define maxn 300005
#define stringio cin.ignore(256, '\n')  //to take string input after taking integer inputs
#define go_t int t;cin>>t;while(t--)
#define go_pt int t;cin>>t;for(int tt=1;tt<=t;tt++)
#define set_ar(arr,v) memset(arr,v,sizeof(arr))

typedef pair<int,int>pii[maxn];
int coun[3];
int num[maxn];
vector<int> prim;
void SieveOfEratosthenes(int n)
{
	bool prime[n+1];
	memset(prime, true, sizeof(prime));

	for (int p=2; p*p<=n; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=n; i += p)
				prime[i] = false;
		}
	}
    int k=1;
	for (int p=2; p<=n; p++)
	if (prime[p])
		prim.pb(p);
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    //freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    //freopen("output1.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);

    int n;
    cin>>n;
    set_ar(coun,0);SieveOfEratosthenes(maxn);
    for(int i=1;i<=n;i++)
    {
        cin>>num[i];
        coun[num[i]]++;
    }
    int last=0;
    for(int p : prim)
    {
        while(p-last>=2)
        {
            if(coun[2]==0)
                break;
            else
            {
                cout<<2<<" ";
                last+=2;
                p-=2;
                coun[2]-=1;
            }
        }
        while(p-last>=1)
        {
            if(coun[1]==0)
                break;
            else{
                cout<<1<<" ";
                last+=1;
                p-=1;
                coun[1]-=1;
            }
        }
        if(coun[1]==0 && coun[2]!=0)
        {
            while(coun[2]>0)
            {
                cout<<2<<" ";
                coun[2]-=1;
                last+=2;
            }
        }
        if(coun[1]!=0 && coun[2]==0)
        {
            while(coun[1]>0)
            {
                cout<<1<<" ";
                coun[1]-=1;
                last+=1;
            }
        }
        if(coun[1]==0 && coun[2]==0)
            break;
    }
    return 0;
}


/***
	chauhan176's code
***/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define fi first
#define se second
#define maxn 400005
#define stringio cin.ignore(256, '\n')  //to take string input after taking integer inputs
#define go_t int t;cin>>t;while(t--)
#define go_pt int t;cin>>t;for(int tt=1;tt<=t;tt++)
#define set_ar(arr,v) memset(arr,v,sizeof(arr))

typedef pair<int,int>pii[maxn];
int coun[3];
int num[maxn];
vector<int> prim;
void SieveOfEratosthenes(int n)
{
	bool prime[n+1];
	memset(prime, true, sizeof(prime));

	for (int p=2; p*p<=n; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=n; i += p)
				prime[i] = false;
		}
	}
    int k=1;
	for (int p=2; p<=n; p++)
	if (prime[p])
		prim.pb(p);
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    //freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    //freopen("output1.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);

    int n;
    cin>>n;
    set_ar(coun,0);SieveOfEratosthenes(maxn);
    for(int i=1;i<=n;i++)
    {
        cin>>num[i];
        coun[num[i]]++;
    }
    int last=0;
    for(int p : prim)
    {
        while(p-last>=2 && coun[2]>0)
        {
                cout<<2<<" ";
                last+=2;
                coun[2]-=1;
        }
        while(p-last==1 && coun[1]>0)
        {
                cout<<1<<" ";
                last+=1;
                coun[1]-=1;
        }
        if(coun[1]==0 && coun[2]!=0)
        {
            while(coun[2]>0)
            {
                cout<<2<<" ";
                coun[2]-=1;
                last+=2;
            }
            break;
        }
        if(coun[1]!=0 && coun[2]==0)
        {
            while(coun[1]>0)
            {
                cout<<1<<" ";
                coun[1]-=1;
                last+=1;
            }
            break;
        }
        if(coun[1]==0 && coun[2]==0)
            break;
    }
    return 0;
}


/***
	chauhan176's code
***/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define fi first
#define se second
#define maxn 400005
#define stringio cin.ignore(256, '\n')  //to take string input after taking integer inputs
#define go_t int t;cin>>t;while(t--)
#define go_pt int t;cin>>t;for(int tt=1;tt<=t;tt++)
#define set_ar(arr,v) memset(arr,v,sizeof(arr))

typedef pair<int,int>pii[maxn];
int coun[3];
int num[maxn];
vector<int> prim;
void SieveOfEratosthenes(int n)
{
	bool prime[n+1];
	memset(prime, true, sizeof(prime));

	for (int p=2; p*p<=n; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=n; i += p)
				prime[i] = false;
		}
	}
    int k=1;
	for (int p=2; p<=n; p++)
	if (prime[p])
		prim.pb(p);
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    //freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    //freopen("output1.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);

    int n;
    cin>>n;
    set_ar(coun,0);SieveOfEratosthenes(maxn);
    for(int i=1;i<=n;i++)
    {
        cin>>num[i];
        coun[num[i]]++;
    }
    int last=0;
    for(int p : prim)
    {
        while(p-last>=2)
        {
            if(coun[2]==0)
                break;
            else
            {
                cout<<2<<" ";
                last+=2;
                coun[2]-=1;
            }
        }
        while(p-last>=1)
        {
            if(coun[1]==0)
                break;
            else{
                cout<<1<<" ";
                last+=1;
                coun[1]-=1;
            }
        }
        if(coun[1]==0 && coun[2]!=0)
        {
            while(coun[2]>0)
            {
                cout<<2<<" ";
                coun[2]-=1;
                last+=2;
            }
            break;
        }
        if(coun[1]!=0 && coun[2]==0)
        {
            while(coun[1]>0)
            {
                cout<<1<<" ";
                coun[1]-=1;
                last+=1;
            }
            break;
        }
        if(coun[1]==0 && coun[2]==0)
            break;
    }
    return 0;
}


/***
	chauhan176's code
***/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define fi first
#define se second
#define maxn 400005
#define stringio cin.ignore(256, '\n')  //to take string input after taking integer inputs
#define go_t int t;cin>>t;while(t--)
#define go_pt int t;cin>>t;for(int tt=1;tt<=t;tt++)
#define set_ar(arr,v) memset(arr,v,sizeof(arr))

typedef pair<int,int>pii[maxn];
int coun[3];
int num[maxn];
vector<int> prim;
void SieveOfEratosthenes(int n)
{
	bool prime[n+1];
	memset(prime, true, sizeof(prime));

	for (int p=2; p*p<=n; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=n; i += p)
				prime[i] = false;
		}
	}
    int k=1;
	for (int p=2; p<=n; p++)
	if (prime[p])
		prim.pb(p);
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    //freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    //freopen("output1.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);

    int n;
    cin>>n;
    set_ar(coun,0);SieveOfEratosthenes(maxn);
    for(int i=1;i<=n;i++)
    {
        cin>>num[i];
        coun[num[i]]++;
    }
    int last=0;
    for(int p : prim)
    {
        while(p-last>=2)
        {
            if(coun[2]==0)
                break;
            else
            {
                cout<<2<<" ";
                last+=2;
                p-=2;
                coun[2]-=1;
            }
        }
        while(p-last>=1)
        {
            if(coun[1]==0)
                break;
            else{
                cout<<1<<" ";
                last+=1;
                p-=1;
                coun[1]-=1;
            }
        }
        if(coun[1]==0 && coun[2]!=0)
        {
            while(coun[2]>0)
            {
                cout<<2<<" ";
                coun[2]-=1;
                last+=2;
            }
        }
        if(coun[1]!=0 && coun[2]==0)
        {
            while(coun[1]>0)
            {
                cout<<1<<" ";
                coun[1]-=1;
                last+=1;
            }
        }
        if(coun[1]==0 && coun[2]==0)
            break;
    }
    return 0;
}


/***
	chauhan176's code
***/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define fi first
#define se second
#define maxn 300005
#define stringio cin.ignore(256, '\n')  //to take string input after taking integer inputs
#define go_t int t;cin>>t;while(t--)
#define go_pt int t;cin>>t;for(int tt=1;tt<=t;tt++)
#define set_ar(arr,v) memset(arr,v,sizeof(arr))

typedef pair<int,int>pii[maxn];
int coun[3];
int num[maxn];
vector<int> prim;
void SieveOfEratosthenes(int n)
{
	bool prime[n+1];
	memset(prime, true, sizeof(prime));

	for (int p=2; p*p<=n; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=n; i += p)
				prime[i] = false;
		}
	}
    int k=1;
	for (int p=2; p<=n; p++)
	if (prime[p])
		prim.pb(p);
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    //freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    //freopen("output1.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);

    int n;
    cin>>n;
    set_ar(coun,0);SieveOfEratosthenes(maxn);
    for(int i=1;i<=n;i++)
    {
        cin>>num[i];
        coun[num[i]]++;
    }
    int last=0;
    for(int p : prim)
    {
        while(p-last>=2)
        {
            if(coun[2]==0)
                break;
            else
            {
                cout<<2<<" ";
                last+=2;
                p-=2;
                coun[2]-=1;
            }
        }
        while(p-last>=1)
        {
            if(coun[1]==0)
                break;
            else{
                cout<<1<<" ";
                last+=1;
                p-=1;
                coun[1]-=1;
            }
        }
        if(coun[1]==0 && coun[2]!=0)
        {
            while(coun[2]>0)
            {
                cout<<2<<" ";
                coun[2]-=1;
                last+=2;
            }
        }
        if(coun[1]==0 && coun[2]==0)
            break;
    }
    return 0;
}


/***
	chauhan176's code
***/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long int
#define fi first
#define se second
#define maxn 400005
#define stringio cin.ignore(256, '\n')  //to take string input after taking integer inputs
#define go_t int t;cin>>t;while(t--)
#define go_pt int t;cin>>t;for(int tt=1;tt<=t;tt++)
#define set_ar(arr,v) memset(arr,v,sizeof(arr))

typedef pair<int,int>pii[maxn];
int coun[3];
int num[maxn];
vector<int> prim;
void SieveOfEratosthenes(int n)
{
	bool prime[n+1];
	memset(prime, true, sizeof(prime));

	for (int p=2; p*p<=n; p++)
	{
		if (prime[p] == true)
		{
			for (int i=p*p; i<=n; i += p)
				prime[i] = false;
		}
	}
    int k=1;
	for (int p=2; p<=n; p++)
	if (prime[p])
		prim.pb(p);
}
int main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    //freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    //freopen("output1.txt", "w", stdout);
    #endif

    ios_base::sync_with_stdio(false);

    int n;
    cin>>n;
    set_ar(coun,0);
    for(int i=1;i<=n;i++)
    {
        cin>>num[i];
        coun[num[i]]++;
    }
    if(n==1)
        cout<<num[1]<<endl;
    else
    {
        if(coun[1]==0 || coun[2]==0)
        {
            for(int i=1;i<=n;i++)
                cout<<num[i]<<" ";
        }
        else
        {
            cout<<2<<" "<<1<<" ";
            coun[2]-=1;coun[1]-=1;
            while(coun[2]-->0)
                cout<<2<<" ";
            while(coun[1]-->0)
                cout<<1<<" ";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; ++i){
        cin >> vec[i];
    }
    sort(vec.begin(), vec.end());
    for (auto v: vec){
        cout << v << " ";
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; ++i){
        cin >> vec[i];
    }
    //
    sort(vec.rbegin(), vec.rend());
    int j;
    for (int i = 0; i < n; ++i){
        if (vec[i] == 1){
            j = i;
            break;
        }
    }
    int pos = j;
    for (int i = 1; i < pos &&j < n; i+=2){
        vec[i] = 1;
        vec[j] = 2;
        ++j;
    }
    for (auto v: vec){
        cout << v << " ";
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; ++i){
        cin >> vec[i];
    }
    //
    sort(vec.rbegin(), vec.rend());
    if (vec.size() > 1){
        vec[1] = vec[n-1];
    }
    for (int i = 2; i < n; ++i){
        if (vec[i] == 1){
            vec[i] = 2;
            break;
        }
    }

    for (auto v: vec){
        cout << v << " ";
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; ++i){
        cin >> vec[i];
    }
    //
    sort(vec.rbegin(), vec.rend());
    int x = vec[1];
    if (vec.size() > 1){
        vec[1] = vec[n-1];
    }
    for (int i = 2; i < n; ++i){
        if (vec[i] == 1){
            vec[i] = x;
            break;
        }
    }

    for (auto v: vec){
        cout << v << " ";
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; ++i){
        cin >> vec[i];
    }
    //
    sort(vec.begin(), vec.end());
    for (auto v: vec){
        cout << v << " ";
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; ++i){
        cin >> vec[i];
    }
    //
    sort(vec.begin(), vec.end());
    if (vec.size() < 3){
        int t = vec[0];
        vec[0] = vec[n-1];
        vec[n-1] = t;
    }
    for (auto v: vec){
        cout << v << " ";
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; ++i){
        cin >> vec[i];
    }
    //
    sort(vec.rbegin(), vec.rend());
    if (vec.size() > 1){
        int t = vec[1];
        vec[1] = vec[n-1];
        vec[n-1] = t;
    }
    for (auto v: vec){
        cout << v << " ";
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; ++i){
        cin >> vec[i];
    }
    //
    sort(vec.begin(), vec.end());
    int t = vec[0];
    vec[0] = vec[n-1];
    vec[n-1] = t;
    for (auto v: vec){
        cout << v << " ";
    }
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> vec(n);
    for (int i = 0; i < n; ++i){
        cin >> vec[i];
    }
    //
    sort(vec.begin(), vec.end());
    for (auto v: vec){
        cout << v << " ";
    }
    int t = vec[0];
    vec[0] = vec[n-1];
    vec[n-1] = t;
    cout << endl;
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,b,e) for(LL i=(b);i<(e);++i)
#define dep(i,b,e) for(LL i=(b);i>=(e);--i)
#define LL long long
#define L long

using namespace std;

int n,cnt[3]={0,0,0};
int a[200001],idx=1;

namespace Ela{
//欧拉筛
    #define Ela_N 400000//上限，注意修改
    int mindivisor[Ela_N+1],prime[Ela_N+1];
    int &prime_count=prime[0];
    void Euler_prime(int N){
        memset(prime,0,sizeof(prime));
        memset(mindivisor,0,sizeof(mindivisor));
        rep(i,2,N+1){
            if(!mindivisor[i]){
                mindivisor[i]=i;prime[++prime_count]=i;
            }
            rep(j,1,prime_count+1){
                auto t=i*prime[j];
                if(t<=N&&!mindivisor[t]){
                    mindivisor[t]=prime[j];
                }
                if(i%prime[j]==0) break;
            }
        }
    }
}
using namespace Ela;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;int t;
    if(n==1){
        cin>>n;cout<<n<<endl;return 0;
    }
    rep(i,0,n){
        cin>>t;cnt[t]++;cnt[0]+=t;
    }
    Euler_prime(cnt[0]);cnt[0]=0;
    rep(i,1,prime_count+1){
        auto &p=prime[i];t=0;
        rep(j,0,min(cnt[2],(p-cnt[0])/2)){a[idx++]=2;t+=2;}
        cnt[2]-=t/2;cnt[0]+=t;
        rep(j,0,p-cnt[0]){a[idx++]=1;cnt[1]--;}
    }
    rep(i,1,n+1) cout<<a[i]<<' ';
    cout<<endl;
    //system("pause");
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,b,e) for(LL i=(b);i<(e);++i)
#define dep(i,b,e) for(LL i=(b);i>=(e);--i)
#define LL long long
#define L long

using namespace std;

int n,cnt[3]={0,0,0};
int a[200001],idx=1;

namespace Ela{
//欧拉筛
    #define Ela_N 400000//上限，注意修改
    int mindivisor[Ela_N+1],prime[Ela_N+1];
    int &prime_count=prime[0];
    void Euler_prime(int N){
        memset(prime,0,sizeof(prime));
        memset(mindivisor,0,sizeof(mindivisor));
        rep(i,2,N+1){
            if(!mindivisor[i]){
                mindivisor[i]=i;prime[++prime_count]=i;
            }
            rep(j,1,prime_count+1){
                auto t=i*prime[j];
                if(t<=N&&!mindivisor[t]){
                    mindivisor[t]=prime[j];
                }
                if(i%prime[j]==0) break;
            }
        }
    }
}
using namespace Ela;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;int t;
    rep(i,0,n){
        cin>>t;cnt[t]++;cnt[0]+=t;
    }
    Euler_prime(cnt[0]);cnt[0]=0;
    rep(i,1,prime_count+1){
        auto &p=prime[i];t=0;
        rep(j,0,min(cnt[2],(p-cnt[0])/2)){a[idx++]=2;t+=2;}
        cnt[2]-=t/2;cnt[0]+=t;
        rep(j,0,p-cnt[0]){a[idx++]=1;cnt[1]--;}
    }
    rep(i,1,n+1) cout<<a[i]<<' ';
    cout<<endl;
    //system("pause");
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,b,e) for(LL i=(b);i<(e);++i)
#define dep(i,b,e) for(LL i=(b);i>=(e);--i)
#define LL long long
#define L long

using namespace std;

int n,cnt[3]={0,0,0};
int a[200001],idx=1;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;int t;
    if(n==1){
        cin>>n;cout<<n<<endl;return 0;
    }
    rep(i,0,n){
        cin>>t;cnt[t]++;cnt[0]+=t;
    }
    if(cnt[2]){a[idx++]=2;cnt[2]--;}
    if(cnt[1]){a[idx++]=1;cnt[1]--;}
    rep(i,0,cnt[2]) a[idx++]=2;
    rep(i,0,cnt[1]) a[idx++]=1;
    rep(i,1,n+1) cout<<a[i]<<' ';
    cout<<endl;
    //system("pause");
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,b,e) for(LL i=(b);i<(e);++i)
#define dep(i,b,e) for(LL i=(b);i>=(e);--i)
#define LL long long
#define L long

using namespace std;

int n,cnt[3]={0,0,0};
int a[200001],idx=1;

namespace Ela{
//欧拉筛
    #define Ela_N 400000//上限，注意修改
    int mindivisor[Ela_N+1],prime[Ela_N+1];
    int &prime_count=prime[0];
    void Euler_prime(int N){
        memset(prime,0,sizeof(prime));
        memset(mindivisor,0,sizeof(mindivisor));
        rep(i,2,N+1){
            if(!mindivisor[i]){
                mindivisor[i]=i;prime[++prime_count]=i;
            }
            rep(j,1,prime_count+1){
                auto t=i*prime[j];
                if(t<=N&&!mindivisor[t]){
                    mindivisor[t]=prime[j];
                }
                if(i%prime[j]==0) break;
            }
        }
    }
}
using namespace Ela;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;int t;
    if(n==1){
        cin>>n;cout<<n<<endl;return 0;
    }
    rep(i,0,n){
        cin>>t;cnt[t]++;cnt[0]+=t;
    }
    Euler_prime(cnt[0]);cnt[0]=0;
    rep(i,1,prime_count+1){
        auto &p=prime[i];t=0;
        rep(j,0,min(cnt[2],(p-cnt[0])/2)){a[idx++]=2;t+=2;}
        cnt[2]-=t/2;cnt[0]+=t;
        rep(j,0,p-cnt[0]){a[idx++]=1;t++;}
        cnt[0]+=t;
    }
    rep(i,1,n+1) cout<<a[i]<<' ';
    cout<<endl;
    //system("pause");
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,b,e) for(LL i=(b);i<(e);++i)
#define dep(i,b,e) for(LL i=(b);i>=(e);--i)
#define LL long long
#define L long

using namespace std;

int n,cnt[3]={0,0,0};
int a[200001],idx=1;

namespace Ela{
//欧拉筛
    #define Ela_N 400000//上限，注意修改
    int mindivisor[Ela_N+1],prime[Ela_N+1];
    int &prime_count=prime[0];
    void Euler_prime(int N){
        memset(prime,0,sizeof(prime));
        memset(mindivisor,0,sizeof(mindivisor));
        rep(i,2,N+1){
            if(!mindivisor[i]){
                mindivisor[i]=i;prime[++prime_count]=i;
            }
            rep(j,1,prime_count+1){
                auto t=i*prime[j];
                if(t<=N&&!mindivisor[t]){
                    mindivisor[t]=prime[j];
                }
                if(i%prime[j]==0) break;
            }
        }
    }
}
using namespace Ela;

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;int t;
    if(n==1){
        cin>>n;cout<<n<<endl;return 0;
    }
    rep(i,0,n){
        cin>>t;cnt[t]++;cnt[0]+=t;
    }
    Euler_prime(cnt[0]);cnt[0]=0;
    rep(i,1,prime_count+1){
        auto &p=prime[i];t=0;
        rep(j,0,min(cnt[2],(p-cnt[0])/2)){a[idx++]=2;t+=2;}
        cnt[2]-=t/2;cnt[0]+=t;t=0;
        rep(j,0,p-cnt[0]){a[idx++]=1;t++;}
        cnt[0]+=t;
    }
    rep(i,1,n+1) cout<<a[i]<<' ';
    cout<<endl;
    //system("pause");
    return 0;
}


/**
 *    Author  : Ador
 *    Created : 19.08.2020
**/
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <cctype>
#define pi acos(-1.0)
#define L 31250000
#define SP(x) cout << fixed << setprecision(x)
//#define FILE_IO
typedef long long int ll ;
typedef unsigned long long int ull ;
using namespace std ;
ll prime[125000] ;
int main(void)
{
    ios_base::sync_with_stdio(false) ; cin.tie(NULL) ; cout.tie(NULL) ;
    #ifdef FILE_IO
    freopen("input.txt" , "r" , stdin) ;
    freopen("output.txt" , "w" , stdout) ;
    #endif
    prime[0] |= (1 << 0) ; prime[1] |= (1 << 1) ;
    for(ll i = 4 ; i <= 125000 ; i += 2) prime[i >> 5] |= (1 << (i & 31)) ;
    for(ll i = 3 ; i * i <= 125000 ; i += 2)
        if(!(prime[i >> 5] & (1 << (i & 31))))
            for(ll j = i * i ; j <= 125000 ; j += 2 * i) prime[j >> 5] |= (1 << (j & 31)) ;
    ll n ; cin >> n ;
    ll ar[n] ;
    ar[0] = 1 ;
    ll x , i , one = 0 , two = 0 ;
    for(i = 0 ; i < n ; ++i)
    {
        cin >> x ;
        ar[0] = max(ar[0] , x) ;
        if(x == 1) ++one ;
        else ++two ;
    }
    ll sum = ar[0] ;
    if(ar[0] == 1) --one ;
    else --two ;
    for(i = 1 ; i < n ; ++i)
    {
        if(!(prime[(sum + 1) >> 5] & (1 << ((sum + 1) & 31))) and one)
        {
            ++sum ;
            --one ;
            ar[i] = 1 ;
        }
        else if(!(prime[(sum + 2) >> 5] & (1 << ((sum + 2) & 31))) and two)
        {
            sum += 2 ;
            --two ;
            ar[i] = 2 ;
        }
        else
        {
            if(one >= two)
            {
                ++sum ;
                --one ;
                ar[i] = 1 ;
            }
            else
            {
                sum += 2 ;
                --two ;
                ar[i] = 2 ;
            }
        }
    }
    for(i = 0 ; i < n ; ++i) cout << ar[i] << " " ;
    cout << "\n" ;
    return 0 ;
}


/**
 *    Author  : Ador
 *    Created : 19.08.2020
**/
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <cctype>
#define pi acos(-1.0)
#define L 31250000
#define SP(x) cout << fixed << setprecision(x)
//#define FILE_IO
typedef long long int ll ;
typedef unsigned long long int ull ;
using namespace std ;
ll prime[1250001] ;
int main(void)
{
    ios_base::sync_with_stdio(false) ; cin.tie(NULL) ; cout.tie(NULL) ;
    #ifdef FILE_IO
    freopen("input.txt" , "r" , stdin) ;
    freopen("output.txt" , "w" , stdout) ;
    #endif
    prime[0] |= (1 << 0) ; prime[0] |= (1 << 1) ;
    for(ll i = 4 ; i <= 1250000 ; i += 2) prime[i >> 5] |= (1 << (i & 31)) ;
    for(ll i = 3 ; i * i <= 1250000 ; i += 2)
        if(!(prime[i >> 5] & (1 << (i & 31))))
            for(ll j = i * i ; j <= 1250000 ; j += (2 * i)) prime[j >> 5] |= (1 << (j & 31)) ;
    ll n ; cin >> n ;
    ll ar[n] ;
    ar[0] = 1 ;
    ll x , i , one = 0 , two = 0 ;
    for(i = 0 ; i < n ; ++i)
    {
        cin >> x ;
        ar[0] = max(ar[0] , x) ;
        if(x == 1) ++one ;
        else ++two ;
    }
    ll sum = ar[0] ;
    if(ar[0] == 1) --one ;
    else --two ;
    for(i = 1 ; i < n ; ++i)
    {
        if(!(prime[(sum + 1) >> 5] & (1 << ((sum + 1) & 31))) and one)
        {
            ++sum ;
            --one ;
            ar[i] = 1 ;
        }
        else if(!(prime[(sum + 2) >> 5] & (1 << ((sum + 2) & 31))) and two)
        {
            sum += 2 ;
            --two ;
            ar[i] = 2 ;
        }
        else
        {
            if(one >= two)
            {
                ++sum ;
                --one ;
                ar[i] = 1 ;
            }
            else
            {
                sum += 2 ;
                --two ;
                ar[i] = 2 ;
            }
        }
    }
    for(i = 0 ; i < n ; ++i) cout << ar[i] << " " ;
    cout << "\n" ;
    return 0 ;
}


/**
 *    Author  : Ador
 *    Created : 12.08.2020
**/
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <cctype>
#define pi acos(-1.0)
#define L 10000050
typedef long long int ll ;
typedef unsigned long long int ull ;
using namespace std ;
ll prime[L] ;
int main(void)
{
    ios_base::sync_with_stdio(false) ; cin.tie(NULL) ; cout.tie(NULL) ;
    //freopen("input.txt" , "r" , stdin) ;
    //freopen("output.txt" , "w" , stdout) ;
    prime[0] = prime[1] = 1 ;
    for(ll i = 4 ; i <= L ; i += 2) prime[i] = 1 ;
    for(ll i = 3 ; i * i <= L ; i += 2)
        if(!prime[i])
            for(ll j = i * i ; j <= L ; j += 2 * i) prime[j] = 1 ;
    ll n ; cin >> n ;
    ll ar[n] ;
    ar[0] = 2 ;
    ll x , i , one = 0 , two = 0 , sum = 1 ;
    for(i = 0 ; i < n ; ++i)
    {
        cin >> x ;
        ar[0] = min(ar[0] , x) ;
        if(x == 1) ++one ;
        else ++two ;
    }
    if(ar[0] == 1) --one ;
    else if(ar[0] == 2) --two ;
    for(i = 1 ; i < n ; ++i)
    {
        if(!prime[sum + 1] and one)
        {
            ++sum ;
            --one ;
            ar[i] = 1 ;
        }
        else if(!prime[sum + 2] and two)
        {
            sum += 2 ;
            --two ;
            ar[i] = 2 ;
        }
        else
        {
            if(one >= two)
            {
                ++sum ;
                --one ;
                ar[i] = 1 ;
            }
            else
            {
                sum += 2 ;
                --two ;
                ar[i] = 2 ;
            }
        }
    }
    for(i = 0 ; i < n ; ++i) cout << ar[i] << " " ;
    cout << "\n" ;
    return 0 ;
}


/**
 *    Author  : Ador
 *    Created : 19.08.2020
**/
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <cctype>
#define pi acos(-1.0)
#define L 31250000
#define SP(x) cout << fixed << setprecision(x)
//#define FILE_IO
typedef long long int ll ;
typedef unsigned long long int ull ;
using namespace std ;
ll prime[400001] ;
int main(void)
{
    ios_base::sync_with_stdio(false) ; cin.tie(NULL) ; cout.tie(NULL) ;
    #ifdef FILE_IO
    freopen("input.txt" , "r" , stdin) ;
    freopen("output.txt" , "w" , stdout) ;
    #endif
    prime[0] |= (1 << 0) ; prime[0] |= (1 << 1) ;
    for(ll i = 4 ; i <= 400000 ; i += 2) prime[i >> 5] |= (1 << (i & 31)) ;
    for(ll i = 3 ; i * i <= 400000 ; i += 2)
        if(!(prime[i >> 5] & (1 << (i & 31))))
            for(ll j = i * i ; j <= 400000 ; j += (2 * i)) prime[j >> 5] |= (1 << (j & 31)) ;
    ll n ; cin >> n ;
    ll ar[n] ;
    ar[0] = 1 ;
    ll x , i , one = 0 , two = 0 ;
    for(i = 0 ; i < n ; ++i)
    {
        cin >> x ;
        ar[0] = max(ar[0] , x) ;
        if(x == 1) ++one ;
        else ++two ;
    }
    ll sum = ar[0] ;
    if(ar[0] == 1) --one ;
    else --two ;
    for(i = 1 ; i < n ; ++i)
    {
        if(!(prime[(sum + 1) >> 5] & (1 << ((sum + 1) & 31))) and one)
        {
            ++sum ;
            --one ;
            ar[i] = 1 ;
        }
        else if(!(prime[(sum + 2) >> 5] & (1 << ((sum + 2) & 31))) and two)
        {
            sum += 2 ;
            --two ;
            ar[i] = 2 ;
        }
        else
        {
            if(one >= two)
            {
                ++sum ;
                --one ;
                ar[i] = 1 ;
            }
            else
            {
                sum += 2 ;
                --two ;
                ar[i] = 2 ;
            }
        }
    }
    for(i = 0 ; i < n ; ++i) cout << ar[i] << " " ;
    cout << "\n" ;
    return 0 ;
}


/**
 *    Author  : Ador
 *    Created : 12.08.2020
**/
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <cctype>
#define pi acos(-1.0)
#define L 10000050
typedef long long int ll ;
typedef unsigned long long int ull ;
using namespace std ;
ll prime[L] ;
int main(void)
{
    ios_base::sync_with_stdio(false) ; cin.tie(NULL) ; cout.tie(NULL) ;
    //freopen("input.txt" , "r" , stdin) ;
    //freopen("output.txt" , "w" , stdout) ;
    prime[0] = prime[1] = 1 ;
    for(ll i = 4 ; i <= L ; i += 2) prime[i] = 1 ;
    for(ll i = 3 ; i * i <= L ; i += 2)
        if(!prime[i])
            for(ll j = i * i ; j <= L ; j += 2 * i) prime[j] = 1 ;
    ll n ; cin >> n ;
    ll ar[n] ;
    ar[0] = 2 ;
    ll x , i , one = 0 , two = 0 , sum = 1 ;
    for(i = 0 ; i < n ; ++i)
    {
        cin >> x ;
        ar[0] = min(ar[0] , x) ;
        if(x == 1) ++one ;
        else ++two ;
    }
    for(i = 1 ; i < n ; ++i)
    {
        if(!prime[sum + i] and one)
        {
            ++sum ;
            --one ;
            ar[i] = 1 ;
        }
        else if(!prime[sum + 2] and two)
        {
            sum += 2 ;
            --two ;
            ar[i] = 2 ;
        }
        else
        {
            if(one >= two)
            {
                ++sum ;
                --one ;
                ar[i] = 1 ;
            }
            else
            {
                sum += 2 ;
                --two ;
                ar[i] = 2 ;
            }
        }
    }
    for(i = 0 ; i < n ; ++i) cout << ar[i] << " " ;
    cout << "\n" ;
    return 0 ;
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <cmath>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <limits.h>
#include <time.h>
#include <bitset>
#include <list>
#include <cassert>

#define EPS 1e-11
#define PI acos(-1)
#define LL long long
#define INF 1000000009
#define MP(a,b) make_pair(a,b)
#define PB(a) push_back(a)
#define OPENR(a) freopen(a,"r",stdin)
#define OPENW(a) freopen(a,"w",stdout)
#define pii pair<int,int>

int x4[4] = { 0, 0,-1, 1};
int y4[4] = {-1, 1, 0, 0};
int x8[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
int y8[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
int xhorse[8] = {1,2,1,2,-1,-2,-1,-2};
int yhorse[8] = {2,1,-2,-1,2,1,-2,-1};

using namespace std;

#define MAXN 100005

int n,q;
int v[MAXN],c[MAXN];
int dp[MAXN];
bool ada[MAXN];

int main()
{
	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]);

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

		for (int i=1;i<=n;i++) dp[i] = -INF;
		memset(ada, false,sizeof(ada));

		int ans = 0;
		int maxi[2];
		maxi[0] = maxi[1] = INF;
		for(int i=0;i<n;i++)
		{
			int col = c[i];
			if (ada[col]) dp[col] = max(dp[col], dp[col] + a*v[i]);
			else dp[col] = b*v[i];
			ada[col] = true;

			dp[col] = max(dp[col], b*v[i]);

			int other = maxi[0];
			if (col==other) other = maxi[1];

			if (other!=INF) dp[col] = max(dp[col], dp[other] + b*v[i]);

			if (maxi[0]==INF) maxi[0] = col;
			else if (maxi[1]==INF) maxi[1] = col;
			else if (dp[maxi[1]] < dp[col]) maxi[1] = col;

			if (maxi[0]!=INF && maxi[1]!=INF && dp[maxi[0]] < dp[maxi[1]]) swap(maxi[0],maxi[1]);

			ans = max(ans, dp[col]);
		//	printf("dp[%d]:%d\n",col,dp[col]);
		}
		printf("%d\n",ans);
	}

	return 0;
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <cmath>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <limits.h>
#include <time.h>
#include <bitset>
#include <list>
#include <cassert>

#define EPS 1e-11
#define PI acos(-1)
#define LL long long
#define INF 1000000009
#define MP(a,b) make_pair(a,b)
#define PB(a) push_back(a)
#define OPENR(a) freopen(a,"r",stdin)
#define OPENW(a) freopen(a,"w",stdout)
#define pii pair<int,int>

int x4[4] = { 0, 0,-1, 1};
int y4[4] = {-1, 1, 0, 0};
int x8[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
int y8[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
int xhorse[8] = {1,2,1,2,-1,-2,-1,-2};
int yhorse[8] = {2,1,-2,-1,2,1,-2,-1};

using namespace std;

#define MAXN 100005

int n,q;
LL v[MAXN],c[MAXN];
LL dp[MAXN];
bool ada[MAXN];

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

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

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

		for (LL i=1;i<=n;i++) dp[i] = -INF;
		memset(ada, false,sizeof(ada));

		LL ans = 0;
		LL maxi[2];
		maxi[0] = maxi[1] = INF;
		for(LL i=0;i<n;i++)
		{
			LL col = c[i];
			if (ada[col]) dp[col] = max(dp[col], dp[col] + a*v[i]);
			else dp[col] = b*v[i];
			ada[col] = true;

			dp[col] = max(dp[col], b*v[i]);

			LL other = maxi[0];
			if (col==other) other = maxi[1];

			if (other!=INF) dp[col] = max(dp[col], dp[other] + b*v[i]);

			if (maxi[0]==INF) maxi[0] = col;
			else if (maxi[1]==INF) maxi[1] = col;
			else if (dp[maxi[1]] < dp[col]) maxi[1] = col;

			if (maxi[0]!=INF && maxi[1]!=INF && dp[maxi[0]] < dp[maxi[1]]) swap(maxi[0],maxi[1]);

			ans = max(ans, dp[col]);
		//	printf("dp[%d]:%d\n",col,dp[col]);
		}
		printf("%I64d\n",ans);
	}

	return 0;
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <math.h>
#include <cmath>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <limits.h>
#include <time.h>
#include <bitset>
#include <list>
#include <cassert>

#define EPS 1e-11
#define PI acos(-1)
#define LL long long
#define INF 1000000009
#define MP(a,b) make_pair(a,b)
#define PB(a) push_back(a)
#define OPENR(a) freopen(a,"r",stdin)
#define OPENW(a) freopen(a,"w",stdout)
#define pii pair<int,int>

int x4[4] = { 0, 0,-1, 1};
int y4[4] = {-1, 1, 0, 0};
int x8[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
int y8[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
int xhorse[8] = {1,2,1,2,-1,-2,-1,-2};
int yhorse[8] = {2,1,-2,-1,2,1,-2,-1};

using namespace std;

#define MAXN 100005

int n,q;
LL v[MAXN],c[MAXN];
LL dp[MAXN];
bool ada[MAXN];

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

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

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

		for (LL i=1;i<=n;i++) dp[i] = -INF;
		memset(ada, false,sizeof(ada));

		LL ans = 0;
		LL maxi[2];
		maxi[0] = maxi[1] = INF;
		for(LL i=0;i<n;i++)
		{
			LL col = c[i];
			if (ada[col]) dp[col] = max(dp[col], dp[col] + a*v[i]);
			else dp[col] = b*v[i];
			ada[col] = true;

			dp[col] = max(dp[col], b*v[i]);

			LL other = maxi[0];
			if (col==other) other = maxi[1];

			if (other!=INF) dp[col] = max(dp[col], dp[other] + b*v[i]);

			if (maxi[0]!=col && maxi[1]!=col)
			{
				if (maxi[0]==INF) maxi[0] = col;
				else if (maxi[1]==INF) maxi[1] = col;
				else if (dp[maxi[1]] < dp[col]) maxi[1] = col;

				if (maxi[0]!=INF && maxi[1]!=INF && dp[maxi[0]] < dp[maxi[1]]) swap(maxi[0],maxi[1]);
			}

			ans = max(ans, dp[col]);
		//	printf("dp[%d]:%d\n",col,dp[col]);
		}
		printf("%I64d\n",ans);
	}

	return 0;
}


//Bismillahir Rahmanir Rahim
//Md. Jahidul Hasan Shakil
//Dept. of ICT, Comilla University

#include<bits/stdc++.h>
using namespace std;

#define i64 long long int
#define u64 unsigned long long int
#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)
#define repc(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define repr(i,a,b) for(__typeof(b) i=(a); i>=(b); i--)
#define clr(a) a.clear()
#define sz(a) (int)a.size()
#define mem(a,b) memset(a,b,sizeof a)
#define ERASE(a) a.erase(a.begin(),a.end())

#define sc scanf
#define S(z) scanf("%d",&z)
#define SL(z) scanf("%I64d",&z)
#define S2(xx,zz) scanf("%d %d",&xx,&zz)
#define SL2(xx,zz) scanf("%I64d %I64d",&xx,&zz)
#define SC(z) scanf("%s",&z)

#define pf printf
#define pfn printf("\n")
#define pfs printf(" ")
#define PF(z) printf("%d",z)
#define PFL(z) printf("%I64d",z)
#define PF2(x,y) printf("%d %d",x,y)
#define PFS(z) printf("%s",z)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define inf 2000000007
#define pi  acos(-1.0)
#define MAX 200007
#define MOD 1000000007LL
#define eps 1e-11

template <class T>T sqr(T x) {return x*x;}
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template <class T> inline T bigmod(T p,T e,T M)
{
    if(e==0) return 1;
    if(e%2==0){i64 t=bigmod(p,e/2,M);return (T)((t*t)%M);}
    return (T)((i64)bigmod(p,e-1,M)*(i64)p)%M;
}
template <class T> inline T bigexp(T p,T e)
{
    if(e==0)return 1;
    if(e%2==0){i64 t=bigexp(p,e/2);return (T)((t*t));}
    return (T)((i64)bigexp(p,e-1)*(i64)p);
}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

int dx4[]={1,0,-1,0};int dy4[]={0,1,0,-1}; //4 Direction
int dx8[]={1,1,0,-1,-1,-1,0,1};int dy8[]={0,1,1,1,0,-1,-1,-1};//8 direction
int nx8[]={1,1,-1,-1,2,2,-2,-2};int ny8[]={2,-2,2,-2,1,-1,1,-1};//8 direction
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
/*
struct Graph
{
    int u,v,w;
    bool operator<(const Graph& p)
    const {return w<p.w;} // oporerta chotor jnne
}edge[10];
struct compare
{
    bool operator()(const int&l,const int&r)
    {
        return l>r;
    }
};
priority_queue<int,vector<int>,compare>pq;

i64 ncr[1005][1005];
void nCr()
{
    repc(i,0,1002) ncr[i][0]=1LL;
    repc(i,1,1002)
    repc(j,1,i)
    ncr[i][j]=(ncr[i-1][j-1]+ncr[i-1][j])%MOD;
}

*/
/******************* Code Starts here *******************/

int n,q;
i64 tmp,tmp1,v[100005],c[100005],mx,id,mx2,id2,a,b,res,dp[100005];
bool bl[100005];

int main()
{
    S2(n,q);
    repc(i,1,n) sc("%I64d",&v[i]);
    repc(i,1,n) sc("%I64d",&c[i]);
    while(q--)
    {
       sc("%I64d %I64d",&a,&b);
       mx=0;mx2=0;res=0;id=0;id2=0;
       repc(i,1,n) bl[i]=false,dp[i]=0;
       repc(i,1,n)
       {
           if(bl[c[i]]==false)
           {
              tmp=v[i]*b;
              if(tmp<0) continue;
              tmp1=mx+tmp;
             // tmp+=dp[c[i]];
              tmp=max(tmp1,tmp);
              dp[c[i]]=max(dp[c[i]],tmp);
              if(tmp>=mx)
              {
                mx2=mx;
                id2=id;
                mx=tmp;
                id=c[i];
              }
              else if(tmp>=mx2)
              {
                mx2=tmp;
                id2=c[i];
              }
              bl[c[i]]=true;
           }
           else
           {
              tmp=v[i]*a;
              tmp1=v[i]*b;
               if(tmp<0 && tmp1<0) continue;
               if(tmp>=0)
               tmp+=dp[c[i]];
               else tmp=0;
               i64 tmp3=0;

               if(tmp1>=0)
               {
                  i64 tmp3=tmp1;

              if(id!=c[i]) tmp1+=mx;
              else if(id2!=c[i]) tmp1+=mx2;

               }
               else tmp1=0;
              tmp=max(tmp1,tmp);
              tmp=max(tmp,tmp3);
              dp[c[i]]=max(dp[c[i]],tmp);
             // cout<<i<<"---"<<tmp<<" "<<tmp1<<" "<<tmp3<<" "<<dp[c[i]];pfn;
              if(tmp>=mx)
              {
                if(id!=c[i])
                {
                   mx2=mx;
                   id2=id;
                   mx=tmp;
                   id=c[i];
                }
                else if(id==c[i])
                {
                   mx=tmp;
                }
              }
              else if(tmp>=mx2)
              {
                if(id==c[i])
                {
                   ;
                }
                else
                {
                   mx2=tmp;
                   id2=c[i];
                }
              }
           }
           //cout<<i<<"---"<<mx<<" "<<mx2<<" "<<tmp<<" "<<tmp1<<" "<<id<<" "<<id2<<" "<<dp[c[i]];pfn;
       }
       res=0;
       repc(i,1,n) res=max(res,dp[c[i]]);
       pf("%I64d\n",res);
    }
    return 0;
}


//Bismillahir Rahmanir Rahim
//Md. Jahidul Hasan Shakil
//Dept. of ICT, Comilla University

#include<bits/stdc++.h>
using namespace std;

#define i64 long long int
#define u64 unsigned long long int
#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)
#define repc(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define repr(i,a,b) for(__typeof(b) i=(a); i>=(b); i--)
#define clr(a) a.clear()
#define sz(a) (int)a.size()
#define mem(a,b) memset(a,b,sizeof a)
#define ERASE(a) a.erase(a.begin(),a.end())

#define sc scanf
#define S(z) scanf("%d",&z)
#define SL(z) scanf("%I64d",&z)
#define S2(xx,zz) scanf("%d %d",&xx,&zz)
#define SL2(xx,zz) scanf("%I64d %I64d",&xx,&zz)
#define SC(z) scanf("%s",&z)

#define pf printf
#define pfn printf("\n")
#define pfs printf(" ")
#define PF(z) printf("%d",z)
#define PFL(z) printf("%I64d",z)
#define PF2(x,y) printf("%d %d",x,y)
#define PFS(z) printf("%s",z)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define inf 2000000007
#define pi  acos(-1.0)
#define MAX 200007
#define MOD 1000000007LL
#define eps 1e-11

template <class T>T sqr(T x) {return x*x;}
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template <class T> inline T bigmod(T p,T e,T M)
{
    if(e==0) return 1;
    if(e%2==0){i64 t=bigmod(p,e/2,M);return (T)((t*t)%M);}
    return (T)((i64)bigmod(p,e-1,M)*(i64)p)%M;
}
template <class T> inline T bigexp(T p,T e)
{
    if(e==0)return 1;
    if(e%2==0){i64 t=bigexp(p,e/2);return (T)((t*t));}
    return (T)((i64)bigexp(p,e-1)*(i64)p);
}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

int dx4[]={1,0,-1,0};int dy4[]={0,1,0,-1}; //4 Direction
int dx8[]={1,1,0,-1,-1,-1,0,1};int dy8[]={0,1,1,1,0,-1,-1,-1};//8 direction
int nx8[]={1,1,-1,-1,2,2,-2,-2};int ny8[]={2,-2,2,-2,1,-1,1,-1};//8 direction
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
/*
struct Graph
{
    int u,v,w;
    bool operator<(const Graph& p)
    const {return w<p.w;} // oporerta chotor jnne
}edge[10];
struct compare
{
    bool operator()(const int&l,const int&r)
    {
        return l>r;
    }
};
priority_queue<int,vector<int>,compare>pq;

i64 ncr[1005][1005];
void nCr()
{
    repc(i,0,1002) ncr[i][0]=1LL;
    repc(i,1,1002)
    repc(j,1,i)
    ncr[i][j]=(ncr[i-1][j-1]+ncr[i-1][j])%MOD;
}

*/
/******************* Code Starts here *******************/

int n,q;
i64 tmp,tmp1,v[100005],c[100005],mx,id,mx2,id2,a,b,res,dp[100005];
bool bl[100005];

int main()
{
    S2(n,q);
    repc(i,1,n) sc("%I64d",&v[i]);
    repc(i,1,n) sc("%I64d",&c[i]);
    while(q--)
    {
       sc("%I64d %I64d",&a,&b);
       mx=0;mx2=0;res=0;id=0;id2=0;
       repc(i,1,n) bl[i]=false,dp[i]=0;
       repc(i,1,n)
       {
           if(bl[c[i]]==false)
           {
              tmp=v[i]*b;
              dp[c[i]]+=tmp;
              tmp1=mx+tmp;
              tmp=max(tmp1,dp[c[i]]);
              dp[c[i]]=tmp;
              if(tmp>=mx)
              {
                mx2=mx;
                id2=id;
                mx=tmp;
                id=c[i];
              }
              else if(tmp>=mx2)
              {
                mx2=tmp;
                id2=c[i];
              }
              bl[c[i]]=true;
           }
           else
           {
              tmp=v[i]*a;
              dp[c[i]]+=tmp;
              tmp1=v[i]*b;
              i64 tmp3=tmp1;
              if(id==c[i]) tmp1+=mx2;
              else if(id2==c[i]) tmp1+=mx;
              else tmp1+=mx;
              tmp=max(tmp1,dp[c[i]]);
              tmp=max(tmp,tmp3);
              dp[c[i]]=tmp;
             // cout<<i<<"---"<<tmp<<" "<<tmp1<<" "<<tmp3<<" "<<dp[c[i]];pfn;
              if(id==c[i])
              {
                mx=tmp;
                id=c[i];
              }
              else
              {
                  if(tmp>=mx)
                  {
                    mx2=mx;
                    id2=id;
                    mx=tmp;
                    id=c[i];
                  }
                  else if(tmp>=mx2)
                  {
                    mx2=tmp;
                    id2=c[i];
                  }
              }
           }
           //cout<<i<<"---"<<mx<<" "<<mx2<<" "<<tmp<<" "<<tmp1<<" "<<id<<" "<<id2<<" "<<dp[c[i]];pfn;
       }
       pf("%I64d\n",mx);
    }
    return 0;
}


//Bismillahir Rahmanir Rahim
//Md. Jahidul Hasan Shakil
//Dept. of ICT, Comilla University

#include<bits/stdc++.h>
using namespace std;

#define i64 long long int
#define u64 unsigned long long int
#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)
#define repc(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define repr(i,a,b) for(__typeof(b) i=(a); i>=(b); i--)
#define clr(a) a.clear()
#define sz(a) (int)a.size()
#define mem(a,b) memset(a,b,sizeof a)
#define ERASE(a) a.erase(a.begin(),a.end())

#define sc scanf
#define S(z) scanf("%d",&z)
#define SL(z) scanf("%I64d",&z)
#define S2(xx,zz) scanf("%d %d",&xx,&zz)
#define SL2(xx,zz) scanf("%I64d %I64d",&xx,&zz)
#define SC(z) scanf("%s",&z)

#define pf printf
#define pfn printf("\n")
#define pfs printf(" ")
#define PF(z) printf("%d",z)
#define PFL(z) printf("%I64d",z)
#define PF2(x,y) printf("%d %d",x,y)
#define PFS(z) printf("%s",z)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define inf 2000000007
#define pi  acos(-1.0)
#define MAX 200007
#define MOD 1000000007LL
#define eps 1e-11

template <class T>T sqr(T x) {return x*x;}
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template <class T> inline T bigmod(T p,T e,T M)
{
    if(e==0) return 1;
    if(e%2==0){i64 t=bigmod(p,e/2,M);return (T)((t*t)%M);}
    return (T)((i64)bigmod(p,e-1,M)*(i64)p)%M;
}
template <class T> inline T bigexp(T p,T e)
{
    if(e==0)return 1;
    if(e%2==0){i64 t=bigexp(p,e/2);return (T)((t*t));}
    return (T)((i64)bigexp(p,e-1)*(i64)p);
}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

int dx4[]={1,0,-1,0};int dy4[]={0,1,0,-1}; //4 Direction
int dx8[]={1,1,0,-1,-1,-1,0,1};int dy8[]={0,1,1,1,0,-1,-1,-1};//8 direction
int nx8[]={1,1,-1,-1,2,2,-2,-2};int ny8[]={2,-2,2,-2,1,-1,1,-1};//8 direction
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
/*
struct Graph
{
    int u,v,w;
    bool operator<(const Graph& p)
    const {return w<p.w;} // oporerta chotor jnne
}edge[10];
struct compare
{
    bool operator()(const int&l,const int&r)
    {
        return l>r;
    }
};
priority_queue<int,vector<int>,compare>pq;

i64 ncr[1005][1005];
void nCr()
{
    repc(i,0,1002) ncr[i][0]=1LL;
    repc(i,1,1002)
    repc(j,1,i)
    ncr[i][j]=(ncr[i-1][j-1]+ncr[i-1][j])%MOD;
}

*/
/******************* Code Starts here *******************/
struct T
{
    i64 val,id;
    bool operator<(const T& p)
    const
    {
        return val<p.val;
    }
};
int n,q;
i64 tmp,tmp1,v[100005],c[100005],mx,id,mx2,id2,a,b,res,dp[100005];
bool bl[100005];

int main()
{
    S2(n,q);
    repc(i,1,n) sc("%I64d",&v[i]);
    repc(i,1,n) sc("%I64d",&c[i]);
    T u;
    while(q--)
    {
       sc("%I64d %I64d",&a,&b);
       priority_queue<T>pq;
       mx=0;mx2=0;res=0;id=0;id2=0;
       repc(i,1,n) bl[i]=false,dp[i]=0;
       repc(i,1,n)
       {
           if(bl[c[i]]==false)
           {
              tmp=v[i]*b;
              u.val=0;
              while(!pq.empty())
              {
                  u=pq.top(); break;
              }
              u.val+=tmp;
              u.id=c[i];
              dp[c[i]]=max(tmp,u.val);
              u.val=dp[c[i]];
              pq.push(u);
              bl[c[i]]=true;
              res=max(u.val,res);
           }
           else
           {
              tmp=v[i]*a;
              tmp+=dp[c[i]];
               u.val=0;u.id=0;
               i64 z=-1000000000000000;
               while(!pq.empty())
              {
                  u=pq.top();
                  if(u.id==c[i])
                  {
                      if(u.val<=tmp) pq.pop();
                      else
                      {
                        z=max(z,u.val);
                        pq.pop();
                      }
                  }
                  if(u.id!=c[i]) break;
              }
              if(u.id==c[i])
              {
                tmp1=v[i]*b;
              }
              else
              {
                tmp1=v[i]*b;
                tmp1=max(tmp1,u.val+tmp1);
              }
              tmp=max(tmp,tmp1);
              u.val=tmp; dp[c[i]]=tmp;
              u.id=c[i];
              u.val=max(u.val,z);
              pq.push(u);
              res=max(u.val,res);
           }
             // cout<<i<<"---"<<tmp<<" "<<tmp1<<" "<<tmp3<<" "<<dp[c[i]];pfn;

           //cout<<i<<"---"<<mx<<" "<<mx2<<" "<<tmp<<" "<<tmp1<<" "<<id<<" "<<id2<<" "<<dp[c[i]];pfn;
       }
       pf("%I64d\n",res);
    }
    return 0;
}


//Bismillahir Rahmanir Rahim
//Md. Jahidul Hasan Shakil
//Dept. of ICT, Comilla University

#include<bits/stdc++.h>
using namespace std;

#define i64 long long int
#define u64 unsigned long long int
#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)
#define repc(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define repr(i,a,b) for(__typeof(b) i=(a); i>=(b); i--)
#define clr(a) a.clear()
#define sz(a) (int)a.size()
#define mem(a,b) memset(a,b,sizeof a)
#define ERASE(a) a.erase(a.begin(),a.end())

#define sc scanf
#define S(z) scanf("%d",&z)
#define SL(z) scanf("%I64d",&z)
#define S2(xx,zz) scanf("%d %d",&xx,&zz)
#define SL2(xx,zz) scanf("%I64d %I64d",&xx,&zz)
#define SC(z) scanf("%s",&z)

#define pf printf
#define pfn printf("\n")
#define pfs printf(" ")
#define PF(z) printf("%d",z)
#define PFL(z) printf("%I64d",z)
#define PF2(x,y) printf("%d %d",x,y)
#define PFS(z) printf("%s",z)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define inf 2000000007
#define pi  acos(-1.0)
#define MAX 200007
#define MOD 1000000007LL
#define eps 1e-11

template <class T>T sqr(T x) {return x*x;}
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template <class T> inline T bigmod(T p,T e,T M)
{
    if(e==0) return 1;
    if(e%2==0){i64 t=bigmod(p,e/2,M);return (T)((t*t)%M);}
    return (T)((i64)bigmod(p,e-1,M)*(i64)p)%M;
}
template <class T> inline T bigexp(T p,T e)
{
    if(e==0)return 1;
    if(e%2==0){i64 t=bigexp(p,e/2);return (T)((t*t));}
    return (T)((i64)bigexp(p,e-1)*(i64)p);
}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

int dx4[]={1,0,-1,0};int dy4[]={0,1,0,-1}; //4 Direction
int dx8[]={1,1,0,-1,-1,-1,0,1};int dy8[]={0,1,1,1,0,-1,-1,-1};//8 direction
int nx8[]={1,1,-1,-1,2,2,-2,-2};int ny8[]={2,-2,2,-2,1,-1,1,-1};//8 direction
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
/*
struct Graph
{
    int u,v,w;
    bool operator<(const Graph& p)
    const {return w<p.w;} // oporerta chotor jnne
}edge[10];
struct compare
{
    bool operator()(const int&l,const int&r)
    {
        return l>r;
    }
};
priority_queue<int,vector<int>,compare>pq;

i64 ncr[1005][1005];
void nCr()
{
    repc(i,0,1002) ncr[i][0]=1LL;
    repc(i,1,1002)
    repc(j,1,i)
    ncr[i][j]=(ncr[i-1][j-1]+ncr[i-1][j])%MOD;
}

*/
/******************* Code Starts here *******************/
struct T
{
    i64 val,id;
    bool operator<(const T& p)
    const
    {
        return val<p.val;
    }
};
int n,q;
i64 tmp,tmp1,v[100005],c[100005],mx,id,mx2,id2,a,b,res,dp[100005];
bool bl[100005];

int main()
{
    S2(n,q);
    repc(i,1,n) sc("%I64d",&v[i]);
    repc(i,1,n) sc("%I64d",&c[i]);
    T u;
    while(q--)
    {
       sc("%I64d %I64d",&a,&b);
       priority_queue<T>pq;
       mx=0;mx2=0;res=0;id=0;id2=0;
       repc(i,1,n) bl[i]=false,dp[i]=0;
       repc(i,1,n)
       {
           if(bl[c[i]]==false)
           {
              tmp=v[i]*b;
              u.val=0;
              while(!pq.empty())
              {
                  u=pq.top(); break;
              }
              u.val+=tmp;
              u.id=c[i];
              dp[c[i]]=max(tmp,u.val);
              u.val=dp[c[i]];
              pq.push(u);
              bl[c[i]]=true;
              res=max(u.val,res);
           }
           else
           {
              tmp=v[i]*a;
              tmp+=dp[c[i]];
               u.val=0;u.id=0;
               i64 z=0;
               while(!pq.empty())
              {
                  u=pq.top();
                  if(u.id==c[i])
                  {
                      if(u.val<=tmp) pq.pop();
                      else
                      {
                        z=max(z,u.val);
                        pq.pop();
                      }
                  }
                  if(u.id!=c[i]) break;
              }
              if(u.id==c[i])
              {
                tmp1=v[i]*b;
              }
              else
              {
                tmp1=v[i]*b;
                tmp1=max(tmp1,u.val+tmp1);
              }
              tmp=max(tmp,tmp1);
              u.val=tmp; dp[c[i]]=tmp;
              u.id=c[i];
              u.val=max(u.val,z);
              pq.push(u);
              res=max(u.val,res);
           }
             // cout<<i<<"---"<<tmp<<" "<<tmp1<<" "<<tmp3<<" "<<dp[c[i]];pfn;

           //cout<<i<<"---"<<mx<<" "<<mx2<<" "<<tmp<<" "<<tmp1<<" "<<id<<" "<<id2<<" "<<dp[c[i]];pfn;
       }
       pf("%I64d\n",res);
    }
    return 0;
}


//Bismillahir Rahmanir Rahim
//Md. Jahidul Hasan Shakil
//Dept. of ICT, Comilla University

#include<bits/stdc++.h>
using namespace std;

#define i64 long long int
#define u64 unsigned long long int
#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)
#define repc(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define repr(i,a,b) for(__typeof(b) i=(a); i>=(b); i--)
#define clr(a) a.clear()
#define sz(a) (int)a.size()
#define mem(a,b) memset(a,b,sizeof a)
#define ERASE(a) a.erase(a.begin(),a.end())

#define sc scanf
#define S(z) scanf("%d",&z)
#define SL(z) scanf("%I64d",&z)
#define S2(xx,zz) scanf("%d %d",&xx,&zz)
#define SL2(xx,zz) scanf("%I64d %I64d",&xx,&zz)
#define SC(z) scanf("%s",&z)

#define pf printf
#define pfn printf("\n")
#define pfs printf(" ")
#define PF(z) printf("%d",z)
#define PFL(z) printf("%I64d",z)
#define PF2(x,y) printf("%d %d",x,y)
#define PFS(z) printf("%s",z)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define inf 2000000007
#define pi  acos(-1.0)
#define MAX 200007
#define MOD 1000000007LL
#define eps 1e-11

template <class T>T sqr(T x) {return x*x;}
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template <class T> inline T bigmod(T p,T e,T M)
{
    if(e==0) return 1;
    if(e%2==0){i64 t=bigmod(p,e/2,M);return (T)((t*t)%M);}
    return (T)((i64)bigmod(p,e-1,M)*(i64)p)%M;
}
template <class T> inline T bigexp(T p,T e)
{
    if(e==0)return 1;
    if(e%2==0){i64 t=bigexp(p,e/2);return (T)((t*t));}
    return (T)((i64)bigexp(p,e-1)*(i64)p);
}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

int dx4[]={1,0,-1,0};int dy4[]={0,1,0,-1}; //4 Direction
int dx8[]={1,1,0,-1,-1,-1,0,1};int dy8[]={0,1,1,1,0,-1,-1,-1};//8 direction
int nx8[]={1,1,-1,-1,2,2,-2,-2};int ny8[]={2,-2,2,-2,1,-1,1,-1};//8 direction
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
/*
struct Graph
{
    int u,v,w;
    bool operator<(const Graph& p)
    const {return w<p.w;} // oporerta chotor jnne
}edge[10];
struct compare
{
    bool operator()(const int&l,const int&r)
    {
        return l>r;
    }
};
priority_queue<int,vector<int>,compare>pq;

i64 ncr[1005][1005];
void nCr()
{
    repc(i,0,1002) ncr[i][0]=1LL;
    repc(i,1,1002)
    repc(j,1,i)
    ncr[i][j]=(ncr[i-1][j-1]+ncr[i-1][j])%MOD;
}

*/
/******************* Code Starts here *******************/

int n,q;
i64 tmp,tmp1,v[100005],c[100005],a,b,res,dp[100005][2];
int st[100005][20],co;

i64 call(int p, int t)
{
    if(p>=n) return 0;
    if(st[p][t]==co) return dp[p][t];
    st[p][t]=co;
    i64 ret=0;
    for(int i=p+1;i<=n;i++)
    {
        if(c[p]==c[i] && v[i]*a>0)
        {
           ret=max(ret,v[i]*a+call(i,1));
        }
        else if(v[i]*b>0)
        {
           ret=max(ret,v[i]*b+call(i,0));
        }
    }
    if(ret==-1e15) ret=0;
    return dp[p][t]=ret;
}
int main()
{
    S2(n,q);
    repc(i,1,n) sc("%I64d",&v[i]);
    repc(i,1,n) sc("%I64d",&c[i]);

    while(q--)
    {
        co++;
       sc("%I64d %I64d",&a,&b);
       res=-1e15;
       repc(i,1,n) res=max(res,v[i]*b+call(i,0));
       pf("%I64d\n",res);
    }
    return 0;
}


//Bismillahir Rahmanir Rahim
//Md. Jahidul Hasan Shakil
//Dept. of ICT, Comilla University

#include<bits/stdc++.h>
using namespace std;

#define i64 long long int
#define u64 unsigned long long int
#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)
#define repc(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define repr(i,a,b) for(__typeof(b) i=(a); i>=(b); i--)
#define clr(a) a.clear()
#define sz(a) (int)a.size()
#define mem(a,b) memset(a,b,sizeof a)
#define ERASE(a) a.erase(a.begin(),a.end())

#define sc scanf
#define S(z) scanf("%d",&z)
#define SL(z) scanf("%I64d",&z)
#define S2(xx,zz) scanf("%d %d",&xx,&zz)
#define SL2(xx,zz) scanf("%I64d %I64d",&xx,&zz)
#define SC(z) scanf("%s",&z)

#define pf printf
#define pfn printf("\n")
#define pfs printf(" ")
#define PF(z) printf("%d",z)
#define PFL(z) printf("%I64d",z)
#define PF2(x,y) printf("%d %d",x,y)
#define PFS(z) printf("%s",z)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define inf 2000000007
#define pi  acos(-1.0)
#define MAX 200007
#define MOD 1000000007LL
#define eps 1e-11

template <class T>T sqr(T x) {return x*x;}
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template <class T> inline T bigmod(T p,T e,T M)
{
    if(e==0) return 1;
    if(e%2==0){i64 t=bigmod(p,e/2,M);return (T)((t*t)%M);}
    return (T)((i64)bigmod(p,e-1,M)*(i64)p)%M;
}
template <class T> inline T bigexp(T p,T e)
{
    if(e==0)return 1;
    if(e%2==0){i64 t=bigexp(p,e/2);return (T)((t*t));}
    return (T)((i64)bigexp(p,e-1)*(i64)p);
}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

int dx4[]={1,0,-1,0};int dy4[]={0,1,0,-1}; //4 Direction
int dx8[]={1,1,0,-1,-1,-1,0,1};int dy8[]={0,1,1,1,0,-1,-1,-1};//8 direction
int nx8[]={1,1,-1,-1,2,2,-2,-2};int ny8[]={2,-2,2,-2,1,-1,1,-1};//8 direction
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
/*
struct Graph
{
    int u,v,w;
    bool operator<(const Graph& p)
    const {return w<p.w;} // oporerta chotor jnne
}edge[10];
struct compare
{
    bool operator()(const int&l,const int&r)
    {
        return l>r;
    }
};
priority_queue<int,vector<int>,compare>pq;

i64 ncr[1005][1005];
void nCr()
{
    repc(i,0,1002) ncr[i][0]=1LL;
    repc(i,1,1002)
    repc(j,1,i)
    ncr[i][j]=(ncr[i-1][j-1]+ncr[i-1][j])%MOD;
}

*/
/******************* Code Starts here *******************/

int n,q;
i64 tmp,tmp1,v[100005],c[100005],mx,id,mx2,id2,a,b,res,dp[100005];
bool bl[100005];

int main()
{
    S2(n,q);
    repc(i,1,n) sc("%I64d",&v[i]);
    repc(i,1,n) sc("%I64d",&c[i]);
    while(q--)
    {
       sc("%I64d %I64d",&a,&b);
       mx=0;mx2=0;res=0;id=0;id2=0;
       repc(i,1,n) bl[i]=false,dp[i]=0;
       repc(i,1,n)
       {
           if(bl[c[i]]==false)
           {
              tmp=v[i]*b;

              tmp1=mx+tmp;
              tmp+=dp[c[i]];
              tmp=max(tmp1,tmp);
              dp[c[i]]=tmp;
              if(tmp>=mx)
              {
                mx2=mx;
                id2=id;
                mx=tmp;
                id=c[i];
              }
              else if(tmp>=mx2)
              {
                mx2=tmp;
                id2=c[i];
              }
              bl[c[i]]=true;
           }
           else
           {
              tmp=v[i]*a;
              tmp+=dp[c[i]];

              tmp1=v[i]*b;
              i64 tmp3=tmp1;

              if(id!=c[i]) tmp1+=mx;
              else if(id2!=c[i]) tmp1+=mx2;
              tmp=max(tmp1,tmp);
              tmp=max(tmp,tmp3);
              dp[c[i]]=tmp;
             // cout<<i<<"---"<<tmp<<" "<<tmp1<<" "<<tmp3<<" "<<dp[c[i]];pfn;
              if(tmp>=mx)
              {
                if(id!=c[i])
                {
                   mx2=mx;
                   id2=id;
                   mx=tmp;
                   id=c[i];
                }
                else if(id2!=c[i])
                {
                   mx=tmp;
                }
              }
              else if(tmp>=mx2)
              {
                if(id==c[i])
                {
                   continue;
                }
                else
                {
                   mx2=tmp;
                   id2=c[i];
                }
              }
           }
           //cout<<i<<"---"<<mx<<" "<<mx2<<" "<<tmp<<" "<<tmp1<<" "<<id<<" "<<id2<<" "<<dp[c[i]];pfn;
       }
       pf("%I64d\n",mx);
    }
    return 0;
}


//Bismillahir Rahmanir Rahim
//Md. Jahidul Hasan Shakil
//Dept. of ICT, Comilla University

#include<bits/stdc++.h>
using namespace std;

#define i64 long long int
#define u64 unsigned long long int
#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)
#define repc(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define repr(i,a,b) for(__typeof(b) i=(a); i>=(b); i--)
#define clr(a) a.clear()
#define sz(a) (int)a.size()
#define mem(a,b) memset(a,b,sizeof a)
#define ERASE(a) a.erase(a.begin(),a.end())

#define sc scanf
#define S(z) scanf("%d",&z)
#define SL(z) scanf("%I64d",&z)
#define S2(xx,zz) scanf("%d %d",&xx,&zz)
#define SL2(xx,zz) scanf("%I64d %I64d",&xx,&zz)
#define SC(z) scanf("%s",&z)

#define pf printf
#define pfn printf("\n")
#define pfs printf(" ")
#define PF(z) printf("%d",z)
#define PFL(z) printf("%I64d",z)
#define PF2(x,y) printf("%d %d",x,y)
#define PFS(z) printf("%s",z)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define inf 2000000007
#define pi  acos(-1.0)
#define MAX 200007
#define MOD 1000000007LL
#define eps 1e-11

template <class T>T sqr(T x) {return x*x;}
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template <class T> inline T bigmod(T p,T e,T M)
{
    if(e==0) return 1;
    if(e%2==0){i64 t=bigmod(p,e/2,M);return (T)((t*t)%M);}
    return (T)((i64)bigmod(p,e-1,M)*(i64)p)%M;
}
template <class T> inline T bigexp(T p,T e)
{
    if(e==0)return 1;
    if(e%2==0){i64 t=bigexp(p,e/2);return (T)((t*t));}
    return (T)((i64)bigexp(p,e-1)*(i64)p);
}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

int dx4[]={1,0,-1,0};int dy4[]={0,1,0,-1}; //4 Direction
int dx8[]={1,1,0,-1,-1,-1,0,1};int dy8[]={0,1,1,1,0,-1,-1,-1};//8 direction
int nx8[]={1,1,-1,-1,2,2,-2,-2};int ny8[]={2,-2,2,-2,1,-1,1,-1};//8 direction
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
/*
struct Graph
{
    int u,v,w;
    bool operator<(const Graph& p)
    const {return w<p.w;} // oporerta chotor jnne
}edge[10];
struct compare
{
    bool operator()(const int&l,const int&r)
    {
        return l>r;
    }
};
priority_queue<int,vector<int>,compare>pq;

i64 ncr[1005][1005];
void nCr()
{
    repc(i,0,1002) ncr[i][0]=1LL;
    repc(i,1,1002)
    repc(j,1,i)
    ncr[i][j]=(ncr[i-1][j-1]+ncr[i-1][j])%MOD;
}

*/
/******************* Code Starts here *******************/

int n,q;
i64 tmp,tmp1,v[100005],c[100005],mx,id,mx2,id2,a,b,res,dp[100005];
bool bl[100005];

int main()
{
    S2(n,q);
    repc(i,1,n) sc("%I64d",&v[i]);
    repc(i,1,n) sc("%I64d",&c[i]);
    while(q--)
    {
       sc("%I64d %I64d",&a,&b);
       mx=-1e15;mx2=-1e15;res=0;id=0;id2=0;
       repc(i,1,n) bl[i]=false,dp[i]=0;
       repc(i,1,n)
       {
           if(bl[c[i]]==false)
           {
              tmp=v[i]*b;
              if(mx>=0)tmp1=mx+tmp;
              else tmp1=0;
              tmp+=dp[c[i]];
              tmp=max(tmp1,tmp);
              dp[c[i]]=max(dp[c[i]],tmp);
              if(tmp>=mx)
              {
                mx2=mx;
                id2=id;
                mx=tmp;
                id=c[i];
              }
              else if(tmp>=mx2)
              {
                mx2=tmp;
                id2=c[i];
              }
              bl[c[i]]=true;
           }
           else
           {
              tmp=v[i]*a;
              tmp+=dp[c[i]];

              tmp1=v[i]*b;
              i64 tmp3=tmp1;

              if(id!=c[i] && mx>=0) tmp1+=mx;
              else if(id2!=c[i] && mx2>=0) tmp1+=mx2;
              tmp=max(tmp1,tmp);
              tmp=max(tmp,tmp3);
              dp[c[i]]=max(dp[c[i]],tmp);
             // cout<<i<<"---"<<tmp<<" "<<tmp1<<" "<<tmp3<<" "<<dp[c[i]];pfn;
              if(tmp>=mx)
              {
                if(id!=c[i])
                {
                   mx2=mx;
                   id2=id;
                   mx=tmp;
                   id=c[i];
                }
                else if(id==c[i])
                {
                   mx=tmp;
                }
              }
              else if(tmp>=mx2)
              {
                if(id==c[i])
                {
                   ;
                }
                else
                {
                   mx2=tmp;
                   id2=c[i];
                }
              }
           }
           //cout<<i<<"---"<<mx<<" "<<mx2<<" "<<tmp<<" "<<tmp1<<" "<<id<<" "<<id2<<" "<<dp[c[i]];pfn;
       }
       res=0;
       repc(i,1,n) res=max(res,dp[c[i]]);
       pf("%I64d\n",res);
    }
    return 0;
}


//Bismillahir Rahmanir Rahim
//Md. Jahidul Hasan Shakil
//Dept. of ICT, Comilla University

#include<bits/stdc++.h>
using namespace std;

#define i64 long long int
#define u64 unsigned long long int
#define fin(a) freopen(a,"r",stdin)
#define fout(a) freopen(a,"w",stdout)
#define repc(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define repr(i,a,b) for(__typeof(b) i=(a); i>=(b); i--)
#define clr(a) a.clear()
#define sz(a) (int)a.size()
#define mem(a,b) memset(a,b,sizeof a)
#define ERASE(a) a.erase(a.begin(),a.end())

#define sc scanf
#define S(z) scanf("%d",&z)
#define SL(z) scanf("%I64d",&z)
#define S2(xx,zz) scanf("%d %d",&xx,&zz)
#define SL2(xx,zz) scanf("%I64d %I64d",&xx,&zz)
#define SC(z) scanf("%s",&z)

#define pf printf
#define pfn printf("\n")
#define pfs printf(" ")
#define PF(z) printf("%d",z)
#define PFL(z) printf("%I64d",z)
#define PF2(x,y) printf("%d %d",x,y)
#define PFS(z) printf("%s",z)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define inf 2000000007
#define pi  acos(-1.0)
#define MAX 200007
#define MOD 1000000007LL
#define eps 1e-11

template <class T>T sqr(T x) {return x*x;}
template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
template <class T> inline T bigmod(T p,T e,T M)
{
    if(e==0) return 1;
    if(e%2==0){i64 t=bigmod(p,e/2,M);return (T)((t*t)%M);}
    return (T)((i64)bigmod(p,e-1,M)*(i64)p)%M;
}
template <class T> inline T bigexp(T p,T e)
{
    if(e==0)return 1;
    if(e%2==0){i64 t=bigexp(p,e/2);return (T)((t*t));}
    return (T)((i64)bigexp(p,e-1)*(i64)p);
}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

int dx4[]={1,0,-1,0};int dy4[]={0,1,0,-1}; //4 Direction
int dx8[]={1,1,0,-1,-1,-1,0,1};int dy8[]={0,1,1,1,0,-1,-1,-1};//8 direction
int nx8[]={1,1,-1,-1,2,2,-2,-2};int ny8[]={2,-2,2,-2,1,-1,1,-1};//8 direction
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
/*
struct Graph
{
    int u,v,w;
    bool operator<(const Graph& p)
    const {return w<p.w;} // oporerta chotor jnne
}edge[10];
struct compare
{
    bool operator()(const int&l,const int&r)
    {
        return l>r;
    }
};
priority_queue<int,vector<int>,compare>pq;

i64 ncr[1005][1005];
void nCr()
{
    repc(i,0,1002) ncr[i][0]=1LL;
    repc(i,1,1002)
    repc(j,1,i)
    ncr[i][j]=(ncr[i-1][j-1]+ncr[i-1][j])%MOD;
}

*/
/******************* Code Starts here *******************/

int n,q;
i64 tmp,tmp1,v[100005],c[100005],a,b,res,dp[100005][2];
int st[100005][20],co;

i64 call(int p, int t)
{
    if(p>=n) return 0;
    if(st[p][t]==co) return dp[p][t];
    st[p][t]=co;
    i64 ret=0;
    for(int i=p+1;i<=n;i++)
    {
        if(c[p]==c[i])
        {
           ret=max(ret,v[i]*a+call(i,1));
        }
        else
        {
           ret=max(ret,v[i]*b+call(i,0));
        }
    }
    if(ret==-1e15) ret=0;
    return dp[p][t]=ret;
}
int main()
{
    S2(n,q);
    repc(i,1,n) sc("%I64d",&v[i]);
    repc(i,1,n) sc("%I64d",&c[i]);

    while(q--)
    {
        co++;
       sc("%I64d %I64d",&a,&b);
       res=0;
       repc(i,1,n) res=max(res,v[i]*b+call(i,0));
       pf("%I64d\n",res);
    }
    return 0;
}