fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31. vector<int> b;
  32.  
  33. int consistency(int n){
  34.  
  35. vector<pair<int,int>> p;
  36. for(int i=0; i<n; i++){
  37. p.push_back({a[i], b[i]});
  38. }
  39.  
  40. sort(begin(p), end(p));
  41.  
  42. int pos = 0;
  43. int neg = 0;
  44.  
  45. int maxi = INT32_MIN;
  46. for(int i=n-1; i>=0; i--){
  47. if(p[i].first >= 0){
  48. int sum = p[i].second + pos;
  49. int val = p[i].first * sum;
  50. maxi = max(maxi, val);
  51. }
  52. else{
  53. int sum = p[i].second + neg;
  54. int val = p[i].first * sum;
  55. maxi = max(maxi, val);
  56. }
  57. if(p[i].second >= 0) pos += p[i].second;
  58. else neg += p[i].second;
  59. }
  60.  
  61. return maxi;
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. int practice(int n){
  79.  
  80.  
  81. return 0;
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. void solve() {
  89.  
  90. int n;
  91. cin>> n;
  92.  
  93. a.resize(n);
  94. b.resize(n);
  95. for(int i=0; i<n; i++) cin >> a[i];
  96. for(int i=0; i<n; i++) cin >> b[i];
  97.  
  98. cout << consistency(n) << endl;
  99.  
  100.  
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. int32_t main() {
  108. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  109.  
  110. int t = 1;
  111. cin >> t;
  112. while (t--) {
  113. solve();
  114. }
  115.  
  116. return 0;
  117. }
Success #stdin #stdout 0.01s 5288KB
stdin
3
5
1 2 4 3 5
5 -1 10 -5 15
5
-2 3 4 6 8
20 -3 1 2 -4
5
-2 -3 4 6 8
20 -30 1 2 -4
stdout
100
12
102