Submission #168157

#TimeUsernameProblemLanguageResultExecution timeMemory
168157muhammad_hokimiyonK blocks (IZhO14_blocks)C++14
0 / 100
10 ms2040 KiB
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #define fi first #define se second #define ll long long using namespace std; const int N = 1e5 + 7; const int mod = 1e9 + 7; int n,k; int a[N]; int d[111][N]; void solve() { cin >> n >> k; for( int i = 1; i <= n; i++ ){ cin >> a[i]; d[1][i] = max( d[1][i - 1] , a[i] ); } for( int i = 2; i <= k; i++ ){ stack < pair < int , int > > st; d[i][i] = d[i - 1][i - 1] + a[i]; st.push({d[i][i] - a[i] , a[i]}); for( int j = i + 1; j <= n; j++ ){ int cnt = d[i - 1][j - 1]; while( !st.empty() && st.top().se <= a[j] ){ cnt = min(cnt , st.top().fi); st.pop(); } int x = 1e9; if( !st.empty() )x = st.top().fi + st.top().se; st.push({cnt , a[j]}); d[i][j] = min(x , cnt + a[j]); } } cout << d[k][n]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen( "input.txt" , "r" , stdin ); //freopen( "output.txt" , "w" , stdout ); int t = 1;//cin >> t; while( t-- ){ 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...