Submission #336956

# Submission time Handle Problem Language Result Execution time Memory
336956 2020-12-17T13:38:11 Z boykut K blocks (IZhO14_blocks) C++14
0 / 100
5 ms 492 KB
#include <fstream>
#include <map>
#include <vector>
#include <stack>

using namespace std;

ifstream cin("triangles.in");
ofstream cout("triangles.out");

const int inf = 2e9;

int dp[100001][101];
int a[100001];

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= K; j++) {
      dp[i][j] = inf;
    }
  }
  
  dp[0][0] = 0;
  dp[1][1] = a[1];
  for (int i = 2; i <= N; i++) {
    dp[i][1] = max(dp[i - 1][1], a[i]);
  }
  
  for (int k = 2; k <= K; k++) {
    stack < pair < int, int > > st;
    for (int i = 1; i <= N; i++) {
      int last = dp[i - 1][k - 1];
      
      while (!st.empty() && a[st.top().first] <= a[i]) {
        last = min(last, st.top().second);
        st.pop();
      }
      if (!st.empty())
        dp[i][k] = dp[st.top().first][k];
      
      dp[i][k] = min(dp[i][k], last + a[i]);
      st.push({i, last});
    }
  }
  
  cout << dp[N][K] << '\n';
  
  return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 492 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 492 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 492 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 5 ms 492 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -