fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void solve(vector<int> rates, vector<int> stra, int k) {
  5. int n = rates.size();
  6. int original = 0;
  7. for(int i=0;i<n;i++) {
  8. original += stra[i] * rates[i];
  9. }
  10. int i=0,j=0,wind=0,owind=0;
  11. int ans = original;
  12. // cout << original << endl;
  13. while(i<n) {
  14. owind += stra[i] * rates[i];
  15. // cout << i << ' ' << j << ' ' << owind << endl;
  16. if(i >= k/2) {
  17. wind += rates[i];
  18. }
  19. if(i-j+1 == k) {
  20. int tans = original - owind + wind;
  21. ans = max(ans, tans);
  22. // cout << j << ' ' << i << ' ' << owind << ' ' << wind << ' ' << tans << endl;
  23. owind -= stra[j]*rates[j];
  24. int mid_ind = (j+k/2);
  25. wind -= rates[mid_ind];
  26. j++;
  27. }
  28. i++;
  29. }
  30. cout << ans << endl;
  31. }
  32.  
  33. int main() {
  34. solve({2,4,1,5,10,6}, {-1,1,0,1,-1,0}, 4);
  35. return 0;
  36. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
18