Advertisement
bibaboba12345

Untitled

May 25th, 2022
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <set>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int N = 505;
  10. bool poss[505][250001];
  11. vector<int> answ, pos, pos2;
  12. int a[N], n, sumAll = 0;
  13. int dp[N * N];
  14.  
  15.  
  16. int main()
  17. {
  18. #ifdef _DEBUG
  19. freopen("input.txt", "r", stdin);
  20. #else
  21. std::ios::sync_with_stdio(false);
  22. cin.tie(0);
  23. #endif
  24. cin >> n;
  25. for (int i = 0; i < n; i++) {
  26. cin >> a[i];
  27. sumAll += a[i];
  28. }
  29. sort(a, a + n);
  30. //reverse(a, a + n);
  31. for (int i = 0; i <= 500 * 500; i++) {
  32. dp[i] = 1e9;
  33. }
  34. for (int i = 0; i < n; i++) {
  35. dp[a[i]] = min(i, dp[a[i]]);
  36. }
  37. for (int sm = 0; sm <= 500 * 500; sm++) {
  38. if (dp[sm] == 1e9) {
  39. continue;
  40. }
  41. int nextInd = dp[sm] + 1;
  42. dp[sm + a[nextInd]] = min(dp[sm + a[nextInd]], nextInd); // в конец
  43. dp[sm + a[nextInd] - a[dp[sm]]] = min(dp[sm + a[nextInd] - a[dp[sm]]], nextInd); // вместо
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement