제출 #13631

#제출 시각아이디문제언어결과실행 시간메모리
13631gs14004등산 경로 (IZhO12_route)C++14
95 / 100
228 ms13548 KiB
#include <cstdio> #include <algorithm> #include <stack> using namespace std; int n,k,a[1000005]; int cost[1000005]; stack<int> s,t; int main(){ scanf("%d %d",&n,&k); for (int i=0; i<n; i++) { scanf("%d",&a[i]); } int p = (int)(max_element(a,a+n) - a); for (int i=p; i<=p+n; i++) { if(s.empty() || s.top() >= a[i%n]){ s.push(a[i%n]); t.push(i); } else{ while (!s.empty() && s.top() < a[i%n]) { int ptop = s.top(); s.pop(); t.pop(); cost[i - t.top() - 1] += min(s.top(),a[i%n]) - ptop; } s.push(a[i%n]); t.push(i); } } int ret = 0; for (int i=1; i<=n && i<=k; i++) { int change = min(cost[i],k/i); ret += change * 2; k -= change * i; } printf("%d",ret); }
#Verdict Execution timeMemoryGrader output
Fetching results...