fork download
  1. #include <bits/stdc++.h>
  2. #define pb push_back
  3. #define mp make_pair
  4. using namespace std;
  5. typedef long long ll;
  6. ll n,k,*arr,m,**dp;
  7. ll foo(ll s,ll c){
  8. if(s>=n){return INT_MAX;}
  9. //if(c==m){return m*k;}
  10. if(c>=m){return -1;}
  11. if(dp[s][c]!=-1){return dp[s][c];}
  12. if(arr[s]==1){
  13. if(c==0){return dp[s][c]=1+min(foo(s+1,c+1),foo(s+1,c));}
  14. return dp[s][c]=min(c*k+foo(s+1,c+1),c*k+foo(s+1,c));
  15. }
  16. else{
  17. if(c==0){return dp[s][c]=1+foo(s+1,c);}
  18. else{return dp[s][c]=c*k+foo(s+1,c);}
  19. }
  20. }
  21. int main() {
  22. ll cnt=0,a1,a2;
  23. cin>>n>>m>>k;
  24. dp=(ll **)malloc(n * sizeof(ll *));
  25. for(a1=0;a1<n;++a1){
  26. dp[a1]=(ll *)malloc(sizeof(ll)*m);
  27. }
  28. for(a1=0;a1<n;++a1){
  29. for(a2=0;a2<m;++a2){
  30. dp[a1][a2]=-1;
  31. }
  32. }
  33. arr=(ll *)malloc(n*sizeof(ll));
  34. for(a1=0;a1<n;++a1){
  35. cin>>arr[a1];
  36. if(arr[a1]==1){++cnt;}
  37. }
  38. if(cnt<m){cout<<-1;return 0;}
  39. cout<<foo(0,0);
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
2147483647