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

#include <algorithm>
#include <cstdio>
#include <set>
#include <tuple>

typedef std::tuple<int, int> P;

const int INF = 1001001001;

int pixels[301][301];
int sumH[301][301], sumV[301][301];
P _array[301];

int main() {
  int N;
  scanf("%d", &N);

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      scanf("%d", &pixels[i][j]);
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      sumH[i][j] += sumH[i][j - 1] + pixels[i][j];
      // printf("%d ", sumH[i][j]);
    }
    // puts("");
  }

  // puts(std::string(40, '-').c_str());

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      sumV[i][j] += sumV[i - 1][j] + pixels[i][j];
      // printf("%d ", sumV[i][j]);
    }
    // puts("");
  }

  int res = -INF;
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      // std::set<P> set;
      for (int k = 1; k <= N; k++) {
        _array[k] = std::make_tuple(
            -(sumH[i][k - 1] + sumH[j][k - 1] + sumV[j][k] - sumV[i - 1][k]),
            -k);
      }

      std::sort(_array + 1, _array + 1 + N);

      for (int k = 1, p = 1; k <= N; k++) {
        int x, idx;
        std::tie(x, idx) = _array[p];
        idx = -idx;

        for (; k <= idx; k++) {
          res = std::max(res, k != idx ? -x - sumH[i][k] - sumH[j][k] +
                                             sumV[j][k] - sumV[i - 1][k]
                                       : -x - sumH[i][k - 1] - sumH[j][k - 1]);
          // printf("(;%d, %d, %d) %d, %d, %d, %d\\n", i, j, k, x, idx,
          // -x-sumH[i][k]-sumH[j][k]+sumV[j][k]-sumV[i-1][k],
          // -x-sumH[i][k-1]-sumH[j][k-1]);
        }

        while (p <= N) {
          int idx2;
          std::tie(std::ignore, idx2) = _array[p];
          idx2 = -idx2;

          if (idx2 <= idx) {
            ++p;
          } else {
            break;
          }
        }
      }
    }

    // std::set<P> set;
    for (int j = 1; j <= N; j++) {
      _array[j] = std::make_tuple(-sumH[i][j], -j);
    }

    std::sort(_array + 1, _array + 1 + N);

    for (int j = 1, p = 1; j <= N; j++) {
      int x, idx;
      std::tie(x, idx) = _array[p];
      idx = -idx;

      for (; j <= idx; j++) {
        res = std::max(res, -x - sumH[i][j - 1]);
      }

      while (p <= N) {
        int idx2;
        std::tie(std::ignore, idx2) = _array[p];
        idx2 = -idx2;

        if (idx2 <= idx) {
          ++p;
        } else {
          break;
        }
      }
    }
  }

  printf("%d\\n", res);
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <set>
#include <tuple>

typedef std::tuple<int, int> P;

const int INF = 1001001001;

int pixels[301][301];
int sumH[301][301], sumV[301][301];

int main() {
  int N;
  scanf("%d", &N);

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      scanf("%d", &pixels[i][j]);
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      sumH[i][j] += sumH[i][j - 1] + pixels[i][j];
      // printf("%d ", sumH[i][j]);
    }
    // puts("");
  }

  // puts(std::string(40, '-').c_str());

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      sumV[i][j] += sumV[i - 1][j] + pixels[i][j];
      // printf("%d ", sumV[i][j]);
    }
    // puts("");
  }

  int res = -INF;
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      std::set<P> set;
      for (int k = 1; k <= N; k++) {
        set.insert(std::make_tuple(
            -(sumH[i][k - 1] + sumH[j][k - 1] + sumV[j][k] - sumV[i - 1][k]),
            -k));
      }

      for (int k = 1; k <= N; k++) {
        int x, idx;
        std::tie(x, idx) = *set.begin();
        idx = -idx;

        for (; k <= idx; k++) {
          res = std::max(res, k != idx ? -x - sumH[i][k] - sumH[j][k] +
                                             sumV[j][k] - sumV[i - 1][k]
                                       : -x - sumH[i][k - 1] - sumH[j][k - 1]);
          // printf("(;%d, %d, %d) %d, %d, %d, %d\\n", i, j, k, x, idx,
          // -x-sumH[i][k]-sumH[j][k]+sumV[j][k]-sumV[i-1][k],
          // -x-sumH[i][k-1]-sumH[j][k-1]);
        }

        while (!set.empty()) {
          int idx2;
          std::tie(std::ignore, idx2) = *set.begin();
          idx2 = -idx2;

          if (idx2 <= idx) {
            set.erase(set.begin());
          } else {
            break;
          }
        }
      }
    }

    std::set<P> set;
    for (int j = 1; j <= N; j++) {
      set.insert(std::make_tuple(-sumH[i][j], -j));
    }

    for (int j = 1; j <= N; j++) {
      int x, idx;
      std::tie(x, idx) = *set.begin();
      idx = -idx;

      for (; j <= idx; j++) {
        res = std::max(res, -x - sumH[i][j - 1]);
      }

      while (!set.empty()) {
        int idx2;
        std::tie(std::ignore, idx2) = *set.begin();
        idx2 = -idx2;

        if (idx2 <= idx) {
          set.erase(set.begin());
        } else {
          break;
        }
      }
    }
  }

  printf("%d\\n", res);
}