Submission #288475

#TimeUsernameProblemLanguageResultExecution timeMemory
288475BlagojceDischarging (NOI20_discharging)C++11
11 / 100
137 ms17040 KiB
#include <bits/stdc++.h> #define fr(i, n, m) for(int i = (n); i < (m); i ++) #define pb push_back #define st first #define nd second #define pq priority_queue #define all(x) begin(x), end(x) #include <time.h> #include <cmath> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; const int i_inf = 1e9; const ll inf = 1e18; const ll mod = 1000000007; const ld eps = 1e-13; const ld pi = 3.14159265359; mt19937 _rand(time(NULL)); clock_t timer = clock(); const int mxn = 2e5+5; const ll A = 911382323; const ll B = 972663749; void solve(){ int n; cin >> n; ll a[n]; fr(i, 0, n){ cin >> a[i]; } stack<int> S; pq<pair<ll, int> > Q; bool active[n]; memset(active, false, sizeof(active)); ll dp[n+1]; dp[n] = 0; for(int i = n-1; i >= 0; i --){ while(!S.empty() && a[S.top()] <= a[i]){ active[S.top()] = false; S.pop(); } while(!Q.empty() && !active[Q.top().nd]){ Q.pop(); } dp[i] = a[i]*(n-i); if(!Q.empty()){ dp[i] += -(int)Q.top().st; } Q.push({-dp[i], i}); active[i] = true; S.push(i); } ll mx = 0; ll ans = inf; fr(i, 0, n){ mx = max(mx, a[i]); ans = min(ans, dp[i+1]+mx*n); } cout<<ans<<endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int tc = 1; //cin >> tc; while(tc--) solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...