Advertisement
Ahmed_Negm

Untitled

Apr 11th, 2023
603
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define OO 2'000'000'000
  8. #define ull unsigned long long
  9. #define nl '\n'
  10. #define sz(x) (ll)(x.size())
  11. #define all(x) x.begin(),x.end()
  12. #define rall(s)  s.rbegin(), s.rend()
  13. #define getline(s) getline(cin>>ws,s)
  14. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  15. #define pi  3.141592653589793
  16. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  17. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  18.  
  19.  
  20. void Fast_IO(){
  21. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  22. // freopen("filename.in", "r", stdin);
  23. // freopen("filename.txt", "w", stdout);
  24. #ifndef ONLINE_JUDGE
  25. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  33. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  34.  
  35. ll MaxSubarray(vector<ll> &a, vector<ll>&b,ll l,ll r){
  36.     // kadane's algorithm
  37.     ll maxi = 0, sum = 0,n=sz(a);
  38.     for(int i=0;i<n;i++){
  39.         if(i>=l and i<=r) sum+=b[i];
  40.         else sum+=a[i];
  41.         maxi = max(maxi,sum);
  42.         if(sum<0) sum=0;
  43.     }
  44.     return maxi;
  45. }
  46.  
  47.  
  48.  
  49. void solve(){
  50.    ll n, x;
  51.     cin >> n >> x;
  52.     vector<ll> a(n),b(n);
  53.     for (int i = 0; i < n; i++) {
  54.         cin >> a[i];
  55.         b[i] = a[i]/x;
  56.     }
  57.  
  58.     bool neg = false;
  59.  
  60.     ll ans = MaxSubarray(a,b,0,n-1);
  61.  
  62.     ll cnt = 0, pos=-1;
  63.  
  64.     for(int i=0;i<n;i++){
  65.         if(neg and a[i]<0) cnt++;
  66.         else if(a[i]<0){
  67.             neg = true;
  68.             pos = i;
  69.             cnt++;
  70.         }else{
  71.             if(neg){
  72.                 ll tmp = MaxSubarray(a,b,pos,i-1);
  73.                 ans = max(ans,tmp);
  74.                 neg = false;
  75.             }
  76.         }
  77.     }
  78.  
  79.     if(neg){
  80.         ll tmp = MaxSubarray(a,b,pos,n-1);
  81.         ans = max(ans,tmp);
  82.     }
  83.  
  84.     cout << ans << nl;
  85.  
  86.  
  87.  
  88.  
  89. }
  90.  
  91. int main(){
  92.     Fast_IO();
  93. int t =1;
  94. //cin>>t;
  95. while(t--){
  96. solve();
  97. }
  98. return 0;
  99. }  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement