Problem p03782 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define PB push_back
using namespace std;
typedef pair<int, int> P1;
typedef pair<int, pair<int, int>> P2;
static const int INF = 1ll << 60;
static const int dx[] = {
    1,
    -1,
    0,
    0,
};
static const int dy[] = {0, 0, 1, -1};
static const int mod = 1000000007;

int N, K;
int a[5005];
int dp[5005][5005];
vector<int> A;
vector<int> B;

bool solve(int i) {
  A.erase(A.begin() + i);
  // for(auto a:A)cout<<a<<" ";
  // cout<<endl;
  int n = A.size();
  for (int j = 0; j <= N; ++j)
    for (int k = 0; k <= K; ++k)
      dp[j][k] = 0;
  if (A[0] <= K)
    dp[0][A[0]] = 1;
  dp[0][0] = 1;
  int res = 0;
  for (int j = 1; j < n; ++j) {
    for (int k = 0; k < K; ++k) {
      if (k - A[j] < 0)
        dp[j][k] = dp[j - 1][k];
      else
        dp[j][k] = (dp[j - 1][k] || dp[j - 1][k - A[j]]);
    }
  }
  // cout<<i<<endl;
  for (int k = max(0ll, K - a[i]); k <= K; ++k) {
    if (dp[n - 1][k])
      res = 1;
    // cout<<k<<" "<<dp[n-1][k]<<endl;
  }
  A.insert(A.begin() + i, a[i]);
  if (res)
    return true;
  else
    return false;
}

signed main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    A.PB(a[i]);
    B.PB(a[i]);
  }
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(a, a + N);
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  int ans = 0;
  int l = 0, r = N - 1;
  int mid;
  // for(auto a:A)cout<<a<<" ";
  // cout<<endl;
  if (!solve(r)) {
    cout << N << endl;
    return 0;
  }
  for (int i = 0; i < 100; ++i) {
    mid = (l + r) / 2;
    if (!solve(mid))
      l = mid;
    else
      r = mid;
    // cout<<l<<" "<<r<<endl;
  }
  if (r == l) {
    if (solve(l))
      cout << l << endl;
    else
      cout << l + 1 << endl;
  }
  // cout<<l<<" "<<r<<endl;
  // int it = upper_bound(A.begin(),A.end(),A[l])-A.begin();
  // cout<<l<<" "<<r<<" "<<mid<<endl;
  else
    cout << l + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define PB push_back
using namespace std;
typedef pair<int, int> P1;
typedef pair<int, pair<int, int>> P2;
static const int INF = 1ll << 60;
static const int dx[] = {
    1,
    -1,
    0,
    0,
};
static const int dy[] = {0, 0, 1, -1};
static const int mod = 1000000007;

int N, K;
int a[5005];
int dp[5005][5005];
vector<int> A;
vector<int> B;

bool solve(int i) {
  A.erase(A.begin() + i);
  // for(auto a:A)cout<<a<<" ";
  // cout<<endl;
  int n = A.size();
  for (int j = 0; j <= N; ++j)
    for (int k = 0; k <= K; ++k)
      dp[j][k] = 0;
  if (A[0] <= K)
    dp[0][A[0]] = 1;
  dp[0][0] = 1;
  int res = 0;
  for (int j = 1; j < n; ++j) {
    for (int k = 0; k < K; ++k) {
      if (k - A[j] < 0)
        dp[j][k] = dp[j - 1][k];
      else
        dp[j][k] = (dp[j - 1][k] || dp[j - 1][k - A[j]]);
    }
  }
  // cout<<i<<endl;
  for (int k = max(0ll, K - a[i]); k <= K; ++k) {
    if (dp[n - 1][k])
      res = 1;
    // cout<<k<<" "<<dp[n-1][k]<<endl;
  }
  A.insert(A.begin() + i, a[i]);
  if (res)
    return true;
  else
    return false;
}

signed main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    cin >> a[i];
    A.PB(a[i]);
    B.PB(a[i]);
  }
  if (N == 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(a, a + N);
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  int ans = 0;
  int l = 0, r = N - 1;
  int mid;
  // for(auto a:A)cout<<a<<" ";
  // cout<<endl;
  if (!solve(r)) {
    cout << N << endl;
    return 0;
  }
  for (int i = 0; i < 20; ++i) {
    mid = (l + r) / 2;
    if (!solve(mid))
      l = mid;
    else
      r = mid;
    // cout<<l<<" "<<r<<endl;
  }
  if (r == l) {
    if (solve(l))
      cout << l << endl;
    else
      cout << l + 1 << endl;
  }
  // cout<<l<<" "<<r<<endl;
  // int it = upper_bound(A.begin(),A.end(),A[l])-A.begin();
  // cout<<l<<" "<<r<<" "<<mid<<endl;
  else
    cout << l + 1 << endl;
}