This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <unordered_map>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define fi first
#define se second
typedef pair<int,int> ii;
int n, s, d;
long long A[100003];
long long solve0()
{
int rem = d;
long long sum = 0, res = 0;
priority_queue<long long, vector<long long>, greater<long long> > pq;
for(int i = 0; i < n; i++)
{
pq.push(A[i]);
sum += A[i];
while((int)pq.size() > rem && pq.size())
{
// printf("%d %lld\n", i, pq.top());
sum -= pq.top();
pq.pop();
}
res = max(res, sum);
rem--;
}
return res;
}
long long best[100003];
long long get_big()
{
// printf("start : %d\n", s);
// for(int i = 0; i < n; i++)
// printf("%lld ", A[i]); printf("\n");
memset(best, 0, sizeof best);
int rem = d, used = 0;
multiset<long long, greater<long long> > st;
st.clear();
for(int i = s-1; i >= 0; i--)
{
rem--;
used++;
st.insert(A[i]);
long long sum = 0;
auto it = st.begin();
for(int j = 1; j <= rem && it != st.end(); j++)
{
sum += *it;
best[used+j] = max(best[used+j], sum);
++it;
}
}
for(int i = 1; i <= d; i++)
best[i] = max(best[i-1], best[i]);
rem = d;
long long mx = best[d];
st.clear();
for(int i = s; i < n; i++)
{
st.insert(A[i]);
if(rem < 0) break;
long long sum = 0;
mx = max(mx, best[rem]);
auto it = st.begin();
for(int j = 1; j <= rem && it != st.end(); j++)
{
sum += *it;
mx = max(mx, sum+best[rem-j]);
++it;
}
rem-=2;
}
return mx;
}
long long solve2()
{
long long res = solve0();
long long a = get_big();
// printf("%lld\n", a);
reverse(A, A+n);
s = n-1-s;
long long b = get_big();
// printf("RES : %lld\n", res);
return max(a,b);
}
long long findMaxAttraction(int N, int start, int D, int attraction[])
{
n = N;
s = start;
d = D;
for(int i = 0; i < n; i++)
{
A[i] = attraction[i];
}
if(start == 0) return solve0();
else return solve2();
}
Compilation message (stderr)
holiday.cpp: In function 'long long int solve2()':
holiday.cpp:100:12: warning: unused variable 'res' [-Wunused-variable]
long long res = solve0();
^~~
# | 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... |