Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #define task ""
- using namespace std;
- using ll = long long;
- using ld = long double;
- const int N = 4e2 + 2;
- int n, m, k;
- const ll Inf = 1e18;
- ll a[N], ans(1e18);
- ll dp[N][N];
- void Read(){
- cin >> n >> m >> k;
- for(int i = 1; i <= n; ++i)
- cin >> a[i];
- }
- /// dp[i][j] : số thứ i đã chọn j số
- /// TH1: Không chọn i : dp[i][j] = dp[i - 1][j] + ((i - j) % k == 0 ? 0 : a[i])
- /// TH2 : Chọn i: dp[i][j] = dp[i - 1][j - 1] + ((n - s + j) % k == 0 ? 0 : a[i]
- void Solve(){
- ll ans = Inf;
- for(int s = 0; s <= m; ++s){
- fill_n(&dp[0][0], N * N, Inf);
- dp[0][0] = 0;
- for(int i = 1; i <= n; ++i)
- for(int j = 0; j <= s; ++j){
- if(j > i){
- dp[i][j] = Inf;
- continue;
- }
- /// Don't choose i
- dp[i][j] = dp[i - 1][j] + ((i - j) % k == 0 ? 0 : a[i]);
- /// Choose i
- if(j)
- dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + ((n - s + j) % k == 0 ? 0 : a[i]));
- }
- ans = min(ans, dp[n][s]);
- }
- cout << ans;
- }
- int32_t main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0); if(fopen(task".INP", "r"))
- freopen(task".INP", "r", stdin),
- freopen(task".OUT", "w", stdout);
- Read();
- Solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment