Dang_Quan_10_Tin

BALLS

Aug 4th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #define task "BALLS"
  5. using namespace std;
  6. using ll = long long;
  7. using ld = long double;
  8.  
  9. const int N = 1e5 + 2;
  10. int c[N], n, q;
  11. ll v[N], a, b, dp[N], st[N];
  12.  
  13. void Read(){
  14.     cin >> n >> q;
  15.     for(int i = 1; i <= n; ++i)
  16.         cin >> v[i];
  17.     for(int i = 1; i <= n; ++i)
  18.         cin >> c[i];
  19. }
  20.  
  21. void Reset(){
  22.     for(int i = 0; i <= n; ++i)
  23.         st[i] = -1e17;
  24. }
  25.  
  26. void Solve(){
  27.     while(q--){
  28.         cin >> a >> b;
  29.         int max1 = 0, max2 = 0;
  30.         Reset();
  31.         ll res = 0;
  32.         for(int i = 1; i <= n; ++i){
  33.             dp[i] = v[i] * b;
  34.             if(max1 != c[i]){
  35.                 dp[i] = max(dp[i], st[c[i]] + v[i] * a);
  36.                 dp[i] = max(dp[i], st[max1] + v[i] * b);
  37.             }
  38.             else{
  39.                 dp[i] = max(dp[i], st[max1] + v[i] * a);
  40.                 dp[i] = max(dp[i], st[max2] + v[i] * b);
  41.             }
  42.             if(max1 == 0 || st[max1] < dp[i]){
  43.                 if(max1 != c[i])
  44.                     max2 = max1;
  45.                 max1 = c[i];
  46.             }
  47.             else if(max2 == 0 || st[max2] < dp[i])
  48.                 max2 = c[i];
  49.             st[c[i]] = max(st[c[i]], dp[i]);
  50.             res = max(res, dp[i]);
  51.         }
  52.         cout << res << "\n";
  53.     }
  54. }
  55.  
  56. int32_t main(){
  57.     ios::sync_with_stdio(0);
  58.     cin.tie(0);
  59.     cout.tie(0); if(fopen(task".INP", "r"))
  60.     freopen(task".IN", "r", stdin),
  61.     freopen(task".OUT", "w", stdout);
  62.     Read();
  63.     Solve();
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment