alexjercan/bugnet huggingface dataset:
Problem p00263 (C++) — buggy submission, verdict: Runtime Error

#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main() {

  int N;
  cin >> N;
  while (N-- > 0) {
    ll s;
    cin >> hex >> s;

    bool c = (s >> 31) & 1 ? true : false;

    ll a, b, t;
    char *e;
    a = b = s;
    t = strtol("7fffffff", &e, 16);

    a &= t;
    a >>= 7;
    t = strtol("0000007f", &e, 16);
    b &= t;
    bitset<7> dou(b);
    ll new_b = 0;
    vector<ll> val(7);
    val[6] = 5000000;
    for (int i = 5; i >= 0; i--)
      val[i] = val[i + 1] * 0.5;

    for (int i = 6; i >= 0; i--) {
      if (dou[i])
        new_b += val[i];
    }
    if (c)
      cout << "-";
    cout << a;
    cout << ".";
    if (!new_b)
      cout << 0 << endl;
    else {
      cout << string(6 - (int)log10(new_b), '0');
      while (new_b / 10 * 10 == new_b)
        new_b /= 10;
      cout << new_b << endl;
    }
  }
  return 0;
}

Error: 0
8
0
80000000
80
40
c0
100
80000780
80000f70


Fixed submission:

#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int main() {

  int N;
  cin >> N;
  cerr << N << endl;
  while (N-- > 0) {
    ll s;
    cin >> hex >> s;
    cerr << hex << s << endl;
    bool c = (s >> 31) & 1 ? true : false;

    ll a, b, t;
    char *e;
    a = b = s;
    t = strtol("7fffffff", &e, 16);

    a &= t;
    a >>= 7;
    t = strtol("0000007f", &e, 16);
    b &= t;
    bitset<7> dou(b);
    ll new_b = 0;
    vector<ll> val(7);
    val[6] = 5000000;
    for (int i = 5; i >= 0; i--) {
      val[i] = val[i + 1] * 0.5;
    }

    for (int i = 6; i >= 0; i--) {
      if (dou[i])
        new_b += val[i];
    }
    if (c)
      cout << "-";
    cout << a;
    cout << ".";
    if (!new_b)
      cout << 0 << endl;
    else {
      while (new_b / 10 * 10 == new_b)
        new_b /= 10;
      cout << new_b << endl;
    }
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MAX 300000
int main() {
  bool T[MAX + 1];
  int N, Q, q, x, L[MAX + 1];

  for (int i = 0; i <= MAX; i++) {
    T[i] = false;
    L[i] = 0;
  }

  scanf("%d %d", &N, &Q);

  for (int i = 0; i < N; i++) {
    scanf("%d", &x);
    T[x] = true;
  }

  int m = 0;
  for (int i = 1; i <= MAX; i++) {
    L[i] = m;
    if (T[i])
      m = i;
  }

  for (int i = 0; i < Q; i++) {
    scanf("%d", &q);
    int maxv = 0;
    int cur = m;

    while (cur) {
      int p = cur % q;
      maxv = max(maxv, p);
      if (cur - p < 0)
        break;
      cur = L[cur - p];
    }

    printf("%d\\n", maxv);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX 15
int main() {
  bool T[MAX + 1];
  int N, Q, q, x, L[MAX + 1];

  for (int i = 0; i <= MAX; i++) {
    T[i] = false;
    L[i] = 0;
  }

  scanf("%d %d", &N, &Q);

  for (int i = 0; i < N; i++) {
    scanf("%d", &x);
    T[x] = true;
  }

  int m = 0;
  for (int i = 1; i <= MAX; i++) {
    L[i] = m;
    if (T[i])
      m = i;
  }

  for (int i = 0; i < Q; i++) {
    scanf("%d", &q);
    int maxv = 0;
    int cur = m;

    while (cur) {
      int p = cur % q;
      maxv = max(maxv, p);
      if (cur - p < 0)
        break;
      cur = L[cur - p];
    }

    printf("%d\\n", maxv);
  }
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int n, q;
  cin >> n >> q;
  int m = n;
  int a[300010];
  while (m--)
    cin >> a[m];
  sort(a, a + n);
  for (int i = 0; i < q; i++) {
    int ans = 0;
    int x;
    cin >> x;
    for (int j = x; j < 300000; j += x)
      ans = max(ans, a[lower_bound(a, a + n, j) - a - 1] % x);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main(void) {
  int n, q;
  cin >> n >> q;
  int a[300010];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int i = 0; i < q; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < n; j++) {
      ans = max(ans, a[j] % x);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;

typedef pair<bool, int> P;
const int MAX = 3000001;
int N, Q, maxi, mini;
P data[MAX];

void make() {
  int now = 0;
  for (int i = 0; i <= maxi; i++) {
    data[i].second = now;
    if (data[i].first)
      now = i;
  }
}

void input() {
  cin >> N >> Q;

  for (int i = 0; i < N; i++) {
    int n;
    scanf("%d", &n);
    data[n].first = true;
    maxi = max(maxi, n);
    mini = min(mini, n);
  }
}

int getans(int q) {
  if (q == 1)
    return 0;
  int start = mini;
  while (start % q) {
    start -= start % q;
    start += q;
  }

  int ans = 0;

  for (int i = start; i <= maxi; i += q) {
    if (ans == q - 1)
      return ans;
    ans = max(ans, data[i].second % q);
  }
  return max(ans, maxi % q);
}

void solve() {

  for (int i = 0; i < Q; i++) {
    int q;
    scanf("%d", &q);
    printf("%d\\n", getans(q));
  }
}

int main() {

  maxi = 0;
  mini = MAX;

  input();
  make();
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

typedef pair<bool, int> P;
const int MAX = 3000001;
int N, Q, maxi, mini;
P data[MAX];

void make() {
  int now = 0;
  for (int i = 0; i <= maxi; i++) {
    data[i].second = now;
    if (data[i].first)
      now = i;
  }
}

void input() {
  cin >> N >> Q;

  for (int i = 0; i < N; i++) {
    int n;
    scanf("%d", &n);
    data[n].first = true;
    maxi = max(maxi, n);
    mini = min(mini, n);
  }
}

int getans(int q) {
  int start = mini;
  while (start % q)
    start++;

  int ans = 0;

  for (int i = start; i <= maxi; i += q) {
    if (ans == q - 1)
      return ans;
    ans = max(ans, data[i].second % q);
  }
  return max(ans, maxi % q);
}

void solve() {

  for (int i = 0; i < Q; i++) {
    int q;
    scanf("%d", &q);
    printf("%d\\n", getans(q));
  }
}

int main() {
  maxi = 0;
  mini = MAX;

  input();
  make();
  solve();

  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int n, q, x, s[300003];
bool a[300003];
int sum(int l, int r) { return s[r - 1] - s[l - 1]; }
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++)
    scanf("%d", &x), a[x] = true;
  for (int i = 1; i <= 300000; i++)
    s[i] = s[i - 1] + (a[i] ? 1 : 0);
  for (int i = 0; i < q; i++) {
    scanf("%d", &x);
    int ret = 0;
    for (int j = x; j <= 300000; j += x) {
      if (sum(j - x, j) == 0)
        continue;
      int l = j - x, r = j;
      while (r - l > 1) {
        int m = (l + r) / 2;
        if (sum(m, j) != 0)
          l = m;
        else
          r = m;
      }
      // printf("%d %d\\n", j - x, l);
      ret = max(ret, l - (j - x));
    }
    printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n, q, x;
bool a[300003];
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++)
    scanf("%d", &x), a[x] = true;
  for (int i = 0; i < q; i++) {
    scanf("%d", &x);
    int ret = 0;
    for (int j = x - 1; j <= 300000; j += x) {
      bool flag = false;
      for (int k = j; k > j - x; k--) {
        if (a[k]) {
          flag = true;
          ret = max(ret, k - j + x - 1);
          break;
        }
      }
    }
    printf("%d\\n", ret);
  }
}
Problem p00265 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, P> P3;
const ll MOD = ll(1e9 + 7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(3e5 + 5);
const double EPS = 1e-4;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int n, q, cmax, cnt[MAX_N];

int solve(int m) {
  int ma = 0;
  for (int i = 0; i < cmax; i += m) {
    int bg = i + ma, en = min(i + m - 1, cmax);
    if (bg > cmax || cnt[en] - cnt[bg] <= 0)
      continue;
    ma = lower_bound(cnt + bg, cnt + en, cnt[en]) - (cnt + i);
  }
  return ma;
}

int main() {
  cin >> n >> q;
  REP(i, n) {
    int c;
    cin >> c;
    cnt[c]++;
    cmax = max(cmax, c);
  }
  REP(i, cmax) { cnt[i + 1] += cnt[i]; }
  REP(i, q) {
    int m;
    cin >> m;
    cout << solve(m) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, P> P3;
const ll MOD = ll(1e9 + 7);
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(3e5 + 5);
const double EPS = 1e-4;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

int n, q, cmax, cnt[MAX_N];

int solve(int m) {
  int ma = 0;
  for (int i = 0; i < cmax; i += m) {
    int bg = i + ma, en = min(i + m - 1, cmax);
    if (cnt[en] - cnt[bg] <= 0)
      continue;
    ma = lower_bound(cnt + bg, cnt + en, cnt[en]) - (cnt + i);
  }
  return ma;
}

int main() {
  cin >> n >> q;
  REP(i, n) {
    int c;
    cin >> c;
    cnt[c]++;
    cmax = max(cmax, c);
  }
  REP(i, cmax) { cnt[i + 1] += cnt[i]; }
  REP(i, q) {
    int m;
    cin >> m;
    cout << solve(m) << endl;
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int a[300001];
int b[3000001];

int main() {
  int n, q;
  cin >> n >> q;

  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  map<int, bool> m;
  int t;

  rep(i, n) {
    cin >> t;
    m[t] = true;
  }

  t = -1;
  rep(i, 300001) {
    b[i] = t;

    if (m[i])
      t = i;
  }

  rep(i, q) {
    int x;
    cin >> x;
    int ans = 0;

    for (int j = 0; j < 300001; j += x) {
      if (b[j] != -1) {
        ans = max(ans, b[j] % x);
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int n, q;
  cin >> n >> q;

  vector<int> v(n);
  rep(i, n) cin >> v[i];

  rep(i, q) {
    int x;
    cin >> x;
    int ans = 0;

    rep(j, v.size()) ans = max(ans, v[j] % x);
    cout << ans << endl;
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define VI vector<int>
#define VS vector<string>
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << #x << ": " << x << endl
using namespace std;
typedef pair<int, int> P;
int c[300001];
int main() {

  long long n;
  long long q;

  cin >> n >> q;

  int a;
  for (long long i = 0; i < n; i++) {
    cin >> a;
    c[a] = 1;
  }
  int b = 0;
  for (long long i = 0; i < 300001; i++) {
    if (c[i])
      b = i;
    c[i] = b;
  }
  for (long long i = 0; i < q; i++) {
    long long m;
    cin >> m;
    long long ans = 0;
    for (long long j = m - 1; j <= 300000; j += m) {
      ans = max(ans, c[j] - j + m - 1);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {

  int n, q;

  cin >> n >> q;
  vector<int> c(n);

  for (int i = 0; i < n; i++)
    cin >> c[i];
  for (int i = 0; i < q; i++) {
    int a;
    cin >> a;

    int max = -1;
    for (int j = 0; j < n; j++) {
      if (max < c[j] % a)
        max = c[j] % a;
      if (max == a - 1)
        break;
    }
    cout << max << endl;
  }
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

int N, Q;
vector<ll> c, q;

int main() {
  cin >> N >> Q;
  c.resize(N);
  q.resize(Q);
  ll tmp;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &tmp);
    c[i] = tmp;
    ;
  }
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &tmp);
    q[i] = tmp;
  }

  sort(c.begin(), c.end());
  for (ll i = 0; i < q.size(); i++) {
    ll res = 0;
    for (ll j = 1; (j - 1) * q[i] < c.back(); j++) {
      vector<ll>::reverse_iterator it;
      it = upper_bound(c.rbegin(), c.rend(), j * q[i], greater<ll>());
      if (it != c.rend())
        res = max(res, (*it) % q[i]);
    }
    printf("%lld\\n", res);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int N, Q;
vector<int> c, q;

int main() {
  cin >> N >> Q;
  c.resize(N);
  q.resize(Q);
  for (int i = 0; i < N; i++)
    cin >> c[i];
  for (int i = 0; i < N; i++)
    cin >> q[i];

  sort(c.begin(), c.end());
  for (int i = 0; i < q.size(); i++) {
    int res = 0;
    for (int j = 1; (j - 1) * q[i] < c.back(); j++) {
      vector<int>::reverse_iterator it;
      it = upper_bound(c.rbegin(), c.rend(), j * q[i], greater<int>());
      if (it != c.rend())
        res = max(res, (*it) % q[i]);
    }
    cout << res << endl;
  }
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;

int main(void) {
  int n, q;
  cin >> n >> q;
  vector<int> d(300010);
  int in;
  for (int i = 0; i < n; i++) {
    cin >> in;
    d[in] = in;
  }
  for (int i = 0; i < 300000; i++) {
    if (!d[i + 1])
      d[i + 1] = d[i];
  }
  while (q--) {
    cin >> in;

    int ret = 0;
    for (int i = in - 1; i <= 300000; i += in)
      ret = max(ret, d[i] % in);
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main(void) {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];
  int in;
  while (q--) {
    cin >> in;
    int ret = 0;
    for (int i = 0; i < n; i++)
      ret = max(ret, c[i] % in);

    cout << ret << endl;
  }
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

const int MX = 300001;
bool chck[MX];
int idx[MX];

int main() {
  int n, q, inp, lim;

  cin >> n >> q;
  lim = 0;
  while (n--) {
    cin >> inp;
    chck[inp] = true;
    lim = max(lim, inp);
  }

  int pre = 0;
  for (int i = 1; i <= lim; i++) {
    if (chck[i]) {
      idx[i] = pre;
      pre = i;
    } else
      idx[i] = pre;
  }

  while (q--) {
    int s = lim;
    int ans = 0;
    cin >> inp;

    while (s > 0) {
      ans = max(ans, s % inp);

      s = idx[s - (s % inp)];
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

const int MX = 300002;
int chck[MX];

int main() {
  int n, q, c;

  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> c;
    for (int j = 1; j <= MX; j++)
      chck[j] = max(chck[j], c % j);
  }

  while (q--) {
    int que;

    cin >> que;
    cout << chck[que] << endl;
  }

  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
int num[300010];
int main() {
  int n, q;
  scanf("%d%d", &n, &q);

  int x = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    num[x] = x;
  }

  for (int i = 0; i < 300000; i++) {
    num[i + 1] = max(num[i], num[i + 1]);
  }

  int ans = 0;
  for (int i = 0; i < q; i++) {
    scanf("%d", &x);
    ans = 0;

    for (int j = x; j < 300001; j += x) {
      ans = max(ans, num[j - 1] % x);
    }
    ans = max(ans, num[300000] % x);
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
  int n;
  int q;
  scanf("%d%d", &n, &q);

  vector<int> card;
  int x;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    card.push_back(x);
  }

  sort(card.begin(), card.end());

  int qu;
  int ans = 0;
  for (int i = 0; i < q; i++) {
    scanf("%d", &qu);
    for (int j = 0; j < card.size(); j++)
      ans = max(ans, card[j] % qu);
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded


#include "bits/stdc++.h"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

int main() {
  int n, q;
  cin >> n >> q;
  vi vec(n);
  rep(i, n) scanf("%d", &vec[i]);
  sort(all(vec));
  int vec_MAX = vec[vec.size() - 1];

  rep(i, q) {
    int query;
    scanf("%d", &query);

    int ans = 0;
    int c = 1;
    while ((c - 1) * query <= vec_MAX) {
      int pos = lower_bound(all(vec), c * query) - vec.begin();
      c++;
      if (pos == 0)
        continue;
      ans = max(ans, vec[pos - 1] % query);
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

int main() {
  int n, q;
  cin >> n >> q;
  vi vec(n);
  rep(i, n) cin >> vec[i];
  sort(all(vec));
  int vec_MAX = vec[vec.size() - 1];

  rep(i, q) {
    int query;
    cin >> query;

    int ans = 0;
    int c = 1;
    while ((c - 1) * query <= vec_MAX) {
      int pos = lower_bound(all(vec), c * query) - vec.begin();
      if (pos == 0)
        continue;
      ans = max(ans, vec[pos - 1] % query);
      c++;
    }
    cout << ans << endl;
  }
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
bool c[300010];
int N, Q, maxc = -1, ans = 0;
int s[300010];
int main() {
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    maxc = max(maxc, a);
    c[a] = true;
  }
  for (int i = 0; i <= maxc; i++) {
    if (c[i])
      s[i + 1] = i;
    else
      s[i + 1] = s[i];
  }
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    ans = 0;
    for (int d = maxc;;) {
      int p = d % q;
      ans = max(ans, p);
      if (d - p <= 0)
        break;
      d = s[d - p];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
bool c[300010];
int N, Q, maxc = -1, ans = 0;
int s[300010];
int main() {
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    maxc = max(maxc, a);
    c[a] = true;
  }
  for (int i = 0; i <= maxc; i++) {
    if (c[i])
      s[i + 1] = i;
    else
      s[i + 1] = s[i];
  }
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    ans = 0;
    for (int d = maxc; maxc;) {
      int p = d % q;
      ans = max(ans, p);
      if (d - p < 0)
        break;
      d = s[d - p];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

vector<int> card;

int main(void) {
  int N, Q, c, q;
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> c;
    card.push_back(c);
  }
  sort(card.begin(), card.end());
  int me = card[N - 1];
  for (int i = 0; i < Q; i++) {
    cin >> q;
    int M = -1;
    int u = int(1.0 * me / q + 0.9);
    for (int i = 1; i <= u; i++) {
      vector<int>::iterator it = lower_bound(card.begin(), card.end(), q * i);
      if (it != card.begin())
        M = max(M, *(it - 1) % q);
    }
    cout << M << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

vector<int> card;

int main(void) {
  int N, Q, c, q;
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> c;
    card.push_back(c);
  }

  int max;
  for (int i = 0; i < Q; i++) {
    max = 0;
    cin >> q;
    for (int j = 0; j < N; j++) {
      if (card[j] % q > max)
        max = card[j] % q;
    }
    cout << max << endl;
  }
  return 0;
}