Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #define ll long long
- #define OO 2'000'000'000
- #define ull unsigned long long
- #define nl '\n'
- #define sz(x) (ll)(x.size())
- #define all(x) x.begin(),x.end()
- #define rall(s) s.rbegin(), s.rend()
- #define getline(s) getline(cin>>ws,s)
- #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
- #define pi 3.141592653589793
- #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
- #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
- void Fast_IO(){
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- // freopen("filename.in", "r", stdin);
- // freopen("filename.txt", "w", stdout);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
- int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
- ll MaxSubarray(vector<ll> &a, vector<ll>&b,ll l,ll r){
- // kadane's algorithm
- ll maxi = 0, sum = 0,n=sz(a);
- for(int i=0;i<n;i++){
- if(i>=l and i<=r) sum+=b[i];
- else sum+=a[i];
- maxi = max(maxi,sum);
- if(sum<0) sum=0;
- }
- return maxi;
- }
- void solve(){
- ll n, x;
- cin >> n >> x;
- vector<ll> a(n),b(n);
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- b[i] = a[i]/x;
- }
- bool neg = false;
- ll ans = MaxSubarray(a,b,0,n-1);
- ll cnt = 0, pos=-1;
- for(int i=0;i<n;i++){
- if(neg and a[i]<0) cnt++;
- else if(a[i]<0){
- neg = true;
- pos = i;
- cnt++;
- }else{
- if(neg){
- ll tmp = MaxSubarray(a,b,pos,i-1);
- ans = max(ans,tmp);
- neg = false;
- }
- }
- }
- if(neg){
- ll tmp = MaxSubarray(a,b,pos,n-1);
- ans = max(ans,tmp);
- }
- cout << ans << nl;
- }
- int main(){
- Fast_IO();
- int t =1;
- //cin>>t;
- while(t--){
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement