#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
//typedef __int128 ull;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
typedef pair<pi, pi> piii;
struct balancedheap
{
multiset<ll> chosen, store;
ll k, sum;
balancedheap(ll _k = 0) {k =_k; sum = 0;}
void balance()
{
while (ll(chosen.size()) > k && chosen.size())
{
sum -= *prev(chosen.end());
store.insert(*prev(chosen.end()));
chosen.erase(prev(chosen.end()));
}
while (ll(chosen.size()) < k && store.size() && *store.begin() < 0)
{
sum += *store.begin();
chosen.insert(*store.begin());
store.erase(store.begin());
}
while (chosen.size() && store.size() && *prev(chosen.end()) > *store.begin())
{
ll x = *store.begin(), y = *prev(chosen.end());
sum += x - y;
store.erase(store.begin()); chosen.erase(--chosen.end());
store.insert(y); chosen.insert(x);
}
}
void insert(ll x)
{
store.insert(x);
//balance();
}
void erase(ll x)
{
if (chosen.find(x)==chosen.end()) {store.erase(store.lb(x));}
else{sum -= x; chosen.erase(chosen.lb(x));}
//balance();
}
void clear()
{
k = 0, sum = 0;
store.clear();
chosen.clear();
}
void setk(ll _k) {k = _k; }
ll query() {balance(); return sum;}
} *cnt;
ll mark[100005];
ll k, ans = 0, ss;
ll l = 1, r = 0;
ll summax(ll x, ll y)
{
if (x > r || y < l)
{
cnt->clear();
l = x, r = x-1;
}
while (r < y) cnt->insert(-mark[++r]);
while (r > y) cnt->erase(-mark[r--]);
while (l < x) cnt->erase(-mark[l++]);
while (l>x) cnt->insert(-mark[--l]);
cnt->setk(max(0LL, k -(2*y- ss- x)));
return -cnt->query();
}
void dnc(ll from, ll to, ll lo, ll hi)
{
if (from > to) return;
ll mid = (from + to) >> 1;
pi opt = mp(LLONG_MIN, -1);
for (ll i=min(mid, hi); i>=lo; --i) opt = max(opt, mp(summax(i, mid), i));
ans = max(ans, opt.f);
dnc(from, mid-1, lo, opt.s);
dnc(mid+1, to, opt.s, hi);
}
long long int findMaxAttraction(int n, int start, int d, int attraction[])
{
ans = 0;
cnt = new balancedheap();
l = 1, r= 0;
k = d;
ss = start;
for (ll i=0; i<n; ++i) mark[i] = attraction[i];
dnc(start, n-1, 0, start);
ss = start = n - start - 1;
for (ll i=0; i<n; ++i) mark[i] = attraction[n-1-i];
dnc(start, n-1, 0, start);
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |