Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
2.7 kB
6
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

#define int long long
void __print(int x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \
  cerr << "[" << #x << "] = [";                                                \
  _print(x)
#else
#define debug(x...)
#endif

void solve() {
  int n,p;
  cin >> n >> p;
  vector<pair<int,int>> a_b;
  for(int i =0; i<n;i++){
    int a;
    cin >> a;
    a_b.push_back({a,0});
  }
  for(int i =0; i<n;i++){
    int b;
    cin >> b;
    a_b[i].second = b;
  }
  //sort vector according to a_b.second[] in ascending order and if a_b.second[] is same then sort according to a_b.first[] in descending order
  sort(a_b.begin(), a_b.end(), [](pair<int,int> a, pair<int,int> b){
    if(a.second == b.second){
      return a.first > b.first;
    }
    return a.second < b.second;
  });
  debug(a_b);
  int N = n;
  int ans = 0;
  ans+= p;
  n--;
  int i=0;
  while(n>0){
    if(i==0){
      if(a_b[i].second<p){
        ans = ans + min(a_b[i].first*a_b[i].second,n*a_b[i].second);
        n = n - min(a_b[i].first,n); 
        debug(ans,n,i);
        i++;
      }
      else{
        ans = n*p;
        cout << ans << endl;
        debug(ans,n,i);
        return;
      }
    }
    else{
      if(a_b[i].second<p){
        ans = ans + min(a_b[i].first*a_b[i].second,n*a_b[i].second);
        n = n -  min(a_b[i].first,n);
        debug(ans,n,i);
        i++;
      }
      else{
        ans += (n)*p;
        cout << ans << endl;
        debug(ans,n,i);
        return;
        
      }
    }
    
  }
  cout << ans << endl;
}

signed main() {
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}
Leave a Comment