Problem p00774 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long int N;
  while (cin >> N, N) {
    long long int ans = 0;
    vector<vector<long long int>> d(N + 1, vector<long long int>(5));
    for (size_t i = 1; i <= N; i++) {
      for (size_t j = 0; j < 5; j++) {
        cin >> d[i][j];
        ans += d[i][j];
      }
    }
    bool f = true;
    while (f) {
      f = false;
      for (size_t i = 1; i <= N; i++) {
        if (d[i][2]) {
          int now = d[i][2];
          int count = 1;
          int j = 3, k = 1;
          while (j < 5 && now == d[i][j])
            j++;
          while (k >= 0 && now == d[i][k])
            k--;
          if (j - k > 3) {
            f = true;
            for (size_t l = k + 1; l < j; l++) {
              d[i][l] = 0;
            }
          }
        }
      }
      for (int i = N; i >= 1; i--) {
        for (size_t j = 0; j < 5; j++) {
          if (!d[i][j]) {
            int k = i;
            while (k > 0 && !d[k][j])
              k--;
            if (k != 0)
              swap(d[i][j], d[k][j]);
          }
        }
      }
    }
    for (size_t i = 0; i <= N; i++) {
      for (size_t j = 0; j < 5; j++) {
        ans -= d[i][j];
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

int main() {
  int N;
  while (cin >> N, N) {
    vector<vector<int>> data;
    data.clear();
    data.resize(N);
    for (size_t i = 0; i < N; i++) {
      data[i].resize(5);
      for (size_t j = 0; j < 5; j++) {
        cin >> data[i][j];
      }
    }
    int ans = 0;
    bool f = 1;
    while (f) {
      f = 0;
      for (size_t i = 0; i < N; i++) {
        int a = data[i][0];
        int b;
        int count = 1;
        int count2 = 0;
        for (size_t j = 1; j < 5; j++) {
          if (data[i][j] == a && data[i][j]) {
            count++;
          } else if (data[i][j]) {
            if (count2 < count) {
              count2 = count;
              b = a;
            }
            a = data[i][j];
            count = 1;
          }
        }
        if (count2 < count) {
          count2 = count;
          b = data[i][4];
        }
        if (count2 >= 3) {
          f = 1;
          ans += count2 * b;
          for (size_t j = 0; j < 5; j++) {
            if (data[i][j] == b) {
              data[i][j] = 0;
            }
          }
        }
      }
      for (int i = N - 1; i >= 0; i--) {
        // cout << "?" << endl;
        for (size_t j = 0; j < 5; j++) {
          if (data[i][j] == 0) {
            int k = i;
            while (k >= 0 && data[k][j] == 0) {
              k--;
            }
            if (k < 0)
              data[i][j] = 0;
            else {
              data[i][j] = data[k][j];
              data[k][j] = 0;
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}