This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std ;
const int MAX = 1e6 + 10 ;
int arr[MAX] ;
int n ;
long long dp[MAX] ;
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 1 ; i <= n ; ++i)
cin>>arr[i] ;
stack<int>s ;
for(int i = n ; i >= 1 ; --i)
{
while(s.size() && arr[i] >= arr[s.top()])
s.pop() ;
if(s.size())
dp[i] = dp[s.top()] ;
dp[i] += 1ll * arr[i] * (n-i+1ll) ;
s.push(i) ;
}
long long ans = 4e18 ;
while(s.size())
{
ans = min(ans , dp[s.top()] + 1ll * arr[s.top()] * (s.top() - 1ll)) ;
s.pop() ;
}
return cout<<ans<<"\n" , 0 ;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |