Submission #471400

#TimeUsernameProblemLanguageResultExecution timeMemory
471400xhuy8248K blocks (IZhO14_blocks)C++14
100 / 100
175 ms1632 KiB
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define TASK "XHUY"

const int N = 1e5 + 5;

int n, k, a[N], dP[N], M[N];

int main()
{
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> n >> k;
    FOR(i, 1, n)
        cin >> a[i], dP[i] = max(dP[i - 1], a[i]);
    FOR(i, 2, k)
    {
        stack <int> st;
        FOR(j, i, n)
            M[j] = dP[j - 1];
        FOR(j, i, n)
        {
            while (!st.empty() && a[st.top()] <= a[j])
            {
                M[j] = min(M[j], M[st.top()]);
                st.pop();
            }
            dP[j] = M[j] + a[j];
            if (!st.empty())
                dP[j] = min(dP[j], dP[st.top()]);
            st.push(j);
        }
    }
    cout << dP[n];
    return 0;
}

Compilation message (stderr)

blocks.cpp: In function 'int main()':
blocks.cpp:14:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:15:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...