fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31. vector<int> b;
  32.  
  33. int consistency(int n, int k){
  34.  
  35. bool dp[n+1][k+1][151][151] = {false};
  36.  
  37. dp[0][0][0][0] = true;
  38. for(int i = 1; i <= n; i++) {
  39. dp[i][0][0][0] = true;
  40. }
  41.  
  42. for(int i=1; i<=n; i++){
  43. for(int j=1; j<=k; j++){
  44. for(int s1=0; s1<=150; s1++){
  45. for(int s2=0; s2<=150; s2++){
  46. int x = a[i-1];
  47. int y = b[i-1];
  48.  
  49. dp[i][j][s1][s2] = dp[i-1][j][s1][s2];
  50.  
  51. if(s1-x>= 0 && s2-y >= 0)
  52. dp[i][j][s1][s2] = dp[i][j][s1][s2] || dp[i-1][j-1][s1-x][s2-y];
  53. }
  54. }
  55. }
  56. }
  57.  
  58. int ans = -INF;
  59.  
  60. for(int s1=0; s1<=150; s1++){
  61. for(int s2=0; s2<=150; s2++){
  62. if(dp[n][k][s1][s2]){
  63. ans = max(ans, min(s1,s2));
  64. }
  65. }
  66. }
  67.  
  68. return ans;
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. int practice(int n, int k){
  86.  
  87.  
  88. return 0;
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95. void solve() {
  96.  
  97. int n, k;
  98. cin>> n >> k;
  99.  
  100. a.resize(n);
  101. for(int i=0; i<n; i++) cin >> a[i];
  102.  
  103. b.resize(n);
  104. for(int i=0; i<n; i++) cin >> b[i];
  105.  
  106. cout << consistency(n, k) << endl;
  107.  
  108.  
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115. int32_t main() {
  116. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  117.  
  118. int t = 1;
  119. // cin >> t;
  120. while (t--) {
  121. solve();
  122. }
  123.  
  124. return 0;
  125. }
Success #stdin #stdout 0.01s 5312KB
stdin
5 1
1 5 8 10 88
31 25 15 34 38
stdout
38