제출 #489268

#제출 시각아이디문제언어결과실행 시간메모리
489268SirCovidThe19th등산 경로 (IZhO12_route)C++17
100 / 100
542 ms65536 KiB
#include <bits/stdc++.h> using namespace std; const int mx = 1e6 + 5; int n, k, A[mx], ans; vector<int> range[mx]; vector<int> stk; int main(){ cin >> n >> k; for (int i = 1; i <= n; i++){ cin >> A[i]; while (!stk.empty() and A[stk.back()] <= A[i]) stk.pop_back(); stk.push_back(i); } for (int i = 1; i <= n; i++){ int mxTop = 0; while (!stk.empty()){ int L = stk.back(); if (L < i) range[i - L - 1].push_back(min(A[L], A[i]) - mxTop); else range[n - (L - i + 1)].push_back(min(A[L], A[i]) - mxTop); if (A[L] > A[i]) break; mxTop = max(mxTop, A[L]); stk.pop_back(); } stk.push_back(i); } for (long long len = 1; len <= n; len++){ for (int lim : range[len]){ if (len * lim >= k) ans += 2 * (k / len), k = 0; else ans += 2 * lim, k -= len * lim; } } cout<<ans<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...