Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. public static class SumOfK
  4. {
  5. public static int? chooseBestSum(int t, int k, List<int> ls) {
  6. int? ans = null;
  7. System.Console.WriteLine(ls.Count);
  8. if(k != 0 && ls.Count == 0){
  9. return null;
  10. }
  11. for(int i = 0; i < ls.Count; i++){
  12. if(k == 0){
  13. return 0;
  14. }
  15. var ls2 = new List<int>(ls);
  16. ls2.RemoveAt(i);
  17. System.Console.WriteLine(ls2.Count);
  18. var sum = chooseBestSum(t-ls[i], k - 1, ls2);
  19. if(sum + ls[i] <= t){
  20. sum +=ls[i];
  21. }else{
  22. continue;
  23. }
  24. if(ans == null || sum > ans){
  25. ans = sum;
  26. }
  27. }
  28. return ans;
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement