Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int N, S, M, ans = -1;
  4. int V[100];
  5. int dp[100][1000];
  6.  
  7. void play(int idx, int vol) {
  8. if (idx < N) {
  9. if (vol + V[idx] <= M) {
  10. if (!dp[idx][vol + V[idx]]) {
  11. dp[idx][vol +V[idx]] = 1;
  12. play(idx + 1, vol + V[idx]);
  13. }
  14. }
  15.  
  16. if (vol - V[idx] >= 0) {
  17. if (!dp[idx][vol - V[idx]]) {
  18. dp[idx][vol - V[idx]] = 1;
  19. play(idx + 1, vol - V[idx]);
  20. }
  21. }
  22. } else {
  23. if (vol > ans) ans = vol;
  24. }
  25. }
  26.  
  27. int main() {
  28. scanf("%d %d %d", &N, &S, &M);
  29.  
  30. for (int i = 0; i < N; i++) {
  31. scanf("%d", &V[i]);
  32. }
  33.  
  34. play(0, S);
  35.  
  36. printf("%d\n", ans);
  37.  
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement