#include <bits/stdc++.h>
#define X first
#define Y second
using namespace std;
const int N = 2e5 + 3;
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];
}
c[0] = -1e18;
c[n + 1] = -1e18;
r[n + 1] = n + 2;
ll ans = 0;
int k = n/2 + (n%2 != 0);
// cout << k << endl;
// cout << n << 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 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
556 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
812 KB |
Output is correct |
6 |
Correct |
4 ms |
812 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
1028 KB |
Output is correct |
10 |
Correct |
3 ms |
1028 KB |
Output is correct |
11 |
Correct |
4 ms |
1028 KB |
Output is correct |
12 |
Correct |
4 ms |
1028 KB |
Output is correct |
13 |
Correct |
4 ms |
1028 KB |
Output is correct |
14 |
Correct |
4 ms |
1028 KB |
Output is correct |
15 |
Correct |
6 ms |
1048 KB |
Output is correct |
16 |
Correct |
4 ms |
1068 KB |
Output is correct |
17 |
Correct |
5 ms |
1088 KB |
Output is correct |
18 |
Correct |
3 ms |
1108 KB |
Output is correct |
19 |
Correct |
3 ms |
1128 KB |
Output is correct |
20 |
Correct |
4 ms |
1148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
556 KB |
Output is correct |
4 |
Correct |
3 ms |
592 KB |
Output is correct |
5 |
Correct |
3 ms |
812 KB |
Output is correct |
6 |
Correct |
4 ms |
812 KB |
Output is correct |
7 |
Correct |
3 ms |
888 KB |
Output is correct |
8 |
Correct |
3 ms |
888 KB |
Output is correct |
9 |
Correct |
3 ms |
1028 KB |
Output is correct |
10 |
Correct |
3 ms |
1028 KB |
Output is correct |
11 |
Correct |
4 ms |
1028 KB |
Output is correct |
12 |
Correct |
4 ms |
1028 KB |
Output is correct |
13 |
Correct |
4 ms |
1028 KB |
Output is correct |
14 |
Correct |
4 ms |
1028 KB |
Output is correct |
15 |
Correct |
6 ms |
1048 KB |
Output is correct |
16 |
Correct |
4 ms |
1068 KB |
Output is correct |
17 |
Correct |
5 ms |
1088 KB |
Output is correct |
18 |
Correct |
3 ms |
1108 KB |
Output is correct |
19 |
Correct |
3 ms |
1128 KB |
Output is correct |
20 |
Correct |
4 ms |
1148 KB |
Output is correct |
21 |
Correct |
219 ms |
11564 KB |
Output is correct |
22 |
Correct |
192 ms |
13576 KB |
Output is correct |
23 |
Correct |
181 ms |
15396 KB |
Output is correct |
24 |
Correct |
110 ms |
17300 KB |
Output is correct |
25 |
Correct |
117 ms |
18988 KB |
Output is correct |
26 |
Correct |
125 ms |
20776 KB |
Output is correct |
27 |
Correct |
119 ms |
22680 KB |
Output is correct |
28 |
Correct |
119 ms |
24760 KB |
Output is correct |
29 |
Correct |
155 ms |
26636 KB |
Output is correct |
30 |
Correct |
134 ms |
28548 KB |
Output is correct |
31 |
Correct |
148 ms |
30632 KB |
Output is correct |
32 |
Correct |
154 ms |
32632 KB |
Output is correct |
33 |
Correct |
130 ms |
34284 KB |
Output is correct |
34 |
Correct |
134 ms |
36036 KB |
Output is correct |
35 |
Correct |
132 ms |
37816 KB |
Output is correct |
36 |
Correct |
160 ms |
39712 KB |
Output is correct |
37 |
Correct |
159 ms |
41636 KB |
Output is correct |
38 |
Correct |
156 ms |
43660 KB |
Output is correct |
39 |
Correct |
123 ms |
45408 KB |
Output is correct |
40 |
Correct |
105 ms |
47208 KB |
Output is correct |
41 |
Correct |
106 ms |
48932 KB |
Output is correct |
42 |
Correct |
125 ms |
50884 KB |
Output is correct |
43 |
Correct |
147 ms |
52960 KB |
Output is correct |
44 |
Correct |
125 ms |
54924 KB |
Output is correct |
45 |
Correct |
190 ms |
56856 KB |
Output is correct |
46 |
Correct |
188 ms |
58708 KB |
Output is correct |
47 |
Correct |
192 ms |
60780 KB |
Output is correct |
48 |
Correct |
123 ms |
62496 KB |
Output is correct |
49 |
Correct |
138 ms |
64360 KB |
Output is correct |
50 |
Correct |
128 ms |
65988 KB |
Output is correct |