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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) ((x) == 0 ? 0 : __builtin_popcount(x))

#define INF 1e16
#define mod 1000000007

int n;
int a[10], b[10], c[10];

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];

  int S = 0;
  rep(T, 1 << n) {
    if (bcnt(T) <= 1)
      continue;
    int pairnum = 0;
    rep(i, n) rep(j, i) {
      if (((T >> i) & 1) && ((T >> j) & 1)) {
        b[pairnum] |= 1 << S;
      }
      pairnum++;
    }
    rep(i, n) if ((T >> i) & 1) c[i] |= 1 << S;
    S++;
  }
  ll res = INF;
  rep(T, 1 << S) {
    bool ok = true;
    rep(i, n * (n - 1) / 2) {
      if (bcnt(T & b[i]) > 1) {
        ok = false;
        break;
      }
    }
    if (!ok)
      continue;
    ll sum = 0;
    rep(i, n) {
      if (bcnt(T & c[i]) > a[i]) {
        ok = false;
        break;
      }
      sum += a[i] - bcnt(T & c[i]);
    }
    if (!ok)
      continue;
    minch(res, sum + bcnt(T));
  }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) ((x) == 0 ? 0 : __builtin_popcount(x))

#define INF 1e16
#define mod 1000000007

int n;
int a[10], b[10], c[10];

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];

  int S = 0;
  rep(T, 1 << n) {
    if (bcnt(T) <= 1)
      continue;
    int pairnum = 0;
    rep(i, n) rep(j, i) {
      if (((T >> i) & 1) && ((T >> j) & 1)) {
        b[pairnum] |= 1 << S;
      }
      pairnum++;
    }
    rep(i, n) if ((T >> i) & 1) c[i] |= 1 << S;
    S++;
  }
  ll res = INF;
  rep(T, 1 << S) {
    bool ok = true;
    rep(i, n * (n - 1) / 2) {
      if (bcnt(T & b[i]) > 1)
        ok = false;
    }
    if (!ok)
      continue;
    ll sum = 0;
    rep(i, n) {
      if (bcnt(T & c[i]) > a[i])
        ok = false;
      sum += a[i] - bcnt(T & c[i]);
    }
    if (!ok)
      continue;
    minch(res, sum + bcnt(T));
  }
  cout << res << endl;
  return 0;
}
Problem p01743 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long Long;

namespace {

int N;
vector<Long> A;

void input() {
  cin >> N;
  A.clear();
  A.resize(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
}

bool comp(int x, int y) {
  return __builtin_popcount(x) < __builtin_popcount(y);
}

void solve() {
  vector<int> r;
  Long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += A[i];
    Long a = A[i];
    sort(r.begin(), r.end(), comp);
    int used = 0;
    vector<int> nr;
    for (int j = 0; j < r.size(); j++) {
      int x = r[j];
      if (a == 0 || (used & x)) {
        nr.push_back(x);
      } else {
        ans--;
        a--;
        used |= x;
        nr.push_back(x | 1 << i);
      }
    }
    for (int j = 0; j < min(Long(5), a); j++) {
      nr.push_back(1 << i);
    }
    r = nr;
  }
  cout << ans << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long Long;

namespace {

int N;
vector<Long> A;

void input() {
  cin >> N;
  A.clear();
  A.resize(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
}

bool comp(int x, int y) {
  return __builtin_popcount(x) < __builtin_popcount(y);
}

void solve() {
  vector<int> r;
  Long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += A[i];
    Long a = A[i];
    sort(r.begin(), r.end(), comp);
    int used = 0;
    vector<int> c;
    for (int j = 0; a >= 1 && j < r.size(); j++) {
      int x = r[j];
      if (used & x)
        continue;
      ans--;
      a--;
      used |= x;
      c.push_back(x | 1 << i);
    }
    r.insert(r.end(), c.begin(), c.end());
    for (int j = 0; j < a; j++) {
      r.push_back(1 << i);
    }
  }
  cout << ans << endl;
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}
Problem p01744 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
int main() {
  ll H, W, N;
  cin >> H >> W >> N;
  const ll width = 2 * W;
  auto get = [&](const ll d, const ll n) {
    const ll ans = (n + ((n + d) % 2 == 1 ? 1 : width - 1) * d % width) % width;
    return (ans < W) ? ans : width - ans - 1;
  };
  vector<ll> vec(W);
  for (ll i = 0; i < W; i++) {
    vec[i] = i;
  }
  using P = pair<ll, ll>;
  vector<P> edge(N);
  for (ll i = 0; i < N; i++) {
    ll a, b;
    cin >> a >> b;
    a--, b--;
    edge[i] = {a, b};
  }
  sort(edge.begin(), edge.end());
  for (ll i = 0; i < N; i++) {
    const ll a = edge[i].first;
    const ll b = edge[i].second;
    const ll u = get(a, b);
    const ll v = get(a, b + 1);
    swap(vec[u], vec[v]);
  }
  vector<ll> rev(W);
  for (ll i = 0; i < W; i++) {
    rev[vec[get(H, i)]] = i + 1;
  }
  for (ll i = 0; i < W; i++) {
    cout << rev[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int H, W, N;
  cin >> H >> W >> N;
  const int width = 2 * W;
  auto get = [&](const int d, const int n) {
    const int ans =
        (n + ((n + d) % 2 == 1 ? 1 : width - 1) * d % width) % width;
    return (ans < W) ? ans : width - ans - 1;
  };
  vector<int> vec(W);
  for (int i = 0; i < W; i++) {
    vec[i] = i;
  }
  using P = pair<int, int>;
  vector<P> edge(N);
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    edge[i] = {a, b};
  }
  sort(edge.begin(), edge.end());
  for (int i = 0; i < N; i++) {
    const int a = edge[i].first;
    const int b = edge[i].second;
    const int u = get(a, b);
    const int v = get(a, b + 1);
    swap(vec[u], vec[v]);
  }
  vector<int> rev(W);
  for (int i = 0; i < W; i++) {
    rev[vec[get(H, i)]] = i + 1;
  }
  for (int i = 0; i < W; i++) {
    cout << rev[i] << endl;
  }
  return 0;
}
Problem p01750 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;

using ll = long long;
const ll mod = 1000000007LL;

int l[90010];
ll dp[90010];

int main(void) {
  int d, s;
  cin >> d;
  rep(i, d) cin >> l[i];
  cin >> s;

  dp[s] = 1LL;
  rep(i, d) rep(j, s + 1) {
    if (j - l[i] >= 0)
      dp[j - l[i]] += mod - dp[j];
    dp[j] %= mod;
  }

  ll cut = 0LL;
  rep(i, s + 1) {
    ll tmp = dp[i];
    rep(j, d) tmp = (tmp * i) % mod;
    cut = (cut + tmp) % mod;
  }

  cout << cut << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)

#define _rrep(i, n) _rrange(i, n, 0)
#define _rrange(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)
#define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__)

#define _all(arg) begin(arg), end(arg)
#define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg))
#define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary)
#define clr(a, b) memset((a), (b), sizeof(a))
#define bit(n) (1LL << (n))
#define popcount(n) (__builtin_popcountll(n))

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;

using ll = long long;
const ll mod = 1000000007LL;

int l[310];
ll dp[310];

int main(void) {
  int d, s;
  cin >> d;
  rep(i, d) cin >> l[i];
  cin >> s;

  dp[s] = 1LL;
  rep(i, d) rep(j, s + 1) {
    if (j - l[i] >= 0)
      dp[j - l[i]] += mod - dp[j];
    dp[j] %= mod;
  }

  ll cut = 0LL;
  rep(i, s + 1) {
    ll tmp = dp[i];
    rep(j, d) tmp = (tmp * i) % mod;
    cut = (cut + tmp) % mod;
  }

  cout << cut << endl;
  return 0;
}
Problem p01750 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll mod = 1e9 + 7;

ll add(ll a, ll b) { return (a + b + mod + mod) % mod; }

ll mul(ll a, ll b) { return (a * b) % mod; }

ll mpow(ll a, ll b) {
  ll res = 1;
  for (int i = 0; i < b; i++)
    res = mul(res, a);
  return res;
}

ll d, s, t[301];
ll sum = 0;

int dp[2][2][900001];

int main() {

  cin >> d;
  for (int i = 0; i < d; i++) {
    cin >> t[i];
    sum += t[i];
  }

  cin >> s;
  dp[0][0][0] = 1;

  ll ans = 0;

  for (int i = 0; i <= d; i++) {
    int ai = i & 1;
    int bi = 1 - ai;
    fill((ll *)dp[bi], (ll *)dp[bi + 1], 0);
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k <= s; k++) {

        if (i == d) {
          if (j == 0)
            ans = add(ans, mul(dp[ai][j][k], mpow(s - k, d)));
          else
            ans = add(ans, -mul(dp[ai][j][k], mpow(s - k, d)));
        }

        dp[bi][j][k] += dp[ai][j][k];
        dp[bi][j][k] %= mod;
        if (k + t[i] <= s) {
          dp[bi][1 - j][k + t[i]] += dp[ai][j][k];
          dp[bi][1 - j][k + t[i]] %= mod;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll mod = 1e9 + 7;

ll add(ll a, ll b) { return (a + b + mod + mod) % mod; }

ll mul(ll a, ll b) { return (a * b) % mod; }

ll mpow(ll a, ll b) {
  ll res = 1;
  for (int i = 0; i < b; i++)
    res = mul(res, a);
  return res;
}

ll d, s, t[301];
ll sum = 0;

map<ll, ll> mem[301][2];

ll rec(ll p, bool cnt, ll sum) {
  if (mem[p][cnt].count(sum))
    return mem[p][cnt][sum];
  if (sum > s)
    return mem[p][cnt][sum] = 0;
  if (p == d) {
    if (cnt)
      return mem[p][cnt][sum] = mpow(s - sum, d);
    return mem[p][cnt][sum] = mod - mpow(s - sum, d);
  }
  ll res = 0;
  res = add(rec(p + 1, !cnt, sum + t[p]), rec(p + 1, cnt, sum));
  return mem[p][cnt][sum] = res;
}

int main() {

  cin >> d;
  for (int i = 0; i < d; i++) {
    cin >> t[i];
    sum += t[i];
  }

  cin >> s;

  cout << rec(0, true, 0) << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
signed dp[2][303][90909];
int d, s;
int l[303];
int mod_pow(int x, int n, int m) {
  int res = 1;
  while (n) {
    if (n & 1)
      (res *= x) %= m;
    x = (x * x) % m;
    n >>= 1;
  }
  return res;
}
int dfs(int f, int x, int y) {
  if (~dp[f][x][y])
    return dp[f][x][y];
  int res = 0;
  if (x == d) {
    res = mod_pow(y, d, MOD);
    if (f)
      res = (MOD - res) % MOD;
    return dp[f][x][y] = res;
  }
  (res += dfs(f, x + 1, y)) %= MOD;
  if (l[x] < y)
    (res += dfs(!f, x + 1, y - l[x])) %= MOD;
  return dp[f][x][y] = res;
}
signed main() {
  cin >> d;
  for (int i = 0; i < d; i++)
    cin >> l[i];
  cin >> s;
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, 0, s) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
int dp[2][303][90909];
int d, s;
int l[303];
int mod_pow(int x, int n, int m) {
  int res = 1;
  while (n) {
    if (n & 1)
      (res *= x) %= m;
    x = (x * x) % m;
    n >>= 1;
  }
  return res;
}
int dfs(int f, int x, int y) {
  if (~dp[f][x][y])
    return dp[f][x][y];
  int res = 0;
  if (x == d) {
    res = mod_pow(y, d, MOD);
    if (f)
      res = (MOD - res) % MOD;
    return dp[f][x][y] = res;
  }
  (res += dfs(f, x + 1, y)) %= MOD;
  if (l[x] < y)
    (res += dfs(!f, x + 1, y - l[x])) %= MOD;
  return dp[f][x][y] = res;
}
signed main() {
  cin >> d;
  for (int i = 0; i < d; i++)
    cin >> l[i];
  cin >> s;
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, 0, s) << endl;
  return 0;
}
Problem p01750 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll MOD = (ll)(1e9 + 7);
constexpr ll MAX = 300 * 300;

ll power(const ll p, const ll n) {
  if (n == 0) {
    return 1;
  }
  if (n % 2 == 1) {
    return (power(p, n - 1) * p) % MOD;
  } else {
    const ll pp = power(p, n / 2);
    return (pp * pp) % MOD;
  }
}

int main() {
  ll d;
  cin >> d;
  vector<ll> l(d);
  for (ll i = 0; i < d; i++) {
    cin >> l[i];
  }
  ll s;
  cin >> s;

  vector<vector<ll>> dp(2, vector<ll>(s + 1, 0));
  vector<vector<ll>> prev(2, vector<ll>(s + 1, 0));

  prev[0][0] = 1;
  for (ll i = 0; i < d; i++) {
    for (ll j = 0; j < 2; j++) {
      for (ll k = 0; k <= s; k++) {
        dp[j][k] += prev[j][k];
        dp[j][k] %= MOD;
        const ll sum = k + l[i];
        if (sum <= s) {
          dp[(j + 1) % 2][sum] -= prev[j][k];
          dp[(j + 1) % 2][sum] %= MOD;
        }
      }
    }
    prev = dp;
    fill(dp.begin(), dp.end(), vector<ll>(s + 1, 0));
  }
  ll sum = 0;
  for (ll i = 0; i <= s; i++) {
    for (ll j = 0; j < 2; j++) {
      sum += prev[j][i] * power(s - i, d);
      sum %= MOD;
      sum = (sum + MOD) % MOD;
    }
  }
  cout << sum << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

constexpr ll MOD = (ll)(1e9 + 7);
constexpr ll MAX = 300 * 300;

ll power(const ll p, const ll n) {
  if (n == 0) {
    return 1;
  }
  if (n % 2 == 1) {
    return (power(p, n - 1) * p) % MOD;
  } else {
    const ll pp = power(p, n / 2);
    return (pp * pp) % MOD;
  }
}

int main() {
  ll d;
  cin >> d;
  vector<ll> l(d);
  for (ll i = 0; i < d; i++) {
    cin >> l[i];
  }
  ll s;
  cin >> s;

  vector<vector<vector<ll>>> dp(d + 1,
                                vector<vector<ll>>(2, vector<ll>(s + 1, 0)));
  dp[0][0][0] = 1;
  for (ll i = 0; i < d; i++) {
    for (ll j = 0; j < 2; j++) {
      for (ll k = 0; k <= s; k++) {
        dp[i + 1][j][k] += dp[i][j][k];
        dp[i + 1][j][k] %= MOD;
        const ll sum = k + l[i];
        if (sum <= s) {
          dp[i + 1][(j + 1) % 2][sum] -= dp[i][j][k];
          dp[i + 1][(j + 1) % 2][sum] %= MOD;
        }
      }
    }
  }
  ll sum = 0;
  for (ll i = 0; i <= s; i++) {
    for (ll j = 0; j < 2; j++) {
      sum += dp[d][j][i] * power(s - i, d);
      sum %= MOD;
      sum = (sum + MOD) % MOD;
    }
  }
  cout << sum << endl;

  return 0;
}
Problem p01750 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

ll mod_pow(ll a, ll x) {
  ll ret = 1;
  while (x > 0) {
    if (x & 1) {
      (ret *= a) %= MOD;
    }
    (a *= a) %= MOD;
    x >>= 1;
  }
  return ret;
}

int main() {
  ll d;
  cin >> d;
  vector<ll> a(d);
  for (auto &I : a) {
    cin >> I;
  }
  ll s;
  cin >> s;
  if (s == 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> dp(s + 10, 0);
  dp[0] = 1;
  for (auto &I : a) {
    for (ll t = s - 1; t >= 0; t--) {
      if (t + I < s) {
        dp[t + I] -= dp[t];
        dp[t + I] %= MOD;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < s; i++) {
    ans += dp[i] * mod_pow(s - i, d) % MOD;
    ans %= MOD;
  }
  if (ans < 0) {
    ans += MOD;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<double, ll> pdl;
#define F first
#define S second
const ll E = 1e18 + 7;
const ll MOD = 1000000007;

ll mod_pow(ll a, ll x) {
  ll ret = 1;
  while (x > 0) {
    if (x & 1) {
      (ret *= a) %= MOD;
    }
    (a *= a) %= MOD;
    x >>= 1;
  }
  return ret;
}

int main() {
  ll d;
  cin >> d;
  vector<ll> a(d);
  for (auto &I : a) {
    cin >> I;
  }
  ll s;
  cin >> s;
  vector<ll> dp(s, 0);
  dp[0] = 1;
  for (auto &I : a) {
    for (ll t = s - 1; t >= 0; t--) {
      if (t + I < s) {
        dp[t + I] -= dp[t];
        dp[t + I] %= MOD;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < s; i++) {
    ans += dp[i] * mod_pow(s - i, d) % MOD;
    ans %= MOD;
  }
  if (ans < 0) {
    ans += MOD;
  }
  cout << ans << endl;

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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXD = 303;
const int MOD = 1e9 + 7;
int D, S;
int l[MAXD];
int dp[MAXD][MAXD * MAXD][2];

ll mod_pow(ll x, ll p, int MOD) {
  if (x == 0)
    return 0;
  ll a = 1;
  while (p) {
    if (p % 2)
      a = a * x % MOD;
    x = x * x % MOD;
    p /= 2;
  }
  return a;
}

int dfs(int d, int rest, int flag) {
  int &ret = dp[d][rest][flag];
  if (ret >= 0)
    return ret;
  if (d == D) {
    ret = mod_pow(rest, D, MOD);
    if (flag)
      ret = -ret;
    ret = (ret % MOD + MOD) % MOD;
  } else {
    ret = dfs(d + 1, rest, flag);
    if (rest >= l[d]) {
      ret += dfs(d + 1, rest - l[d], flag ^ 1);
    }
    ret %= MOD;
  }

  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> D;
  for (int i = 0; i < D; i++)
    cin >> l[i];
  cin >> S;
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, S, 0) << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXD = 303;
const int MOD = 1e9 + 7;
int D, S;
int l[MAXD];
ll dp[MAXD][MAXD * MAXD][2];

ll mod_pow(ll x, ll p, ll MOD) {
  if (x == 0)
    return 0;
  ll a = 1;
  while (p) {
    if (p % 2)
      a = a * x % MOD;
    x = x * x % MOD;
    p /= 2;
  }
  return a;
}

ll dfs(int d, int rest, int flag) {
  ll &ret = dp[d][rest][flag];
  if (ret >= 0)
    return ret;
  if (d == D) {
    ret = mod_pow(rest, D, MOD);
    if (flag)
      ret = -ret;
    ret = (ret % MOD + MOD) % MOD;
  } else {
    ret = dfs(d + 1, rest, flag);
    if (rest >= l[d]) {
      ret += dfs(d + 1, rest - l[d], flag ^ 1);
    }
    ret %= MOD;
  }

  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> D;
  for (int i = 0; i < D; i++)
    cin >> l[i];
  cin >> S;
  memset(dp, -1, sizeof(dp));
  cout << dfs(0, S, 0) << endl;
  return 0;
}
Problem p01750 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

struct I {
  I() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} init;

typedef long long LL;
typedef vector<LL> V;
typedef vector<V> VV;
const LL MOD = 1e9 + 7;

LL f(LL v, int n) {
  if (n == 0)
    return 1;
  else
    return v * f(v, n - 1) % MOD;
}
int main() {
  int d;
  cin >> d;
  V l(d);
  for (auto &it : l)
    cin >> it;
  int s;
  cin >> s;
  sort(l.begin(), l.end());
  VV dp(2, V(s + 1, 0));
  dp[0][s] = 1;
  for (int i = 0; i < d; i++) {
    VV nxt = dp;
    for (int j = 0; j < 2; j++)
      for (int k = l[i] + 1; k <= s; k++) {
        (nxt[1 - j][k - l[i]] += dp[j][k]) %= MOD;
      }
    swap(nxt, dp);
  }
  LL res = 0;
  for (int i = 1; i <= s; i++) {
    LL mul = f(i, d);
    V sgn({1, -1});
    for (int j = 0; j < 2; j++) {
      (res += MOD + sgn[j] * (mul * dp[j][i] % MOD)) %= MOD;
    }
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct I {
  I() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} init;

typedef long long LL;
typedef vector<LL> V;
typedef vector<V> VV;
const LL MOD = 1e9 + 7;

LL f(LL v, int n) {
  if (n == 0)
    return 1;
  else
    return v * f(v, n - 1) % MOD;
}
int main() {
  int d;
  cin >> d;
  V l(d);
  for (auto &it : l)
    cin >> it;
  int s;
  cin >> s;
  sort(l.begin(), l.end());
  VV dp(d + 1, V(s + 1, 0));
  dp[0][s] = 1;
  for (int i = 0; i < d; i++) {
    for (int j = i; j >= 0; j--) {
      for (int k = l[i] + 1; k <= s; k++)
        (dp[j + 1][k - l[i]] += dp[j][k]) %= MOD;
    }
  }
  LL res = 0;
  for (int i = 1; i <= s; i++) {
    LL mul = f(i, d);
    V sgn({1, -1});
    for (int j = 0; j <= d; j++) {
      // cout << j << " " <<i<<" "<<dp[j][i]<<endl;
      (res += MOD + sgn[j % 2] * (mul * dp[j][i] % MOD)) %= MOD;
    }
  }
  cout << res << endl;
  return 0;
}