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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int dp[1000][1000]; // minimum edit distance
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s1;
  string s2;

  cin >> s1;
  cin >> s2;

  // init. dp table
  for (int i = 0; i <= s1.length(); i++) {
    dp[i][0] = i;
  }
  for (int j = 0; j <= s2.length(); j++) {
    dp[0][j] = j;
  }

  for (int i = 1; i <= s1.length(); i++) {
    for (int j = 1; j <= s2.length(); j++) {
      int RepCost = s1[i - 1] == s2[j - 1] ? 0 : 1;
      dp[i][j] = min(min(dp[i][j - 1] + 1,      // insertion
                         dp[i - 1][j] + 1),     // deletion
                     dp[i - 1][j - 1] + RepCost // replacement
      );
    }
  }

  /*
      for(int i=0; i<=s1.length(); i++){
          for(int j=0; j<=s2.length(); j++){
              cout << dp[i][j] << " ";
          }
          cout << "\n";
      }
  */
  cout << dp[s1.length()][s2.length()] << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int dp[1001][1001]; // minimum edit distance
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s1;
  string s2;

  cin >> s1;
  cin >> s2;

  // init. dp table
  for (int i = 0; i <= s1.length(); i++) {
    dp[i][0] = i;
  }
  for (int j = 0; j <= s2.length(); j++) {
    dp[0][j] = j;
  }

  for (int i = 1; i <= s1.length(); i++) {
    for (int j = 1; j <= s2.length(); j++) {
      int RepCost = s1[i - 1] == s2[j - 1] ? 0 : 1;
      dp[i][j] = min(min(dp[i][j - 1] + 1,      // insertion
                         dp[i - 1][j] + 1),     // deletion
                     dp[i - 1][j - 1] + RepCost // replacement
      );
    }
  }

  /*
      for(int i=0; i<=s1.length(); i++){
          for(int j=0; j<=s2.length(); j++){
              cout << dp[i][j] << " ";
          }
          cout << "\n";
      }
  */
  cout << dp[s1.length()][s2.length()] << "\n";

  return 0;
}
Problem p02318 (C++)

Buggy submission (status: Runtime Error):
class _in {
  struct my_iterator {
    int it;
    const bool rev;
    explicit constexpr my_iterator(int it_, bool rev = false)
        : it(it_), rev(rev) {}
    constexpr int operator*() { return it; }
    constexpr bool operator!=(my_iterator &r) { return it != r.it; }
    void operator++() { rev ? --it : ++it; }
  };
  const my_iterator i, n;

public:
  explicit constexpr _in(int n) : i(0), n(n) {}
  explicit constexpr _in(int i, int n) : i(i, n < i), n(n) {}
  constexpr const my_iterator &begin() { return i; }
  constexpr const my_iterator &end() { return n; }
};

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

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int N = s1.size(), M = s2.size();
  vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));
  for (int i : _in(N + 1))
    dp[i][0] = i;
  for (int j : _in(N + 1))
    dp[0][j] = j;
  for (int i : _in(N))
    for (int j : _in(M)) {
      dp[i + 1][j + 1] = dp[i][j];
      if (s1[i] != s2[j])
        ++dp[i + 1][j + 1];
      int temp = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1);
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], temp);
    }
  cout << dp[N][M] << endl;
}

Fixed submission:
class _in {
  struct my_iterator {
    int it;
    const bool rev;
    explicit constexpr my_iterator(int it_, bool rev = false)
        : it(it_), rev(rev) {}
    constexpr int operator*() { return it; }
    constexpr bool operator!=(my_iterator &r) { return it != r.it; }
    void operator++() { rev ? --it : ++it; }
  };
  const my_iterator i, n;

public:
  explicit constexpr _in(int n) : i(0), n(n) {}
  explicit constexpr _in(int i, int n) : i(i, n < i), n(n) {}
  constexpr const my_iterator &begin() { return i; }
  constexpr const my_iterator &end() { return n; }
};

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

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int N = s1.size(), M = s2.size();
  vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));
  for (int i : _in(N + 1))
    dp[i][0] = i;
  for (int j : _in(M + 1))
    dp[0][j] = j;
  for (int i : _in(N))
    for (int j : _in(M)) {
      dp[i + 1][j + 1] = dp[i][j];
      if (s1[i] != s2[j])
        ++dp[i + 1][j + 1];
      int temp = min(dp[i][j + 1] + 1, dp[i + 1][j] + 1);
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], temp);
    }
  cout << dp[N][M] << endl;
}
Problem p02318 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;

int A[1000][1000];

int main() {
  string s1, s2;
  cin >> s1;
  cin >> s2;

  for (int i = 0; i <= s1.size(); ++i) {
    for (int j = 0; j <= s2.size(); ++j) {
      if (i == 0) {
        A[0][j] = j;
        continue;
      }
      if (j == 0) {
        A[i][0] = i;
        continue;
      } else {
        if (s1[i - 1] == s2[j - 1]) {
          A[i][j] = A[i - 1][j - 1];
        } else {
          A[i][j] = min(A[i][j - 1], A[i - 1][j]) + 1;
          A[i][j] = min(A[i][j], A[i - 1][j - 1] + 1);
        }
      }
      // cout<<A[i][j]<<" ";
    }
    // cout<<endl;
  }

  cout << A[s1.size()][s2.size()] << endl;
}


Fixed submission:
#include <iostream>
#include <string>
using namespace std;

int A[1001][1001];

int main() {
  string s1, s2;
  cin >> s1;
  cin >> s2;

  for (int i = 0; i <= s1.size(); ++i) {
    for (int j = 0; j <= s2.size(); ++j) {
      if (i == 0) {
        A[0][j] = j;
        continue;
      }
      if (j == 0) {
        A[i][0] = i;
        continue;
      } else {
        if (s1[i - 1] == s2[j - 1]) {
          A[i][j] = A[i - 1][j - 1];
        } else {
          A[i][j] = min(A[i][j - 1], A[i - 1][j]) + 1;
          A[i][j] = min(A[i][j], A[i - 1][j - 1] + 1);
        }
      }
      // cout<<A[i][j]<<" ";
    }
    // cout<<endl;
  }

  cout << A[s1.size()][s2.size()] << endl;
}
Problem p02318 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
int main() { return 2; }


Fixed submission:
#include <iostream>
#include <string>
int main() {

  int i, j;
  std::string x, y;
  int a[1001][1001];
  int knum;
  for (i = 0; i < 1001; i++) {
    for (j = 0; j < 1001; j++) {
      if (j == 0)
        a[i][j] = i;
      else if (i == 0)
        a[i][j] = j;
      else
        a[i][j] = 0;
    }
  }
  std::cin >> x;
  std::cin >> y;
  int vx, vy, vz;
  for (i = 1; i < x.size() + 1; i++) {
    for (j = 1; j < y.size() + 1; j++) {
      vx = a[i - 1][j] + 1;
      vy = a[i][j - 1] + 1;
      vz = a[i - 1][j - 1];
      if (x[i - 1] == y[j - 1]) {
        if (vx <= vy && vx <= vz) {
          a[i][j] = vx;
        } else if (vy <= vx && vy <= vz) {
          a[i][j] = vy;
        } else {
          a[i][j] = vz;
        }
      } else {
        if (vx <= vy && vx <= vz + 1) {
          a[i][j] = vx;
        } else if (vy <= vx && vy <= vz + 1) {
          a[i][j] = vy;
        } else {
          a[i][j] = vz + 1;
        }
      }
    }
    knum = a[x.size()][y.size()];
  }
  std::cout << knum << "\n";
  return 0;
}
Problem p02318 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define ll long long
#define inf 1999999999
#define pa pair<int, int>
using namespace std;

int main() {
  // cout<<"1"<<endl;
  string s, t;
  cin >> s >> t;
  int ls = s.length(), lt = t.length();
  // cout<<ls<<endl;
  // cout<<s<<endl;

  int dp[101][101];
  for (int i = 0; i <= max(ls, lt); i++) {
    dp[i][0] = i;
    dp[0][i] = i;
  }

  for (int i = 1; i <= ls; i++) {
    for (int j = 1; j <= lt; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] =
            min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1]);
      else
        dp[i][j] =
            min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + 1);
      // cout<<dp[i][j]<<endl;
    }
  }
  cout << dp[ls][lt] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define ll long long
#define inf 1999999999
#define pa pair<int, int>
using namespace std;

int main() {
  // cout<<"1"<<endl;
  string s, t;
  cin >> s >> t;
  int ls = s.length(), lt = t.length();
  // cout<<ls<<endl;
  // cout<<s<<endl;

  int dp[1010][1010];
  for (int i = 0; i <= max(ls, lt); i++) {
    dp[i][0] = i;
    dp[0][i] = i;
  }

  for (int i = 1; i <= ls; i++) {
    for (int j = 1; j <= lt; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] =
            min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1]);
      else
        dp[i][j] =
            min(min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + 1);
      // cout<<dp[i][j]<<endl;
    }
  }
  cout << dp[ls][lt] << endl;

  return 0;
}
Problem p02318 (C++)

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

int main() {
  string A, B;
  cin >> A >> B;
  int a = A.length(), b = B.length();
  vector<vector<int>> Dist(a + 1, vector<int>(b, 0));
  for (int i = 0; i <= a; i++)
    Dist[i][0] = i;
  for (int i = 0; i <= b; i++)
    Dist[0][i] = i;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      Dist[i + 1][j + 1] = min({Dist[i + 1][j] + 1, Dist[i][j + 1] + 1,
                                Dist[i][j] + (A[i] != B[j])});
    }
  }
  cout << Dist[a][b] << "\n";
}


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

int main() {
  string A, B;
  cin >> A >> B;
  int a = A.length(), b = B.length();
  vector<vector<int>> Dist(a + 1, vector<int>(b + 1, 0));
  for (int i = 0; i <= a; i++)
    Dist[i][0] = i;
  for (int i = 0; i <= b; i++)
    Dist[0][i] = i;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      Dist[i + 1][j + 1] = min({Dist[i + 1][j] + 1, Dist[i][j + 1] + 1,
                                Dist[i][j] + (A[i] != B[j])});
    }
  }
  cout << Dist[a][b] << "\n";
}
Problem p02318 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;

int main() {
  string s1, s2;
  cin >> s1 >> s2;

  int dist[1000][1000];
  for (int i = 0; i <= s1.size(); ++i) {
    for (int j = 0; j <= s2.size(); ++j) {
      if (i == 0) {
        dist[i][j] = j;
      } else if (j == 0) {
        dist[i][j] = i;
      } else {
        int cost = (s1[i - 1] == s2[j - 1] ? 0 : 1);
        int add = dist[i - 1][j] + 1;
        int dlt = dist[i][j - 1] + 1;
        int exch = dist[i - 1][j - 1] + cost; // why * is necessary

        if (add <= dlt && add <= exch) {
          dist[i][j] = add;
        } else if (dlt <= add && dlt <= exch) {
          dist[i][j] = dlt;
        } else {
          dist[i][j] = exch;
        }
      }
    }
  }
  cout << dist[s1.size()][s2.size()] << "\n";
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

int main() {
  string s1, s2;
  cin >> s1 >> s2;

  int dist[1001][1001];
  for (int i = 0; i <= s1.size(); ++i) {
    for (int j = 0; j <= s2.size(); ++j) {
      if (i == 0) {
        dist[i][j] = j;
      } else if (j == 0) {
        dist[i][j] = i;
      } else {
        int cost = (s1[i - 1] == s2[j - 1] ? 0 : 1);
        int add = dist[i - 1][j] + 1;
        int dlt = dist[i][j - 1] + 1;
        int exch = dist[i - 1][j - 1] + cost; // why * is necessary

        if (add <= dlt && add <= exch) {
          dist[i][j] = add;
        } else if (dlt <= add && dlt <= exch) {
          dist[i][j] = dlt;
        } else {
          dist[i][j] = exch;
        }
      }
    }
  }
  cout << dist[s1.size()][s2.size()] << "\n";
}
Problem p02318 (C++)

Buggy submission (status: Runtime Error):
/*
 * Contents   : AOJ DPL 1 E
 * Author     : KitauraHiromi
 * LastUpdate : 20180614
 * Since      : 20180614
 */
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

string S, T;
int INF = 1e9 + 1;
int dp[1001][1001];
int main(void) {
  cin >> S >> T;
  Fill(dp, INF);
  dp[0][0] = 0;
  for (int i = -1; i <= (int)S.size(); ++i) {
    for (int j = -1; j <= (int)T.size(); ++j) {
      if (i == -1 && j == -1)
        continue;
      // 変更
      if (i >= 0 && j >= 0) {
        if (S[i] == T[j])
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
        else
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + 1);
      }
      // 削除
      if (i >= 0)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j + 1] + 1);
      // 追加
      if (j >= 0)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i + 1][j] + 1);
    }
  }
  cout << dp[S.size()][T.size()] << endl;
  return 0;
}


Fixed submission:
/*
 * Contents   : AOJ DPL 1 E
 * Author     : KitauraHiromi
 * LastUpdate : 20180614
 * Since      : 20180614
 */
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

string S, T;
int INF = 1e9 + 1;
int dp[1010][1010];
int main(void) {
  cin >> S >> T;
  Fill(dp, INF);
  dp[0][0] = 0;
  for (int i = -1; i <= (int)S.size(); ++i) {
    for (int j = -1; j <= (int)T.size(); ++j) {
      if (i == -1 && j == -1)
        continue;
      // 変更
      if (i >= 0 && j >= 0) {
        if (S[i] == T[j])
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
        else
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + 1);
      }
      // 削除
      if (i >= 0)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j + 1] + 1);
      // 追加
      if (j >= 0)
        dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i + 1][j] + 1);
    }
  }
  cout << dp[S.size()][T.size()] << endl;
  return 0;
}
Problem p02318 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>

int main() {
  std::string str1;
  std::string str2;
  std::cin >> str1;
  std::cin >> str2;
  int A[1000][1000] = {0};
  for (int i = 0; i <= str1.size(); i++) {
    A[i][0] = i;
  }
  for (int j = 0; j <= str2.size(); j++) {
    A[0][j] = j;
  }
  for (int i = 1; i <= str1.size(); i++) {
    for (int j = 1; j <= str2.size(); j++) {
      if (str1[i - 1] == str2[j - 1]) {
        A[i][j] = A[i - 1][j - 1];
      } else {
        A[i][j] = std::min(std::min(A[i - 1][j] + 1, A[i][j - 1] + 1),
                           A[i - 1][j - 1] + 1);
      }
    }
  }
  std::cout << A[str1.size()][str2.size()] << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>

int main() {
  std::string str1;
  std::string str2;
  std::cin >> str1;
  std::cin >> str2;
  int A[1001][1001] = {0};
  for (int i = 0; i <= str1.size(); i++) {
    A[i][0] = i;
  }
  for (int j = 0; j <= str2.size(); j++) {
    A[0][j] = j;
  }
  for (int i = 1; i <= str1.size(); i++) {
    for (int j = 1; j <= str2.size(); j++) {
      if (str1[i - 1] == str2[j - 1]) {
        A[i][j] = A[i - 1][j - 1];
      } else {
        A[i][j] = std::min(std::min(A[i - 1][j] + 1, A[i][j - 1] + 1),
                           A[i - 1][j - 1] + 1);
      }
    }
  }
  std::cout << A[str1.size()][str2.size()] << "\n";
  return 0;
}
Problem p02318 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int min(int a, int b, int c) {
  int t = a < b ? a : b;
  return t < c ? t : c;
}
int main() {
  string s1, s2;
  int d[1000][1000];
  cin >> s1 >> s2;
  int n = s1.length();
  int m = s2.length();
  if (n == 0) {
    return n;
  }
  if (m == 0) {
    return m;
  }
  for (int i = 0; i <= n; i++) {
    d[i][0] = i;
  }
  for (int j = 0; j <= m; j++) {
    d[0][j] = j;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1]) {
        d[i][j] = d[i - 1][j - 1];
      } else {
        d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + 1);
      }
    }
  }
  cout << d[n][m] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int min(int a, int b, int c) {
  int t = a < b ? a : b;
  return t < c ? t : c;
}
int main() {
  string s1, s2;
  int d[1010][1010];
  cin >> s1 >> s2;
  int n = s1.length();
  int m = s2.length();
  if (n == 0) {
    return n;
  }
  if (m == 0) {
    return m;
  }
  for (int i = 0; i <= n; i++) {
    d[i][0] = i;
  }
  for (int j = 0; j <= m; j++) {
    d[0][j] = j;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1]) {
        d[i][j] = d[i - 1][j - 1];
      } else {
        d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + 1);
      }
    }
  }
  cout << d[n][m] << endl;
}
Problem p02319 (C++)

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

int main() {
  int n, w = 0;
  std::cin >> n >> w;
  std::vector<std::vector<long>> dp(n + 1, std::vector<long>(n * 100 + 1, 0));
  dp[0][0] = 0;
  for (int i = 1; i < dp[0].size(); ++i) {
    dp[0][i] = LONG_MAX;
  }
  std::vector<int> iv;
  std::vector<long> iw;
  for (int i = 0; i < n; ++i) {
    int a, b = 0;
    std::cin >> a >> b;
    iv.emplace_back(a);
    iw.emplace_back(b);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= dp[0].size(); ++j) {
      if (j < iv[i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = std::min(dp[i - 1][j], dp[i - 1][j - iv[i - 1]] + iw[i - 1]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < dp[0].size(); ++i) {
    if (dp[n][i] <= w)
      ans = i;
  }
  std::cout << ans << std::endl;
  return 0;
}


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

int main() {
  int n, w = 0;
  std::cin >> n >> w;
  std::vector<std::vector<long>> dp(n + 1, std::vector<long>(n * 100 + 1, 0));
  dp[0][0] = 0;
  for (int i = 1; i < dp[0].size(); ++i) {
    dp[0][i] = 1000000001;
  }
  std::vector<int> iv;
  std::vector<long> iw;
  for (int i = 0; i < n; ++i) {
    int a, b = 0;
    std::cin >> a >> b;
    iv.emplace_back(a);
    iw.emplace_back(b);
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= dp[0].size(); ++j) {
      if (j < iv[i - 1]) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = std::min(dp[i - 1][j], dp[i - 1][j - iv[i - 1]] + iw[i - 1]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < dp[0].size(); ++i) {
    if (dp[n][i] <= w)
      ans = i;
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p02319 (C++)

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

using P = pair<ll, ll>;
#define v first
#define w second

// v/w 高い順
bool comp1(P a, P b) { return a.v * b.w > b.v * a.w; }

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  ll N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  sort(all(a), comp1);
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (auto &s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (auto &s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
    assert(dp.size() <= N * N);
  }
  cout << dp[0].v << endl;
  return 0;
}


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

using P = pair<ll, ll>;
#define v first
#define w second

// v/w 高い順
bool comp1(P a, P b) { return a.v * b.w > b.v * a.w; }

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  ll N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  sort(all(a), comp1);
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (auto &s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (auto &s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
    assert(dp.size() <= N * N * 2);
  }
  cout << dp[0].v << endl;
  return 0;
}
Problem p02320 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  int N, W, v_, w, m, x = 0;
  scanf("%d%d", &N, &W);
  vector<pair<int, int>> v;
  for (; N--;) {
    scanf("%d%d%d", &v_, &w, &m);
    for (int i = 1; i < m; m -= i, i *= 2)
      v.emplace_back(v_ * i, w * i);
    if (m)
      v.emplace_back(v_ * m, w * m);
  }
  vector<int> bag(W + 1);
  bag[0] = 1;
  for (auto &e : v) {
    x += e.second;
    for (int j = x; j >= e.second; j--)
      if (bag[j - e.second] && bag[j] < bag[j - e.second] + e.first)
        bag[j] = bag[j - e.second] + e.first;
  }
  for (int i = 0; i < W; i++)
    bag[i + 1] = max(bag[i + 1], bag[i]);
  printf("%d\n", bag[W] - 1);
}

Fixed submission:
#include <cstdio>
#include <vector>
using namespace std;

int main() {
  int N, W, v_, w, m, x = 0;
  scanf("%d%d", &N, &W);
  vector<pair<int, int>> v;
  for (; N--;) {
    scanf("%d%d%d", &v_, &w, &m);
    for (int i = 1; i < m; m -= i, i *= 2)
      v.emplace_back(v_ * i, w * i);
    if (m)
      v.emplace_back(v_ * m, w * m);
  }
  vector<int> bag(W + 1);
  bag[0] = 1;
  for (auto &e : v) {
    x += e.second;
    if (x > W)
      x = W;
    for (int j = x; j >= e.second; j--)
      if (bag[j - e.second] && bag[j] < bag[j - e.second] + e.first)
        bag[j] = bag[j - e.second] + e.first;
  }
  for (int i = 0; i < W; i++)
    bag[i + 1] = max(bag[i + 1], bag[i]);
  printf("%d\n", bag[W] - 1);
}
Problem p02323 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

int v, n, dis[15][15];
int dp[(1 << 15)][15];
const int INF = 100000000;

int main() {
  cin >> v >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      dis[i][j] = INF;
  for (int i = 0; i < (1 << v); i++)
    for (int j = 0; j < v; j++)
      dp[i][j] = INF;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    dis[a][b] = c;
  }
  dp[0][0] = 0;
  for (int i = 0; i < (1 << v) - 1; i++) {
    for (int j = 0; j < v; j++) {
      if (i && !(i & (1 << j)))
        continue;
      for (int k = 0; k < v; k++) {
        if (j == k)
          continue;
        if (!(i & (1 << k))) {
          dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + dis[j][k]);
        }
      }
    }
  }
  if (dp[(1 << v) - 1][0] == INF)
    cout << -1 << endl;
  else
    cout << dp[(1 << v) - 1][0] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

int v, n, dis[15][15];
int dp[(1 << 15)][15];
const int INF = 100000000;

int main() {
  cin >> v >> n;
  for (int i = 0; i < v; i++)
    for (int j = 0; j < v; j++)
      dis[i][j] = INF;
  for (int i = 0; i < (1 << v); i++)
    for (int j = 0; j < v; j++)
      dp[i][j] = INF;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    dis[a][b] = c;
  }
  dp[0][0] = 0;
  for (int i = 0; i < (1 << v) - 1; i++) {
    for (int j = 0; j < v; j++) {
      if (i && !(i & (1 << j)))
        continue;
      for (int k = 0; k < v; k++) {
        if (j == k)
          continue;
        if (!(i & (1 << k))) {
          dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + dis[j][k]);
        }
      }
    }
  }
  if (dp[(1 << v) - 1][0] == INF)
    cout << -1 << endl;
  else
    cout << dp[(1 << v) - 1][0] << endl;
  return 0;
}
Problem p02323 (C++)

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

using namespace std;

const int V_MAX = 15;
const int INF = 10000000;

int n, e;
int g[V_MAX][V_MAX];
int dp[1 << V_MAX][V_MAX];

int tsp(int visited, int v) {
  if (dp[visited][v] != -1)
    return dp[visited][v];

  if (visited == (1 << n) - 1 && v == 0)
    return 0;

  int res = INF;
  for (int u = 0; u < n; ++u) {
    if (!(visited & (1 << u)) && g[v][u] != INF) {
      res = min(res, tsp(visited | (1 << u), u) + g[v][u]);
    }
  }
  return res;
}

int main() {
  scanf("%d %d", &n, &e);
  fill(&g[0][0], &g[n][0], INF);
  for (int i = 0; i < e; ++i) {
    int s, t, d;
    scanf("%d %d %d", &s, &t, &d);
    g[s][t] = d;
  }

  fill(&dp[0][0], &dp[1 << V_MAX][0], -1);
  int d = tsp(0, 0);
  if (d == INF)
    printf("-1\n");
  else
    printf("%d\n", d);

  return 0;
}

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

using namespace std;

const int V_MAX = 15;
const int INF = 10000000;

int n, e;
int g[V_MAX][V_MAX];
int dp[1 << V_MAX][V_MAX];

int tsp(int visited, int v) {
  if (dp[visited][v] != -1)
    return dp[visited][v];

  if (visited == (1 << n) - 1 && v == 0)
    return 0;

  int res = INF;
  for (int u = 0; u < n; ++u) {
    if (!(visited & (1 << u)) && g[v][u] != INF) {
      res = min(res, tsp(visited | (1 << u), u) + g[v][u]);
    }
  }
  return dp[visited][v] = res;
}

int main() {
  scanf("%d %d", &n, &e);
  fill(&g[0][0], &g[n][0], INF);
  for (int i = 0; i < e; ++i) {
    int s, t, d;
    scanf("%d %d %d", &s, &t, &d);
    g[s][t] = d;
  }

  fill(&dp[0][0], &dp[1 << V_MAX][0], -1);
  int d = tsp(0, 0);
  if (d == INF)
    printf("-1\n");
  else
    printf("%d\n", d);

  return 0;
}
Problem p02323 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 200005
#define MAX_N 15
#define INF 10000005

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int V, E;
ll K;
int d[MAX_N][MAX_N];
int dp[1 << MAX_N][MAX_N];
int color[MAX_N];

void solve() {
  for (int S = 0; S < 1 << V; S++) {
    fill(dp[S], dp[S] + V, INF);
  }
  dp[(1 << V) - 1][0] = 0;
  for (int S = (1 << V) - 2; S >= 0; S--) {
    for (int v = 0; v < V; v++) {
      for (int u = 0; u < V; u++) {
        if (!(S >> u & 1)) {
          dp[S][v] = min(dp[S][v], dp[S | 1 << u][u] + d[v][u]);
        }
      }
    }
  }
  if (dp[0][0] >= INF)
    printf("-1\n");
  else
    printf("%d\n", dp[0][0]);
}

// dfsで塗る
int dfs(int v, int a) {
  color[v] = 1;
  a++;
  for (int i = 0; i < V; i++) {
    if (d[v][i] != INF && color[i] == 0) {
      a = dfs(i, a);
    }
  }
  return a;
}

int main() {
  int s, t, dd;
  scanf("%d %d", &V, &E);
  if (E == 0) {
    printf("-1\n");
    return 0;
  }
  for (int i = 0; i <= V; i++) {
    fill(d[i], d[i] + V + 1, INF);
  }
  for (int i = 0; i < E; i++) {
    scanf("%d%d%d", &s, &t, &dd);
    d[s][t] = dd;
  }
  // printf("%d\n",dfs(0,0));
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define SIZE 200005
#define MAX_N 16
#define INF 10000005

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int V, E;
ll K;
int d[MAX_N][MAX_N];
int dp[1 << MAX_N][MAX_N];
int color[MAX_N];

void solve() {
  for (int S = 0; S < 1 << V; S++) {
    fill(dp[S], dp[S] + V, INF);
  }
  dp[(1 << V) - 1][0] = 0;
  for (int S = (1 << V) - 2; S >= 0; S--) {
    for (int v = 0; v < V; v++) {
      for (int u = 0; u < V; u++) {
        if (!(S >> u & 1)) {
          dp[S][v] = min(dp[S][v], dp[S | 1 << u][u] + d[v][u]);
        }
      }
    }
  }
  if (dp[0][0] >= INF)
    printf("-1\n");
  else
    printf("%d\n", dp[0][0]);
}

// dfsで塗る
int dfs(int v, int a) {
  color[v] = 1;
  a++;
  for (int i = 0; i < V; i++) {
    if (d[v][i] != INF && color[i] == 0) {
      a = dfs(i, a);
    }
  }
  return a;
}

int main() {
  int s, t, dd;
  scanf("%d %d", &V, &E);
  if (E == 0) {
    printf("-1\n");
    return 0;
  }
  for (int i = 0; i <= V; i++) {
    fill(d[i], d[i] + V + 1, INF);
  }
  for (int i = 0; i < E; i++) {
    scanf("%d%d%d", &s, &t, &dd);
    d[s][t] = dd;
  }
  // printf("%d\n",dfs(0,0));
  solve();
  return 0;
}
Problem p02323 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int inf = 987654321;
int n, m;
vector<vector<int>> G;
vector<vector<int>> dp;

int rec(int visited, int now) {
  if (dp[visited][now] != -1)
    return dp[visited][now];
  if (visited == (1 << n) - 1 && now == 0)
    return dp[visited][now] = 0;
  int res = inf;
  for (int i = 0; i < n; i++) {
    if (visited >> i & 1)
      continue; /* ??¢????¨??????? */
    res = min(res, rec(visited | (1 << i), i) + G[now][i]);
  }
  return dp[visited][now] = res;
}

int main(void) {
  cin >> n >> m;
  G.assign(n, vector<int>(m, inf));
  for (int i = 0; i < m; i++) {
    int p, q, c;
    cin >> p >> q >> c;
    G[p][q] = c;
  }
  dp.assign(1 << n, vector<int>(n, -1));
  int res = rec(0, 0);
  if (res != inf)
    cout << res << endl;
  else
    cout << -1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int inf = 987654321;
int n, m;
vector<vector<int>> G;
vector<vector<int>> dp;

int rec(int visited, int now) {
  if (dp[visited][now] != -1)
    return dp[visited][now];
  if (visited == (1 << n) - 1 && now == 0)
    return dp[visited][now] = 0;
  int res = inf;
  for (int i = 0; i < n; i++) {
    if (visited >> i & 1)
      continue; /* ??¢????¨??????? */
    res = min(res, rec(visited | (1 << i), i) + G[now][i]);
  }
  return dp[visited][now] = res;
}

int main(void) {
  cin >> n >> m;
  G.assign(n, vector<int>(n, inf));
  for (int i = 0; i < m; i++) {
    int p, q, c;
    cin >> p >> q >> c;
    G[p][q] = c;
  }
  dp.assign(1 << n, vector<int>(n, -1));
  int res = rec(0, 0);
  if (res != inf)
    cout << res << endl;
  else
    cout << -1 << endl;
  return 0;
}
Problem p02323 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
        f(v, v', k):- min f(v, v',k - 1) + e(v, v'),

*/
#include <bits/stdc++.h>

using namespace std;

const int INF = (1 << 30) - 1;
const int mxv = 16;
int V, E;
int adj[mxv][mxv], dp[(1 << mxv)][mxv], a, b, c;

int tsp(int v, int bitmask) {

  if (bitmask == (1 << V) - 1)
    return adj[v][0];

  if (dp[bitmask][v] < INF)
    return dp[bitmask][v];

  for (int i = 0; i < V; ++i)
    if (!(bitmask & (1 << i)))
      dp[bitmask][v] =
          min(dp[bitmask][v], adj[v][i] + tsp(i, bitmask | (1 << i)));
  return dp[bitmask][v];
}

int main(void) {

  // freopen("input.in", "r", stdin);
  scanf("%d%d", &V, &E);

  for (int i = 0; i < V; ++i)
    for (int j = 0; j < V; ++j)
      adj[i][j] = INF;

  for (int i = 0; i < (1 << V); ++i)
    for (int j = 0; j < V; ++j)
      dp[i][j] = INF;

  for (int i = 0; i < E; ++i)
    scanf("%d%d%d", &a, &b, &c), adj[a][b] = c;

  int sol = tsp(0, 1);
  printf("%d\n", (sol == INF) ? -1 : sol);

  return 0;
}

Fixed submission:
/*
        f(v, v', k):- min f(v, v',k - 1) + e(v, v'),

*/
#include <bits/stdc++.h>

using namespace std;

const int INF = (1 << 30) - 1;
const int mxv = 16;
int V, E;
int adj[mxv][mxv], dp[(1 << mxv)][mxv], a, b, c;

int tsp(int v, int bitmask) {

  if (bitmask == (1 << V) - 1)
    return adj[v][0];

  if (dp[bitmask][v] < INF)
    return dp[bitmask][v];

  for (int i = 0; i < V; ++i)
    if (!(bitmask & (1 << i)))
      dp[bitmask][v] =
          min(dp[bitmask][v], adj[v][i] + tsp(i, bitmask | (1 << i)));
  return dp[bitmask][v];
}

int main(void) {

  // freopen("input.in", "r", stdin);
  scanf("%d%d", &V, &E);

  for (int i = 0; i < V; ++i)
    for (int j = 0; j < V; ++j)
      adj[i][j] = INF;

  for (int i = 0; i < (1 << V); ++i)
    for (int j = 0; j < V; ++j)
      dp[i][j] = INF;

  for (int i = 0; i < E; ++i)
    scanf("%d%d%d", &a, &b, &c), adj[a][b] = c;

  dp[1][0] = 0;
  for (int i = 1; i < (1 << V); ++i)
    for (int j = 0; j < V; ++j) {
      if (!(i & (1 << j)))
        continue;
      for (int k = 0; k < V; ++k) {
        if (i & (1 << k))
          continue;
        dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + adj[j][k]);
      }
    }

  int solve = INF;
  for (int i = 1; i < V; ++i)
    solve = min(solve, dp[(1 << V) - 1][i] + adj[i][0]);

  printf("%d\n", (solve == INF) ? -1 : solve);

  return 0;
}
Problem p02323 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i >= n; i--)
#define INF 99999999
#define INF_M 2147483647
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int dist[20][20];
int dp[1 << 20][20];
int V, E;
int s, t, d;

int rec(int S, int v) {
  if (dp[S][v] >= 0)
    return dp[S][v];
  if (S == (1 << V) - 1 && v == 0)
    return dp[S][v] = 0;

  int res = INF;
  for (int r = 0; r < V; r++) {
    if (!(S & 1 << r)) {
      res = min(res, rec(S | 1 << r, r) + dist[v][r]);
    }
  }
  return dp[S][v] = res;
}

int main() {
  cin >> V >> E;
  rep(i, 0, 20) rep(j, 0, 20) dist[i][j] = INF;

  rep(i, 0, E) {
    cin >> s >> t >> d;
    dist[s][t] = d;
  }
  memset(dp, -1, sizeof(dp));

  int ans = rec(0, 0);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, a, n) for (int i = a; i >= n; i--)
#define INF 99999999
#define INF_M 2147483647
#define pb(a) push_back(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef vector<pii> VP;

int dy[] = {0, 0, 1, -1};
int dx[] = {1, -1, 0, 0};

int dist[15][15];
int dp[1 << 15][15];
int V, E;
int s, t, d;

int rec(int S, int v) {
  if (dp[S][v] >= 0)
    return dp[S][v];
  if (S == (1 << V) - 1 && v == 0)
    return dp[S][v] = 0;

  int res = INF;
  for (int r = 0; r < V; r++) {
    if (!(S & 1 << r)) {
      res = min(res, rec(S | 1 << r, r) + dist[v][r]);
    }
  }
  return dp[S][v] = res;
}

int main() {
  cin >> V >> E;
  rep(i, 0, 20) rep(j, 0, 20) dist[i][j] = INF;

  rep(i, 0, E) {
    cin >> s >> t >> d;
    dist[s][t] = d;
  }
  memset(dp, -1, sizeof(dp));

  int ans = rec(0, 0);
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}
Problem p02326 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define HMAX 140
#define WMAX 140
using namespace std;
int main() {
  int H, W, ms = 0;
  int dp[HMAX + 10][WMAX + 10] = {};
  cin >> H >> W;
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      cin >> dp[i][j];
      if (dp[i][j] == 0) {
        dp[i][j] = 1;
      } else {
        dp[i][j] = 0;
      }
    }
  }
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      if (dp[i][j] != 0) {
        int m = min(dp[i - 1][j - 1], dp[i - 1][j]);
        dp[i][j] += min(m, dp[i][j - 1]);
        ms = max(ms, dp[i][j]);
      }
    }
  }
  cout << ms * ms << "\n";
  return 0;
}

Fixed submission:
#include <iostream>
#define HMAX 1400
#define WMAX 1400
using namespace std;
int main() {
  int H, W, ms = 0;
  int dp[HMAX + 10][WMAX + 10] = {};
  cin >> H >> W;
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      cin >> dp[i][j];
      if (dp[i][j] == 0) {
        dp[i][j] = 1;
      } else {
        dp[i][j] = 0;
      }
    }
  }
  for (int i = 1; i < H + 1; ++i) {
    for (int j = 1; j < W + 1; ++j) {
      if (dp[i][j] != 0) {
        int m = min(dp[i - 1][j - 1], dp[i - 1][j]);
        dp[i][j] += min(m, dp[i][j - 1]);
        ms = max(ms, dp[i][j]);
      }
    }
  }
  cout << ms * ms << "\n";
  return 0;
}
Problem p02326 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define mo 1005
int a[mo][mo], dp[mo][mo];
int n, m;
int main() {
  while (cin >> n >> m) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
      }
    }
    int maxs = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j])
          dp[i][j] = 0;
        else
          dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
        maxs = max(maxs, dp[i][j]);
        // cout<<dp[i][j]<<' ';
      }
      // cout<<endl;
    }
    cout << maxs * maxs << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mo 1505
int a[mo][mo], dp[mo][mo];
int n, m;
int main() {
  while (cin >> n >> m) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
      }
    }
    int maxs = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j])
          dp[i][j] = 0;
        else
          dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i][j - 1])) + 1;
        maxs = max(maxs, dp[i][j]);
        // cout<<dp[i][j]<<' ';
      }
      // cout<<endl;
    }
    cout << maxs * maxs << endl;
  }
}
Problem p02326 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
int tate[1005][1005] = {};
int yoko[1005][1005] = {};
int dp[1005][1005] = {};
int c[1005][1005];
int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      scanf("%d", &c[i][j]);
  }
  int ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (c[i][j] == 0) {
        tate[i][j] = tate[i - 1][j] + 1;
        yoko[i][j] = yoko[i][j - 1] + 1;
        dp[i][j] = min(tate[i][j], yoko[i][j]);
        dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
      }
      ans = max(ans, dp[i][j] * dp[i][j]);
    }
  }
  printf("%d\n", ans);
  return (0);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int tate[1405][1405] = {};
int yoko[1405][1405] = {};
int dp[1405][1405] = {};
int c[1405][1405];
int main() {
  int h, w;
  scanf("%d %d", &h, &w);
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      scanf("%d", &c[i][j]);
  }
  int ans = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (c[i][j] == 0) {
        tate[i][j] = tate[i - 1][j] + 1;
        yoko[i][j] = yoko[i][j - 1] + 1;
        dp[i][j] = min(tate[i][j], yoko[i][j]);
        dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
      }
      ans = max(ans, dp[i][j] * dp[i][j]);
    }
  }
  printf("%d\n", ans);
  return (0);
}
Problem p02326 (C++)

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

const int MAX = 140;

int main() {
  int H, W;
  int tile[MAX][MAX];
  int ans = 0;

  scanf("%d%d", &H, &W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &tile[i][j]);
    }
  }

  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (tile[y][x] == 1)
        tile[y][x] = 0;
      else if (x < 1 || y < 1)
        tile[y][x] = 1;
      else
        tile[y][x] =
            min(tile[y - 1][x - 1], min(tile[y - 1][x], tile[y][x - 1])) + 1;
      ans = max(tile[y][x], ans);
    }
  }

  printf("%d\n", ans * ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;

const int MAX = 1400;

int main() {
  int H, W;
  int tile[MAX][MAX];
  int ans = 0;

  scanf("%d%d", &H, &W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      scanf("%d", &tile[i][j]);
    }
  }

  for (int y = 0; y < H; y++) {
    for (int x = 0; x < W; x++) {
      if (tile[y][x] == 1)
        tile[y][x] = 0;
      else if (x < 1 || y < 1)
        tile[y][x] = 1;
      else
        tile[y][x] =
            min(tile[y - 1][x - 1], min(tile[y - 1][x], tile[y][x - 1])) + 1;
      ans = max(tile[y][x], ans);
    }
  }

  printf("%d\n", ans * ans);
  return 0;
}
Problem p02327 (C++)

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

int main() {
  int h, w;
  char c;
  int mp[1010][1010] = {0};
  int ans = 0;

  cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      scanf(" %c", &c);
      if (c == '1')
        mp[i][j] = 0;
      if (c == '0')
        mp[i][j] = mp[i - 1][j] + 1;
    }
  }

  int mx;
  int target;
  stack<pair<int, int>> st;
  for (int i = 1; i <= h; i++) {
    mx = 0;

    for (int j = 1; j <= w; j++) {

      if (mx < mp[i][j]) {
        st.push(make_pair(mp[i][j], j));
        mx = mp[i][j];
      }

      else if (mx > mp[i][j]) {
        target = j;
        while (!st.empty()) {
          pair<int, int> f = st.top();
          if (f.first < mp[i][j])
            break; // ...?
          st.pop();
          // Debug
          /*
          cout<<i<<" "<<j<<" "<<f.first<<" "<<f.second<<endl;
          cout<<f.first*(j-f.second)<<endl;
          */
          ans = max(ans, (f.first * (j - f.second)));
          target = f.second;
        }
        if (mp[i][j] > 0)
          st.push(make_pair(mp[i][j], target));
        mx = mp[i][j];
      }
    }

    while (!st.empty()) {
      pair<int, int> f = st.top();
      st.pop();
      // Debug
      // cout<<i<<"  "<<f.second<<endl;
      ans = max(ans, (f.first * (w + 1 - f.second)));
      //  cout<<ans<<endl;
    }
  }

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

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

int main() {
  int h, w;
  char c;
  int mp[1410][1410] = {0};
  int ans = 0;

  cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      scanf(" %c", &c);
      if (c == '1')
        mp[i][j] = 0;
      if (c == '0')
        mp[i][j] = mp[i - 1][j] + 1;
    }
  }

  int mx;
  int target;
  stack<pair<int, int>> st;
  for (int i = 1; i <= h; i++) {
    mx = 0;

    for (int j = 1; j <= w; j++) {

      if (mx < mp[i][j]) {
        st.push(make_pair(mp[i][j], j));
        mx = mp[i][j];
      }

      else if (mx > mp[i][j]) {
        target = j;
        while (!st.empty()) {
          pair<int, int> f = st.top();
          if (f.first < mp[i][j])
            break; // ...?
          st.pop();
          // Debug
          /*
          cout<<i<<" "<<j<<" "<<f.first<<" "<<f.second<<endl;
          cout<<f.first*(j-f.second)<<endl;
          */
          ans = max(ans, (f.first * (j - f.second)));
          target = f.second;
        }
        if (mp[i][j] > 0)
          st.push(make_pair(mp[i][j], target));
        mx = mp[i][j];
      }
    }

    while (!st.empty()) {
      pair<int, int> f = st.top();
      st.pop();
      // Debug
      // cout<<i<<"  "<<f.second<<endl;
      ans = max(ans, (f.first * (w + 1 - f.second)));
      //  cout<<ans<<endl;
    }
  }

  cout << ans << endl;
  return (0);
}
Problem p02327 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

#define MAX_N 2000
int x[MAX_N][MAX_N];
int H, W;
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> x[i][j];
    }
    if (x[i][0] == 1) {
      x[i][0] = 0;
    } else {
      x[i][0] = 1;
    }
    for (int j = 1; j < W; j++) {
      if (x[i][j] == 1) {
        x[i][j] = 0;
      } else {
        x[i][j] = x[i][j - 1] + 1;
      }
    }
  }
  int maxn = 0;
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < H; j++) {
      int X = x[j][i], cnt = 0;
      if (x[j][i] == 0)
        continue;
      for (int k = j; k >= 0; k--) {
        cnt++;
        X = min(X, x[k][i]);
        if (X == 0) {
          break;
        }
        maxn = max(maxn, X * cnt);
      }
    }
  }
  cout << maxn << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

#define MAX_N 2000
int x[MAX_N][MAX_N];
int H, W;
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> x[i][j];
    }
    if (x[i][0] == 1) {
      x[i][0] = 0;
    } else {
      x[i][0] = 1;
    }
    for (int j = 1; j < W; j++) {
      if (x[i][j] == 1) {
        x[i][j] = 0;
      } else {
        x[i][j] = x[i][j - 1] + 1;
      }
    }
  }
  int maxn = 0;
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < H; j++) {
      int X = x[j][i], cnt = 0;
      if (x[j][i] == 0)
        continue;
      for (int k = j; k >= 0; k--) {
        cnt++;
        X = min(X, x[k][i]);
        if (X * (j + 1) <= maxn) {
          break;
        }
        maxn = max(maxn, X * cnt);
      }
    }
  }
  cout << maxn << endl;
  return 0;
}
Problem p02330 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

int N, K;
ll L, R;
ll a[41];

ll bitsum(int b, int offset) {
  ll s = 0;
  for (int i = 1; b > 0; b >>= 1, i += 1) {
    if (b & 1) {
      s += a[offset + i];
    }
  }
  return s;
}

ll solve() {
  int M = N / 2;
  vector<vector<ll>> h(M + 1);
  for (int i = 0; i < (1 << M); ++i) {
    int bc = __builtin_popcount(i);
    h[bc].push_back(bitsum(i, 0));
  }
  for (int i = 1; i <= M; ++i) {
    sort(h[i].begin(), h[i].end());
  }
  ll ans = 0;
  for (int i = 0; i < (1 << (N - M)); ++i) {
    ll s = bitsum(i, M);
    int bc = __builtin_popcount(i);
    if (bc > K) {
      continue;
    }
    ans += upper_bound(h[K - bc].begin(), h[K - bc].end(), R - s) -
           lower_bound(h[K - bc].begin(), h[K - bc].end(), L - s);
  }
  return ans;
}

int main() {
  cin >> N >> K >> L >> R;
  for (int i = 0; i < N; ++i) {
    cin >> a[i + 1];
  }
  cout << solve() << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

int N, K;
ll L, R;
ll a[41];

ll bitsum(int b, int offset) {
  ll s = 0;
  for (int i = 1; b > 0; b >>= 1, i += 1) {
    if (b & 1) {
      s += a[offset + i];
    }
  }
  return s;
}

ll solve() {
  int M = N / 2;
  vector<vector<ll>> h(M + 1);
  for (int i = 0; i < (1 << M); ++i) {
    int bc = __builtin_popcount(i);
    h[bc].push_back(bitsum(i, 0));
  }
  for (int i = 1; i <= M; ++i) {
    sort(h[i].begin(), h[i].end());
  }
  ll ans = 0;
  for (int i = 0; i < (1 << (N - M)); ++i) {
    ll s = bitsum(i, M);
    int bc = __builtin_popcount(i);
    if (K < bc || K - bc > M) {
      continue;
    }
    ans += upper_bound(h[K - bc].begin(), h[K - bc].end(), R - s) -
           lower_bound(h[K - bc].begin(), h[K - bc].end(), L - s);
  }
  return ans;
}

int main() {
  cin >> N >> K >> L >> R;
  for (int i = 0; i < N; ++i) {
    cin >> a[i + 1];
  }
  cout << solve() << endl;
  return 0;
}
Problem p02335 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stack>
using namespace std;
typedef long long ll;

ll mod = 1e9 + 7; // 1e9+9

ll add(ll x, ll y) { return (x + y) % mod; }
ll mul(ll x, ll y) { return (x * y) % mod; }

ll mpow(ll x, ll y) {
  ll v = 1;
  for (; y; x = mul(x, x), y >>= 1)
    if (y & 1)
      v = mul(v, x);
  return v;
}

ll ncr[5000][5000];

ll nCr(ll n, ll r) {
  if (n == r)
    return 1;
  if (r == 0)
    return 1;
  if (ncr[n][r])
    return ncr[n][r];
  ncr[n][r] = (nCr(n - 1, r - 1) + nCr(n - 1, r)) % mod;
  return ncr[n][r];
}

ll nHr(ll n, ll r) { return nCr(n + r - 1, n); }

int main() {
  ll n, k;
  cin >> n >> k;
  cout << nCr(k, n) << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <stack>
using namespace std;
typedef long long ll;

ll mod = 1e9 + 7; // 1e9+9

ll add(ll x, ll y) { return (x + y) % mod; }
ll mul(ll x, ll y) { return (x * y) % mod; }

ll mpow(ll x, ll y) {
  ll v = 1;
  for (; y; x = mul(x, x), y >>= 1)
    if (y & 1)
      v = mul(v, x);
  return v;
}

ll ncr[5000][5000];

ll nCr(ll n, ll r) {
  if (n < r)
    return 0;
  if (n == r)
    return 1;
  if (r == 0)
    return 1;
  if (ncr[n][r])
    return ncr[n][r];
  ncr[n][r] = (nCr(n - 1, r - 1) + nCr(n - 1, r)) % mod;
  return ncr[n][r];
}

ll nHr(ll n, ll r) { return nCr(n + r - 1, n); }

int main() {
  ll n, k;
  cin >> n >> k;
  cout << nCr(k, n) << endl;
  return 0;
}