660F - Bear and Bowling 4 - CodeForces Solution


binary search data structures divide and conquer geometry ternary search *2500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

const int maxn = 2*1e5+5;
long long n,a[maxn];
long long dp[maxn],pref[maxn];
long long ans;

void read()
{
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
    }
    reverse(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        dp[i] = dp[i-1] + (a[i]*(n-i+1));
        pref[i] = pref[i-1] + a[i];
    }
}

long long cost(int l, int r)
{
    long long nz = dp[r] - dp[l-1];
    nz -= (pref[r]-pref[l-1])*(n-r);
    return nz;
}

void divide(int l, int r, int optl, int optr)
{
    if(l>r) return;
    int mid = (l+r)/2;
    pair<long long, int> best = {-1000000000000000000,-1};
    for(int i=optl;i<=min(mid,optr);i++)
    {
        long long pom = cost(i,mid);
        if(pom>best.first) best = {pom,i};
    }
    //cout << best.first << " " << mid << endl;
    ans = max(ans,best.first);
    int opt = best.second;
    divide(l,mid-1,optl,opt);
    divide(mid+1,r,opt,optr);
}
void solve()
{
    divide(1,n,1,n);
    cout << ans << endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	read();
	solve();
return 0;
}
/*
5
10 -3 -3 8 2
*/


Comments

Submit
0 Comments
More Questions

202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus
445A - DZY Loves Chessboard
1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments