Dang_Quan_10_Tin

WSEQ

Aug 12th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #define task ""
  5. using namespace std;
  6. using ll = long long;
  7. using ld = long double;
  8.  
  9. const int N = 4e2 + 2;
  10. int n, m, k;
  11. const ll Inf = 1e18;
  12. ll a[N], ans(1e18);
  13. ll dp[N][N];
  14.  
  15. void Read(){
  16.     cin >> n >> m >> k;
  17.     for(int i = 1; i <= n; ++i)
  18.         cin >> a[i];
  19. }
  20.  
  21. /// dp[i][j] : số thứ i đã chọn j số
  22. /// TH1: Không chọn i : dp[i][j] = dp[i - 1][j] + ((i - j) % k == 0 ? 0 : a[i])
  23. /// TH2 : Chọn i: dp[i][j] = dp[i - 1][j - 1] + ((n - s + j) % k == 0 ? 0 : a[i]
  24.  
  25. void Solve(){
  26.     ll ans = Inf;
  27.     for(int s = 0; s <= m; ++s){
  28.         fill_n(&dp[0][0], N * N, Inf);
  29.         dp[0][0] = 0;
  30.         for(int i = 1; i <= n; ++i)
  31.             for(int j = 0; j <= s; ++j){
  32.                 if(j > i){
  33.                     dp[i][j] = Inf;
  34.                     continue;
  35.                 }
  36.                 /// Don't choose i
  37.                 dp[i][j] = dp[i - 1][j] + ((i - j) % k == 0 ? 0 : a[i]);
  38.                 /// Choose i
  39.                 if(j)
  40.                     dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + ((n - s + j) % k == 0 ? 0 : a[i]));
  41.             }
  42.         ans = min(ans, dp[n][s]);
  43.     }
  44.     cout << ans;
  45. }
  46.  
  47. int32_t main(){
  48.     ios::sync_with_stdio(0);
  49.     cin.tie(0);
  50.     cout.tie(0); if(fopen(task".INP", "r"))
  51.     freopen(task".INP", "r", stdin),
  52.     freopen(task".OUT", "w", stdout);
  53.     Read();
  54.     Solve();
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment