Submission #102743

# Submission time Handle Problem Language Result Execution time Memory
102743 2019-03-27T11:27:09 Z Mahmoud_Adel K blocks (IZhO14_blocks) C++14
0 / 100
8 ms 512 KB
#include <bits/stdc++.h>
using namespace std;
#define s second
#define f first
typedef long long ll;
const int N = 100005, M = 105;
int n, k, a[N], dp[N][M], ch[N][M];
int main()
{
    cin >> n >> k;
    for(int i=1; i<=n; i++) cin >> a[i];
    dp[0][0] = 0;
    for(int i=1; i<=n; i++) dp[1][i] = max(dp[1][i-1], a[i]), ch[1][i] = dp[1][i];
    for(int i=1; i<=k; i++)
    {
        stack<int> s;
        for(int j=i; j<=n; j++)
        {
            dp[i][j] = dp[i-1][j-1]+ a[j], ch[i][j] = a[j];
            while(!s.empty())
            {
                int u = s.top();
                if(a[u] <= a[j])
                {
                    if(dp[i][j] > dp[i][u] - ch[i][u] + max(ch[i][u], a[j]))
                    dp[i][j] = dp[i][u] - ch[i][u] + max(ch[i][u], a[j]), ch[i][j] = max(ch[i][u], a[j]);
                }
                else break;
                s.pop();
            }
            if(!s.empty())
            {
                int u = s.top();
                if(dp[i][u] < dp[i][j])
                {
                    dp[i][j] = dp[i][u];
                    ch[i][j] = ch[i][u];
                }
            }
            s.push(i);
        }
    }
    cout << dp[k][n] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Incorrect 2 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Incorrect 3 ms 384 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 512 KB Output isn't correct
2 Halted 0 ms 0 KB -