fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define pb push_back
  5. #define ll long long
  6. #define bit(n, i) ((n >> i) & 1)
  7. #define all(a) a.begin(), a.end()
  8. #define rep(i, x, n) for (int i = x; i <= n; ++i)
  9. #define red(i, x, n) for (int i = x; i >= n; --i)
  10. #define inp(a) if (fopen(a".inp", "r")) freopen(a".inp", "r", stdin), freopen(a".out", "w", stdout)
  11.  
  12. template<class A, class B> inline bool maxi(A &x, B y) {return (x < y ? x = y, 1 : 0);};
  13. template<class A, class B> inline bool mini(A &x, B y) {return (x > y ? x = y, 1 : 0);};
  14.  
  15. const int N = 30 + 5;
  16. const ll MOD = 1e9 + 7;
  17. const ll INF = LLONG_MAX;
  18.  
  19. // main program
  20.  
  21. const int MAX = (4 * N) * (4 * N);
  22.  
  23. int n;
  24. int sum = 0;
  25. bool dp[N][MAX];
  26. vector<int> s[N];
  27. int trace[N][MAX], takeID[N][MAX];
  28. vector<pair<int, int>> a[N], id[N];
  29.  
  30. void init(){
  31. rep(c, 1, n) rep(i, 0, 3) rep(j, i + 1, 3){
  32. s[c].push_back(a[c][i].first + a[c][j].first);
  33. id[c].push_back({a[c][i].second, a[c][j].second});
  34. }
  35. }
  36. signed main(){
  37. cin.tie(0) -> sync_with_stdio(0);
  38.  
  39. cin >>n;
  40.  
  41. rep(i, 1, 4 * n){
  42. int x, c; cin >>x >>c;
  43.  
  44. a[c].push_back({x, i}); sum += x;
  45. }
  46.  
  47. init(); dp[0][0] = 1;
  48.  
  49. rep(i, 1, n) rep(k, 0, sum){
  50. rep(t, 0, 5){
  51. int w = s[i][t];
  52.  
  53. if (k >= w && dp[i - 1][k - w]){
  54. dp[i][k] = 1;
  55. trace[i][k] = k - w;
  56. takeID[i][k] = t;
  57. }
  58. }
  59. }
  60.  
  61. int last = 0;
  62. int mi = INT_MAX;
  63.  
  64. rep(k, 0, sum) if (dp[n][k]){
  65. int dif = abs(sum - 2 * k);
  66.  
  67. if (mi > dif){
  68. last = k;
  69. mi = dif;
  70. }
  71. }
  72.  
  73. vector<int> ans;
  74.  
  75. red(i, n, 1){
  76. int t = takeID[i][last];
  77. ans.push_back(id[i][t].first);
  78. ans.push_back(id[i][t].second);
  79.  
  80. last = trace[i][last];
  81. }
  82.  
  83. sort(all(ans));
  84. for (int i: ans) cout <<i <<' ';
  85.  
  86. return 0;
  87. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty