#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;
ll 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;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |