제출 #1017779

#제출 시각아이디문제언어결과실행 시간메모리
1017779vjudge1Holding (COCI20_holding)C++17
110 / 110
58 ms16472 KiB
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll inf = 1e18;

int main()
{
  int n, L, R, k;
  cin >> n >> L >> R >> k;
  L--, R--;

  ll sm = 0;
  vector<int> v(n);
  for(int i = 0; i < n; i++)
    cin >> v[i];

  vector<int> f, s, t;
  int sf = L, ss = R - L + 1, st = n - R - 1;

  for(int i = 0; i < sf; i++)
    f.push_back(v[i]);
  
  for(int j = 0; j < ss; j++)
    sm += v[sf + j], s.push_back(v[sf + j]); 

  for(int i = 0; i < st; i++)
    t.push_back(v[ss + sf + i]);

  
  ll dp1[2][ss + 1][1 + k];
  for(int i = 0; i <= ss; i++)
    for(int j = 0; j <= k; j++)
      dp1[1][i][j] = dp1[0][i][j] = 0;
  

  for(int i = 1; i <= sf; i++)
    for(int j = 1; j <= ss; j++)
      for(int c = 0; c <= k; c++)
	{
	  bool b = i & 1, nb = (i - 1) & 1;
	  dp1[b][j][c] = max(dp1[nb][j][c], dp1[b][j - 1][c]);
	  int cost = sf + j - i;
	  if(cost <= c)
	    dp1[b][j][c] = max(dp1[b][j][c], dp1[nb][j - 1][c - cost] + (s[j - 1] - f[i - 1]));
	}


  ll dp[2][ss + 1][1 + k];
  for(int i = 0; i <= ss; i++)
    for(int j = 0; j <= k; j++)
      dp[1][i][j] = dp[0][i][j] = 0;

  for(int i = 1; i <= st; i++)
    for(int j = 1; j <= ss; j++)
      for(int c = 0; c <= k; c++)
	{
	  bool b = i & 1, nb = (i - 1) & 1;
	  dp[b][j][c] = max(dp[nb][j][c], dp[b][j - 1][c]);
	  int cost = j + i - 1;
	  if(cost <= c)
	    dp[b][j][c] = max(dp[b][j][c], dp[nb][j - 1][c - cost] + (s[ss - j] - t[i - 1]));
	}

  ll ans = 0;
  for(int i = 0; i <= ss; i ++)
    for(int c = 0; c <= k; c++)
      ans = max(ans, dp1[sf & 1][i][c] + dp[st & 1][ss - i][k - c]);
  // cerr << ans << endl;
  cout << sm - ans << endl;
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...