Problem:
Fundraising Street
Mr. Tokuichi has come up with the idea of making a Buddha statue for the people of the Isua region and has decided to raise funds for the project. He plans to travel along a road known as the "fundraising road," a road that is said to collect donations well when traveled.
A fund-raising highway is a straight line of towns lined up along the highway, numbered from 1 in order from the town at the beginning to the town at the end. The towns are numbered from one to the last, starting with town 1. Then he moves on to town 2, town 3, and so on. Mr. Tokuichi can collect donations in all towns, but he must follow the rules below.
Mr. Tokuichi sets a target amount of donations to be collected in each town. Once he has raised the amount of money he set for that town, he will move on to the next town.
It is possible to set the target amount to be collected in a certain town to zero and not collect donations in that town, but it is not possible to set the target amount to a negative value.
The target amount in town 1 is either 0 yen or 1 yen.
The target amount in any town other than town 1 must be either an increase of 1 yen, a decrease of 1 yen, or the same amount as the target amount in the immediately preceding town.
Some towns have gates and are further subject to the following rules.
In towns with a customs office, Tokuichi must notify the customs office of the target amount of money to be collected in that town.
When the amount of money reported matches the amount of money set by each customs office, the fund-raising activity is allowed to continue in that town, and the journey to the next town is allowed to continue.
If the amount of money reported does not match the amount set by each customs office, the fundraising activities in that town will not be allowed and the trip must end there.
For example, let's say there is a customs office in town 3 and the amount of money set there is 2 yen. In this case, if the target amount in towns 1, 2, and 3 is 1 yen, 2 yen, and 2 yen, respectively, the target amount in town 3 will match the amount set at the customs office.
Mr. Tokuichi has found out the amount of money set for each barrier. Based on this information, he is trying to figure out how to set a target amount to maximize the total amount of donations collected.
Given the number of towns along the street, the number of the town with the customs office, and the amount of money specified for each customs office, create a program that calculates the maximum total amount of donations that Tokuichi can collect.
Input
Input is given in the following format.
$N$ $M$
$a_1$ $b_1$
$a_2$ $b_2$
 :
$a_M$ $b_M$
In the first line, we are given the number of towns along the road, $N$ ($1 \leq N \leq 1,000,000,000$), and the number of customs offices, $M$ ($1 \leq M \leq $min($N, 100000$)). The following $M$ line gives the number of the town where the customs office is located, $a_i$ ($1 \leq a_i \leq N$), and the amount of money specified for that c
...(truncated)

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
#include <climits>
using namespace std;
#define ll long long 
#define ld long double
#define rep(i,N) for(int i=0;i<(int)N;i++)
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define pb push_back

int main(){
    ll N,M;cin>>N>>M;
    ll nm = 1;ll bef = 1;
    ll ans = 0;ll nsc = 0;
    vector<ll>A,B;
    rep(i,M){
        ll a,b;cin>>a>>b;
        A.pb(a);B.pb(b);
    }
    rep(i,M){
        ll a=A[i],b=B[i];
        ll len = a-bef;
        ans = max(ans,nsc+nm*(len)+(len-1)*len/2);
        if(abs(nm-b)>len)break;
        ll st = max(nm,b)+1;
        if(b==nm){
            len--;
            if(len>=0)nsc+=b;
        }
        if(b>nm){
            len-=(b-nm)+1;
            nsc += (b+nm)*(b-nm+1)/2;
            if(len<0)nsc-=b;
        }
        if(b<nm){
            len-=(nm-b)+1;
            nsc += (b+nm)*(nm-b+1)/2;
            nsc+=nm;nsc-=b;
            if(len<0)nsc-=nm;
        }
        if(len>=1)nsc+=st*len;
        if(len%2==1&&len>=1)nsc+=(len)/2;
        if(len>=2)nsc += (len/2)*((len)/2-1);
        bef = a;
        nm = b;
        ans = max(ans,nsc);
        // cout<<nsc<<":"<<nm<<":"<<bef<<endl;
        if(i==M-1){
            len = N-a+1;
            ans = max(ans,nsc+nm*(len)+(len-1)*len/2);
        }
    }
    nm = 0;bef = 1;
    nsc = 0;
    rep(i,M){
        ll a=A[i],b=B[i];
        ll len = a-bef;
        ans = max(ans,nsc+nm*(len)+(len-1)*len/2);
        if(abs(nm-b)>len)break;
        ll st = max(nm,b)+1;
        if(b==nm){
            len--;
            if(len>=0)nsc+=b;
        }
        if(b>nm){
            len-=(b-nm)+1;
            nsc += (b+nm)*(b-nm+1)/2;
            if(len<0)nsc-=b;
        }
        if(b<nm){
            len-=(nm-b)+1;
            nsc += (b+nm)*(nm-b+1)/2;
            nsc+=nm;nsc-=b;
            if(len<0)nsc-=nm;
        }
        if(len>=1)nsc+=st*len;
        if(len%2==1&&len>=1)nsc+=(len)/2;
        if(len>=2)nsc += (len/2)*((len)/2-1);
        bef = a;
        nm = b;
        ans = max(ans,nsc);
        // cout<<nsc<<":"<<nm<<":"<<bef<<endl;
        if(i==M-1){
            len = N-a+1;
            ans = max(ans,nsc+nm*(len)+(len-1)*len/2);
        }
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 4892
final_rank: 12

code_snippet: #include<bits/stdc++.h>
#include <climits>
using namespace std;
#define ll long long 
#define ld long double
#define rep(i,N) for(int i=0;i<(int)N;i++)
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define pb push_back

int main(){
    ll N,M;cin>>N>>M;
    ll nm = 1;ll bef = 1;
    ll ans = 0;ll nsc = 0;
    vector<ll>A,B;
    rep(i,M){
        ll a,b;cin>>a>>b;
        A.pb(a);B.pb(b);
    }
    rep(i,M){
        ll a=A[i],b=B[i];
        ll len = a-bef;
        ans = max(ans,nsc+nm*(len)+(len-1)*len/2);
        if(abs(nm-b)>len)break;
        ll st = max(nm,b)+1;
        if(b==nm){
            len--;
            nsc+=b;
        }
        if(b>nm){
            len-=(b-nm)+1;
            nsc += (b+nm)*(b-nm+1)/2;
            if(len<0)nsc-=b;
        }
        if(b<nm){
            len-=(nm-b)+1;
            nsc += (b+nm)*(nm-b+1)/2;
            nsc+=nm;nsc-=b;
            if(len<0)nsc-=nm;
        }
        if(len>=1)nsc+=st*len;
        if(len%2==1&&len>=1)nsc+=(len)/2;
        if(len>=2)nsc += (len/2)*((len)/2-1);
        bef = a;
        nm = b;
        ans = max(ans,nsc);
        // cout<<nsc<<":"<<nm<<":"<<bef<<endl;
        if(i==M-1){
            len = N-a+1;
            ans = max(ans,nsc+nm*(len)+(len-1)*len/2);
        }
    }
    nm = 0;bef = 1;
    nsc = 0;
    rep(i,M){
        ll a=A[i],b=B[i];
        ll len = a-bef;
        ans = max(ans,nsc+nm*(len)+(len-1)*len/2);
        if(abs(nm-b)>len)break;
        ll st = max(nm,b)+1;
        if(b==nm){
            len--;
            nsc+=b;
        }
        if(b>nm){
            len-=(b-nm)+1;
            nsc += (b+nm)*(b-nm+1)/2;
            if(len<0)nsc-=b;
        }
        if(b<nm){
            len-=(nm-b)+1;
            nsc += (b+nm)*(nm-b+1)/2;
            nsc+=nm;nsc-=b;
            if(len<0)nsc-=nm;
        }
        if(len>=1)nsc+=st*len;
        if(len%2==1&&len>=1)nsc+=(len)/2;
        if(len>=2)nsc += (len/2)*((len)/2-1);
        bef = a;
        nm = b;
        ans = max(ans,nsc);
        // cout<<nsc<<":"<<nm<<":"<<bef<<endl;
        if(i==M-1){
            len = N-a+1;
            ans = max(ans,nsc+nm*(len)+(len-1)*len/2);
        }
    }
    cout<<ans<<endl;
}
accuracy: 0.6896551724137931
time_ms: 40
memory_kb: 4752
final_rank: 19

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#define int long long
using namespace std;
signed main()
{
  int n,m;
  cin >> n >> m;
  vector<pair<int,int>>town(m);
  for(int i=0;i<m;i++)cin >> town[i].first >> town[i].second;
  town.push_back(make_pair(0,0));
  town.push_back(make_pair(n,1e9+1));
  sort(town.begin(),town.end());
  m+=2;

  long long ans=0;
  long long current=0;
  for(int i=0;i<m-1;i++){
    long long now=town[i].second;

    {
      int cnt=town[i+1].first-town[i].first;
      if(town[i+1].first==n && town[i+1].second==1e9+1)cnt++;
      ans=max(ans,current+(now+now+cnt-1)*cnt/2);

      if(now+cnt<town[i+1].second || now-cnt>town[i+1].second)break;
    }

    int left=now,right=1e9+1;
    while(left+1!=right){
      int mid=(left+right)/2;


      int cnt=mid-now;
      int cnt2=mid-town[i+1].second;

      if(town[i].first+cnt+cnt2<=town[i+1].first)left=mid;
      else right=mid;
    }

    int cnt=left-now;
    int cnt2=left-town[i+1].second;

    long long count=0;
    if(town[i].first+cnt+cnt2+1<=town[i+1].first)count+=left;
    count+=(now+left)*(cnt+1)/2;
    count+=(left+town[i+1].second+1)*cnt2/2;

    count-=left;
    current+=count;
    
    ans=max(ans,current);
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 7236
final_rank: 18

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

constexpr int BIG_INT = 1'000'000'100;
constexpr long BIG_LONG = 1'000'000'000'000'000'100;

long range_sum(long l, long r) { return (r - l + 1) * (r + l) / 2; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector ab(m, make_pair(0, 0l));
    for (auto& [a, b] : ab) {
        cin >> a >> b;
        a--;
    }

    long ans = -1;

    long now = 0;
    long moku = 0;
    long town = -1;
    bool flag = true;
    for (int i = 0; i < m; i++) {
        long towns_to_go = ab[i].first - town;
        ans = max(ans, now + range_sum(moku + 1, moku + towns_to_go - 1));

        if (ab[i].second > moku + towns_to_go ||
            ab[i].second < moku - towns_to_go) {
            flag = false;
            break;
        }

        if (ab[i].second >= moku) {
            if (ab[i].second > moku) now += range_sum(moku + 1, ab[i].second);
            int rem = towns_to_go - (ab[i].second - moku);
            if (rem == 1) {
                now += ab[i].second;
            } else if (rem >= 2) {
                if (rem % 2 == 1) {
                    now += range_sum(ab[i].second + 1, ab[i].second + rem / 2) +
                           range_sum(ab[i].second, ab[i].second + rem / 2);
                } else {
                    now += range_sum(ab[i].second + 1, ab[i].second + rem / 2) +
                           range_sum(ab[i].second, ab[i].second + rem / 2 - 1);
                }
            }
        } else {
            now += range_sum(ab[i].second, moku);
            int rem = towns_to_go - (moku - ab[i].second + 1);
            if (rem == -1) {
                now -= moku;
            } else if (rem == 1) {
                now += moku + 1;
            } else if (rem > 1) {
                if (rem % 2 == 1) {
                    now += range_sum(moku + 1, moku + rem / 2 + 1) +
                           range_sum(moku + 1, moku + rem / 2);
                } else {
                    now += range_sum(moku + 1, moku + rem / 2) +
                           range_sum(moku + 1, moku + rem / 2);
                }
            }
        }
        town = ab[i].first;
        moku = ab[i].second;
        ans = max(ans, now);
        // cout << now << endl;
    }
    if (flag) {
        ans = max(ans, now + range_sum(moku + 1, moku + (n - 1 - town)));
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4636
final_rank: 4

code_snippet: #include<bits/stdc++.h>

using namespace std;

#define rep(i, l, n) for(int i = (int)(l); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template<class T> bool chmin(T &a, T b) {if(a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T &a, T b) {if(a < b) {a = b; return true;} return false;}
template<class T> using spq = priority_queue<T, vector<T>, greater<T>>;

//bool -> Yes/No
string answer(const bool b) {return b ? "Yes" : "No";}

void fix(int k) {cout << fixed << setprecision(k);}

const int inf = 2e9;
const long long INF = 2e18;
const long double eps = 1e-12;
int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, -1, 0, 1, 1, -1, 1, -1};

void main_program() {
    long long n; int m; cin >> n >> m;
    long long from = 0, last = 0;
    long long sum = 0;
    long long final_ans = 0;
    for(int _ = 0; _ <= m; _++) {
        long long d, to;
        if(_ < m) cin >> d >> to;
        else d = n + 1, to = INF;
        long long dist = d - last;
        bool fin = false;
        chmax(final_ans, sum + dist * (dist - 1) / 2 + from * dist);
        if(abs(from - to) > dist) {
            // 無理
            break;
        }
        else if((dist - abs(from - to)) & 1) {
            // 途中平
            long long p = (to + dist - from - 1) >> 1;
            long long z = p * (p - 1) / 2 + from * p;
            z += (from + p);
            p = dist - p - 1;
            z += p * (p - 1) / 2 + (to + 1) * p;
            sum += z;
        }
        else {
            // 角
            long long p = (to + dist - from) >> 1;
            long long z = p * (p - 1) / 2 + from * p;
            p = dist - p;
            z += p * (p - 1) / 2 + (to + 1) * p;
            sum += z;
        }
        last = d;
        from = to;
        chmax(final_ans, sum);
    }
    cout << final_ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    //cin >> t;
    while(t--) main_program();
}
accuracy: 1
time_ms: 10
memory_kb: 3432
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#include <iomanip>
#include <numbers>
#include <cassert>
#include <numeric>
#ifdef _MSC_VER
#include <intrin.h>
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pint = pair<int, int>;
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
template<typename T>
bool chmax(T & a, const T & b) { if (a < b) { a = b; return true; } return false; }
template<typename T>
bool chmin(T & a, const T & b) { if (a > b) { a = b; return true; } return false; }
const int inf = 1e9;
const ll linf = 1e18;
int main() {
    int n, m; cin >> n >> m;
    int town = 0, donate = 0;
    ll total = 0, ans = 0;
    auto sum = [&](ll fst, ll lst, ll len) {
        return len * (fst + lst) / 2;
    };
    rep(i, m) {
        int a, b; cin >> a >> b;
        int dist = a - town;
        chmax(ans, total + sum(donate, donate + dist - 1, dist) - donate);
        if (abs(donate - b) > dist) {
            cout << total + sum(donate, donate + dist - 1, dist) - donate << endl;
            return 0;
        }
        dist -= abs(donate - b);
        total += sum(donate, b, abs(donate - b) + 1) - donate;
        if (dist != 0) {
            int base = max(donate, b);
            total += base;
            dist--;
            total += sum(base + 1, base + dist / 2, dist / 2) * 2;
            if (dist % 2)total += base + dist / 2 + 1;
        }
        chmax(ans, total);
        town = a;
        donate = b;
    }
    int dist = n - town;
    chmax(ans, total + sum(donate, donate + dist, dist + 1) - donate);
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3584
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
// #include<atcoder/all>
// using mint = atcoder::modint998244353;
using ld = long double;
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
template<typename T>bool chmin(T&a,T b){return b<a?(a=b,1):0;}
template<typename T>bool chmax(T&a,T b){return b>a?(a=b,1):0;}

long tsum(long s, long t){
    if(s>t) return 0;
    return (s+t)*(t-s+1)/2;
}
optional<long> f(long l, long r, long w){
    if(r-l>w) return nullopt;
    long ret=0;
    long t=r+(w-(r-l))/2;
    ret+=tsum(l, t);
    ret+=tsum(r, t);
    if((w-(r-l))%2==0) ret-=t;
    return ret;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N,M;
    cin>>N>>M;

    long a=0,b=0;
    long cur=0;
    long ans=0;
    rep(i,M+1){
        if(i==M){
            chmax(ans, cur+tsum(b+1, b+(N-a)));
            break;
        }
        long na,nb;
        cin>>na>>nb;
        chmax(ans, cur+tsum(b+1, b+(na-a-1)));
        int fl=0;
        if(b>nb) fl=1, swap(b,nb); 
        auto r=f(b, nb, na-a);
        if(fl) swap(b, nb);

        if(r.has_value()){
            cur+=r.value();
            cur-=b;
        }
        else{
            break;
        }
        a=na;
        b=nb;
    }

    cout<<ans<<"\n";
}
accuracy: 1
time_ms: 10
memory_kb: 3584
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define INF 1e18
int main(){
    
    ll n,m,ans,sub=0;cin>>n>>m;
    vector<pair<ll,ll>> x(m+2,{1,1});
    for(ll i=1;i<=m;i++)cin>>x[i].first>>x[i].second;
    if(x[1].first==1&&x[1].second<2)x[0].second=x[1].second;
    ans=x[0].second;
    for(ll i=0;i<=m;i++){
        ll a=x[i].second,b=x[i+1].second,f=x[i+1].first-x[i].first,d=a+f-b,c;
        if(i==m){
            f=n-x[i].first;
            ans+=(a*2+f)*(f+1)/2-a;
        }
        else if(b<a-f||a+f<b){
            ans+=(a*2+f)*(f+1)/2-a*2-f;
            break;
        }
        else if(d%2==0){
            d/=2;
            c=f-d-1;
            sub=max(sub,ans+(a*2+f)*(f+1)/2-a*2-f);
            ans+=(a*2+c)*(c+1)/2+(b*2+d)*(d+1)/2-a;
        }else{
            d/=2;
            c=f-d-1;
            sub=max(sub,ans+(a*2+f)*(f+1)/2-a*2-f);
            ans+=(a*2+c)*(c+1)/2+(b*2+d)*(d+1)/2-a;
        }
    }
    cout<<max(ans,sub)<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 4608
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; ++i)
ll INFL = 1e18;
template<typename T> bool chmax(T& a, const T& b) { if (a < b) {a = b; return true;} return false; }
int main() {
    ll n,m; cin >> n >> m;
    vector<ll> a(m), b(m);
    rep(i, m) cin >> a[i] >> b[i];
    a.push_back(n+1);
    b.push_back(0);
    auto calc_max = [&] (ll w, ll l, ll r) -> tuple<ll,ll,bool> {
        ll mx1 = (l + 1 + l + w) * w / 2;
        if (r < l - w || r > l + w) return {mx1, -INFL, false};
        ll mid = (l + w + r) / 2;
        ll mx2 = 0;
        if (mid - l + mid - r + 1 != w) mx2 -= mid;
        mx2 += (l + 1 + mid) * (mid - l) / 2;
        mx2 += (r + mid) * (mid - r + 1) / 2;
        return {mx1, mx2, true};
    };
    ll ai(0), bi(0);
    ll ans = 0;
    ll cur = 0;
    rep(i, m+1) {
        auto [mx1, mx2, ok] = calc_max(a[i] - ai, bi, b[i]);
        if (!ok) {
            chmax(ans, cur + mx1 - (bi + a[i] - ai));
            break;
        }
        chmax(ans, cur + max(mx1 - (bi + a[i] - ai), mx2));
        cur += mx2;
        ai = a[i];
        bi = b[i];
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 5464
final_rank: 14

code_snippet: //line 2 "/home/seekworser/.cpp_lib/competitive_library/competitive/std/std.hpp"
#include <bits/stdc++.h>
#ifndef LOCAL_TEST
#pragma GCC target ("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif // LOCAL_TEST
using namespace std;
// 型名の短縮
using ll = long long;
using pii = pair<int, int>; using pll = pair<ll, ll>;
using vi = vector<int>;  using vvi = vector<vi>; using vvvi = vector<vvi>;
using vl = vector<ll>;  using vvl = vector<vl>; using vvvl = vector<vvl>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>;
using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
using vs = vector<string>; using vvs = vector<vector<string>>; using vvvs = vector<vector<vector<string>>>;
template<typename T> vector<vector<T>> vv(int h, int w, T val = T()) { return vector(h, vector<T>(w, val)); }
template<typename T> vector<vector<vector<T>>> vvv(int h1, int h2, int h3, T val = T()) { return vector(h1, vector(h2, vector<T>(h3, val))); }
template<typename T> vector<vector<vector<vector<T>>>> vvvv(int h1, int h2, int h3, int h4, T val = T()) { return vector(h1, vector(h2, vector(h3, vector<T>(h4, val)))); }
template <class T> using priority_queue_min = priority_queue<T, vector<T>, greater<T>>;
// 定数の定義
constexpr double PI = 3.14159265358979323;
constexpr int INF = 100100111; constexpr ll INFL = 3300300300300300491LL;
float EPS = 1e-8; double EPSL = 1e-16;
template<typename T> bool eq(const T x, const T y) { return x == y; }
template<> bool eq<double>(const double x, const double y) { return abs(x - y) < EPSL; }
template<> bool eq<float>(const float x, const float y) { return abs(x - y) < EPS; }
template<typename T> bool neq(const T x, const T y) { return !(eq<T>(x, y)); }
template<typename T> bool ge(const T x, const T y) { return (eq<T>(x, y) || (x > y)); }
template<typename T> bool le(const T x, const T y) { return (eq<T>(x, y) || (x < y)); }
template<typename T> bool gt(const T x, const T y) { return !(le<T>(x, y)); }
template<typename T> bool lt(const T x, const T y) { return !(ge<T>(x, y)); }
constexpr int MODINT998244353 = 998244353;
constexpr int MODINT1000000007 = 1000000007;
// 入出力高速化
struct Nyan { Nyan() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } nyan;
// 汎用マクロの定義
#define all(a) (a).begin(), (a).end()
#define sz(x) ((ll)(x).size())
#define rep1(n) for(ll dummy_iter = 0LL; dummy_iter < n; ++dummy_iter) // 0 から n-1 まで昇順
#define rep2(i, n) for(ll i = 0LL, i##_counter = 0LL; i##_counter < ll(n); ++(i##_counter), (i) = i##_counter) // 0 から n-1 まで昇順
#define rep3(i, s, t) for(ll i = ll(s), i##_counter = ll(s); i##_counter < ll(t); ++(i##_counter), (i) = (i##_counter)) // s から t まで昇順
#define rep4(i, s, t, step) for(ll i##_counter = step > 0 ? ll(s) : -ll(s), i##_end = step > 0 ? ll(t) : -ll(t), i##_step = abs(step), i = ll(s); i##_counter < i##_end; i##_counter += i##_step, i = step > 0 ? i##_counter : -i##_counter) // s から t まで stepずつ
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define repe(a, v) for(auto& a : (v)) // v の全要素（変更可能）
#define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod
#define sdiv(n, m) (((n) - smod(n, m)) / (m)) // 非負div
#define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去
int Yes(bool b=true) { cout << (b ? "Yes\n" : "No\n"); return 0; };
int YES(bool b=true) { cout << (b ? "YES\n" : "NO\n"); return 0; };
int No(bool b=true) {return Yes(!b);};
int NO(bool b=true) {return YES(!b);};
template<typename T, size_t N> T max(array<T, N>& a) { return *max_element(all(a)); };
template<typename T, size_t N> T min(array<T, N>& a) { return *min_element(all(a)); };
template<typename T> T max(vector<T>& a) { return *max_element(all(a)); };
template<typename T> T min(vector<T>& a) { return *min_element(all(a)); };
template<typename T> vector<T> accum(const vector<T>& a) { vector<T> rev(sz(a)+1, 0); rep(i, sz(a)) rev[i+1] = rev[i] + a[i]; return rev; };
template<typename T> vector<T> vec_slice(const vector<T>& a, int l, int r) { vector<T> rev; rep(i, l, r) rev.push_back(a[i]); return rev; };
template<typename T> T sum(vector<T>& a, T zero = T(0)) { T rev = zero; rep(i, sz(a)) rev += a[i]; return rev; };
template<typename T> bool in_range(const T& val, const T& s, const T& t) { return s <= val && val < t; };

template <class T> inline vector<T>& operator--(vector<T>& v) { repe(x, v) --x; return v; }
template <class T> inline vector<T>& operator++(vector<T>& v) { repe(x, v) ++x; return v; }

// modでのpow
ll powm(ll a, ll n, ll mod=INFL) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = (res * a) % mod;
        if (n > 1) a = (a * a) % mod;
        n >>= 1;
    }
    return res;
}
// 整数Sqrt
ll sqrtll(ll x) {
    assert(x >= 0);
    ll rev = sqrt(x);
    while(rev * rev > x) --rev;
    while((rev+1) * (rev+1)<=x) ++rev;
    return rev;
}
template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新（更新されたら true を返す）
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新（更新されたら true を返す）
int digit(ll x, int d=10) { int rev=0; while (x > 0) { rev++; x /= d;}; return rev; } // xのd進数桁数
/**
 * @brief std.hpp
 * @docs docs/std/std.md
 */
//line 3 "/home/seekworser/.cpp_lib/competitive_library/competitive/std/io.hpp"
// 演算子オーバーロード（プロトタイプ宣言）
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p);
template <class T> inline istream& operator>>(istream& is, vector<T>& v);
template <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p);
template <class T> inline ostream& operator<<(ostream& os, const vector<T>& v);
template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp);
template <typename T> ostream &operator<<(ostream &os, const set<T> &st);
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st);
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st);
template <typename T> ostream &operator<<(ostream &os, queue<T> q);
template <typename T> ostream &operator<<(ostream &os, deque<T> q);
template <typename T> ostream &operator<<(ostream &os, stack<T> st);
template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq);

// 演算子オーバーロード
template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repe(x, v) is >> x; return is; }
template <class T, class U> inline ostream& operator<<(ostream& os, const pair<T, U>& p) { os << p.first << " " << p.second; return os; }
template <class T> inline ostream& operator<<(ostream& os, const vector<T>& v) { rep(i, sz(v)) { os << v.at(i); if (i != sz(v) - 1) os << " "; } return os; }
template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &mp) { for (auto &[key, val] : mp) { os << key << ":" << val << " "; } return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { auto itr = st.begin(); for (int i = 0; i < (int)st.size(); i++) { os << *itr << (i + 1 != (int)st.size() ? " " : ""); itr++; } return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { ll cnt = 0; for (auto &e : st) { os << e << (++cnt != (int)st.size() ? " " : ""); } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> q) { while (q.size()) { os << q.front() << " "; q.pop(); } return os; }
template <typename T> ostream &operator<<(ostream &os, deque<T> q) { while (q.size()) { os << q.front() << " "; q.pop_front(); } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> st) { while (st.size()) { os << st.top() << " "; st.pop(); } return os; }
template <class T, class Container, class Compare> ostream &operator<<(ostream &os, priority_queue<T, Container, Compare> pq) { while (pq.size()) { os << pq.top() << " "; pq.pop(); } return os; }

template <typename T> int print_sep_end(string sep, string end, const T& val) { (void)sep; cout << val << end; return 0; };
template <typename T1, typename... T2> int print_sep_end(string sep, string end, const T1 &val, const T2 &...remain) {
    cout << val << sep;
    print_sep_end(sep, end, remain...);
    return 0;
};
template <typename... T> int print(const T &...args) { print_sep_end(" ", "\n", args...); return 0; };
template <typename... T> void flush() { cout << flush; };
template <typename... T> int print_and_flush(const T &...args) { print(args...); flush(); return 0; };
#define debug(...) debug_func(0, #__VA_ARGS__, __VA_ARGS__) // debug print
template <typename T> void input(T &a) { cin >> a; };
template <typename T1, typename... T2> void input(T1&a, T2 &...b) { cin >> a; input(b...); };
#ifdef LOCAL_TEST
template <typename T> void debug_func(int i, const T name) { (void)i; (void)name; cerr << endl; }
template <typename T1, typename T2, typename... T3> void debug_func(int i, const T1 &name, const T2 &a, const T3 &...b) {
    int scope = 0;
    for ( ; (scope != 0 || name[i] != ',') && name[i] != '\0'; i++ ) {
        cerr << name[i];
        if (name[i] == '(' || name[i] == '{') scope++;
        if (name[i] == ')' || name[i] == '}') scope--;
    }
    cerr << ":" << a << " ";
    debug_func(i + 1, name, b...);
}
template <typename T1, typename T2, typename... T3> void debug_func(int i, const T1 &name, T2 &a, T3 &...b) {
    int scope = 0;
    for ( ; (scope != 0 || name[i] != ',') && name[i] != '\0'; i++ ) {
        cerr << name[i];
        if (name[i] == '(' || name[i] == '{') scope++;
        if (name[i] == ')' || name[i] == '}') scope--;
    }
    cerr << ":" << a << " ";
    debug_func(i + 1, name, b...);
}
#endif
#ifndef LOCAL_TEST
template <typename... T>
void debug_func(T &...) {}
template <typename... T>
void debug_func(const T &...) {}
#endif
/**
 * @brief io.hpp
 * @docs docs/std/io.md
 */
//line 3 "answer.cpp"
int main() {
    ll n,m; input(n, m);
    vl a(m), b(m);
    rep(i, m) input(a[i], b[i]);
    a.push_back(n+1);
    b.push_back(0);
    auto calc_max = [&] (ll w, ll l, ll r) -> tuple<ll,ll,bool> {
        ll mx1 = (l + 1 + l + w) * w / 2;
        if (r < l - w || r > l + w) return {mx1, -INFL, false};
        ll mid = (l + w + r) / 2;
        debug(mid);
        ll mx2 = 0;
        if (mid - l + mid - r + 1 != w) mx2 -= mid;
        mx2 += (l + 1 + mid) * (mid - l) / 2;
        mx2 += (r + mid) * (mid - r + 1) / 2;
        return {mx1, mx2, true};
    };
    ll ai(0), bi(0);
    ll ans = 0;
    ll cur = 0;
    rep(i, m+1) {
        auto [mx1, mx2, ok] = calc_max(a[i] - ai, bi, b[i]);
        debug(mx1, mx2, ok);
        if (!ok) {
            chmax(ans, cur + mx1 - (bi + a[i] - ai));
            break;
        }
        chmax(ans, cur + max(mx1 - (bi + a[i] - ai), mx2));
        cur += mx2;
        ai = a[i];
        bi = b[i];
    }
    print(ans);
}
accuracy: 1
time_ms: 10
memory_kb: 5440
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

ll sum(ll Min, ll Max) {
    Min --;
    return Max * (Max + 1) / 2 - Min * (Min + 1) / 2;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    ll ans = 0, s = 0;
    ll p = 0, now = 0;
    vector<ll> x(m), y(m);
    rep(i, m) cin >> x[i] >> y[i];
    rep(_, m) {
        ll a = x[_], b = y[_];
        {
            //chmax
            ll Ma = a - p - 1;
            ans = max(ans, s + sum(now + 1, now + Ma));
        }
        if(a - p < abs(now - b)) {
            cout << ans << endl;
            return 0;
        }
        ll num = (a - p - abs(now - b));
        ll c = num / 2;

        ll Ma = max(now, b) + c;
        if(num & 1) {
            s += sum(now + 1, Ma);
            s += sum(b, Ma);
        } else {
            s += sum(now + 1, Ma);
            s += sum(b, Ma - 1);
        }
        ans = max(ans, s);
        now = b;
        p = a;
    }
    {
        ll Ma = n - p + now;
        s += sum(now + 1, Ma);
        ans = max(s, ans);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4612
final_rank: 3

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

long long n, m, a, b, s = 0, t = 0, u = 0, w = 0;
bool f = 1;

void sum(int c, int d) {
	if (f) {
		w = max(w, u + (t * 2 + c - s) * (c - s - 1) / 2);

		if (b < t - a + s || b > t + a - s)
			f = 0;
		else {
			u += (t * 2 + c - s + 1) * (c - s) / 2;

			long long v = t + c - s - d;

			if (v % 2 == 0)
				u -= v * (v + 2) / 4;
			else
				u -= (v + 1) * (v + 1) / 4;

			s = c;
			t = d;
		}
	}
}

int main()
{
	cin >> n >> m;

	for (int i = 0; i < m; i++) {
		cin >> a >> b;

		sum(a, b);
	}

	sum(n, t + n - s);
	cout << max(u, w) << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 3408
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) begin(x), end(x)
#define Fixed fixed << setprecision(12)
#define int int_fast64_t
using pii = pair<int,int>;
constexpr int32_t INF  = 0x3f3f3f3f;
constexpr int_fast64_t LINF = 0x3f3f3f3f3f3f3f3fll;
constexpr int mod1 = 1e9+7;
constexpr int mod2 = 998244353;

template <class Func>
class FixPoint : Func {
public:
    explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}

    template <class... Args>
    constexpr decltype(auto) operator()(Args&&... args) const {
        return Func::operator()(*this, std::forward<Args>(args)...);
    }
};

template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
    return FixPoint<Func>{forward<Func>(f)};
}

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T> using max_heap = priority_queue<T>;
template <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;
template <class A, class B> using umap = unordered_map<A,B>;

template <class T> inline void bye(T x) { cout << x << '\n'; exit(0); }

inline int square(int a){ return a * a;}
inline int updiv(int a,int b){ return (a + b - 1) / b; }

constexpr int dx[] = {1,0,-1,0,1,1,-1,-1};
constexpr int dy[] = {0,-1,0,1,1,-1,-1,1};

signed main(void){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    vector<int> a(m), b(m);

    for (int i = 0; i < m; ++i) {
        cin >> a[i] >> b[i];
    }

    a.insert(a.begin(), 0);
    b.insert(b.begin(), 0);
    a.emplace_back(n + 1);
    b.emplace_back(LINF);
    m += 2;

    int sum = 0, res = 0;
    for (int i = 1; i < m; ++i) {
        int len = a[i] - a[i - 1];
        int diff = b[i] - b[i - 1];
        chmax(res, sum + b[i - 1] * (len - 1) + len * (len - 1) / 2);
        if (len < abs(diff)) break;
        int u = (len + diff) / 2;
        int d = (len - diff) / 2;
        sum += u * b[i - 1];
        sum += d * b[i];
        sum += u * (u + 1) / 2;
        sum += d * (d - 1) / 2;
        if (u + d != len) {
            sum += b[i - 1] + u;
        }
    }

    cout << res << '\n';

    return (0);
}
accuracy: 1
time_ms: 10
memory_kb: 5436
final_rank: 5

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

int main() {
    long long n, m;
    cin >> n >> m;
    long long a[m + 2], b[m + 2];
    long long sum = 1;
    for (int i = 1; i <= m; i++) {
        cin >> a[i] >> b[i];
    }
    a[0] = 1;
    b[0] = 1;
    a[m + 1] = n + 1;
    b[m + 1] = 1000000000000001LL;
    int start = 1;
    if (a[1] == 1) {
        start = 2;
        if (b[1] == 0) {
            sum = 0;
        } else if (b[1] == 1) {
            sum = 1;
        } else {
            cout << 0 << "\n";
            return(0);
        }
    }
    vector<long long> ans;
    for (int i = start; i <= m + 1; i++) {
        //そのまま上まで行く
        long long x = b[i - 1] + (a[i] - a[i - 1]) - 1; //上側
        long long y = b[i - 1] + 1; //下側
        ans.push_back(sum + (x - y + 1) * (x + y) / 2LL);
        // cout << "! " << sum + (x - y + 1) * (x + y) / 2LL << "\n";
        //うまく調整する m ~ n: (n - m + 1) * (m + n) / 2
        if (a[i] - a[i - 1] < abs(b[i] - b[i - 1])) {
            break;
        }
        long long Max = (a[i] - a[i - 1] + b[i] + b[i - 1]) / 2LL;
        long long l = (Max - b[i - 1]);
        long long r = (Max - b[i]);
        long long mid = (a[i] - a[i - 1]) - (l + r);
        // cout << Max << " " << l << " " << mid << " " << r << "\n";
        x = Max;
        y = Max - (l - 1);
        if (y < 0) {
            break;
        }
        sum += (x - y + 1) * (x + y) / 2LL;
        // cout << x << " " << y << " " << Max << " " << sum << "\n";
        sum += (mid - 1) * Max;
        // cout << sum << "\n";
        x = Max;
        y = Max - r;
        if (y < 0) {
            break;
        }
        sum += (x - y + 1) * (x + y) / 2LL;
        // cout << x << " " << y << " " << Max << " " << sum << "\n";
        ans.push_back(sum);
        // cout << sum << "\n";
        // cout << "---------\n";
    }
    sort(ans.rbegin(), ans.rend());
    cout << ans[0] << "\n";
    return(0);
}
accuracy: 1
time_ms: 40
memory_kb: 6684
final_rank: 15

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

int main() {
    int n, m;
    cin >> n >> m;
    long long a[m + 2], b[m + 2];
    long long sum = 1;
    for (int i = 1; i <= m; i++) {
        cin >> a[i] >> b[i];
    }
    a[0] = 1;
    b[0] = 1;
    a[m + 1] = n + 1;
    b[m + 1] = 1000000001LL;
    int start = 1;
    if (a[1] == 1) {
        start = 2;
    }
    if (a[1] == 1) {
        if (b[1] == 0) {
            sum = 0;
        } else if (b[1] == 1) {
            sum = 1;
        } else {
            cout << 0 << "\n";
            return(0);
        }
    }
    vector<long long> ans;
    for (int i = start; i <= m + 1; i++) {
        //そのまま上まで行く
        long long x = b[i - 1] + (a[i] - a[i - 1]) - 1; //上側
        long long y = b[i - 1] + 1; //下側
        ans.push_back(sum + (x - y + 1) * (x + y) / 2LL);
        // cout << "! " << sum + (x - y + 1) * (x + y) / 2LL << "\n";
        //うまく調整する m ~ n: (n - m + 1) * (m + n) / 2
        if (a[i] - a[i - 1] < abs(b[i] - b[i - 1])) {
            break;
        }
        long long Max = (a[i] - a[i - 1] + b[i] + b[i - 1]) / 2LL;
        long long l = (Max - b[i - 1]);
        long long r = (Max - b[i]);
        long long mid = (a[i] - a[i - 1]) - (l + r);
        // cout << Max << " " << l << " " << mid << " " << r << "\n";
        x = Max;
        y = Max - (l - 1);
        if (y < 0) {
            break;
        }
        sum += (x - y + 1) * (x + y) / 2LL;
        // cout << x << " " << y << " " << Max << " " << sum << "\n";
        sum += (mid - 1) * Max;
        // cout << sum << "\n";
        x = Max;
        y = Max - r;
        if (y < 0) {
            break;
        }
        sum += (x - y + 1) * (x + y) / 2LL;
        // cout << x << " " << y << " " << Max << " " << sum << "\n";
        ans.push_back(sum);
        // cout << sum << "\n";
        // cout << "---------\n";
    }
    sort(ans.rbegin(), ans.rend());
    cout << ans[0] << "\n";
    return(0);
}
accuracy: 0.6896551724137931
time_ms: 40
memory_kb: 4888
final_rank: 20

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

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;
template<class T> istream &operator>>(istream &is, vector<T> &v)
    {for (auto &x : v){is >> x;} return is;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return true;} return false;}

#define pb emplace_back
#define all(a) (a).begin(),(a).end()
#define fi first
#define se second

using T = pair<int, ll>;
T f(T A1, T A2, bool flag) {
    ll x = A2.fi-A1.fi;
    ll y = A2.se-A1.se;
    if (flag) {
        ll area = x*(x-1)/2+A1.se*(x-1);
        return {0, area};
    }
    if (abs(y)>x||A2.se==-1) {
        ll area = x*(x+1)/2 + A1.se*x;
        if (A2.se!=-1) area -= x+A1.se;
        return {-1, area};
    }
    if (y>=0) {
        ll area = y*(y+1)/2 + A1.se*y;
        ll t = x-y;
        if (t%2==0) {
            area += (t/2)*(t/2+1)/2+(t/2-1)*(t/2)/2;
            area += A2.se*t;
        }
        else {
            area += (t/2)*(t/2+1) + A2.se*t;
        }
        return {0, area};
    }
    else {
        y *= -1;
        swap(A1.se, A2.se);
        ll area = y*(y+1)/2 + A1.se*y;
        ll t = x-y;
        if (t%2==0) {
            area += (t/2)*(t/2+1)/2+(t/2-1)*(t/2)/2;
            area += A2.se*t;
        }
        else {
            area += (t/2)*(t/2+1) + A2.se*t;
        }
        area += -A2.se+A1.se;
        return {0, area};
    }
}

int main() {
    int N, M; cin >> N >> M;
    vector<T> A(M+2);
    A[0] = {0, 0};
    for (int i = 1; i <= M; i++) cin >> A[i].fi >> A[i].se;
    if (A[M].fi==N) A.pop_back();
    else A[M+1] = {N, -1};
    
    ll ans = 0, cnt = 0;
    for (int i = 0; i < int(A.size())-1; i++) {
        chmax(ans, cnt + f(A[i], A[i+1], true).se);
        cnt += f(A[i], A[i+1], false).se;
        chmax(ans, cnt);
        //cout << A[i].fi << " " << f(A[i], A[i+1], false).se << " " << ans << endl;
        if (f(A[i], A[i+1], false).fi==-1) break;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 4664
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, a, b) for (ll i = (a); i < (b); i++)
void chmax(ll& a, const ll& b) { if (a < b) a = b; }

int main()
{
	ll n, m; cin >> n >> m;
	V<pair<ll, ll>> ab(m);
	REP(i, m) cin >> ab[i].first >> ab[i].second;
	ab.emplace_back(n + 1, 1ll << 60);
	ll a1{}, b1{}, ans{}, cur{};
	for (auto& [a2, b2] : ab) {
		ll d = a2 - a1;
		chmax(ans, cur + (b1 + b1 + d - 1) * d / 2); // 手前まで
		if (abs(b2 - b1) > d) break; // 無理なら
		if (abs(a1 - a2) % 2 == abs(b1 - b2) % 2) {
			ll p1a = a2 - (b1 + d - b2) / 2, p1b = (p1a - a1) + b1;
			cur += (b1 + p1b - 1) * (p1a - a1) / 2;
			cur += (b2 + p1b + 1) * (a2 - p1a) / 2;
		} else {
			ll p1a = a2 - (b1 + d - b2 - 1) / 2, p1b = (p1a - a1) + b1 - 1;
			cur += (b1 + p1b) * (p1a - a1) / 2;
			cur += (b2 + p1b + 1) * (a2 - p1a) / 2;
		}
		a1 = a2;
		b1 = b2;
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 7536
final_rank: 17

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep2(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
ll mod=998244353;
ll inf=(1ll<<62)+(1ll<<30);
ll ruijo(ll a,ll b){// return a^b
  vector<ll>ab(60);ab[0]=a;rep2(i,1,61)ab[i]=ab[i-1]*ab[i-1]%mod;
  ll n2=1,c=0,ret=1;while(n2<=b)n2*=2,c++;n2/=2,c--;
  while(b>0){while(n2>b)n2/=2,c--;b-=n2,ret=ret*ab[c]%mod;}
  return ret;
}
ll sum(ll l,ll r){
  return (r+l)*(abs(r-l)+1)/2;
}
int main(){
  int n,m;cin>>n>>m;
  vector<ll>a(m),b(m);
  rep(i,m)cin>>a[i]>>b[i];
  if(a[m-1]<n)a.push_back(n+1),b.push_back(0);
  ll ans=0;
  int pos=0;
  ll cost=0,costsum=0;
  rep(i,(int)a.size()){
    if(a[i]-pos>1)ans=max(ans,costsum+sum(cost+1,cost+a[i]-pos-1));
    if(cost-(a[i]-pos)>b[i]||cost+(a[i]-pos)<b[i])break;
    if((cost+pos-b[i]-a[i])%2==0){
      ll x=(a[i]-pos+b[i]-cost)/2;
      costsum+=sum(cost,cost+x)+sum(cost+x,b[i])-cost-(cost+x);
    }
    else{
      ll x=(a[i]-pos+b[i]-cost)/2;
      costsum+=sum(cost,cost+x)+sum(cost+x,b[i])-cost;
    }
    pos=a[i],cost=b[i];
  }
  cout<<max(costsum,ans)<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 5364
final_rank: 13

code_snippet: #include <bits/stdc++.h>

long long sum(int v) {
    return (long long)v * (v + 1) / 2;
}

const long long INVALID{-100};

long long calc(int s, int t, int sv, int tv) {
    // std::cerr << s << ' ' << t << ' ' << sv << ' ' << tv << std::endl;
    assert(s < t);
    int len{t - s};
    long long res{};
    if (sv < tv) {
        if (sv + len < tv) return INVALID;
        res += sum(tv - 1) - sum(sv - 1);
        s += tv - sv;
        sv = tv;
        len = t - s;
        assert(len >= 0);
    }
    else if (sv == tv) {
        // nothing to do
    }
    else { // sv > tv
        if (sv - len > tv) return INVALID;
        res += sum(sv - 1) - sum(tv - 1);
        t -= sv - tv;
        tv = sv;
        len = t - s;
        assert(len >= 0);
    }
    int up{len / 2}, dn{len / 2};
    res += sum(sv + up) - sum(sv - 1);
    res += sum(tv + (len % 2 == 0 ? dn - 1 : dn)) - sum(tv - 1);
    // std::cerr << res << std::endl;
    return res;
}

int main() {
    int N, M;
    std::cin >> N >> M;
    std::vector<int> A(M), B(M);
    for (int i{} ; i < M ; i++) {
        std::cin >> A[i] >> B[i];
    }
    long long ans{sum(A[0] - 1)};
    long long cur{};
    cur += calc(0, A[0], 0, B[0]);
    if (cur == INVALID) {
        std::cout << sum(A[0] - 1) << '\n';
        return 0;
    }
    for (int i{} ; i + 1 < M ; i++) {
        cur -= B[i];
        ans = std::max(ans, cur + sum(B[i] + A[i + 1] - A[i] - 1) - sum(B[i] - 1));
        long long val{calc(A[i], A[i + 1], B[i], B[i + 1])};
        if (val == INVALID) {
            cur += sum(B[i] + (A[i + 1] - A[i]) - 1) - sum(B[i] - 1);
            std::cout << cur << '\n';
            return 0;
        }
        else {
            cur += val;
        }
    }
    if (A[M - 1] < N) {
        cur -= B[M - 1];
        long long val{calc(A[M - 1], N, B[M - 1], B[M - 1] + N - A[M - 1])};
        assert(val >= 0);
        cur += val;
    }
    ans = std::max(ans, cur);
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 40
memory_kb: 3900
final_rank: 9

code_snippet: // #include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<string>
using namespace std;
using ll = long long;
using vi=vector<int>;   
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vs=vector<string>;
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define INF (ll)(2e18)
#define IINF (int)(1e9+7)
//#define MOD ((ll)1e9+7)
#define MOD (998244353)
#define all(a) (a).begin(),(a).end()
const int di[]={0,1,-1,0};
const int dj[]={1,0,0,-1};


#define MAX 100000
ll f(ll x){
	return (x)*(x+1)/2;
}
int main(){
	ll N, M;
    cin >> N >> M;
    vector<pair<ll, ll>> A(M);
    for(auto& [a, b] : A) cin >> a >> b;
    A.emplace_back(N + 1, INF);
    ll ans = 0, sum = 0, a1 = 0, b1 = 0;
    for(ll i = 0; i <= M; i++){
        auto [a2, b2] = A[i];
        ans=max(ans, sum + (b1 + b1 + (a2 - a1) - 1) * (a2 - a1) / 2);
        if(b1 + (a2 - a1) < b2 || b1 - (a2 - a1) > b2) break;
        if((a1 ^ a2 ^ b1 ^ b2) & 1){
            const ll a3 = (a1 + a2 + b2 - b1) / 2, b3 = a3 + b1 - a1;
            const ll a4 = a3 + 1, b4 = b3;
            sum += (b1 + b3) * (a3 - a1 + 1) / 2;
            sum += (b4 + b2 + 1) * (a2 - a4) / 2;
        }
        else{
            const ll a3 = (a1 + a2 + b2 - b1) / 2, b3 = a2 + b2 - a3;
            sum += (b1 + b3 - 1) * (a3 - a1) / 2;
            sum += (b3 + b2 + 1) * (a2 - a3) / 2;
        }
        a1 = a2;
        b1 = b2;
    }
    cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 7296
final_rank: 16