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

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

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  REP(i, H) cin >> S[i];

  int ans = 0;
  // (i, j) からいける場所の最短経路の最長距離を求める
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#')
        continue;

      vector<vector<int>> dist(H, vector<int>(W, 1e+3));
      dist[i][j] = 0;

      queue<pair<int, int>> q;
      q.push({i, j});

      while (!q.empty()) {
        auto tmp = q.front();
        q.pop();
        int x = tmp.first;
        int y = tmp.second;
        vector<pair<int, int>> next = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        for (auto n : next) {
          int nx = x + n.first;
          int ny = y + n.second;
          if (0 <= nx and ny < H and 0 <= nx and ny < W and S[nx][ny] == '.') {
            if (dist[nx][ny] > dist[x][y] + 1) {
              dist[nx][ny] = dist[x][y] + 1;
              q.push({nx, ny});
            }
          }
        }
      }
      REP(k, H) {
        REP(l, W) {
          if (dist[k][l] != 1e+3)
            ans = max(ans, dist[k][l]);
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}

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

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  REP(i, H) cin >> S[i];

  int ans = 0;
  // (i, j) からいける場所の最短経路の最長距離を求める
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#')
        continue;

      vector<vector<int>> dist(H, vector<int>(W, 1e+3));
      dist[i][j] = 0;

      queue<pair<int, int>> q;
      q.push({i, j});

      while (!q.empty()) {
        auto tmp = q.front();
        q.pop();
        int x = tmp.first;
        int y = tmp.second;
        vector<pair<int, int>> next = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        for (auto n : next) {
          int nx = x + n.first;
          int ny = y + n.second;
          if (0 <= nx and nx < H and 0 <= ny and ny < W and S[nx][ny] == '.') {
            if (dist[nx][ny] > dist[x][y] + 1) {
              dist[nx][ny] = dist[x][y] + 1;
              q.push({nx, ny});
            }
          }
        }
      }
      REP(k, H) {
        REP(l, W) {
          if (dist[k][l] != 1e+3)
            ans = max(ans, dist[k][l]);
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02803 (C++)

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

int h, w, vis[20][20];
string gr[20];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> gr[i];
  int mx = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (gr[i][j] == '#')
        continue;
      for (int k = 0; k < h; k++)
        for (int l = 0; l < w; l++)
          vis[k][l] = -1;
      queue<tuple<int, int, int>> que;
      que.push(make_tuple(i, j, 0));
      while (!que.empty()) {
        int a, b, c;
        tie(a, b, c) = que.front();
        que.pop();
        vis[a][b] = c;
        int ii[] = {-1, 0, 1, 0}, jj[] = {0, -1, 0, 1};
        for (int k = 0; k < 4; k++)
          if (a + ii[k] >= 0 && a + ii[k] < h && b + jj[k] >= 0 &&
              b + jj[k] < w && vis[a + ii[k]][b + jj[k]] == -1 &&
              gr[a + ii[k]][b + jj[k]] == '.')
            que.push(make_tuple(a + ii[k], b + jj[k], c + 1));
      }
      for (int k = 0; k < h; k++)
        for (int l = 0; l < w; l++)
          mx = max(mx, vis[k][l]);
    }
  cout << mx << "\n";
  return 0;
}

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

int h, w, vis[20][20];
string gr[20];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    cin >> gr[i];
  int mx = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (gr[i][j] == '#')
        continue;
      for (int k = 0; k < h; k++)
        for (int l = 0; l < w; l++)
          vis[k][l] = -1;
      queue<tuple<int, int, int>> que;
      que.push(make_tuple(i, j, 0));
      while (!que.empty()) {
        int a, b, c;
        tie(a, b, c) = que.front();
        que.pop();
        if (vis[a][b] != -1)
          continue;
        vis[a][b] = c;
        int ii[] = {-1, 0, 1, 0}, jj[] = {0, -1, 0, 1};
        for (int k = 0; k < 4; k++)
          if (a + ii[k] >= 0 && a + ii[k] < h && b + jj[k] >= 0 &&
              b + jj[k] < w && vis[a + ii[k]][b + jj[k]] == -1 &&
              gr[a + ii[k]][b + jj[k]] == '.')
            que.push(make_tuple(a + ii[k], b + jj[k], c + 1));
      }
      for (int k = 0; k < h; k++)
        for (int l = 0; l < w; l++)
          mx = max(mx, vis[k][l]);
    }
  cout << mx << "\n";
  return 0;
}
Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 1e9 + 7;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s;
  rep(i, h) {
    string t;
    cin >> t;
    s.push_back(t);
  }

  int ans = 0;
  for (int sy = 0; sy < h; sy++) {
    for (int sx = 0; sx < w; sx++) {
      for (int gy = 0; gy < h; gy++) {
        for (int gx = 0; gx < w; gx++) {
          if ((sx == gx && sy == gy) ||
              (s[sy][sx] == '#' || s[gy][gx] == '#')) {
            continue;
          }
          vector<vector<int>> dist(h, vector<int>(w, INF));
          queue<P> que;
          que.push({sx, sy});
          dist[sy][sx] = 0;
          vector<int> dx{1, -1, 0, 0};
          vector<int> dy{0, 0, 1, -1};
          while (!que.empty()) {
            int x = que.front().first;
            int y = que.front().second;
            que.pop();
            rep(i, 4) {
              int ix = x + dx[i];
              int iy = y + dy[i];
              if (ix < 0 || ix >= w || iy < 0 || iy >= h)
                continue;
              if (dist[iy][ix] < dist[y][x] + 1 || s[iy][ix] == '#')
                continue;
              dist[iy][ix] = dist[y][x] + 1;
              que.push({ix, iy});
            }
          }
          ans = max(ans, dist[gy][gx]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll INFL = 1e17;
const int MOD = 1e9 + 7;

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s;
  rep(i, h) {
    string t;
    cin >> t;
    s.push_back(t);
  }

  int ans = 0;
  for (int sy = 0; sy < h; sy++) {
    for (int sx = 0; sx < w; sx++) {
      for (int gy = 0; gy < h; gy++) {
        for (int gx = 0; gx < w; gx++) {
          if ((sx == gx && sy == gy) ||
              (s[sy][sx] == '#' || s[gy][gx] == '#')) {
            continue;
          }
          vector<vector<int>> dist(h, vector<int>(w, INF));
          queue<P> que;
          que.push({sx, sy});
          dist[sy][sx] = 0;
          vector<int> dx{1, -1, 0, 0};
          vector<int> dy{0, 0, 1, -1};
          while (!que.empty()) {
            int x = que.front().first;
            int y = que.front().second;
            que.pop();
            rep(i, 4) {
              int ix = x + dx[i];
              int iy = y + dy[i];
              if (ix < 0 || ix >= w || iy < 0 || iy >= h)
                continue;
              if (dist[iy][ix] <= dist[y][x] + 1 || s[iy][ix] == '#')
                continue;
              dist[iy][ix] = dist[y][x] + 1;
              que.push({ix, iy});
            }
          }
          ans = max(ans, dist[gy][gx]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02803 (C++)

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

int main() {
  ios::sync_with_stdio(0);
  cout << setprecision(10) << fixed;
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      char c;
      cin >> c;
      g[i][j] = (c == '.');
    }
  }
  vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
  int ans = 0;
  queue<pair<int, int>> q;
  for (int xx = 0; xx < h; ++xx) {
    for (int yy = 0; yy < w; ++yy) {
      if (!g[xx][yy])
        continue;
      vector<vector<int>> d(h + 1, vector<int>(w + 1, -1));
      d[xx][yy] = 0;
      q.push({xx, yy});
      while (q.size()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; ++i) {
          int nx = x + dx[i], ny = y + dy[i];
          if (!g[nx][ny])
            continue;
          if (d[nx][ny] > -1)
            continue;
          d[nx][ny] = d[x][y] + 1;
          ans = max(ans, d[nx][ny]);
          q.push({nx, ny});
        }
      }
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
bool g[22][22];

int main() {
  ios::sync_with_stdio(0);
  cout << setprecision(10) << fixed;
  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      char c;
      cin >> c;
      g[i][j] = (c == '.');
    }
  }
  vector<int> dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
  int ans = 0;
  queue<pair<int, int>> q;
  for (int xx = 0; xx < h; ++xx) {
    for (int yy = 0; yy < w; ++yy) {
      if (!g[xx][yy])
        continue;
      vector<vector<int>> d(h + 1, vector<int>(w + 1, -1));
      d[xx][yy] = 0;
      q.push({xx, yy});
      while (q.size()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        for (int i = 0; i < 4; ++i) {
          int nx = x + dx[i], ny = y + dy[i];
          if (nx < 0 || nx > h || ny < 0 || ny > w)
            continue;
          if (!g[nx][ny])
            continue;
          if (d[nx][ny] > -1)
            continue;
          d[nx][ny] = d[x][y] + 1;
          ans = max(ans, d[nx][ny]);
          q.push({nx, ny});
        }
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02803 (C++)

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

int main() {
  using vi = std::vector<int>;
  using vvi = std::vector<vi>;
  int H, W;
  scanf("%d%d", &H, &W);
  using vb = std::vector<bool>;
  using vvb = std::vector<vb>;
  vvb room(H + 2, vb(W + 2));
  for (int r{1}; r <= H; r++)
    for (int c{1}; c <= W; c++) {
      char S;
      scanf(" %c", &S);
      room[r][c] = S == '.';
    }

  constexpr int inf{1 << 30};
  int ans{};
  int dir[4][2]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  for (int start_r{1}; start_r <= H; start_r++)
    for (int start_c{1}; start_c <= W; start_c++) {
      if (!room[start_r][start_c])
        continue;
      vvi distance(H + 2, vi(W + 2, inf));
      std::queue<std::pair<int, int>> que;
      distance[start_r][start_c] = 0;
      que.push({start_r, start_c});
      while (!que.empty()) {
        auto now{que.front()};
        que.pop();
        for (auto &e : dir) {
          std::pair<int, int> next{now.first + e[0], now.second + e[1]};
          if (!room[next.first][next.second])
            continue;
          if (distance[next.first][next.second] <=
              distance[now.first][now.second])
            continue;
          distance[next.first][next.second] =
              distance[now.first][now.second] + 1;
          que.push(next);
        }
      }
      for (auto &e : distance)
        for (auto &f : e)
          if (f != inf)
            ans = std::max(ans, f);
    }
  printf("%d\n", ans);

  return 0;
}

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

int main() {
  using vi = std::vector<int>;
  using vvi = std::vector<vi>;
  int H, W;
  scanf("%d%d", &H, &W);
  using vb = std::vector<bool>;
  using vvb = std::vector<vb>;
  vvb room(H + 2, vb(W + 2));
  for (int r{1}; r <= H; r++)
    for (int c{1}; c <= W; c++) {
      char S;
      scanf(" %c", &S);
      room[r][c] = S == '.';
    }

  constexpr int inf{1 << 30};
  int ans{};
  int dir[4][2]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  for (int start_r{1}; start_r <= H; start_r++)
    for (int start_c{1}; start_c <= W; start_c++) {
      if (!room[start_r][start_c])
        continue;
      vvi distance(H + 2, vi(W + 2, inf));
      std::queue<std::pair<int, int>> que;
      distance[start_r][start_c] = 0;
      que.push({start_r, start_c});
      while (!que.empty()) {
        auto now{que.front()};
        que.pop();
        for (auto &e : dir) {
          std::pair<int, int> next{now.first + e[0], now.second + e[1]};
          if (!room[next.first][next.second])
            continue;
          if (distance[next.first][next.second] <=
              distance[now.first][now.second] + 1)
            continue;
          distance[next.first][next.second] =
              distance[now.first][now.second] + 1;
          que.push(next);
        }
      }
      for (auto &e : distance)
        for (auto &f : e)
          if (f != inf)
            ans = std::max(ans, f);
    }
  printf("%d\n", ans);

  return 0;
}
Problem p02803 (C++)

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

const int H_MAX = 20;
const int W_MAX = 20;
vector<vector<char>> S(H_MAX + 5, vector<char>(W_MAX + 5, '#'));
vector<vector<int>> dist(H_MAX, vector<int>(W_MAX, -1));

vector<pair<int, int>> d_que = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main() {
  int H, W;
  cin >> H >> W;
  rep2(i, 1, H + 1) {
    rep2(j, 1, W + 1) { cin >> S[i][j]; }
  }

  int ans = 0;
  rep2(i, 1, H + 1) {
    rep2(j, 1, W + 1) {
      if (S[i][j] == '#')
        continue;
      // 探索対象をqueに
      queue<pair<int, int>> que;
      dist.assign(H_MAX, vector<int>(W_MAX, -1));

      // スタート地点の距離を0に
      dist[i][j] = 0;

      // 探索対象にスタート地点を代入
      que.push({i, j});

      // BFS開始
      while (!que.empty()) {
        // 現在地の代入
        int x = que.front().first;
        int y = que.front().second;
        ans = max(ans, dist[x][y]);
        // 代入したら削除
        que.pop();

        // 次の探索地点の条件判定
        rep(j, 4) {
          int x_next = x + d_que[j].first;
          int y_next = y + d_que[j].second;

          // アウト条件の場合continue
          // 訪問済み
          if (dist[x_next][y_next] != -1)
            continue;
          // 障害物
          if (S[x_next][y_next] == '#')
            continue;

          // 以下問題なかった場合
          dist[x_next][y_next] = dist[x][y] + 1;
          que.push({x_next, y_next});
        }
      }
    }
  }

  cout << ans << endl;
}

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

const int H_MAX = 25;
const int W_MAX = 25;
vector<vector<char>> S(H_MAX, vector<char>(W_MAX, '#'));
vector<vector<int>> dist(H_MAX, vector<int>(W_MAX, -1));

vector<pair<int, int>> d_que = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

int main() {
  int H, W;
  cin >> H >> W;
  rep2(i, 1, H + 1) {
    rep2(j, 1, W + 1) { cin >> S[i][j]; }
  }

  int ans = 0;
  rep2(i, 1, H + 1) {
    rep2(j, 1, W + 1) {
      if (S[i][j] == '#')
        continue;
      // 探索対象をqueに
      queue<pair<int, int>> que;
      dist.assign(H_MAX, vector<int>(W_MAX, -1));

      // スタート地点の距離を0に
      dist[i][j] = 0;

      // 探索対象にスタート地点を代入
      que.push({i, j});

      // BFS開始
      while (!que.empty()) {
        // 現在地の代入
        int x = que.front().first;
        int y = que.front().second;
        ans = max(ans, dist[x][y]);
        // 代入したら削除
        que.pop();

        // 次の探索地点の条件判定
        rep(j, 4) {
          int x_next = x + d_que[j].first;
          int y_next = y + d_que[j].second;

          // アウト条件の場合continue
          // 訪問済み
          if (dist[x_next][y_next] != -1)
            continue;
          // 障害物
          if (S[x_next][y_next] == '#')
            continue;

          // 以下問題なかった場合
          dist[x_next][y_next] = dist[x][y] + 1;
          que.push({x_next, y_next});
        }
      }
    }
  }

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

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

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

int H, W;
vector<string> Graph(21);

int solve(int i, int j, int k, int l) {
  if (Graph[i][j] == '#' || Graph[k][l] == '#')
    return 0;

  vector<vector<int>> dist(H, vector<int>(W, -1)); // -1: 未訪問
  queue<P> que;

  que.push(make_pair(i, j)); // スタート地点をプッシュ
  dist[i][j] = 0;

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    for (int direction = 0; direction < 4; ++direction) {
      int nh = p.first + dx[direction];
      int nw = p.second + dy[direction];
      if (nh < 0 || nh >= H || nw < 0 || nw >= W)
        continue;
      if (dist[nh][nw] > -1)
        continue;
      if (Graph[nh][nw] == '#')
        continue;
      dist[nh][nw] = dist[p.first][p.second] + 1;
      if (nh == k && nw == l) {
        // ゴール
        return dist[k][l];
      }
      que.push(make_pair(nh, nw));
    }
  }
}

int main() {
  cin >> H >> W;
  rep(i, H) cin >> Graph.at(i);

  int ans = 0;
  rep(i, H) rep(j, W) rep(k, H) rep(l, W) { ans = max(ans, solve(i, j, k, l)); }

  cout << ans << endl;
}


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

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

int H, W;
vector<string> Graph(21);

int solve(int i, int j, int k, int l) {
  if (Graph[i][j] == '#' || Graph[k][l] == '#')
    return 0;

  vector<vector<int>> dist(H, vector<int>(W, -1)); // -1: 未訪問
  queue<P> que;

  que.push(make_pair(i, j)); // スタート地点をプッシュ
  dist[i][j] = 0;

  while (!que.empty()) {
    P p = que.front();
    que.pop();
    for (int direction = 0; direction < 4; ++direction) {
      int nh = p.first + dx[direction];
      int nw = p.second + dy[direction];
      if (nh < 0 || nh >= H || nw < 0 || nw >= W)
        continue;
      if (dist[nh][nw] > -1)
        continue;
      if (Graph[nh][nw] == '#')
        continue;
      dist[nh][nw] = dist[p.first][p.second] + 1;
      if (nh == k && nw == l) {
        // ゴール
        return dist[k][l];
      }
      que.push(make_pair(nh, nw));
    }
  }
  return 0;
}

int main() {
  cin >> H >> W;
  rep(i, H) cin >> Graph.at(i);

  int ans = 0;
  rep(i, H) rep(j, W) rep(k, H) rep(l, W) { ans = max(ans, solve(i, j, k, l)); }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, a, n) for (ll i = a; i < (ll)n; ++i)
const ll MOD = 1000000007;
using namespace std;

const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
int main(void) {
  int h, w;
  cin >> h >> w;
  vector<string> v(h);
  rep(i, 0, h) cin >> v[i];
  int ans = 0;
  rep(i, 0, h) {
    rep(j, 0, w) {
      if (v[i][j] == '#')
        continue;
      queue<pair<int, int>> p;
      vector<vector<int>> dist(h, vector<int>(w, -1));
      p.push({i, j});
      dist[i][j] = 0;
      while (!p.empty()) {
        pair<int, int> tmp = p.front();
        p.pop();
        rep(k, 0, 4) {
          int ny = tmp.first + dy[k];
          int nx = tmp.second + dx[k];
          if (ny < 0 || ny >= h || nx < 0 || nx >= w)
            continue;
          if (v[ny][nx] == '.') {
            if (ny < 0 || nx < 0 || ny >= h || nx >= w)
              continue;
            if (dist[ny][nx] >= dist[tmp.first][tmp.second] + 1 ||
                dist[ny][nx] == -1)
              dist[ny][nx] = dist[tmp.first][tmp.second] + 1,
              ans = max(ans, dist[ny][nx]), p.push({ny, nx});
          }
        }
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i, a, n) for (ll i = a; i < (ll)n; ++i)
const ll MOD = 1000000007;
using namespace std;

const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
int main(void) {
  int h, w;
  cin >> h >> w;
  vector<string> v(h);
  rep(i, 0, h) cin >> v[i];
  int ans = 0;
  rep(i, 0, h) {
    rep(j, 0, w) {
      if (v[i][j] == '#')
        continue;
      queue<pair<int, int>> p;
      vector<vector<int>> dist(h, vector<int>(w, -1));
      p.push({i, j});
      dist[i][j] = 0;
      while (!p.empty()) {
        pair<int, int> tmp = p.front();
        p.pop();
        rep(k, 0, 4) {
          int ny = tmp.first + dy[k];
          int nx = tmp.second + dx[k];
          if (ny < 0 || ny >= h || nx < 0 || nx >= w)
            continue;
          if (v[ny][nx] == '.') {
            if (ny < 0 || nx < 0 || ny >= h || nx >= w)
              continue;
            if (dist[ny][nx] > dist[tmp.first][tmp.second] + 1 ||
                dist[ny][nx] == -1)
              dist[ny][nx] = dist[tmp.first][tmp.second] + 1,
              ans = max(ans, dist[ny][nx]), p.push({ny, nx});
          }
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define N 100005
using namespace std;

typedef long long ll;
ll M = 1e9 + 7;
ll n, k, s, a[N];
ll fac[N]{1, 1}, inv[N]{1, 1}, fiv[N]{1, 1};

ll com(ll p, ll q) { return fac[p] * fiv[p - q] % M * fiv[q] % M; }

int main() {
  ll i;
  cin >> n >> k;
  for (i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i % M;
    inv[i] = inv[M % i] * (M - M / i) % M;
    fiv[i] = fiv[i - 1] * inv[i] % M;
  }
  for (i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  sort(a, a + n);
  for (i = 0; i < n; i++) {
    s = (s + a[i] * (com(i, k - 1) - com(n - i - 1, k - 1))) % M;
  }
  cout << s << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define N 100005
using namespace std;

typedef long long ll;
ll M = 1e9 + 7;
ll n, k, s, a[N];
ll fac[N]{1, 1}, inv[N]{1, 1}, fiv[N]{1, 1};

ll com(ll p, ll q) {
  if (p < q)
    return 0;
  return fac[p] * fiv[p - q] % M * fiv[q] % M;
}

int main() {
  ll i;
  cin >> n >> k;
  for (i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i % M;
    inv[i] = inv[M % i] * (M - M / i) % M;
    fiv[i] = fiv[i - 1] * inv[i] % M;
  }
  for (i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  sort(a, a + n);
  for (i = 0; i < n; i++) {
    s = (s + a[i] * (com(i, k - 1) - com(n - i - 1, k - 1))) % M;
  }
  cout << s << endl;
  return 0;
}
Problem p02804 (C++)

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

const int MAX = 50000;
const int MOD = 1000000007;

int64_t fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

int64_t COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {

  COMinit();

  int N, K;
  cin >> N >> K;
  vector<int64_t> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

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

  int64_t ans = 0;
  for (int i = 0; i < N; ++i) {
    ans += A[i] * (COM(i, K - 1) - COM(N - i - 1, K - 1)) % MOD;
  }
  cout << ans % MOD << endl;
}

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

const int MAX = 500000;
const int MOD = 1000000007;

int64_t fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

int64_t COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {

  COMinit();

  int N, K;
  cin >> N >> K;
  vector<int64_t> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];

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

  int64_t ans = 0;
  for (int i = 0; i < N; ++i) {
    ans += A[i] * (COM(i, K - 1) - COM(N - i - 1, K - 1)) % MOD;
  }
  cout << ans % MOD << endl;
}
Problem p02804 (C++)

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

using namespace std;

#define sqr(x) ((x) * (x))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef long long ll;

const int MOD = 1000000007;

ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}

void cmb_init(int n, vll &fac, vll &finv) {
  vll inv(n + 1, 1);
  for (int i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

ll cmb(int n, int k, vll fac, vll finv) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

void solve() {
  int N, K;
  cin >> N >> K;
  vi A(N, 0);
  vll fac(N + 1, 1), finv(N + 1, 1);
  cmb_init(N, fac, finv);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(all(A));
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    sum -= (ll)A[i] * cmb(N - i - 1, K - 1, fac, finv) % MOD;
    sum += (ll)A[i] * cmb(i, K - 1, fac, finv) % MOD;
    sum %= MOD;
  }
  cout << sum << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();

  return 0;
}


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

using namespace std;

#define sqr(x) ((x) * (x))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef long long ll;

const int MOD = 1000000007;

ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}

void cmb_init(int n, vll &fac, vll &finv) {
  vll inv(n + 1, 1);
  for (int i = 2; i <= n; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

ll cmb(int n, int k, vll &fac, vll &finv) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

void solve() {
  int N, K;
  cin >> N >> K;
  vi A(N, 0);
  vll fac(N + 1, 1), finv(N + 1, 1);
  cmb_init(N, fac, finv);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  sort(all(A));
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    sum -= (ll)A[i] * cmb(N - i - 1, K - 1, fac, finv) % MOD;
    sum += (ll)A[i] * cmb(i, K - 1, fac, finv) % MOD;
    sum %= MOD;
  }
  cout << sum << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();

  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
//
// Created by Mahmoud Rashad on 11/27/19.
//

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<pair<ii, ii>> vii;

const int N = 1e5 + 5, MOD = 1e9 + 7;
long long fact[N], fact_inv[N];

long long mul(long long a, long long b) { return (a + MOD) * b % MOD; }

long long power(long long a, long long b) {
  if (!b)
    return 1;
  long long r = power(a, b / 2);
  r = mul(r, r);
  if (b & 1)
    return mul(r, a);
  return r;
}

long long mod_inv(long long x) { return power(x, MOD - 2); }

long long nCr(long long n, long long r) {
  if (!n)
    return !r;
  if (r > n)
    return 0;
  return mul(fact[n], mul(fact_inv[n - r], fact_inv[r]));
}

void pre() {
  fact[0] = fact_inv[0] = 1;
  for (int i = 1; i < N; i++) {
    fact[i] = mul(fact[i - 1], i);
    fact_inv[i] = mod_inv(fact[i]);
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
//    freopen("output.out", "w", stdout);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
      cout << fixed;

  pre();
  int n, k;
  cin >> n >> k;
  vector<int> arr(n);
  for (int &x : arr)
    cin >> x;
  sort(arr.begin(), arr.end());

  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    if (i + 1 >= k) {
      ans += mul(arr[i], nCr(i, i + 1 - k));
      ans %= MOD;
    }
    if (n - i + 1 >= k) {
      ans += mul(-arr[i], nCr(n - i - 1, k - 1));
      ans %= MOD;
    }
  }

  cout << ans << '\n';

  return 0;
}

Fixed submission:
//
// Created by Mahmoud Rashad on 11/27/19.
//

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<pair<ii, ii>> vii;

const int N = 1e5 + 5, MOD = 1e9 + 7;
long long fact[N], fact_inv[N];

long long mul(long long a, long long b) { return (a + MOD) * b % MOD; }

long long power(long long a, long long b) {
  if (!b)
    return 1;
  long long r = power(a, b / 2);
  r = mul(r, r);
  if (b & 1)
    return mul(r, a);
  return r;
}

long long mod_inv(long long x) { return power(x, MOD - 2); }

long long nCr(long long n, long long r) {
  if (!n)
    return !r;
  if (r > n)
    return 0;
  return mul(fact[n], mul(fact_inv[n - r], fact_inv[r]));
}

void pre() {
  fact[0] = fact_inv[0] = 1;
  for (int i = 1; i < N; i++) {
    fact[i] = mul(fact[i - 1], i);
    fact_inv[i] = mod_inv(fact[i]);
  }
}

int main() {
#ifndef ONLINE_JUDGE
//    freopen("input.txt", "r", stdin);
//    freopen("output.out", "w", stdout);
#endif
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
      cout << fixed;

  pre();
  int n, k;
  cin >> n >> k;
  vector<int> arr(n);
  for (int &x : arr)
    cin >> x;
  sort(arr.begin(), arr.end());

  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    if (i + 1 >= k) {
      ans += mul(arr[i], nCr(i, i + 1 - k));
      ans %= MOD;
    }
    if (n - i + 1 >= k) {
      ans += mul(-arr[i], nCr(n - i - 1, k - 1));
      ans %= MOD;
    }
  }

  cout << ans << '\n';

  return 0;
}
Problem p02804 (C++)

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

const int MAX = 50001;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  // cout << fixed << setprecision(10);

  COMinit();

  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<int> max_n(N);
  rep(i, N) cin >> A[i];
  if (K <= 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(A.begin(), A.end());

  // max Aiとなる回数を数える
  repr(i, K - 1, N) {
    int n = i - 1;
    int r = K - 2;
    // cout << "n: " << n << " r: " << r << endl;
    max_n[i] = (max_n[i - 1] + COM(n, r)) % MOD;
  }
  // rep(i, N) cout << max_n[i] << " ";
  // cout << endl;
  ll ans = 0;
  rep(i, N) {
    ans += (A[i] * ll(max_n[i]) - A[N - 1 - i] * ll(max_n[i])) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

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

const int MAX = 100001;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  // cout << fixed << setprecision(10);

  COMinit();

  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  vector<int> max_n(N);
  rep(i, N) cin >> A[i];
  if (K <= 1) {
    cout << 0 << endl;
    return 0;
  }
  sort(A.begin(), A.end());

  // max Aiとなる回数を数える
  repr(i, K - 1, N) {
    int n = i - 1;
    int r = K - 2;
    // cout << "n: " << n << " r: " << r << endl;
    max_n[i] = (max_n[i - 1] + COM(n, r)) % MOD;
  }
  // rep(i, N) cout << max_n[i] << " ";
  // cout << endl;
  ll ans = 0;
  rep(i, N) {
    ans += (A[i] * ll(max_n[i]) - A[N - 1 - i] * ll(max_n[i])) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02804 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define INF (1 << 30)
#define INFL (1LL << 62)
#define MOD7 1000000007
#define MOD9 1000000009
#define EPS 1e-10

using ll = long long;
using ull = unsigned long long;

int n, k;
ll a[100000];
ll fact[100000];
ll inv[100000];

ll pow(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return a * pow(a, b - 1);
  return pow(a * a, b / 2);
}
// a^(-1)≡a^(p-2) (mod p) (but p is prime)
ll powMod(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b % 2)
    return (a * powMod(a, b - 1, p)) % p;
  return powMod(a * a % p, b / 2, p);
}

ll combi(ll nn, ll kk) {
  return (((fact[nn] * inv[nn - kk]) % MOD7) * inv[kk]) % MOD7;
}

void pre() {
  fact[0] = 1;
  inv[0] = 1;
  for (int i = 1; i < n; ++i) {
    fact[i] = (fact[i - 1] * i) % MOD7;
    inv[i] = (inv[i - 1] * powMod(i, MOD7 - 2, MOD7)) % MOD7;
  }
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);

  pre();

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += (a[i] * combi(i, k - 1)) % MOD7;
    ans %= MOD7;
    ans -= (a[i] * combi(n - i - 1, k - 1)) % MOD7;
    ans %= MOD7;
  }
  if (ans < 0)
    ans += MOD7;
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define INF (1 << 30)
#define INFL (1LL << 62)
#define MOD7 1000000007
#define MOD9 1000000009
#define EPS 1e-10

using ll = long long;
using ull = unsigned long long;

int n, k;
ll a[100000];
ll fact[100000];
ll inv[100000];

ll pow(ll a, ll b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return a * pow(a, b - 1);
  return pow(a * a, b / 2);
}
// a^(-1)≡a^(p-2) (mod p) (but p is prime)
ll powMod(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b % 2)
    return (a * powMod(a, b - 1, p)) % p;
  return powMod(a * a % p, b / 2, p);
}

ll combi(ll nn, ll kk) {
  if (nn < kk)
    return 0;
  return (((fact[nn] * inv[nn - kk]) % MOD7) * inv[kk]) % MOD7;
}

void pre() {
  fact[0] = 1;
  inv[0] = 1;
  for (int i = 1; i < n; ++i) {
    fact[i] = (fact[i - 1] * i) % MOD7;
    inv[i] = (inv[i - 1] * powMod(i, MOD7 - 2, MOD7)) % MOD7;
  }
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  sort(a, a + n);

  pre();

  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += (a[i] * combi(i, k - 1)) % MOD7;
    ans %= MOD7;
    ans -= (a[i] * combi(n - i - 1, k - 1)) % MOD7;
    ans %= MOD7;
  }
  if (ans < 0)
    ans += MOD7;
  cout << ans << endl;

  return 0;
}
Problem p02804 (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 int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll mod = 1000000007;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

long long f[4005], fi[4005];
long long MOD = 1e9 + 7;

void init(int n) {
  f[0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i] = f[i - 1] * i % MOD;
  }
  fi[n] = mod_pow(f[n], MOD - 2, MOD);
  for (int i = n; i > 0; i--) {
    fi[i - 1] = fi[i] * i % MOD;
  }
}

long long comb(int r, int c) {
  if (r < c)
    return 0;
  return f[r] * fi[c] % MOD * fi[r - c] % MOD;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());
  init(n + 1);
  ll ans = 0;
  rep(i, n) {
    if (i < k - 1)
      continue;
    ans = (ans + (a[i] * comb(i, k - 1)) % mod) % mod;
  }
  rep(i, n) {
    if (n - i - 1 < k - 1)
      continue;
    ll sub = (a[i] * comb(n - i - 1, k - 1)) % mod;
    if (ans <= sub)
      ans += mod;
    ans = (ans - sub) % mod;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll mod = 1000000007;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

long long f[100005], fi[100005];
long long MOD = 1e9 + 7;

void init(int n) {
  f[0] = 1;
  for (int i = 1; i <= n; i++) {
    f[i] = f[i - 1] * i % MOD;
  }
  fi[n] = mod_pow(f[n], MOD - 2, MOD);
  for (int i = n; i > 0; i--) {
    fi[i - 1] = fi[i] * i % MOD;
  }
}

long long comb(int r, int c) {
  if (r < c)
    return 0;
  return f[r] * fi[c] % MOD * fi[r - c] % MOD;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  sort(a.begin(), a.end());
  init(n + 1);
  ll ans = 0;
  rep(i, n) {
    if (i < k - 1)
      continue;
    ans = (ans + (a[i] * comb(i, k - 1)) % mod) % mod;
  }
  rep(i, n) {
    if (n - i - 1 < k - 1)
      continue;
    ll sub = (a[i] * comb(n - i - 1, k - 1)) % mod;
    if (ans <= sub)
      ans += mod;
    ans = (ans - sub) % mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02804 (C++)

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

const int MOD = 1e9 + 7;

const int MAX = 100010;

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end(), greater<ll>());
  ll ans = 0;
  COMinit();
  for (int i = 0; i < n; i++) {
    if (n - i - 1 >= k - 1) {
      ll c = COM(n - i - 1, k - 1);
      ll b = a[i] - a[n - 1 - i];
      while (b > 0)
        b += MOD;
      ans += ((b % MOD) * (c % MOD)) % MOD;
      ans = ans % MOD;
    }
  }
  cout << ans << endl;
}

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

const int MOD = 1e9 + 7;

const int MAX = 100010;

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
ll COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end(), greater<ll>());
  ll ans = 0;
  COMinit();
  for (int i = 0; i < n; i++) {
    if (n - i - 1 >= k - 1) {
      ll c = COM(n - i - 1, k - 1);
      ll b = a[i] - a[n - 1 - i];
      while (b < 0)
        b += MOD;
      ans += ((b % MOD) * (c % MOD)) % MOD;
      ans = ans % MOD;
    }
  }
  cout << ans << endl;
}
Problem p02804 (C++)

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

struct combination {
  combination(ll Nmax, ll mod) {
    (this->MOD) = mod;
    (this->Nmax) = Nmax;
    factorial.resize(Nmax + 1, 1);
    inverse_factorial.resize(Nmax + 1, 1);
    init_factorial();
  }

  ll calc(ll n, ll r) {
    if (r < 0 || r > n)
      return 0;
    ll ret = (factorial[n] * inverse_factorial[r]) % MOD;
    ret = (ret * inverse_factorial[n - r]) % MOD;
    return ret;
  }

private:
  ll MOD, Nmax;
  vector<ll> factorial;
  vector<ll> inverse_factorial;
  ll mod_pow(ll n, ll p, ll mod) {
    n %= mod;
    if (p == 0)
      return 1;
    ll res = mod_pow(n * n % mod, p / 2, mod);
    if (p % 2 == 1)
      res = res * n % mod;
    return res;
  }

  void init_factorial() {
    for (ll i = 1; i <= Nmax; i++) {
      factorial[i] = (factorial[i - 1] * i) % MOD;
    }
    inverse_factorial[Nmax] = mod_pow(factorial[Nmax], MOD - 2, MOD);
    for (ll i = Nmax - 1; i >= 1; i--) {
      inverse_factorial[i] = (inverse_factorial[i + 1] * (i + 1)) % MOD;
    }
  }
};

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll &a : A) {
    cin >> a;
  }
  sort(A.begin(), A.end());
  ll mod = 1e9 + 7;
  combination comb(N, mod);

  ll ans = 0;
  for (int i = 0; i + K - 1 < N; i++) {
    ans -= A[i] * comb.calc(N - 1 - i, K - 1);
    while (ans < 0) {
      ans += mod;
    }
    ans %= mod;
  }
  for (int i = K - 1; i < N; i++) {
    ans += A[i] * comb.calc(i, K - 1);
    ans %= mod;
  }
  cout << ans << endl;
}


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

struct combination {
  combination(ll Nmax, ll mod) {
    (this->MOD) = mod;
    (this->Nmax) = Nmax;
    factorial.resize(Nmax + 1, 1);
    inverse_factorial.resize(Nmax + 1, 1);
    init_factorial();
  }

  ll calc(ll n, ll r) {
    if (r < 0 || r > n)
      return 0;
    ll ret = (factorial[n] * inverse_factorial[r]) % MOD;
    ret = (ret * inverse_factorial[n - r]) % MOD;
    return ret;
  }

private:
  ll MOD, Nmax;
  vector<ll> factorial;
  vector<ll> inverse_factorial;
  ll mod_pow(ll n, ll p, ll mod) {
    n %= mod;
    if (p == 0)
      return 1;
    ll res = mod_pow(n * n % mod, p / 2, mod);
    if (p % 2 == 1)
      res = res * n % mod;
    return res;
  }

  void init_factorial() {
    for (ll i = 1; i <= Nmax; i++) {
      factorial[i] = (factorial[i - 1] * i) % MOD;
    }
    inverse_factorial[Nmax] = mod_pow(factorial[Nmax], MOD - 2, MOD);
    for (ll i = Nmax - 1; i >= 1; i--) {
      inverse_factorial[i] = (inverse_factorial[i + 1] * (i + 1)) % MOD;
    }
  }
};

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (ll &a : A) {
    cin >> a;
  }
  sort(A.begin(), A.end());
  ll mod = 1e9 + 7;
  combination comb(N, mod);

  ll ans = 0;
  for (int i = 0; i + K - 1 < N; i++) {
    ans -= (A[i] * comb.calc(N - 1 - i, K - 1)) % mod;
    while (ans < 0) {
      ans += mod;
    }
    ans %= mod;
  }
  for (int i = K - 1; i < N; i++) {
    ans += A[i] * comb.calc(i, K - 1);
    ans %= mod;
  }
  cout << ans << endl;
}
Problem p02804 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  COMinit();
  ll n, k;
  cin >> n >> k;
  // ll MOD = 1000000007
  vector<ll> A(n);
  for (ll i = 0; i < n; i++) {
    cin >> A.at(i);
  }
  vector<ll> fact(n + 1);

  ll ans = 0;
  sort(A.begin(), A.end());
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (ll i = k - 1; i < n; i++) {
    // cout << i << endl;
    ll MAX = A.at(i);
    for (ll j = 0; k <= i - j + 1; j++) {
      // cout << j << endl;
      ll MIN = A.at(j);
      ans = ans % MOD;
      ans += ((MAX - MIN) % MOD) * COM(i - j - 1, k - 2) % MOD;
    }
  }
  ans = ans % MOD;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

int main() {
  COMinit();
  ll n, k;
  cin >> n >> k;
  // ll MOD = 1000000007
  vector<ll> A(n);
  for (ll i = 0; i < n; i++) {
    cin >> A.at(i);
  }
  vector<ll> fact(n + 1);

  ll ans = 0;
  sort(A.begin(), A.end());
  if (k == 1) {
    cout << 0 << endl;
    return 0;
  }
  for (ll i = k - 1; i < n; i++) {

    ans += A.at(i) * COM(i, k - 1) % MOD;
  }
  for (ll i = k - 1; i < n; i++) {

    ans -= A.at(n - i - 1) * COM(i, k - 1) % MOD;
  }
  ans = ans % MOD;
  cout << ans << endl;
}