fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5.  
  6.  
  7. const int M = 1000000007;
  8. const int N = 3e5+9;
  9. const int INF = 2e9+1;
  10. const int MAXN = 100000;
  11. const int LINF = 2000000000000000001;
  12.  
  13. //_ ***************************** START Below *******************************
  14.  
  15.  
  16.  
  17. vector<int> a;
  18. vector<int>b;
  19.  
  20.  
  21. int consistency(int n, int k){
  22. int ans = 0;
  23.  
  24. int s = 0, e = 0;
  25. int weights = 0;
  26. int costs = 0;
  27.  
  28. while(e<n){
  29. weights += a[e];
  30. costs += b[e];
  31.  
  32. if(weights < k){
  33. ans = max(ans, costs);
  34. e++;
  35. }
  36. else{
  37. while(s<=e && weights > k){
  38. weights -= a[s];
  39. costs -= b[s];
  40. s++;
  41. }
  42. ans = max(ans, costs);
  43. e++;
  44. }
  45. }
  46.  
  47. return ans;
  48.  
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. void solve() {
  62.  
  63. int n, k;
  64. cin >> n >> k;
  65. a.resize(n);
  66. b.resize(n);
  67. for(int i=0; i<n; i++) cin >> a[i];
  68. for(int i=0; i<n; i++) cin >> b[i];
  69.  
  70. cout << consistency(n, k) << endl;
  71.  
  72.  
  73.  
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. int32_t main() {
  82. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  83.  
  84. int t = 1;
  85. while (t--) {
  86. solve();
  87. }
  88.  
  89. return 0;
  90. }
Success #stdin #stdout 0s 5324KB
stdin
6 20
9 7 6 5 8 4
7 1 3 6 8 3
stdout
17