fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. const int num = 4;
  6. int times[num] = { 1, 2, 5, 10 };
  7. int dp[2][num];
  8.  
  9. int solve(int dir, int n)
  10. {
  11. if (0 < dp[dir][n]) return dp[dir][n];
  12. if (n < 2) return times[!dir && n];
  13.  
  14. dp[dir][n] = 99999;
  15. if (dir)
  16. {
  17. if (2 < n) dp[dir][n] = min(dp[dir][n], solve(0, n - 2) + times[1] + times[n]);
  18. dp[dir][n] = min(dp[dir][n], solve(1, n - 1) + times[0] + times[n]);
  19. }
  20. else
  21. dp[dir][n] = min(dp[dir][n], solve(1, n) + times[0] + times[1]);
  22.  
  23. return dp[dir][n];
  24. }
  25.  
  26. int main()
  27. {
  28. sort(times, times + num);
  29. cout << solve(0, num - 1) << endl;
  30.  
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
17