# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
100510 | MohamedAhmed0 | K blocks (IZhO14_blocks) | C++14 | 152 ms | 40976 KiB |
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;
int main()
{
int n , k ;
scanf("%d %d" , &n , &k) ;
int arr[n+1] ;
for(int i = 1 ; i <= n ; ++i)
scanf("%d" , &arr[i]);
int dp[k+1][n+1] ;
memset(dp , 0x3f3f3f , sizeof(dp));
dp[1][0] = -1e9 ;
for(int j = 1 ; j <= n ; ++j)
dp[1][j] = max(arr[j] , dp[1][j-1]) ;
for(int i = 2 ; i <= k ; ++i)
{
vector< pair<int , int> >v ;
for(int j = i ; j <= n ; ++j)
{
int now = dp[i-1][j-1] ;
int MAX = arr[j] ;
while(v.size() > 0)
{
pair<int , int>p = v.back();
if(p.first + p.second >= now + MAX)
break;
if(p.first + max(p.second , MAX) < now+MAX )
now = p.first , MAX = max(MAX , p.second) ;
v.pop_back();
}
if(v.size() == 0)
v.push_back({now , MAX}) ;
else if(v.back().second < MAX)
v.push_back({now , MAX}) ;
dp[i][j] = now + MAX ;
}
}
return printf("%d\n" , dp[k][n]) , 0 ;
}
Compilation message (stderr)
# | 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... |