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 = 1 ; i <= n ; ++i)
{
while(s.size() && arr[i] >= s.top())
s.pop() ;
if(s.size())
dp[i] = dp[s.top()] + 1ll * arr[i] * (i - s.top()) ;
else
dp[i] = 1ll * arr[i] * i ;
s.push(i) ;
}
long long ans = 4e18 ;
while(s.size())
{
ans = min(ans , dp[s.top()] + 1ll * arr[s.top()] * (n - s.top())) ;
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... |