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 "aliens.h"
#include <bits/stdc++.h>
using namespace std;
#define file  if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); }
// #define int long long
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
    if (s.size()) setIn(s+".in"), setOut(s+".out");
}
const int maxn = 1002, sz = 502;
vector <pair <int, int>> rem, tmp;
int lf[maxn], rg[maxn], dp[maxn][sz];
pair <int, int> cur[maxn][sz];
int sqr(int p)
{
    return p * p;
}
int area(int l, int r)
{
    return sqr(r - l + 1);
}
bool cMp (const pair <int, int> &a, const pair <int, int> &b)
{
    if (a.first == b.first)
        return a.second > b.second;
    else
        return a.first < b.first;
}
long long take_photos(int n, int m, int k, vector <int> R, vector <int> c)
{
    //square n x n
    //picture <= m
    swap(n, m);
    memset(dp, 0x3f3f3f3f, sizeof(dp));
    for (int i = 0; i < m; i++)
    {
        tmp.push_back({min(R[i], c[i]), max(R[i], c[i])});
    }
    sort(tmp.begin(), tmp.end(), cMp);
    
    rem.push_back(tmp[0]);
    for (int i = 1; i < tmp.size(); i++)
    {
        if (tmp[i].first >= rem.back().first && tmp[i].second <= rem.back().second)
            continue;
        
        rem.push_back(tmp[i]);
    }
    dp[0][1] = sqr(rem[0].second - rem[0].first + 1);
    for (int i = 1; i < rem.size(); i++)
    {
        for (int t = 0; t < i; t++)
        {
            auto [l, r] = rem[i];
            auto [ll, rr] = rem[t];
            for (int j = 1; j <= k; j++)
            {
               dp[i][j] = min({dp[i][j], dp[t][j - 1] + area(l, r), dp[t][j] - area(ll, rr) + area(ll, max(rr, r))});
            }
        }
    }
    int ans = 0x3f3f3f3f;
    for (int j = 1; j <= k; j++)
        ans = min(ans, dp[rem.size() - 1][j]);
    // cout << ans;
    return ans;
}
// signed main()
// {
//     // setIO();
//     file;
//     ios::sync_with_stdio(0); cin.tie(0);
//     // take_photos(2, 6, 2, {1, 4}, {4, 1});
//     // take_photos(5, 7, 2, {0, 4, 4, 4, 4}, {3, 4, 6, 5, 6});
// } 
Compilation message (stderr)
aliens.cpp: In function 'long long int take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for (int i = 1; i < tmp.size(); i++)
      |                     ~~^~~~~~~~~~~~
aliens.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i = 1; i < rem.size(); i++)
      |                     ~~^~~~~~~~~~~~
aliens.cpp: In function 'void setIn(std::string)':
aliens.cpp:8:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    8 | void setIn(string s) { freopen(s.c_str(),"r",stdin); }
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
aliens.cpp: In function 'void setOut(std::string)':
aliens.cpp:9:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 | void setOut(string s) { freopen(s.c_str(),"w",stdout); }
      |                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~| # | 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... |