#include <iostream>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 1000000
int z, n, k; i64 a[N+3];
int main()
{
ShinLena;
cin >> n >> k;
a[0] = a[n+2] = 1e18+1;
for (int i = 1; i <= n; ++i) cin >> a[i];
a[n+1] = a[0];
n += 3;
deque<pair<i64, i64>> v; deque<int> s = {0};
for (int i = 1; i < n; ++i)
{
for (; s.size() > 1 && a[s.back()] <= a[i]; )
{
i64 pv = s.back(); s.pop_back();
i64 nw = min(a[i], a[s.back()]);
if (nw == 1e18+1) break;
v.emplace_back(i - s.back() - 1, nw - a[pv]);
a[pv] = nw;
}
s.push_back(i);
}
sort(ALL(v));
for (; v.size() && v[0].first == 0; ) v.pop_front();
for (; v.size() && k >= v[0].first; )
{
i64 take = min(k / v[0].first, v[0].second);
k -= take * v[0].first;
z += 2 * take;
v.pop_front();
}
cout << z;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |