Problem p01767 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define each(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define clr(a) memset((a), 0, sizeof(a))
#define nclr(a) memset((a), -1, sizoef(a))
#define sz(s) (int)((s).size())
#define INRANGE(x, s, e) ((s) <= (x) && (x) < (e))
#define pb push_back
#define MP(x, y) make_pair((x), (y))

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const ll MAX_N = 300003LL;
const ll MAX_M = 300003LL;
vector<ll> a, suma;
ll b[MAX_M], c[MAX_M];
ll score[MAX_M];
int main() {
  a.clear(), suma.clear();
  memset(b, 0LL, sizeof(b));
  memset(c, 0LL, sizeof(c));
  memset(score, 0LL, sizeof(score));

  ios_base::sync_with_stdio(0);
  int N;
  cin >> N;
  a.resize(N + 1, 0LL);
  suma.resize(N + 1, 0LL);
  rep(i, N) cin >> a[i + 1];
  sort(ALL(a));

  suma[0] = a[0];
  for (int i = 1; i <= N; i++) {
    suma[i] = suma[i - 1] + a[i];
  } // end for

  int M;
  cin >> M;
  rep(j, M) cin >> b[j];
  rep(j, M) cin >> c[j];
  rep(j, M) {
    int ind = upper_bound(ALL(a), b[j]) - a.begin();
    score[j] = suma[ind - 1];
  } // end rep

  rep(j, M) cout << (score[j] >= c[j] ? "Yes" : "No") << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define each(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define clr(a) memset((a), 0, sizeof(a))
#define nclr(a) memset((a), -1, sizoef(a))
#define sz(s) (int)((s).size())
#define INRANGE(x, s, e) ((s) <= (x) && (x) < (e))
#define pb push_back
#define MP(x, y) make_pair((x), (y))

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const ll MAX_N = 300003LL;
const ll MAX_M = 300003LL;
ll a[MAX_N], b[MAX_M], c[MAX_M];
ll score[MAX_M];
int main() {
  memset(a, 0LL, sizeof(a));
  memset(b, 0LL, sizeof(b));
  memset(c, 0LL, sizeof(c));
  memset(score, 0LL, sizeof(score));

  ios_base::sync_with_stdio(0);
  int N;
  cin >> N;
  rep(i, N) cin >> a[i];
  int M;
  cin >> M;
  rep(j, M) cin >> b[j];
  rep(j, M) cin >> c[j];
  rep(j, M) rep(i, N) if (a[i] <= b[j]) score[j] += a[i];

  rep(j, M) cout << (score[j] >= c[j] ? "Yes" : "No") << endl;

  return 0;
}