This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
 
using namespace std;
 
const long long INF = 1e18;
long long dp[4002][4002], cost[4002][4002];
 
void divide (int k, int st, int dr, int opl, int opr) {
  int mid = (st + dr) / 2;
  int opt = opl;
  for (int i = opl; i <= min(mid, opr); i++) {
    if (dp[k][mid] > dp[k - 1][i - 1] + cost[i][mid]) {
        dp[k][mid] = dp[k - 1][i - 1] + cost[i][mid];
        opt = i;
    }
  }
 
  if (st == dr)
    return;
  divide(k, st, mid, opl, opt);
  divide(k, mid + 1, dr, opt, opr);
}
 
long long take_photos(int n, int m, int k, vector<int>r, vector<int> c) {
  vector<pair<long long, long long>>p, p2;
  for (int i = 0; i < n; i++) {
    if (r[i] > c[i])
      swap(r[i], c[i]);
    p.push_back({r[i], -c[i]});
  }
  sort(p.begin(), p.end());
  p2.push_back({-1, -1});
  long long mx = -1;
  for (int i = 0; i < n; i++) {
    if (-p[i].second > mx)
      p2.push_back({p[i].first, -p[i].second});
    mx = max(mx, -p[i].second);
  }
  n = p2.size() - 1;
  k = min(k, n);
  for (int i = 0; i <= k; i++)
    for (int j = 0; j <= n; j++)
      dp[i][j] = INF;
  for (int j = 0; j <= k; j++)
    dp[j][0] = 0;
  for (int i = 1; i <= n; i++)
    for (long long j = i, mx = 0; j <= n; j++) {
      mx = max(mx, p2[j].second);
      cost[i][j] = ( mx - p2[i].first + 1) * (mx - p2[i].first + 1) - (i > 1 ? max(0ll, p2[i - 1].second - p2[i].first + 1) * max(0ll, p2[i - 1].second - p2[i].first + 1) : 0);
    }
  for (int j = 1; j <= n; j++)
    dp[1][j] = cost[1][j];
  for (int i = 2; i <= k; i++) {
    divide(i, 1, n, 1, n);
  }
  return dp[k][n];
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |