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 <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
const int N = 2e5 + 2;
typedef long long ll;
typedef pair<int,int> ii;
struct cmp
{
bool operator() (pair<ii, ll> a, pair <ii, ll> b)
{
return a.Y < b.Y;
}
};
int n;
int a[N];
int r[N], l[N];
priority_queue <pair<ii, ll>, vector<pair<ii, ll> > , cmp > q;
ll c[N];
void home()
{
ios_base::sync_with_stdio(0);
//freopen("a.inp", "r", stdin);
//freopen("a.out", "w", stdout);
}
int main()
{
home();
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i], q.push(make_pair(ii(i, i + 1), a[i]));
r[i] = i + 1;
l[i + 1] = i;
c[i] = a[i];
}
ll ans = 0;
int k = n/2 + (n%2 != 0);
// cout << k << endl;
for (int i = 1; i <= k; ++i)
{
pair <ii, ll> u;
while (1 && !q.empty())
{
// cout << i << endl;
u = q.top();
q.pop();
if (r[u.X.X] != u.X.Y || u.X.X != l[u.X.Y]) continue;
ans += u.Y;
r[l[u.X.X]] = r[u.X.Y];
l[r[u.X.Y]] = l[u.X.X];
c[l[u.X.X]] = c[l[u.X.X]] - c[u.X.X] + c[u.X.Y];
q.push(make_pair(ii(l[u.X.X], r[u.X.Y]), c[l[u.X.X]]));
// cout << l[u.X.X] <<' ' << r[u.X.Y] <<' ' << c[l[u.X.X]] << endl;
cout << ans << '\n';
break;
//cout << 1;
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |