Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3. #define FOR(i,m,n) for(int i = (m); i < (n); i++)
  4. #define ROF(i,m,n) for(int i = (n)-1; i >= (m); i--)
  5. typedef long long LL;
  6. typedef unsigned long long ULL;
  7. typedef vector<int> VI;
  8. typedef vector<LL> VLL;
  9. #define SZ(x) ((int)(x).size())
  10. #define MP make_pair
  11. typedef pair<int,int> PII;
  12. typedef pair<LL,LL> PLL;
  13. #define A first
  14. #define B second
  15.  
  16. const int N = 2005;
  17. const int D = 21;
  18.  
  19. int n, d;
  20. int p[N], a[N];
  21. int dp[N][D];
  22.  
  23. inline int cost(int i, int j) {
  24. int sum = p[j];
  25. if (i > 0) {
  26. sum -= p[i - 1];
  27. }
  28. int d = sum % 10;
  29. if (d >= 5) {
  30. return sum += (10 - d);
  31. } else {
  32. return sum - d;
  33. }
  34. }
  35.  
  36. int main() {
  37. freopen ("inp.in", "r", stdin);
  38. ios :: sync_with_stdio(false);
  39. cin >> n >> d;
  40. for (int i = 1; i <= n; i++) {
  41. cin >> a[i];
  42. p[i] = p[i - 1] + a[i];
  43. }
  44. for (int i = 0; i <= n; i++) {
  45. dp[i][0] = cost(0, i);
  46. }
  47. for (int i = 1; i <= n; i++) {
  48. for (int j = 1; j <= d; j++) {
  49. dp[i][j] = 1e9;
  50. for (int k = 1; k < i; k++) {
  51. dp[i][j] = min(dp[i][j], dp[k][j - 1] + cost(k + 1, i));
  52. }
  53. }
  54. }
  55. cout << dp[n][d] << endl;
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement