Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #define task "BALLS"
- using namespace std;
- using ll = long long;
- using ld = long double;
- const int N = 1e5 + 2;
- int c[N], n, q;
- ll v[N], a, b, dp[N], st[N];
- void Read(){
- cin >> n >> q;
- for(int i = 1; i <= n; ++i)
- cin >> v[i];
- for(int i = 1; i <= n; ++i)
- cin >> c[i];
- }
- void Reset(){
- for(int i = 0; i <= n; ++i)
- st[i] = -1e17;
- }
- void Solve(){
- while(q--){
- cin >> a >> b;
- int max1 = 0, max2 = 0;
- Reset();
- ll res = 0;
- for(int i = 1; i <= n; ++i){
- dp[i] = v[i] * b;
- if(max1 != c[i]){
- dp[i] = max(dp[i], st[c[i]] + v[i] * a);
- dp[i] = max(dp[i], st[max1] + v[i] * b);
- }
- else{
- dp[i] = max(dp[i], st[max1] + v[i] * a);
- dp[i] = max(dp[i], st[max2] + v[i] * b);
- }
- if(max1 == 0 || st[max1] < dp[i]){
- if(max1 != c[i])
- max2 = max1;
- max1 = c[i];
- }
- else if(max2 == 0 || st[max2] < dp[i])
- max2 = c[i];
- st[c[i]] = max(st[c[i]], dp[i]);
- res = max(res, dp[i]);
- }
- cout << res << "\n";
- }
- }
- int32_t main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0); if(fopen(task".INP", "r"))
- freopen(task".IN", "r", stdin),
- freopen(task".OUT", "w", stdout);
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment