alexjercan/bugnet huggingface dataset:
Problem p01341 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

struct node {
  int x, y, id;
};
struct edge {
  int u, v;
  double cost;
  edge() {}
  edge(int x, int y, double z) {
    u = x;
    v = y;
    cost = z;
  }
  bool operator<(const edge &a) const { return cost > a.cost; }
};

edge es[50010];
int par[10010];
node p[10010];
int n, m;
void init() {
  for (int i = 1; i <= n; i++)
    par[i] = i;
}

int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x != y)
    par[x] = y;
}

double dis(int a, int b) {
  return sqrt(1.0 * (p[a].x - p[b].x) * (p[a].x - p[b].x) +
              1.0 * (p[a].y - p[b].y) * (p[a].y - p[b].y));
}

double kruskal() {
  sort(es, es + m);
  // for(int i=0;i<m;i++) printf("%d %d %lf\\n",es[i].u,es[i].v,es[i].cost);
  double s = 0;
  for (int i = 0; i < m; i++) {
    edge e = es[i];
    if (find(e.u) != find(e.v)) {
      unite(e.u, e.v);
    } else {
      s += e.cost;
    }
  }
  return s;
}
int main() {
  // freopen("a.txt","r",stdin);
  int a, b;
  double c, sum;
  while (~scanf("%d%d", &n, &m)) {
    init();
    sum = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &p[i].x, &p[i].y);
    }
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &a, &b);
      c = dis(a, b);
      es[i] = edge(a, b, c);
    }
    printf("%.3lf\\n", kruskal());
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fi first
#define se second
#define pi acos(-1)
#define inf 0x3f3f3f3f
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define rep(i, x, n) for (int i = x; i < n; i++)
#define per(i, n, x) for (int i = n; i >= x; i--)
using namespace std;
typedef pair<int, int> P;
const int MAXN = 100010;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
P p[MAXN];
double Dis(P &a, P &b) {
  return sqrt((a.fi - b.fi) * (a.fi - b.fi) + (a.se - b.se) * (a.se - b.se));
}
struct node {
  int u, v;
  double w;
  node(int _u = 0, int _v = 0, double _w = 0) : u(_u), v(_v), w(_w) {}
  bool operator<(node a) const { return w > a.w; }
} mp[MAXN * 1000];
int f[MAXN];
int getf(int k) { return k == f[k] ? k : f[k] = getf(f[k]); }
double kruskal(int n) {
  int i = 0, cnt = 1;
  double sum = 0;
  while (i < n && cnt < n) {
    int u = getf(mp[i].u);
    int v = getf(mp[i].v);
    if (u != v) {
      f[u] = v;
      cnt++;
      sum += mp[i].w;
    }
    i++;
  }
  return sum;
}
int main() {
  int n, m, u, v;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", &p[i].fi, &p[i].se);
    f[i] = i;
  }
  double sum = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &u, &v);
    mp[i] = node(u, v, Dis(p[u], p[v]));
    sum += Dis(p[u], p[v]);
  }
  sort(mp, mp + m);
  cout << fixed << setprecision(3) << sum - kruskal(n) << endl;
  return 0;
}
Problem p01341 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

struct edge {
  int u, v;
  double cost;
};

bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

edge *es;

int rak[10001], par[10001];
int x[10001], y[10001];

void init(int n) {
  for (int i = 1; i <= n; i++) {
    par[i] = i;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (rak[x] < rak[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rak[x] == rak[y])
      rak[x]++;
  }
}

bool same(int x, int y) { return find(x) == find(y); }

double kruskal(int n, int r) {
  sort(es, es + r, comp);
  init(n);
  double res = 0;
  for (int i = 0; i < r; i++) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
      res += e.cost;
    }
  }
  return res;
}

int main() {

  int N, M;
  cin >> N >> M;

  es = new edge[M];

  for (int i = 1; i <= N; i++) {

    cin >> x[i] >> y[i];
  }

  double mx = 0;

  for (int i = 0; i < M; i++) {

    int a, b;
    cin >> a >> b;

    int p = (x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b]);
    double c = sqrt(p);

    edge e1 = {a, b, -c};
    es[i] = e1;

    mx += c;
  }

  printf("%.8f\\n", mx + kruskal(N, M));
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;
bool cost[10001][10001];
double mincost[10001];
bool used[10001];
const double INF = 100000000;
int x[10001], y[10001];

double prim(int V) {
  for (int i = 1; i <= V; ++i) {
    mincost[i] = INF;
    used[i] = false;
  }

  mincost[1] = 0;
  double res = 0;

  while (true) {
    int v = -1;
    for (int u = 1; u <= V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v]))
        v = u;
    }

    if (v == -1)
      break;
    used[v] = true;
    res += mincost[v];

    for (int u = 1; u <= V; u++) {
      int p = (x[v] - x[u]) * (x[v] - x[u]) + (y[v] - y[u]) * (y[v] - y[u]);
      if (cost[v][u])
        mincost[u] = min(mincost[u], -sqrt(p));
      else
        mincost[u] = min(mincost[u], 0.0);
    }
  }
  return res;
}

int main() {

  int N, M;
  cin >> N >> M;

  for (int i = 1; i <= N; i++) {

    cin >> x[i] >> y[i];
  }

  double mx = 0;

  for (int i = 0; i < M; i++) {

    int a, b;
    cin >> a >> b;

    cost[a][b] = true;
    cost[b][a] = true;

    int p = (x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b]);
    mx += sqrt(p);
  }

  printf("%.8f\\n", mx + prim(N));
}
Problem p01341 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;

struct node {
  int x, y, id;
};
struct edge {
  int u, v;
  double cost;
  edge() {}
  edge(int x, int y, double z) {
    u = x;
    v = y;
    cost = z;
  }
  bool operator<(const edge &a) const { return cost > a.cost; }
};

edge es[50010];
int par[10010];
node p[10010];
int n, m;
void init() {
  for (int i = 1; i <= n; i++)
    par[i] = i;
}

int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x != y)
    par[x] = y;
}

double dis(int a, int b) {
  return sqrt(1.0 * (p[a].x - p[b].x) * (p[a].x - p[b].x) +
              1.0 * (p[a].y - p[b].y) * (p[a].y - p[b].y));
}

double kruskal() {
  sort(es, es + m);
  // for(int i=0;i<m;i++) printf("%d %d %lf\\n",es[i].u,es[i].v,es[i].cost);
  double s = 0;
  for (int i = 0; i < m; i++) {
    edge e = es[i];
    if (find(e.u) != find(e.v)) {
      unite(e.u, e.v);
      s += e.cost;
    }
  }
  return s;
}
int main() {
  // freopen("a.txt","r",stdin);
  int a, b;
  double c, sum;
  while (~scanf("%d%d", &n, &m)) {
    init();
    sum = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &p[i].x, &p[i].y);
    }
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &a, &b);
      c = dis(a, b);
      sum += c;
      es[i] = edge(a, b, c);
    }
    printf("%.3lf\\n", sum - kruskal());
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
using namespace std;
const int maxn = 1e4 + 5;
typedef long long LL;
struct Point {
  int x;
  int y;
};

Point a[maxn];
double cost[maxn][maxn];
bool used[maxn];
int n, m;
double maxcost[maxn];
double cal_distance(Point a, Point b) {
  return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double prim(int p) {
  memset(used, 0, sizeof(used));
  for (int i = 0; i <= n; i++)
    maxcost[i] = -1;
  double ans = 0.0;
  maxcost[p] = 0;
  while (1) {
    int v = -1;
    for (int u = 1; u <= n; u++)
      if (!used[u] && (v == -1 || maxcost[u] > maxcost[v]))
        v = u;
    if (v == -1)
      break;
    used[v] = 1;
    ans += maxcost[v];
    for (int u = 1; u <= n; u++)
      maxcost[u] = max(maxcost[u], cost[v][u]);
  }
  return ans;
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    for (int i = 1; i <= n; i++)
      scanf("%d%d", &a[i].x, &a[i].y);
    memset(cost, 0, sizeof(cost));
    int p1, p2;
    double sum = 0;
    for (int i = 1; i <= m; i++) {
      scanf("%d%d", &p1, &p2);
      sum += cost[p1][p2] = cost[p2][p1] = cal_distance(a[p1], a[p2]);
    }
    //        for(int i=1;i<=n;cout<<endl,i++)
    //            for(int j=1;j<=n;j++)
    //             cout<<cost[i][j]<<" ";
    //        cout<<sum<<endl;
    double ans = (double)10000 * sqrt(2);
    for (int u = 1; u <= n; u++)
      ans = min(ans, sum - prim(u));
    printf("%.3f\\n", ans);
  }
  return 0;
}
Problem p01341 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define INF 1000000000
#define M 10005
#define LL long long
using namespace std;
struct node {
  int x, y;
  double z;
};
node B[M];
vector<node> A;
int fa[M];
double dis(int x, int y) {
  int dx = B[x].x - B[y].x, dy = B[x].y - B[y].y;
  return sqrt(dx * dx + dy * dy);
}
int get(int x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = get(fa[x]);
}
bool cmp(node a, node b) { return a.z > b.z; }
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m) != EOF) {
    int j, k;
    A.clear();
    for (j = 1; j <= n; j++) {
      fa[j] = j;
    }
    for (j = 1; j <= n; j++)
      scanf("%d%d", &B[j].x, &B[j].y);
    double ans = 0;
    for (j = 1; j <= m; j++) {
      int x, y;
      scanf("%d%d", &x, &y);
      double z = dis(x, y);
      // printf("%f\\n",z);
      ans += z;
      A.push_back((node){x, y, z});
    }
    sort(A.begin(), A.end(), cmp);
    for (j = 0; j < A.size(); j++) {
      int a = get(A[j].x), b = get(A[j].y);
      if (a != b) {
        fa[a] = b;
        ans -= A[j].z;
      }
    }
    printf("%.3f\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define mem(a) memset(a, 0, sizeof(a))
#define pi acos(-1)
using namespace std;
// 并?集部分
int par[10005], ran[10005];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    ran[i] = 0;
  }
}
int find(int x) {
  if (par[x] == x)
    return x;
  else {
    return par[x] = find(par[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (ran[x] < ran[y]) {
    par[x] = y;
  } else
    par[y] = x;
  if (ran[x] == ran[y])
    ran[x]++;
}
bool same(int x, int y) { return find(x) == find(y); }
//
// 克?斯????
struct edge {
  int u, v;
  double cost;
  edge(int u = 0, int v = 0, double cost = 0) : u(u), v(v), cost(cost) {}
  bool operator<(const edge &e2) const { return cost > e2.cost; }
};

edge es[50000005];
int V, E;
double kruskal() {
  sort(es, es + E); // 按照??从小到大排序
  init(V);
  double res = 0;
  for (int i = 0; i < E; ++i) {
    edge e = es[i];
    if (!same(e.u, e.v)) {
      unite(e.u, e.v);
    } else {
      res += e.cost;
    }
  }

  return res;
}

pair<int, int> pile[10005];
//
// 主程序
int main() {
  cin >> V >> E;
  for (int i = 0; i < V; i++) {
    cin >> pile[i].first >> pile[i].second;
  }
  for (int i = 0; i < E; i++) {
    cin >> es[i].u >> es[i].v;
    --es[i].u;
    --es[i].v;
    int dx = pile[es[i].u].first - pile[es[i].v].first;
    int dy = pile[es[i].u].second - pile[es[i].v].second;
    es[i].cost = sqrt(dx * dx + dy * dy);
  }
  cout.setf(ios::showpoint);
  cout.precision(3);
  cout.setf(ios::fixed);
  cout << kruskal() << endl;
}
Problem p01341 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
const int N = 1000100;
struct node {
  int l, r;
  double cost;
} p[N];
double x[N], y[N];
int f[N];
int Find(int x) {
  if (x == f[x])
    return x;
  else {
    f[x] = Find(f[x]);
    return f[x];
  }
}
int merg(int a, int b) {
  int t1 = Find(a);
  int t2 = Find(b);
  if (t1 != t2) {
    f[t2] = t1;
    return 0;
  }
  return 1;
}
int cmp(node a, node b) {
  return a.cost > b.cost; //?是从大到小排的
}
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    int i, j;
    for (i = 1; i <= n; i++) {
      scanf("%lf%lf", &x[i], &y[i]);
    }
    double s = 0;
    for (i = 0; i < m; i++) {
      scanf("%d%d", &p[i].l, &p[i].r);
      int a = p[i].l, b = p[i].r;
      p[i].cost =
          sqrt((x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b]));
      s += p[i].cost;
    }
    for (i = 1; i <= n; i++) {
      f[i] = i;
    }
    sort(p, p + m, cmp);
    double sum = 0;
    for (i = 0; i < m; i++) {
      if (!merg(p[i].l, p[i].r))
        sum += p[i].cost;
    }
    printf("%.3lf\\n", s - sum);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int maxn = 10010;
int N, M;
pair<int, int> piles[maxn];

double cal(int a, int b) {
  return sqrt((piles[a].first - piles[b].first) *
                  (piles[a].first - piles[b].first) +
              (piles[a].second - piles[b].second) *
                  (piles[a].second - piles[b].second));
}
struct edge {
  int u, v;
  double cost;
  bool operator>(const edge &e1) const { return cost > e1.cost; }
};
edge es[100500];

int par[maxn];

void init(int n) {
  for (int i = 0; i <= n; i++) {
    par[i] = i;
  }
}

int find(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = find(par[x]);
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x != y)
    par[x] = y;
}

int main() {
  ios::sync_with_stdio(false);
  while (~scanf("%d%d", &N, &M)) {
    for (int i = 1; i <= N; i++) {
      cin >> piles[i].first >> piles[i].second;
    }
    double tot = 0;
    int a, b;
    for (int i = 0; i < M; i++) {
      cin >> es[i].u >> es[i].v;
      es[i].cost = cal(es[i].u, es[i].v);
      tot += es[i].cost;
    }
    sort(es, es + M, greater<edge>());
    init(N);
    double remain = 0;
    int cur = 0;
    for (int i = 0; i < M; ++i) {
      if (find(es[i].u) == find(es[i].v))
        continue;
      ++cur;
      remain += es[i].cost;
      unite(es[i].u, es[i].v);
      if (cur == N - 1)
        break;
    }
    printf("%.3f\\n", tot - remain);
  }
  return 0;
}
Problem p01346 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

#define int long long

int pos[123][42345];
int O = 20000;

signed main() {
  int N, L, M, R;
  cin >> N >> L >> M >> R;
  pos[0][O] = 1;
  bool can[123] = {};
  for (int i = 0; i < N; i++) {
    int m;
    cin >> m;
    int mx = 2 * M;
    for (int j = -mx; j <= mx; j++) {
      if (pos[i][j + O]) {
        int lh = j - 2 * R * m;
        if (-mx <= lh) {
          pos[i + 1][O + max(-mx, j - L * m)]++;
          pos[i + 1][O + lh + 1]--;
        }
        int hl = j + 2 * R * m;
        if (hl <= mx) {
          pos[i + 1][O + hl]++;
          int hh = j + L * m;
          if (hh + 1 <= mx) {
            pos[i + 1][O + hh + 1]--;
          }
        }
      }
    }
    can[i + 1] = pos[i + 1][-mx + O];
    for (int j = -mx + 1 + O; j <= mx + O; j++) {
      pos[i + 1][j] += pos[i + 1][j - 1];
      can[i + 1] |= pos[i + 1][j];
    }
  }
  cout << (can[N] ? "Yes" : "No") << endl;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int pos[123][42345];
int O = 20000;

int main() {
  int N, L, M, R;
  cin >> N >> L >> M >> R;
  pos[0][O] = 1;
  bool can[123] = {};
  for (int i = 0; i < N; i++) {
    int m;
    cin >> m;
    int mx = 2 * M;
    for (int j = -mx; j <= mx; j++) {
      if (pos[i][j + O]) {
        int lh = j - 2 * R * m;
        if (-mx <= lh) {
          pos[i + 1][O + max(-mx, j - L * m)]++;
          pos[i + 1][O + lh + 1]--;
        }
        int hl = j + 2 * R * m;
        if (hl <= mx) {
          pos[i + 1][O + hl]++;
          int hh = j + L * m;
          if (hh + 1 <= mx) {
            pos[i + 1][O + hh + 1]--;
          }
        }
      }
    }
    can[i + 1] = pos[i + 1][-mx + O];
    for (int j = -mx + 1 + O; j <= mx + O; j++) {
      pos[i + 1][j] += pos[i + 1][j - 1];
      can[i + 1] |= pos[i + 1][j];
    }
  }
  cout << (can[N] ? "Yes" : "No") << endl;
}
Problem p01346 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
typedef long long ll;
using namespace std;
int n;
ll l, m, r;
ll a[1000010];
bool dfs(int step, ll mn, ll mx) {
  if (step == n)
    return true;
  if (mn + r * a[step] <= m) {
    if (dfs(step + 1, mn + r * a[step], min(mx + a[step] * l, m)))
      return true;
  }
  if (mx - r * a[step] >= -m) {
    if (dfs(step + 1, max(mn - a[step] * l, -m), mx - r * a[step]))
      return true;
  }
  return false;
}
void solve() {
  cin >> n >> l >> m >> r;
  if (2 * r > l) {
    cout << "No\\n";
    return;
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  r *= 2;
  m *= 2;
  if (dfs(0, 0, 0)) {
    cout << "Yes\\n";
  } else
    cout << "No\\n";
}
int main() {
  // freopen("input.txt","r",stdin);
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>

inline int lowbit(int a) { return a & (-a); }

int update(int tree[], int a, int b, int n) {
  a++;
  while (a <= n) {
    tree[a] += b;
    a += lowbit(a);
  }
  return 0;
}

int ask(int tree[], int a) {
  a++;
  int resu = 0;
  while (a > 0) {
    resu += tree[a];
    a -= lowbit(a);
  }
  return resu;
}

int tree[2][10000];

int main() {
  //  freopen("input.txt","r",stdin);
  int n, l, m, r;
  scanf("%d%d%d%d", &n, &l, &m, &r);
  m *= 2, r *= 2;
  memset(tree[0], 0, sizeof(tree[0]));
  int s, t;
  update(tree[0], 0, 1, m + 1);
  update(tree[0], 1, -1, m + 1);
  for (int i = 1; i <= n; i++) {
    memset(tree[i % 2], 0, sizeof(tree[i % 2]));
    int mass;
    scanf("%d", &mass);
    int a = r * mass, b = l * mass;
    int flag = 0;
    for (int j = 0; j <= m; j++)
      if (ask(tree[(i - 1) % 2], j) > 0) {
        flag = 1;
        s = a + j;
        t = b + j;
        if (s <= m) {
          t = std::min(t, m);
          update(tree[i % 2], s, 1, m + 1);
          update(tree[i % 2], t + 1, -1, m + 1);
        }
        s = j - b;
        t = j - a;
        if (t < 0) {
          int sp = -t;
          int tp = -s;
          s = sp;
          t = tp;
        } else if (s < 0) {
          int sp = 0;
          int tp = std::max(abs(s), abs(t));
          s = sp;
          t = tp;
        }
        if (s <= m) {
          t = std::min(t, m);
          update(tree[i % 2], s, 1, m + 1);
          update(tree[i % 2], t + 1, -1, m + 1);
        }
      }
    if (!flag) {
      printf("No\\n");
      return 0;
    }
  }
  int flag = 0;
  for (int i = 0; i <= m; i++)
    if (ask(tree[n % 2], i) > 0)
      flag = 1;
  printf("%s\\n", flag ? "Yes" : "No");
  return 0;
}
Problem p01346 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define PII pair<int, int>
#define SIZE(x) ((int)x.size())
using namespace std;
int main() {
  int N, M, L, R;
  scanf("%d%d%d%d", &N, &L, &M, &R);
  R *= 2;
  L *= 2;
  M *= 2;
  int l = R, r = L / 2;
  vector<PII> las, cur;
  las.push_back(PII(0, 0));
  for (int i = 1; i <= N; ++i) {
    int m;
    scanf("%d", &m);
    cur.clear();
    int c = m * l, d = m * r;
    for (int i = 0; i < SIZE(las); ++i) {
      int a = las[i].first, b = las[i].second;
      int q = a + c, w = min(b + d, M);
      if (q <= w)
        cur.push_back(PII(q, w));
      q = max(a - d, -M);
      w = b - c;
      if (q <= w)
        cur.push_back(PII(q, w));
    }
    sort(cur.begin(), cur.end());
    las.clear();
    for (int j = 0; j < SIZE(cur); ++j) {
      if (SIZE(las) == 0 || las[SIZE(las) - 1].second < cur[j].first) {
        las.push_back(cur[j]);
      } else {
        las[SIZE(las) - 1].second =
            max(las[SIZE(las) - 1].second, cur[j].second);
      }
    }
    /*for (int i = 0; i < SIZE(las); ++ i)
    {
            printf("%d %d\\n", las[i].first, las[i].second);
    }
    printf("-----\\n")*/
    ;
  }
  bool flag = false;
  for (int i = 0; i < SIZE(las); ++i) {
    // printf("%d %d\\n", las[i].first, las[i].second);
    if (las[i].first <= M || las[i].second >= -M) {
      flag = true;
      break;
    }
  }
  if (flag)
    puts("Yes");
  else
    puts("No");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int N = 10000;
vector<bool> f[N];
vector<int> pack;
int n, l, m, r;

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> l >> m >> r;
  l *= 2;
  m *= 2;
  r *= 2;
  for (int i = 0; i <= n; ++i)
    f[i] = vector<bool>(m + 10, false);
  for (int i = 0; i < n; ++i) {
    int weight;
    cin >> weight;
    pack.push_back(weight);
  }
  f[0][0] = true;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j <= m; ++j) {
      if (!f[i][j])
        continue;
      for (int k = r * pack[i]; k <= l / 2 * pack[i]; ++k) {
        if (abs(j - k) <= m)
          f[i + 1][abs(j - k)] = true;
        if (abs(j + k) <= m)
          f[i + 1][abs(j + k)] = true;
      }
    }
  for (int i = 0; i <= m; ++i)
    if (f[n][i]) {
      cout << "Yes" << endl;
      return 0;
    }
  cout << "No" << endl;
  return 0;
}