Problem:
Emergency Evacuation
The Japanese government plans to increase the number of inbound tourists to forty million in the year 2020, and sixty million in 2030. Not only increasing touristic appeal but also developing tourism infrastructure further is indispensable to accomplish such numbers.
One possible enhancement on transport is providing cars extremely long and/or wide, carrying many passengers at a time. Too large a car, however, may require too long to evacuate all passengers in an emergency. You are requested to help estimating the time required.
The car is assumed to have the following seat arrangement.
A center aisle goes straight through the car, directly connecting to the emergency exit door at the rear center of the car.
The rows of the same number of passenger seats are on both sides of the aisle.
A rough estimation requested is based on a simple step-wise model. All passengers are initially on a distinct seat, and they can make one of the following moves in each step.
Passengers on a seat can move to an adjacent seat toward the aisle. Passengers on a seat adjacent to the aisle can move sideways directly to the aisle.
Passengers on the aisle can move backward by one row of seats. If the passenger is in front of the emergency exit, that is, by the rear-most seat rows, he/she can get off the car.
The seat or the aisle position to move to must be empty; either no other passenger is there before the step, or the passenger there empties the seat by moving to another position in the same step. When two or more passengers satisfy the condition for the same position, only one of them can move, keeping the others wait in their original positions.
The leftmost figure of Figure C.1 depicts the seat arrangement of a small car given in Sample Input 1. The car have five rows of seats, two seats each on both sides of the aisle, totaling twenty. The initial positions of seven passengers on board are also shown.
The two other figures of Figure C.1 show possible positions of passengers after the first and the second steps. Passenger movements are indicated by fat arrows. Note that, two of the passengers in the front seat had to wait for a vacancy in the first step, and one in the second row had to wait in the next step.
Your task is to write a program that gives the smallest possible number of steps for all the passengers to get off the car, given the seat arrangement and passengers' initial positions.
Figure C.1
Input
The input consists of a single test case of the following format.
$r$ $s$ $p$
$i_1$ $j_1$
...
$i_p$ $j_p$
Here, $r$ is the number of passenger seat rows, $s$ is the number of seats on each side of the aisle, and $p$ is the number of passengers. They are integers satisfying $1 \leq r \leq 500$, $1 \leq  s \leq 500$, and $1 \leq p \leq 2rs$.
The following $p$ lines give initial seat positions of the passengers. The $k$-th line with $i_k$ and $j_k$ means that the $k$-th passenger's seat is in the $i_k$-th seat row and it is the $j_k$-th seat o
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

bool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }

const ll INF = 1e18;
ll mod = 998244353;

ifstream in;
ofstream out;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if (argc > 2) {
        in.open(argv[1]);
        cin.rdbuf(in.rdbuf());
        out.open(argv[2]);
        cout.rdbuf(out.rdbuf());
    }

    ll r, s, p;
    cin >> r >> s >> p;
    vector<ll> dist;
    for (int i = 0; i < p; i++) {
        ll u, v;
        cin >> u >> v;
        if (v <= s)
            dist.push_back(r - u + s - v + 2);
        else
            dist.push_back(r - u + v - s + 1);
    }
    sort(dist.rbegin(), dist.rend());
    ll ans = 0;
    for (int i = 0; i < p; i++) {
        chmax(ans, i + dist[i]);
    }
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 8680
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

bool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }

const ll INF = 1e18;
ll mod = 998244353;

ifstream in;
ofstream out;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if (argc > 2) {
        in.open(argv[1]);
        cin.rdbuf(in.rdbuf());
        out.open(argv[2]);
        cout.rdbuf(out.rdbuf());
    }

    ll r, s, p;
    cin >> r >> s >> p;
    vector<deque<char>> gl(r, deque<char>(s, 0)), gr(r, deque<char>(s, 0));
    for (int i = 0; i < p; i++) {
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        if (v < s)
            gl[u][v] = 1;
        else
            gr[u][v - s] = 1;
    }
    vector<char> aisle(r, 0);

    ll sum = 0, ans = 0;
    for (; sum < p; ans++) {
        sum += aisle.back();
        for (int i = r - 1; i >= 1; i--) {
            aisle[i] = aisle[i - 1];
        }
        aisle[0] = 0;
        for (int i = 0; i < r; i++) {
            if (aisle[i] == 0) {
                aisle[i] = gl[i][s - 1];
                gl[i][s - 1] = 0;
            }
            if (ans <= 600)
                for (int j = s - 2; j >= 0; j--) {
                    if (gl[i][j + 1] == 0) {
                        gl[i][j + 1] = gl[i][j];
                        gl[i][j] = 0;
                    }
                }
            else if (gl[i].back() == 0) {
                gl[i].pop_back();
                gl[i].push_front(0);
            }

            if (aisle[i] == 0) {
                aisle[i] = gr[i][0];
                gr[i][0] = 0;
            }
            if (ans <= 600)
                for (int j = 1; j < s; j++) {
                    if (gr[i][j - 1] == 0) {
                        gr[i][j - 1] = gr[i][j];
                        gr[i][j] = 0;
                    }
                }
            else if (gr[i].front() == 0) {
                gr[i].pop_front();
                gr[i].push_back(0);
            }
        }
    }
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 1690
memory_kb: 4736
final_rank: 12

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

int main(){
    ll r, s, p;
    cin >> r >> s >> p;
    vector<ll> v;
    rep(_, p){
        ll i, j;
        cin >> i >> j;
        if(j <= s){
            v.push_back(r - i + 2 + (s - j));
        }else {
            v.push_back(r - i + 2 + (j - s - 1));
        }
    }
    sort(v.begin(), v.end());
    ll m = 0;
    rep(i, v.size()){
        if(v[i] <= m){
            v[i] = ++m;
        }
        else m = v[i];
    }
    cout << *max_element(v.begin(), v.end()) << endl;
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 8508
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dep[1010];
int n,m,p;

int main(){
    memset(dep,0,sizeof(dep));
    scanf("%d%d%d",&n,&m,&p);
    int x,y,ex;
    while(p--){
        scanf("%d%d",&x,&y);
        ex = n-x+1+abs(y-m);
        if (y<=m){
            ex++;
        }
        dep[ex]++;
    }
    int ans=0;
    for (int i=0;i<1010;++i){
        if (dep[i]>0){
            ans = i + dep[i]-1;
            if (i==1009){
                break;
            }
            if (dep[i]>1){
                dep[i+1]+=dep[i]-1;
            }
        }
    }
    cout<<ans<<"\n";
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3524
final_rank: 1

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  ll r,s,p;cin >> r >> s >> p;
  vi v(p);
  rep(i,0,p){
    ll x,y;cin >> x >> y;
    v[i] = r-x + (y <= s ? s-y+1 : y-s) + 1;
  }
  sort(ALL(v));
  ll tm = 0;
  rep(i,0,p){
    tm++;
    if(v[i] <= tm)continue;
    else tm = v[i];
  }
  cout << tm << endl;
  

  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 6916
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    int n, m, k;
    cin >> n >> m >> k;
    vector<pair<int, int>> P(k);
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        a = n + 1 - a;
        if (b > m)
            b -= m;
        else {
            b = m - b + 1;
        }
        P[i] = {a, b};
    }
    sort(P.begin(), P.end());
    set<int> s;
    for (int i = 0; i < 1e6; i++) {
        s.insert(i);
    }
    int ans = 0;
    for (auto u : P) {
        auto v = s.lower_bound(u.first + u.second);
        s.erase(v);
        ans = max(ans, *v);
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 420
memory_kb: 53960
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    int n, m, k;
    cin >> n >> m >> k;
    int ans = 0;
    vector<pair<int, int>> P;
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        a = n - a + 1;
        if (b > m)
            b -= m;
        else {
            b = m - b + 1;
        }
        P.emplace_back(a, b);
    }
    auto U = [&](pair<int, int> &l, pair<int, int> &r) {
        return (l.first > r.first ||
                ((l.first == r.first) || (l.second < r.second)));
    };
    set<int> s;
    for (int i = 1; i <= 1e6; i++) {
        s.insert(i);
    }
    for (auto u : P) {
        auto v = s.lower_bound(u.second + u.first);
        s.erase(v);
        ans = max(ans, *v);
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 390
memory_kb: 53960
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    int n, m, k;
    cin >> n >> m >> k;
    int ans = 0;
    vector<pair<int, int>> P;
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        a = n - a + 1;
        if (b > m)
            b -= m;
        else {
            b = m - b + 1;
        }
        P.emplace_back(a, b);
    }
    auto U = [&](pair<int, int> &l, pair<int, int> &r) {
        return (l.first > r.first ||
                ((l.first == r.first) || (l.second < r.second)));
    };
    set<int> s;
    for (int i = 1; i <= 4* n * m; i++) {
        s.insert(i);
    }
    for (auto u : P) {
        auto v = s.lower_bound(u.second + u.first);
        s.erase(v);
        ans = max(ans, *v);
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 390
memory_kb: 53700
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    int n, m, k;
    cin >> n >> m >> k;
    int ans = 0;
    vector<pair<int, int>> P;
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        a = n - a + 1;
        if (b > m)
            b -= m;
        else {
            b = m - b + 1;
        }
        P.emplace_back(a, b);
    }
    auto U = [&](pair<int, int> &l, pair<int, int> &r) {
        return (l.first > r.first ||
                ((l.first == r.first) || (l.second < r.second)));
    };
    set<int> s;
    for (int i = 1; i <= 2 * n * m; i++) {
        s.insert(i);
    }
    for (auto u : P) {
        auto v = s.lower_bound(u.second + u.first);
        s.erase(v);
        ans = max(ans, *v);
    }
    cout << ans << '\n';
}
accuracy: 0.4375
time_ms: 90
memory_kb: 26576
final_rank: 20

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

int main(){
    int h,w,p;cin >> h >> w >> p;
    vector<int> t(h+w+10,0);
    int mx = 0;
    for (int i = 0; i < p; i++){
        int x,y;cin >> x >> y;
        int time = h - x + 1 + max(w+1-y, y-w);
        t[time]++;
        mx = max(mx, time);
    }

    for (int i = 2; i < mx; i++){
        if (t[i] > 1)t[i+1] += t[i] - 1;
    }
    cout << mx + t[mx] - 1 << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 3584
final_rank: 3

code_snippet: #include <iostream>
#include <fstream>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
#define all(v) v.begin(),v.end()
#define endl "\n"
#define clr(n, r) memset(n,r,sizeof(n))
typedef bitset<10> MASK;

void fast() {
    cin.tie(0);
    cin.sync_with_stdio(0);
}

const int MAX = 1001;
int main() {
    int n,s,p;cin>>n>>s>>p;
    vector<int>ans;
    for (int i = 0; i < p; ++i) {
        int u,v;cin>>u>>v;
        if(v>s)v++;
        ans.push_back(((n+1)-u)+abs(v-(s+1)));
    }
    sort(all(ans));
    for (int i = 1; i < ans.size(); ++i) {
            if(ans[i]<=ans[i-1])ans[i]=ans[i-1]+1;
    }
    cout<<ans.back()<<'\n';

}
accuracy: 1
time_ms: 120
memory_kb: 4964
final_rank: 5

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

int a[500005];
int ans[500005];

bool cmp(int a,int b)
{
    return a>b;
}

int main()
{
    int r,s,p;
    cin>>r>>s>>p;
    far(i,p)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        if(y<=s)
            a[i]=r-x+1+s-y+1;
        else
            a[i]=r-x+1+y-s;
    }
//    far(i,p)
//        cout<<a[i]<<" ";
    sort(a,a+p,cmp);
    for(int i=0;i<p;++i)
    {
        ans[i]=a[i]+i;
    }
    cout<<*max_element(ans,ans+p)<<'\n';
}
accuracy: 1
time_ms: 60
memory_kb: 7088
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int r, s, p, a[N];
bool cmp(int x,int y){
    return x > y;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> r >> s >> p;
    for (int i = 1; i <= p;i++){
        int x, y;
        cin >> x >> y;
        a[i] = r - x + abs(y - s);
        if(y<=s)
            a[i]++;
    }
    sort(a + 1, a + 1 + p, cmp);
    int ans = 0;
    for (int i = 1; i <= p;i++){
        ans = max(ans, a[i] + i);
    }
    cout << ans << '\n';
}
accuracy: 0.625
time_ms: 20
memory_kb: 4544
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#ifdef APURBA
#include "DEBUG_TEMPLATE.h"
#else
#define HERE
#define debug(args...)
#endif
const int N = 100000+5;
typedef pair<int,int> pii;

#define ALL(x) x.begin(),x.end()

vector<int> bam[N];
vector<int> dan[N];
int bami[N];
int dani[N];
int ase[N];

void TEST_CASES()
{
    int r,s,p;
    cin>>r>>s>>p;
    for(int i=0;i<p;i++)
    {
        int x,y;
        cin>>x>>y;
        if(y<=s)
        {
            bam[x].push_back(s-y);
        }
        else
        {
            dan[x].push_back(y-s-1);
        }
    }
    for(int i=1;i<=r;i++)
    {
        sort(ALL(bam[i]));
        reverse(ALL(bam[i]));
        sort(ALL(dan[i]));
        reverse(ALL(dan[i]));

        for(int j=0;j+1<bam[i].size();j++) bam[i][j] -=bam[i][j+1]+1;
        for(int j=0;j+1<dan[i].size();j++) dan[i][j] -=dan[i][j+1]+1;

        debug(i,bam[i],dan[i]);

        bami[i] = int(bam[i].size()) - 1;
        dani[i] = int(dan[i].size()) - 1;
    }
    int tot = 0;
    while(p)
    {
        tot++;
        if(ase[r])
        {
            p--;
            ase[r]=0;
        }
        for(int i=r;i>=1;i--)
        {
            assert(ase[i]==0);
            if(ase[i-1])
            {
                ase[i]=1;
                ase[i-1] = 0;
            }
            while(bami[i]>=0 and bam[i][bami[i]] == 0) bami[i]--;
            while(dani[i]>=0 and dan[i][dani[i]] == 0) dani[i]--;

            if(ase[i] == 0 and !bam[i].empty() and bam[i].back() == 0)
            {
                ase[i]=1;
                bam[i].pop_back();
            }
            else if(bami[i]>=0)
            {
                bam[i][bami[i]]--;
            }

            if(ase[i] == 0 and !dan[i].empty() and dan[i].back() == 0)
            {
                ase[i]=1;
                dan[i].pop_back();
            }
            else if(dani[i]>=0)
            {
                dan[i][dani[i]]--;
            }
        }
    }
    cout<<tot<<"\n";
}


/*
*/

int32_t main()
{
#ifndef APURBA
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    //freopen("input.txt","r",stdin);
    //freopen("out1.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        TEST_CASES();
    }
    return 0;
}
accuracy: 1
time_ms: 580
memory_kb: 10168
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#ifdef APURBA
#include "DEBUG_TEMPLATE.h"
#else
#define HERE
#define debug(args...)
#endif
const int N = 505 +5;
typedef pair<int,int> pii;
mt19937 rng(chrono::steady_clock::now().
			time_since_epoch().count());
int getrand(int a, int b){
 int x = uniform_int_distribution<int>(a, b)(rng);
 return x;
}
int r,s,p;
bitset<N> dan[N],bam[N];
vector<int>danZ[N],bamZ[N];
int danL[N],bamL[N];
void TEST_CASES()
{
    cin>>r>>s>>p;

//    r=s=500;
//    p=50000;
//    map<pair<int,int>,bool>mp;
//    vector<pair<int,int>>all;
//    for(int i=1;i<=p;i++)
//    {
//        while(true)
//        {
//            int a=getrand(1,r);
//            int b=getrand(1,s*2);
//            if(mp[{a,b}]) continue;
//            mp[{a,b}]=1;
//            all.push_back({a,b});
//            break;
//        }
//
//
//    }

    for(int i=1;i<=p;i++)
    {
        int a,b;
        cin>>a>>b;
//        a=all[i-1].first;
//        b=all[i-1].second;
        if(b<=s)
        {
            bam[a][b]=1;
        }
        else
        {
            dan[a][2*s+1-b]=1;
        }
    }
    for(int i=1;i<=r;i++)
    {
        for(int j=1;j<=s;j++)
        {
            if(bam[i][j]==0)
            {
                bamZ[i].push_back(j);
            }
            if(dan[i][j]==0)
            {
                danZ[i].push_back(j);
            }
            danL[i]=bamL[i]=s;
        }
    }
    vector<int>mid(r+1,0);
    int step=0;
    int cnt=r*s*3;
    while(cnt--)
    {
        step++;
        if(mid[r]==1)
        {
            p--;
            mid[r]=0;
        }
        if(p==0) break;

        for(int i=r;i>=1;i--)
        {
            if(i!=r&&mid[i]==1&&mid[i+1]==0)
            {
                mid[i+1]=1;
                mid[i]=0;
            }
            //dan
            if(mid[i]==0)
            {
                if(danL[i]>=1&&dan[i][danL[i]])
                {
                    danL[i]--;
                    mid[i]=1;
                }
                else
                {
                    danL[i]--;

                }
            }
            else
            {
                if(danL[i]>=1&&dan[i][danL[i]])
                {
                    int which=-1;
                    while(danZ[i].size())
                    {
                        if(danZ[i].back()<=danL[i])
                        {
                            which=danZ[i].back();
                            assert(dan[i][which]==0);
                            dan[i][which]=1;
                            danZ[i].pop_back();
                            break;
                        }
                        danZ[i].pop_back();
                    }
                    if(which!=-1)
                        danL[i]--;
                }
                else
                {
                    danL[i]--;
                }

            }

            //bam
            if(mid[i]==0)
            {
                if(bamL[i]>=1&&bam[i][bamL[i]])
                {
                    bamL[i]--;
                    mid[i]=1;
                }
                else
                {
                    bamL[i]--;

                }
            }
            else
            {
                if(bamL[i]>=1&&bam[i][bamL[i]])
                {
                    int which=-1;
                    while(bamZ[i].size())
                    {
                        if(bamZ[i].back()<=bamL[i])
                        {
                            which=bamZ[i].back();
                            assert(bam[i][which]==0);
                            bam[i][which]=1;
                            bamZ[i].pop_back();
                            break;
                        }
                        bamZ[i].pop_back();
                    }
                    if(which!=-1)
                        bamL[i]--;
                }
                else
                {
                    bamL[i]--;
                }

            }

        }
    }
    cout<<step<<"\n";
}




/*
*/

int32_t main()
{
#ifndef APURBA
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    //freopen("input.txt","r",stdin);
    //freopen("out1.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        TEST_CASES();
    }
    return 0;
}
accuracy: 1
time_ms: 600
memory_kb: 5320
final_rank: 10

code_snippet: #include<cstdio>
#include<algorithm>
#include<cstdlib>

const int N=3e5+1;

int r, s, p;
int d[N];

int main() {
    scanf("%d %d %d", &r, &s, &p);
    for(int i=0; i<p; i++) {
        int x, y;
        scanf("%d %d", &x, &y);
        if(y>s) {
            d[i]=r-x+y-s;
        }
        else {
            d[i]=r-x+s-y+1;
        }
    }

    std::sort(d, d+p, [](int x, int y) {
        return x>y;
    });

    int ans=0;
    for(int i=0; i<p; i++) {
        ans=std::max(ans, i+1+d[i]);
    }

    printf("%d\n", ans);
}
accuracy: 0.625
time_ms: 20
memory_kb: 4128
final_rank: 16

code_snippet: #include<bits/stdc++.h>
#define int long long
const int MAXN=505;
int r,s,p;
int d[MAXN*MAXN];
signed main(){
    scanf("%lld%lld%lld",&r,&s,&p);
    for(int i=1;i<=p;i++){
        int x,y;scanf("%lld%lld",&x,&y);
        d[i]=y>s?r-x+y-s:r-x+1+s-y;
    }
    std::sort(d+1,d+p+1,[](int x,int y){return x>y;});
    int ans(0);
    for(int i=1;i<=p;i++) ans=std::max(ans,i+d[i]);
    printf("%lld\n",ans);
}
accuracy: 0.625
time_ms: 10
memory_kb: 5288
final_rank: 19

code_snippet: #include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>
#include <ranges>
namespace ranges = std::ranges;
namespace views = std::views;
// #include "Src/Number/IntegerDivision.hpp"
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// #include "Src/Algebra/Group/AdditiveGroup.hpp"
// #include "Src/DataStructure/FenwickTree/FenwickTree.hpp"
// #include "Src/DataStructure/SegmentTree/SegmentTree.hpp"
// using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int R, S, P;
    std::cin >> R >> S >> P;
    std::vector dist(R, std::vector<int>(2 * S));
    for (int i = R - 1 ; i >= 0 ; i--) {
        const int y = R - i;
        for (int j = 0 ; j < S ; j++) {
            const int x = j + 1;
            dist[i][S + j] = dist[i][S - j - 1] = x + y;
        }
    }
    std::vector<int> cnt(dist[0][0] + 1);
    for (int i = 0 ; i < P ; i++) {
        int r, c;
        std::cin >> r >> c;
        r--; c--;
        cnt[dist[r][c]]++;
    }
    int ans = 0, wait = 0;
    for (int i = 0 ; i < std::ssize(cnt) ; i++) {
        wait = std::max(wait - 1, 0);
        for (int j = 0 ; j < cnt[i] ; j++) {
            ans = std::max(i + wait, ans);
            wait++;
        }
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 5376
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int r, s, p, a[N];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> r >> s >> p;
    for (int i = 1; i <= p;i++){
        int x, y;
        cin >> x >> y;
        a[i] = r - x + abs(y - s);
        if(y<=s)
            a[i]++;
    }
    sort(a + 1, a + 1 + p);
    int ans = 0;
    for (int i = p; i >= 1;i--){
        ans = max(ans, a[i] + p - i + 1);
    }
    cout << ans  << '\n';
}
accuracy: 0.625
time_ms: 20
memory_kb: 4552
final_rank: 18

code_snippet: #include<bits/stdc++.h>
#define int long long
int r,s,p;
int d[3000005];
signed main(){
    scanf("%lld%lld%lld",&r,&s,&p);
    for(int i=1;i<=p;i++){
        int x,y;scanf("%lld%lld",&x,&y);
        d[i]=y>s?r-x+y-s:r-x+1+s-y;
    }
    std::sort(d+1,d+p+1,[](int x,int y){return x>y;});
    int ans(0);
    for(int i=1;i<=p;i++) ans=std::max(ans,i+d[i]);
    printf("%lld\n",ans);
}
accuracy: 1
time_ms: 50
memory_kb: 7640
final_rank: 7