fork(1) download
  1. def getmin(A, B, K):
  2. A.sort()
  3. B.sort(reverse=True)
  4. sum_ = 0
  5. for k in range(K):
  6. if A and B and A[-1]>B[-1]:
  7. sum_ += B[-1]
  8. A.pop()
  9. B.pop()
  10. else:
  11. break
  12. sum_ += sum(A)
  13. return sum_
  14.  
  15. for t in range(int(input())):
  16. N, K = map(int, input().split())
  17. X = list(map(int, input().split()))
  18. best = 10**9
  19. for i in range(N):
  20. for j in range(i, N):
  21. inside, outside = [], []
  22. for k in range(N):
  23. if i<=k and k<=j:
  24. inside.append(X[k])
  25. else:
  26. outside.append(X[k])
  27. sum_ = getmin(inside, outside, K)
  28. best = min(best, sum_)
  29. print(best)
Success #stdin #stdout 0.01s 28384KB
stdin
2
3 2
1 -5 2
4 1
3 -2 1 -1
stdout
-5
-3