prithivMLmods/Coder-Stat huggingface dataset:
Problem p03427 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  string N;
  cin >> N;
  if (N.size() == 1) {
    cout << N << endl;
  } else {
    bool nine = true;
    for (int i = 1; i++; i < N.size()) {
      if (N[i] != '9')
        nine = false;
    }
    int top = N[0] - '0';
    if (nine == true) {
      cout << top + 9 * (N.size() - 1) << endl;
    } else {
      cout << top - 1 + 9 * (N.size() - 1) << endl;
    }
  }
}

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

int main() {
  string N;
  cin >> N;
  if (N.size() == 1) {
    cout << N << endl;
  } else {
    bool nine = true;
    for (int i = 1; i < N.size(); i++) {
      if (N[i] != '9')
        nine = false;
    }
    int top = N[0] - '0';
    if (nine == true) {
      cout << top + 9 * (N.size() - 1) << endl;
    } else {
      cout << top - 1 + 9 * (N.size() - 1) << endl;
    }
  }
}
Problem p03427 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll func(ll x) {
  if (x / 10 == 0)
    return x;
  return x % 10 + func(x / 10);
}

int main() {
  string N;
  cin >> N;
  int n = N.length();

  vector<string> S(10);
  for (char i = '9'; i > '0'; i--) {
    S[i - '0'].push_back(i);
    for (int j = 0; j < n - 1; j++) {
      S[i - '0'].push_back('9');
    }
  }

  for (int i = 0; i < n - 1; i++)
    S[0].push_back('9');

  ll Ma = 0;
  for (int i = 0; i < S.size(); i++) {
    if (stoll(S[i]) <= stoll(N))
      Ma = max(Ma, func(stoll(S[i])));
  }

  cout << Ma << endl;
}

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

typedef long long ll;

ll func(ll x) {
  if (x / 10 == 0)
    return x;
  return x % 10 + func(x / 10);
}

int main() {
  string N;
  cin >> N;
  int n = N.length();

  if (stoll(N) < 10) {
    cout << stoll(N) << endl;
    return 0;
  }

  vector<string> S(10);
  for (char i = '9'; i > '0'; i--) {
    S[i - '0'].push_back(i);
    for (int j = 0; j < n - 1; j++) {
      S[i - '0'].push_back('9');
    }
  }

  for (int i = 0; i < n - 1; i++)
    S[0].push_back('9');

  ll Ma = 0;
  for (int i = 0; i < S.size(); i++) {
    if (stoll(S[i]) <= stoll(N))
      Ma = max(Ma, func(stoll(S[i])));
  }

  cout << Ma << endl;
}
Problem p03427 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long lint;

template <class T> inline T nxi() {
  T x = 0;
  char c;
  while ((c = getchar()) > '9' || c < '0')
    ;
  while (x = x * 10 - 48 + c, (c = getchar()) >= '0' && c <= '9')
    ;
  return x;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
#endif
  lint n = nxi<lint>();
  int ans = 0;
  bool f = 1;
  for (; n / 10; n /= 10) {
    ans += 9;
    f &= n % 10 == 9;
  }
  printf("%lld\n", ans + n - 1 + f);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long lint;

template <class T> inline T nxi() {
  T x = 0;
  char c;
  while ((c = getchar()) > '9' || c < '0')
    ;
  while (x = x * 10 - 48 + c, (c = getchar()) >= '0' && c <= '9')
    ;
  return x;
}

int main() {
#ifndef ONLINE_JUDGE
//	freopen("a.in","r",stdin);
#endif
  lint n = nxi<lint>();
  int ans = 0;
  bool f = 1;
  for (; n / 10; n /= 10) {
    ans += 9;
    f &= n % 10 == 9;
  }
  printf("%lld\n", ans + n - 1 + f);
  return 0;
}
Problem p03427 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define int long long

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define pb(x) push_back(x)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 9999999999

using namespace std;

int32_t main() {
  int n;
  cin >> n;

  int t = n, cnt;
  vector<int> v;
  FOR(i, 1, 17) {
    v.pb(t % 10);
    t /= 10;
    if (t < 1) {
      cnt = i;
      break;
    }
  }

  int u = 0, ans;
  REP(i, cnt) u += v[i];
  ans = u;
  for (int i = cnt - 1; i > 0; i--) {
    u = v[i] - 1;
    for (int j = i - 1; j >= 0; j--) {
      u += 9;
    }
    ans = max(ans, u);
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>

#define int long long

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define pb(x) push_back(x)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 9999999999

using namespace std;

int32_t main() {
  int n;
  cin >> n;

  int t = n, cnt;
  vector<int> v;
  FOR(i, 1, 20) {
    v.pb(t % 10);
    t /= 10;
    if (t < 1) {
      cnt = i;
      break;
    }
  }

  int u = 0, ans;
  REP(i, cnt) u += v[i];
  ans = u;
  for (int i = cnt - 1; i > 0; i--) {
    u = v[i] - 1;
    for (int j = i - 1; j >= 0; j--) {
      u += 9;
    }
    ans = max(ans, u);
  }

  cout << ans << endl;
}
Problem p03427 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int INF = 1e9;
const long double eps = 1e-9;
const int maxdig = 20;
int dig[maxdig];
inline ll read() {
  ll x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
int main() {
  int i;
  ll n;
#ifndef ONLINE_JUDGE
  freopen("A.in", "r", stdin);
  freopen("A.out", "w", stdout);
#endif
  n = read();
  int cnt = 0, sum = 0, ans = 0;
  while (n) {
    dig[++cnt] = n % 10;
    n /= 10;
  }
  for (i = cnt; i >= 1; i--)
    sum += dig[i];
  ans = 9 * (cnt - 1) + dig[cnt] - 1;
  printf("%d\n", max(ans, sum));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int INF = 1e9;
const long double eps = 1e-9;
const int maxdig = 20;
int dig[maxdig];
inline ll read() {
  ll x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    flag = -1, ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * flag;
}
int main() {
  int i;
  ll n;
  n = read();
  int cnt = 0, sum = 0, ans = 0;
  while (n) {
    dig[++cnt] = n % 10;
    n /= 10;
  }
  for (i = cnt; i >= 1; i--)
    sum += dig[i];
  ans = 9 * (cnt - 1) + dig[cnt] - 1;
  printf("%d\n", max(ans, sum));
  return 0;
}
Problem p03427 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ALL(A) (A).begin(), (A).end()
#define ll long long
const ll mod = 1e9 + 7;
const ll INF = 2 * 1e18;
const int inf = 1e9 + 7;

ll p(ll a, ll b) {
  if (b == 0)
    return a;
  return a * p(a, b - 1);
}
int main(void) {
  ll N;
  cin >> N;
  ll K = 0;
  ll T = N;
  while (T) {
    K++;
    T /= 10;
  }
  ll ans = 9 * (K - 1);
  for (int i = 0; i <= 10; i++) {
    if ((i + 1) * p(10, K - 2) - 1 <= N) {
      ans = i + 9 * (K - 1);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(A) (A).begin(), (A).end()
#define ll long long
const ll mod = 1e9 + 7;
const ll INF = 2 * 1e18;
const int inf = 1e9 + 7;

ll p(ll a, ll b) {
  if (b == 0)
    return a;
  return a * p(a, b - 1);
}
int main(void) {
  ll N;
  cin >> N;
  ll K = 0;
  ll T = N;
  while (T) {
    K++;
    T /= 10;
  }

  if (K == 1) {
    cout << N << endl;
    return 0;
  }
  ll ans = 9 * (K - 1);
  for (int i = 0; i <= 10; i++) {
    if ((i + 1) * p(10, K - 2) - 1 <= N) {
      ans = i + 9 * (K - 1);
    }
  }
  cout << ans << endl;
}
Problem p03427 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define lli long long int
int ar[101];
lli dp[16][150][2];

lli getSum(int n, lli sum, int ind, bool flag) {
  if (ind > n)
    return sum;
  lli res = 0;
  int lim = 9;
  if (!flag)
    lim = ar[ind];
  if (dp[ind][sum][flag] != -1)
    return dp[ind][sum][flag];
  for (int i = 0; i <= lim; i++) {
    if (flag || i < lim)
      res = max(res, getSum(n, sum + i, ind + 1, true));
    else
      res = max(res, getSum(n, sum + i, ind + 1, flag));
  }
  dp[ind][sum][flag] = res;
  return res;
}

int main() {
  string n;
  cin >> n;
  int sum = 0;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n.length(); i++) {
    ar[i + 1] = n[i] - '0';
  }
  lli one = getSum(n.size(), 0, 1, 0);
  cout << one;
}

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

#define lli long long int
int ar[1001];
lli dp[18][170][2];

lli getSum(int n, lli sum, int ind, bool flag) {
  if (ind > n)
    return sum;
  lli res = 0;
  int lim = 9;
  if (!flag)
    lim = ar[ind];
  if (dp[ind][sum][flag] != -1)
    return dp[ind][sum][flag];
  for (int i = 0; i <= lim; i++) {
    if (flag || i < lim)
      res = max(res, getSum(n, sum + i, ind + 1, true));
    else
      res = max(res, getSum(n, sum + i, ind + 1, flag));
  }
  dp[ind][sum][flag] = res;
  return res;
}

int main() {
  string n;
  cin >> n;
  int sum = 0;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n.length(); i++) {
    ar[i + 1] = n[i] - '0';
  }
  lli one = getSum(n.size(), 0, 1, 0);
  cout << one;
}
Problem p03427 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int arr[16];
ll dp[16][150][2];
int sums = 0;
ll getsum(int ind, int n, int sum, bool flag) {
  if (ind >= n)
    return sum;

  int limit = 9;
  if (!flag)
    limit = arr[ind];
  // flag=true means earlier ther is some smaller digit used so we can take till
  // 9 this time;
  if (dp[ind][sum][flag] != -1)
    return dp[ind][sum][flag];

  ll res = 0;
  for (int i = 0; i <= limit; i++) {
    if (flag || i < limit)
      res = max(res, getsum(ind + 1, n, sum + i, true));
    else
      res = max(res, getsum(ind + 1, n, sum + i, false));
  }
  return dp[ind][sum][flag] = max(res, dp[ind][sum][flag]);
}

int main() {
  int t = 1;
  while (t--) {
    ll x;
    cin >> x;
    string a = to_string(x);

    int s = 0;
    for (int i = 0; i < a.size(); i++)
      for (int j = 0; j < 150; j++)
        for (int k = 0; k < 2; k++)
          dp[i][j][k] = -1;

    for (int i = 0; i < a.size(); i++) {
      arr[i] = a[i] - '0';
      s += arr[i];
    }
    ll left = getsum(0, a.size(), 0, 0);

    // for(int i=0;i<b.size();i++)for(int j=0;j<150;j++)for(int k=0;k<2;k++)
    //     dp[i][j][k]=-1;

    // for(int i=0;i<b.size();i++) arr[i]=b[i]-'0';
    // ll right=getsum(0, b.size(), 0, 0);

    // ll ans=right-left+s;
    cout << left << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int arr[17];
ll dp[17][150][2];
int sums = 0;
ll getsum(int ind, int n, int sum, bool flag) {
  if (ind >= n)
    return sum;

  int limit = 9;
  if (!flag)
    limit = arr[ind];
  // flag=true means earlier ther is some smaller digit used so we can take till
  // 9 this time;
  if (dp[ind][sum][flag] != -1)
    return dp[ind][sum][flag];

  ll res = 0;
  for (int i = 0; i <= limit; i++) {
    if (flag || i < limit)
      res = max(res, getsum(ind + 1, n, sum + i, true));
    else
      res = max(res, getsum(ind + 1, n, sum + i, false));
  }
  return dp[ind][sum][flag] = max(res, dp[ind][sum][flag]);
}

int main() {
  int t = 1;
  while (t--) {
    ll x;
    cin >> x;
    string a = to_string(x);

    int s = 0;
    for (int i = 0; i < a.size(); i++)
      for (int j = 0; j < 150; j++)
        for (int k = 0; k < 2; k++)
          dp[i][j][k] = -1;

    for (int i = 0; i < a.size(); i++) {
      arr[i] = a[i] - '0';
      s += arr[i];
    }
    ll left = getsum(0, a.size(), 0, 0);

    // for(int i=0;i<b.size();i++)for(int j=0;j<150;j++)for(int k=0;k<2;k++)
    //     dp[i][j][k]=-1;

    // for(int i=0;i<b.size();i++) arr[i]=b[i]-'0';
    // ll right=getsum(0, b.size(), 0, 0);

    // ll ans=right-left+s;
    cout << left << endl;
  }
}
Problem p03427 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  int res = 0;
  for (int i = 1; i <= 9; i++) {
    long long x = i;
    int sum = x;
    if (x > n)
      break;
    while (x * 10 + 9) {
      x = x * 10 + 9;
      sum += 9;
    }
    res = max(res, sum);
  }
  cout << res << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  int res = 0;
  for (int i = 1; i <= 9; i++) {
    long long x = i;
    int sum = x;
    if (x > n)
      break;
    while (x * 10 + 9 <= n) {
      x = x * 10 + 9;
      sum += 9;
    }
    res = max(res, sum);
  }
  cout << res << '\n';
  return 0;
}
Problem p03428 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

int main() {
  int pointCount;
  double x[100], y[100];

  scanf("%d", &pointCount);

  for (int i = 0; i < pointCount; ++i) {
    scanf("%lf%lf", x + i, y + i);
  }

  double res[100] = {};

  int d = 10000000;

  for (int i = 0; i < d; ++i) {
    double r = M_PI * 2.0 / d * i;

    double px = cos(r);
    double py = sin(r);

    int m = 0;
    double md = -10000000000.0;

    for (int j = 0; j < pointCount; ++j) {
      if (md < px * x[j] + py * y[j]) {
        md = px * x[j] + py * y[j];
        m = j;
      }
    }

    res[m] += 1.0 / d;
  }

  for (int i = 0; i < pointCount; ++i) {
    printf("%.10lf\n", res[i]);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

int main() {
  int pointCount;
  double x[100], y[100];

  scanf("%d", &pointCount);

  for (int i = 0; i < pointCount; ++i) {
    scanf("%lf%lf", x + i, y + i);
  }

  double res[100] = {};

  int d = 1000000;

  for (int i = 0; i < d; ++i) {
    double r = M_PI * 2.0 / d * i;

    double px = cos(r);
    double py = sin(r);

    int m = 0;
    double md = -10000000000.0;

    for (int j = 0; j < pointCount; ++j) {
      if (md < px * x[j] + py * y[j]) {
        md = px * x[j] + py * y[j];
        m = j;
      }
    }

    res[m] += 1.0 / d;
  }

  for (int i = 0; i < pointCount; ++i) {
    printf("%.10lf\n", res[i]);
  }

  return 0;
}
Problem p03428 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
// typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define UNIQUE(c) sort(ALL(c)), c.erase(unique(ALL(c)), c.end())

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

int main() {
  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<pair<int, int>> points;
  REP(i, N) {
    int x, y;
    cin >> x >> y;
    points.emplace_back(x, y);
  }

  vector<int> count(N, 0);
  const int n_trial = 5 * 1e6;
  const double PI = acos(-1);
  const double delta = 2 * PI / n_trial;
  for (double i = 0; i < 2 * PI; i += delta) {
    double x = 1e11 * cos(i), y = 1e11 * sin(i);
    // cout << x << ' ' << y << endl;
    int idx = 0;
    double dist = 1e12;
    REP(i, N) {
      double a, b;
      tie(a, b) = points[i];
      double cand = hypot(x - a, y - b);
      if (cand < dist) {
        dist = cand;
        idx = i;
      }
    }
    count[idx]++;
  }
  // REP(i, N) cout << count[i] << (i == N - 1 ? '\n' : ' ');
  cout << setprecision(20);
  for (int c : count)
    cout << 1.0 * c / n_trial << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
// typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define UNIQUE(c) sort(ALL(c)), c.erase(unique(ALL(c)), c.end())

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

int main() {
  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<pair<int, int>> points;
  REP(i, N) {
    int x, y;
    cin >> x >> y;
    points.emplace_back(x, y);
  }

  vector<int> count(N, 0);
  const int n_trial = 1e6;
  const double PI = acos(-1);
  const double delta = 2 * PI / n_trial;
  for (double i = 0; i < 2 * PI; i += delta) {
    double x = 1e11 * cos(i), y = 1e11 * sin(i);
    // cout << x << ' ' << y << endl;
    int idx = 0;
    double dist = 1e12;
    REP(i, N) {
      double a, b;
      tie(a, b) = points[i];
      double cand = hypot(x - a, y - b);
      if (cand < dist) {
        dist = cand;
        idx = i;
      }
    }
    count[idx]++;
  }
  // REP(i, N) cout << count[i] << (i == N - 1 ? '\n' : ' ');
  cout << setprecision(20);
  for (int c : count)
    cout << 1.0 * c / n_trial << endl;
  return 0;
}
Problem p03428 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  ll f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 1e2 + 10;
double pi = acos(-1.0);
double tmp[maxn];
int x[maxn], y[maxn];
int num;
int main() {
  int n = read();
  REP(i, 1, n) x[i] = read(), y[i] = read();
  REP(i, 1, n) {
    REP(j, 1, n) if (i != j) tmp[++num] = atan2(y[j] - y[i], x[j] - x[i]);
    sort(tmp + 1, tmp + num + 1);
    tmp[n] = tmp[1] + 2 * pi;
    double Mx = 0;
    REP(j, 1, n - 1) Mx = max(Mx, tmp[j + 1] - tmp[j]);
    Mx = max(Mx - pi, 0.0);
    printf("%.10lf\n", Mx / (2 * pi));
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, st, ed) for (register int i = st, i##end = ed; i <= i##end; ++i)
#define DREP(i, st, ed) for (register int i = st, i##end = ed; i >= i##end; --i)
typedef long long ll;
inline int read() {
  int x;
  char c;
  int f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ '0');
  return x * f;
}
inline ll readll() {
  ll x;
  char c;
  ll f = 1;
  while ((c = getchar()) != '-' && (c < '0' || c > '9'))
    ;
  if (c == '-')
    c = getchar(), f = -1;
  x = c ^ '0';
  while ((c = getchar()) >= '0' && c <= '9')
    x = (x << 1ll) + (x << 3ll) + (c ^ '0');
  return x * f;
}
const int maxn = 1e2 + 10;
double pi = acos(-1.0);
double tmp[maxn];
int x[maxn], y[maxn];
int num;
int main() {
  int n = read();
  REP(i, 1, n) x[i] = read(), y[i] = read();
  REP(i, 1, n) {
    num = 0;
    REP(j, 1, n) if (i != j) tmp[++num] = atan2(y[j] - y[i], x[j] - x[i]);
    sort(tmp + 1, tmp + num + 1);
    tmp[n] = tmp[1] + 2 * pi;
    double Mx = 0;
    REP(j, 1, n - 1) Mx = max(Mx, tmp[j + 1] - tmp[j]);
    Mx = max(Mx - pi, 0.0);
    printf("%.10lf\n", Mx / (2 * pi));
  }
  return 0;
}
Problem p03430 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;

const int MAXN = 301;

int f[MAXN][MAXN][MAXN];

int main() {
#ifdef BZ
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(20);
  cout.tie(nullptr);
  cin.tie(nullptr);
  string s;
  int k;
  cin >> s >> k;
  string t(s.rbegin(), s.rend());
  int n = s.length();
  s += '#';
  t += '#';

  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      for (int l = 0; l <= k; ++l) {
        int c = f[i][j][l];
        f[i + 1][j][l] = max(f[i + 1][j][l], c);
        f[i][j + 1][l] = max(f[i][j + 1][l], c);
        int &nv = f[i + 1][j + 1][l + (s[i] != t[j])];
        nv = max(nv, c + 1);
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < n; ++i) {
    ans = max(ans, f[i][n - 1 - i][k] * 2 + 1);
  }

  for (int i = 0; i <= n; ++i) {
    ans = max(ans, f[i][n - i][k] * 2);
  }

  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;

const int MAXN = 310;

int f[MAXN][MAXN][MAXN];

int main() {
#ifdef BZ
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(20);
  cout.tie(nullptr);
  cin.tie(nullptr);
  string s;
  int k;
  cin >> s >> k;
  string t(s.rbegin(), s.rend());
  int n = s.length();
  s += '#';
  t += '#';

  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= n; ++j) {
      for (int l = 0; l <= k; ++l) {
        int c = f[i][j][l];
        f[i + 1][j][l] = max(f[i + 1][j][l], c);
        f[i][j + 1][l] = max(f[i][j + 1][l], c);
        int &nv = f[i + 1][j + 1][l + (s[i] != t[j])];
        nv = max(nv, c + 1);
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < n; ++i) {
    ans = max(ans, f[i][n - 1 - i][k] * 2 + 1);
  }

  for (int i = 0; i <= n; ++i) {
    ans = max(ans, f[i][n - i][k] * 2);
  }

  cout << ans << "\n";
}
Problem p03430 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int inf = (int)1e9;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string s;
  int t;
  cin >> s >> t;
  int n = (int)s.size();
  vector<vector<vector<int>>> dp(
      n + 1, vector<vector<int>>(n + 1, vector<int>(t + 2, 0)));
  for (int k = 0; k <= t; k++) {
    for (int len = 1; len <= n; len++) {
      for (int i = 0; i + len <= n; i++) {
        for (int k = 0; k <= t; k++) {
          int j = i + len - 1;
          dp[i][j][k] = max(dp[i][j][k], max(dp[i + 1][j][k],
                                             j - 1 < 0 ? 0 : dp[i][j - 1][k]));
          if (s[i] == s[j]) {
            dp[i][j][k] =
                max(dp[i][j][k],
                    1 + (i < j) + (j - 1 < 0 ? 0 : dp[i + 1][j - 1][k]));
            dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k]);
          } else {
            dp[i][j][k + 1] =
                max(dp[i][j][k + 1], 2 + (j - 1 < 0 ? 0 : dp[i + 1][j - 1][k]));
          }
        }
      }
    }
  }
  cout << dp[0][n - 1][t] << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int inf = (int)1e9;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string s;
  int t;
  cin >> s >> t;
  int n = (int)s.size();
  vector<vector<vector<int>>> dp(
      n + 1, vector<vector<int>>(n + 1, vector<int>(t + 2, 0)));
  for (int len = 1; len <= n; len++) {
    for (int i = 0; i + len <= n; i++) {
      for (int k = 0; k <= t; k++) {
        int j = i + len - 1;
        dp[i][j][k] = max(
            dp[i][j][k], max(dp[i + 1][j][k], j - 1 < 0 ? 0 : dp[i][j - 1][k]));
        if (s[i] == s[j]) {
          dp[i][j][k] = max(
              dp[i][j][k], 1 + (i < j) + (j - 1 < 0 ? 0 : dp[i + 1][j - 1][k]));
          dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k]);
        } else {
          dp[i][j][k + 1] =
              max(dp[i][j][k + 1], 2 + (j - 1 < 0 ? 0 : dp[i + 1][j - 1][k]));
        }
      }
    }
  }
  cout << dp[0][n - 1][t] << '\n';
  return 0;
}
Problem p03430 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

string str;
int k;
int dp[300][301][301];

int main() {
  cin >> str;
  cin >> k;
  int n = str.size();
  int ans = 1;
  rep(i, n) dp[i][1][0] = 1;
  for (int j = 2; j <= n; j++) {
    rep(i, n) {
      for (int u = 0; u <= k; u++) {
        if (str[i] == str[i + j - 1]) {
          dp[i][j][u] = dp[i + 1][j - 2][u] + 2;
        } else {
          dp[i][j][u] = max(dp[i][j - 1][u], dp[i + 1][j - 1][u]);
        }
        if (u >= 1)
          dp[i][j][u] = max(dp[i][j][u], dp[i + 1][j - 2][u - 1] + 2);
        ans = max(ans, dp[i][j][u]);
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;

string str;
int k;
int dp[300][301][301];

int main() {
  cin >> str;
  cin >> k;
  int n = str.size();
  int ans = 1;
  rep(i, n) dp[i][1][0] = 1;
  for (int j = 2; j <= n; j++) {
    rep(i, n - j + 1) {
      for (int u = 0; u <= k; u++) {
        if (str[i] == str[i + j - 1]) {
          dp[i][j][u] = dp[i + 1][j - 2][u] + 2;
        } else {
          dp[i][j][u] = max(dp[i][j - 1][u], dp[i + 1][j - 1][u]);
        }
        if (u >= 1)
          dp[i][j][u] = max(dp[i][j][u], dp[i + 1][j - 2][u - 1] + 2);
        ans = max(ans, dp[i][j][u]);
      }
    }
  }
  cout << ans << endl;
}
Problem p03430 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

char str[303];
int dp[303][303][303], N, K;

int solve(int x, int y, int z) {
  if (x > y)
    return 0;
  if (x == y)
    return 1;
  if (dp[x][y][z])
    return dp[x][y][z];
  if (str[x] == str[y])
    return dp[x][y][z] = solve(x + 1, y - 1, z) + 2;
  return max(
      solve(x + 1, y, z),
      max(solve(x, y - 1, z), z == 0 ? 0 : 2 + solve(x + 1, y - 1, z - 1)));
}

int main() {
  scanf("%s %d", str + 1, &K);
  N = strlen(str + 1);
  cout << solve(1, N, K) << endl;
  return 0;
}

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

char str[303];
int dp[303][303][303], N, K;

int solve(int x, int y, int z) {
  if (x > y)
    return 0;
  if (x == y)
    return 1;
  if (dp[x][y][z])
    return dp[x][y][z];
  if (str[x] == str[y])
    return dp[x][y][z] = solve(x + 1, y - 1, z) + 2;
  return dp[x][y][z] = max(solve(x + 1, y, z),
                           max(solve(x, y - 1, z),
                               z == 0 ? 0 : 2 + solve(x + 1, y - 1, z - 1)));
}

int main() {
  scanf("%s %d", str + 1, &K);
  N = strlen(str + 1);
  cout << solve(1, N, K) << endl;
  return 0;
}
Problem p03430 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <memory.h>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;

#define N 301

int dp[N][N][N];

char s[N];
void solve() {
  scanf(" %s", s + 1);
  int n = strlen(s + 1);
  int k;
  scanf("%d", &k);
  for (int cc = k; cc >= 0; --cc) {
    for (int i = 1; i <= n; ++i) {
      for (int j = n; j >= i + 1; --j) {
        dp[cc][i + 1][j] = max(dp[cc][i + 1][j], dp[cc][i][j]);
        dp[cc][i][j - 1] = max(dp[cc][i][j - 1], dp[cc][i][j]);
        if (s[i] == s[j]) {
          dp[cc][i + 1][j - 1] = max(dp[cc][i + 1][j - 1], dp[cc][i][j] + 1);
        } else if (cc > 0) {
          dp[cc - 1][i + 1][j - 1] =
              max(dp[cc - 1][i + 1][j - 1], dp[cc][i][j] + 1);
        }
      }
    }
  }
  int ret = 0;
  for (int i = 1; i <= n; ++i) {
    for (int cc = 0; cc <= k; ++cc) {
      ret = max(ret, dp[cc][i][i - 1] * 2);
      ret = max(ret, dp[cc][i + 1][i] * 2);
      ret = max(ret, 1 + dp[cc][i][i] * 2);
    }
  }
  printf("%d\n", ret);
}

int main() {
  // freopen("input.txt","r",stdin);
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <iostream>
#include <memory.h>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;

#define N 303

int dp[N][N][N];

char s[N];
void solve() {
  scanf(" %s", s + 1);
  int n = strlen(s + 1);
  int k;
  scanf("%d", &k);
  for (int cc = k; cc >= 0; --cc) {
    for (int i = 1; i <= n; ++i) {
      for (int j = n; j >= i + 1; --j) {
        dp[cc][i + 1][j] = max(dp[cc][i + 1][j], dp[cc][i][j]);
        dp[cc][i][j - 1] = max(dp[cc][i][j - 1], dp[cc][i][j]);
        if (s[i] == s[j]) {
          dp[cc][i + 1][j - 1] = max(dp[cc][i + 1][j - 1], dp[cc][i][j] + 1);
        } else if (cc > 0) {
          dp[cc - 1][i + 1][j - 1] =
              max(dp[cc - 1][i + 1][j - 1], dp[cc][i][j] + 1);
        }
      }
    }
  }
  int ret = 0;
  for (int i = 1; i <= n; ++i) {
    for (int cc = 0; cc <= k; ++cc) {
      ret = max(ret, dp[cc][i][i - 1] * 2);
      ret = max(ret, dp[cc][i + 1][i] * 2);
      ret = max(ret, 1 + dp[cc][i][i] * 2);
    }
  }
  printf("%d\n", ret);
}

int main() {
  // freopen("input.txt","r",stdin);
  solve();
  return 0;
}
Problem p03430 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> l4;

const int N = 301;
char s[N];
int k;
int d[N][N][N];
inline void Max(int &x, int y) {
  if (x < y)
    x = y;
}
int dp(int l, int r, int k) {
  if (l > r)
    return 0;
  int &ret = d[l][r][k];
  if (ret == -1) {
    if (l == r)
      return ret = 1;
    if (s[l] == s[r])
      return ret = 2 + dp(l + 1, r - 1, k);
    ret = 0;
    Max(ret, dp(l, r - 1, k));
    Max(ret, dp(l + 1, r, k));
    if (k != 0)
      Max(ret, 2 + dp(l + 1, r - 1, k - 1));
  }
  return ret;
}
int main() {
  scanf("%s %d", s, k);
  memset(d, -1, sizeof(d));
  printf("%d\n", dp(0, strlen(s) - 1, k));
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> l4;

const int N = 301;
char s[N];
int k;
int d[N][N][N];
inline void Max(int &x, int y) {
  if (x < y)
    x = y;
}
int dp(int l, int r, int k) {
  if (l > r)
    return 0;
  int &ret = d[l][r][k];
  if (ret == -1) {
    if (l == r)
      return ret = 1;
    if (s[l] == s[r])
      return ret = 2 + dp(l + 1, r - 1, k);
    ret = 0;
    Max(ret, dp(l, r - 1, k));
    Max(ret, dp(l + 1, r, k));
    if (k != 0)
      Max(ret, 2 + dp(l + 1, r - 1, k - 1));
  }
  return ret;
}
int main() {
  scanf("%s %d", s, &k);
  memset(d, -1, sizeof(d));
  printf("%d\n", dp(0, strlen(s) - 1, k));
}
Problem p03430 (C++)

Buggy submission (status: Time Limit Exceeded):
/* --- author: dxm --- */
#include <bits/stdc++.h>
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
typedef long long ll;
inline void Fail() {
  printf("0");
  exit(0);
}

const int maxn = 305;
int n, lim, ans;
char c[maxn];
string s;
int dp[maxn][maxn][maxn];

int main() {
  scanf("%s%d", c, &lim);
  s = c;
  n = s.size();
  rep(i, n) {
    rep(j, i + 1) rep(k, n - i) rep(p, min(lim + 1, (i + 1, n - i + 1))) {
      dp[j][k][p] = 0;
      if (s[i - j] == s[i + k])
        dp[j][k][p] = (j >= 1 && k >= 1 ? dp[j - 1][k - 1][p] : 0) +
                      (j == 0 && k == 0 ? 1 : 2);
      if (j >= 1)
        dp[j][k][p] = max(dp[j][k][p], dp[j - 1][k][p]);
      if (k >= 1)
        dp[j][k][p] = max(dp[j][k][p], dp[j][k - 1][p]);
      if (s[i - j] != s[i + k] && p)
        dp[j][k][p] = max((j >= 1 && k >= 1 ? dp[j - 1][k - 1][p - 1] : 0) + 2,
                          dp[j][k][p]);
      ans = max(ans, dp[j][k][p]);
    }
  }
  printf("%d", ans);
  return 0;
}

/*
Input:
-----------------
Output:
*/

Fixed submission:
/* --- author: dxm --- */
#include <bits/stdc++.h>
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
typedef long long ll;
inline void Fail() {
  printf("0");
  exit(0);
}

const int maxn = 305;
int n, lim, ans;
char c[maxn];
string s;
int dp[maxn][maxn][maxn];

int main() {
  scanf("%s%d", c, &lim);
  s = c;
  n = s.size();
  REP(len, n) rep(i, n) rep(k, lim + 1) {
    int j = i + len - 1;
    if (j >= n)
      break;
    if (s[i] == s[j])
      dp[i][j][k] = (len >= 3 ? dp[i + 1][j - 1][k] : 0) + (i == j ? 1 : 2);
    if (i < n - 1)
      dp[i][j][k] = max(dp[i][j][k], dp[i + 1][j][k]);
    if (j - 1 > 0)
      dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
    if (s[i] != s[j] && k)
      dp[i][j][k] =
          max(dp[i][j][k], (len >= 3 ? dp[i + 1][j - 1][k - 1] : 0) + 2);
    ans = max(ans, dp[i][j][k]);
  }
  printf("%d", ans);
  return 0;
}

/*
Input:
-----------------
Output:
*/
Problem p03430 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define ld long double
#define int long long
using namespace std;

const int N = 333;

int dp[N][N][N];

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  string s;
  cin >> s;
  int n = s.size();
  int _k;
  cin >> _k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= _k; j++)
      dp[i][i + 1][j] = 1;
  for (int l = 2; l <= n; l++)
    for (int i = 0; i + l <= n; i++)
      for (int k = 0; k <= _k; k++) {
        int j = i + l;
        dp[i][j][k] = max(dp[i + 1][j][k], dp[i][j - 1][k]);
        if (s[i] == s[j - 1])
          dp[i][j][k] = max(2 + dp[i + 1][j - 1][k], dp[i][j][k]);
        else if (k)
          dp[i][j][k] = max(dp[i][j][k], 2 + dp[i + 1][j - 1][k - 1]);
      }
  cout << dp[0][n][_k] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define X first
#define Y second
#define ld long double
// #define int long long
using namespace std;

const int N = 333;

int dp[N][N][N];

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  string s;
  cin >> s;
  int n = s.size();
  int _k;
  cin >> _k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j <= _k; j++)
      dp[i][i + 1][j] = 1;
  for (int l = 2; l <= n; l++)
    for (int i = 0; i + l <= n; i++)
      for (int k = 0; k <= _k; k++) {
        int j = i + l;
        dp[i][j][k] = max(dp[i + 1][j][k], dp[i][j - 1][k]);
        if (s[i] == s[j - 1])
          dp[i][j][k] = max(2 + dp[i + 1][j - 1][k], dp[i][j][k]);
        else if (k)
          dp[i][j][k] = max(dp[i][j][k], 2 + dp[i + 1][j - 1][k - 1]);
      }
  cout << dp[0][n][_k] << endl;
}
Problem p03430 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, n) for (int i = 1; i <= n; i++)
#define all(x) x.begin(), x.end()

#define geti1(X) scanf("%d", &X)
#define geti2(X, Y) scanf("%d%d", &X, &Y)
#define geti3(X, Y, Z) scanf("%d%d%d", &X, &Y, &Z)
#define geti4(X, Y, Z, W) scanf("%d%d%d%d", &X, &Y, &Z, &W)

#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define geti(...)                                                              \
  GET_MACRO(__VA_ARGS__, geti4, geti3, geti2, geti1)(__VA_ARGS__)

#define INF 7654321
#define IINF 87654321987654321LL
#define MAXV 200500

ll mod = 1e9 + 7;

int dp[500][500][500];
string s;
int N, K;

int solve(int l, int r, int k) {

  if (l == r)
    return 1;
  if (l > r)
    return 0;

  int &res = dp[l][r][k];
  if (res != -1)
    return res;
  res = 0;
  if (s[l] == s[r]) {
    res = max(res, 2 + solve(l + 1, r - 1, k));
  } else {
    if (k > 0) {
      res = max(res, 2 + solve(l + 1, r - 1, k - 1));
    }
    res = max(res, solve(l + 1, r, k));
    res = max(res, solve(l, r - 1, k));
  }

  return res;
}

int main() {
  memset(dp, -1, sizeof dp);
  cin >> s >> K;
  N = sz(s);

  cout << solve(0, N - 1, K);
}

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

typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define repp(i, n) for (int i = 1; i <= n; i++)
#define all(x) x.begin(), x.end()

#define geti1(X) scanf("%d", &X)
#define geti2(X, Y) scanf("%d%d", &X, &Y)
#define geti3(X, Y, Z) scanf("%d%d%d", &X, &Y, &Z)
#define geti4(X, Y, Z, W) scanf("%d%d%d%d", &X, &Y, &Z, &W)

#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define geti(...)                                                              \
  GET_MACRO(__VA_ARGS__, geti4, geti3, geti2, geti1)(__VA_ARGS__)

#define INF 7654321
#define IINF 87654321987654321LL
#define MAXV 200500

ll mod = 1e9 + 7;

int dp[301][301][301];
string s;
int N, K;

int solve(int l, int r, int k) {

  if (l == r)
    return 1;
  if (l > r)
    return 0;

  int &res = dp[l][r][k];
  if (res != -1)
    return res;
  res = 0;
  if (s[l] == s[r]) {
    res = max(res, 2 + solve(l + 1, r - 1, k));
  } else {
    if (k > 0) {
      res = max(res, 2 + solve(l + 1, r - 1, k - 1));
    }
    res = max(res, solve(l + 1, r, k));
    res = max(res, solve(l, r - 1, k));
  }

  return res;
}

int main() {
  memset(dp, -1, sizeof dp);
  cin >> s >> K;
  N = sz(s);

  cout << solve(0, N - 1, K);
}
Problem p03430 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define umax(x, y) x = max(x, y)
#define umin(x, y) x = min(x, y)
#define ll long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define iiii pair<ii, ii>
#define sz(x) ((int)x.size())
#define orta ((bas + son) / 2)
#define all(x) x.begin(), x.end()
#define pw(x) (1 << (x))
#define inf 1000000005
#define MOD 924844033
#define N 100005
#define M 1000003
#define LOG 20
#define KOK 250
#define EPS 0.0000001
using namespace std;

int n, k;
bool vis[305][305][305];
int dp[305][305][305];
char s[305];

int solve(int l, int r, int rem) {

  if (rem < 0)
    return -inf;

  if (l >= r)
    return (l == r);

  bool &v = vis[l][r][rem];
  int &res = dp[l][r][rem];

  if (v)
    return res;

  if (s[l] == s[r])
    umax(res, solve(l + 1, r - 1, rem) + 2);

  umax(res, max(solve(l + 1, r, rem), solve(l, r - 1, rem)));

  umax(res, solve(l + 1, r - 1, rem - 1) + 2);

  return res;
}

int main() {

  scanf("%s %d", s + 1, &k);

  n = strlen(s + 1);

  printf("%d", solve(1, n, k));
}

Fixed submission:
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define umax(x, y) x = max(x, y)
#define umin(x, y) x = min(x, y)
#define ll long long
#define ii pair<int, int>
#define iii pair<int, ii>
#define iiii pair<ii, ii>
#define sz(x) ((int)x.size())
#define orta ((bas + son) / 2)
#define all(x) x.begin(), x.end()
#define pw(x) (1 << (x))
#define inf 1000000005
#define MOD 924844033
#define N 100005
#define M 1000003
#define LOG 20
#define KOK 250
#define EPS 0.0000001
using namespace std;

int n, k;
bool vis[305][305][305];
int dp[305][305][305];
char s[305];

int solve(int l, int r, int rem) {

  if (rem < 0)
    return -inf;

  if (l >= r)
    return (l == r);

  bool &v = vis[l][r][rem];
  int &res = dp[l][r][rem];

  if (v)
    return res;

  v = 1;

  if (s[l] == s[r])
    umax(res, solve(l + 1, r - 1, rem) + 2);

  umax(res, max(solve(l + 1, r, rem), solve(l, r - 1, rem)));

  umax(res, solve(l + 1, r - 1, rem - 1) + 2);

  return res;
}

int main() {

  scanf("%s %d", s + 1, &k);

  n = strlen(s + 1);

  printf("%d", solve(1, n, k));
}
Problem p03430 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll int
#define f first
#define s second
#define pb push_back
using namespace std;
ll dp[301][301][301], k;
string s;
inline ll solve(ll x, ll y, ll p) {
  if (dp[x][y][p])
    return dp[x][y][p];
  if (x == y) {
    dp[x][y][p] = 1;
    return dp[x][y][p];
  }
  if (x + 1 == y) {
    if (s[x] == s[y] || p)
      dp[x][y][p] = 2;
    else
      dp[x][y][p] = 1;
    return dp[x][y][p];
  }
  if (p == 0) {
    if (s[x] == s[y]) {
      dp[x][y][p] = solve(x + 1, y - 1, p) + 2;
      return dp[x][y][p];
    }
    ll t = solve(x, y - 1, p);
    ll r = solve(x + 1, y, p);
    return max(t, r);
  }
  if (s[x] == s[y]) {
    dp[x][y][p] = solve(x + 1, y - 1, p) + 2;
    return dp[x][y][p];
  }
  dp[x][y][p] = solve(x + 1, y - 1, p);
  ll t = solve(x, y - 1, p);
  ll r = solve(x + 1, y, p);
  ll q = solve(x + 1, y - 1, p - 1) + 2;
  q = max(q, max(t, r));
  if (dp[x][y][p] < q)
    dp[x][y][p] = q;
  return dp[x][y][p];
}
int main() {
  ios::sync_with_stdio(false);
  cin >> s >> k;

  cout << solve(0, (int)s.size() - 1, k);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll int
#define f first
#define s second
#define pb push_back
using namespace std;
ll dp[301][301][301], k;
string s;
inline ll solve(ll x, ll y, ll p) {
  if (dp[x][y][p])
    return dp[x][y][p];
  if (x == y) {
    dp[x][y][p] = 1;
    return dp[x][y][p];
  }
  if (x + 1 == y) {
    if (s[x] == s[y] || p)
      dp[x][y][p] = 2;
    else
      dp[x][y][p] = 1;
    return dp[x][y][p];
  }
  if (p == 0) {
    if (s[x] == s[y]) {
      dp[x][y][p] = solve(x + 1, y - 1, p) + 2;
      return dp[x][y][p];
    }
    ll t = solve(x, y - 1, p);
    ll r = solve(x + 1, y, p);
    dp[x][y][p] = max(t, r);
    return dp[x][y][p];
  }
  if (s[x] == s[y]) {
    dp[x][y][p] = solve(x + 1, y - 1, p) + 2;
    return dp[x][y][p];
  }
  dp[x][y][p] = solve(x + 1, y - 1, p);
  ll t = solve(x, y - 1, p);
  ll r = solve(x + 1, y, p);
  ll q = solve(x + 1, y - 1, p - 1) + 2;
  q = max(q, max(t, r));
  if (dp[x][y][p] < q)
    dp[x][y][p] = q;
  return dp[x][y][p];
}
int main() {
  ios::sync_with_stdio(false);
  cin >> s >> k;

  cout << solve(0, (int)s.size() - 1, k);

  return 0;
}
Problem p03430 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

string str;
int dp[500][500][500];
int solve(int l, int r, int i) {
  if (r - l == 1) {
    return dp[l][r][0] = 1;
  } else if (r - l == 2) {
    dp[l][r][i] = 1;
    if (str[l] == str[r - 1] || i > 0)
      dp[l][r][i] = 2;
    return dp[l][r][i];
  }
  if (i == 0) {
    if (str[l] == str[r - 1])
      dp[l][r][0] = dp[l + 1][r - 1][0] + 2;
    else
      dp[l][r][0] = max(dp[l + 1][r][0], dp[l][r - 1][0]);
  } else {
    if (str[l] == str[r - 1])
      dp[l][r][i] = max(dp[l][r][i], dp[l + 1][r - 1][i] + 2);
    int res1 = max(dp[l + 1][r][i], dp[l][r - 1][i]);
    int res2 = dp[l + 1][r - 1][i - 1] + 2;
    int res = max(res1, res2);
    dp[l][r][i] = max(dp[l][r][i], res);
  }
  return dp[l][r][i];
}

int main() {
  int k, ans = 0;
  cin >> str >> k;
  int n = str.size();
  for (int i = 0; i <= k; i++) {
    for (int l = n - 1; l >= 0; l--) {
      for (int r = l + 1; r <= n; r++) {
        int res = solve(l, r, i);
        ans = max(ans, res);
      }
    }
  }

  /*for(int i = 0;i < n;i++){
          for(int j = 0;j <= n;j++)printf("%d ", dp[i][j][0]);
          printf("\n");
  }*/
  printf("%d\n", ans);
  return 0;
}

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

string str;
int dp[302][302][302];
int solve(int l, int r, int i) {
  if (r - l == 1) {
    return dp[l][r][0] = 1;
  } else if (r - l == 2) {
    dp[l][r][i] = 1;
    if (str[l] == str[r - 1] || i > 0)
      dp[l][r][i] = 2;
    return dp[l][r][i];
  }
  if (i == 0) {
    if (str[l] == str[r - 1])
      dp[l][r][0] = dp[l + 1][r - 1][0] + 2;
    else
      dp[l][r][0] = max(dp[l + 1][r][0], dp[l][r - 1][0]);
  } else {
    if (str[l] == str[r - 1])
      dp[l][r][i] = max(dp[l][r][i], dp[l + 1][r - 1][i] + 2);
    int res1 = max(dp[l + 1][r][i], dp[l][r - 1][i]);
    int res2 = dp[l + 1][r - 1][i - 1] + 2;
    int res = max(res1, res2);
    dp[l][r][i] = max(dp[l][r][i], res);
  }
  return dp[l][r][i];
}

int main() {
  int k, ans = 0;
  cin >> str >> k;
  int n = str.size();
  for (int i = 0; i <= k; i++) {
    for (int l = n - 1; l >= 0; l--) {
      for (int r = l + 1; r <= n; r++) {
        int res = solve(l, r, i);
        ans = max(ans, res);
      }
    }
  }

  /*for(int i = 0;i < n;i++){
          for(int j = 0;j <= n;j++)printf("%d ", dp[i][j][0]);
          printf("\n");
  }*/
  printf("%d\n", ans);
  return 0;
}
Problem p03431 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
const int N = 500007, P = 998244353;
int fac[N], ifac[N];
void inc(int &a, int b) { a += b - P, a += a >> 31 & P; }
int mod(int x) { return x + (x >> 31 & P); }
int pow(int a, int b) {
  int r = 1;
  for (; b; b >>= 1, a = 1ll * a * a % P)
    if (b & 1)
      r = 1ll * a * r % P;
  return r;
}
int C(int n, int m) { return 1ll * fac[n] * ifac[m] % P * ifac[n - m] % P; }
int main() {
  int n, k, ans = 0;
  scanf("%d%d", &n, &k);
  for (int i = fac[0] = 1; i <= k; ++i)
    fac[i] = 1ll * fac[i - 1] * i % P;
  ifac[k] = pow(fac[k], P - 2);
  for (int i = k; i; --i)
    ifac[i - 1] = 1ll * ifac[i] * i % P;
  for (int i = 0, f; i + i <= k && i + n <= k; ++i)
    f = (i + i == k),
    inc(ans, mod(C(k - f, i - f) - C(k - f, i + i + n - k - 1 - f)));
  printf("%d", ans);
}


Fixed submission:
#include <cstdio>
const int N = 500007, P = 998244353;
int fac[N], ifac[N];
void inc(int &a, int b) { a += b - P, a += a >> 31 & P; }
int mod(int x) { return x + (x >> 31 & P); }
int pow(int a, int b) {
  int r = 1;
  for (; b; b >>= 1, a = 1ll * a * a % P)
    if (b & 1)
      r = 1ll * a * r % P;
  return r;
}
int C(int n, int m) {
  return n < m || m < 0 ? 0 : 1ll * fac[n] * ifac[m] % P * ifac[n - m] % P;
}
int main() {
  int n, k, ans = 0;
  scanf("%d%d", &n, &k);
  for (int i = fac[0] = 1; i <= k; ++i)
    fac[i] = 1ll * fac[i - 1] * i % P;
  ifac[k] = pow(fac[k], P - 2);
  for (int i = k; i; --i)
    ifac[i - 1] = 1ll * ifac[i] * i % P;
  for (int i = 0, f; i + i <= k && i + n <= k; ++i)
    f = (i + i == k),
    inc(ans, mod(C(k - f, i - f) - C(k - f, i + i + n - k - 1 - f)));
  printf("%d", ans);
}