QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#548#206576#7403. Subset Sum5sbRedDiamondSuccess!2024-02-29 18:13:262024-02-29 18:13:26

详细

Extra Test:

Time Limit Exceeded

input:

1
20000 1000001
20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20000 20...

output:


result:


ID题目提交者结果用时内存语言文件大小提交时间测评时间
#206576#7403. Subset SumRedDiamondTL 3ms4172kbC++23906b2023-10-07 21:23:302024-02-29 18:13:46

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = (int) 2e4 + 7;
int n, a[N];
ll suf[N], c, sol = 0;

void solve(int pos, ll sum = 0) {
  if (sum > c || sol == c) return;
  sol = max(sol, sum);
  if (pos == n + 1) return;
  if (sum + suf[pos] <= sol) return;
  solve(pos + 1, sum + a[pos]);
  if (sum + suf[pos + 1] > sol) solve(pos + 1, sum);
}

signed main() {
#ifdef ONPC
  freopen("input.txt", "r", stdin);
#else
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC

  int tt;
  cin >> tt;
  while (tt--) {
    cin >> n >> c;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);
    suf[n + 1] = 0;
    for (int i = n; i >= 1; i--) {
      suf[i] = suf[i + 1] + a[i];
    }
    sol = 0;
    solve(1);
    cout << sol << "\n";
  }
  return 0;
}