Problem:
Arrows
$L$ squares are lined up in a row on the left and right. The pieces are placed on top of some of the squares. Each piece is marked with an arrow pointing to the left or to the right. No more than one piece can be placed on a single square.
A piece in any square can be moved to an unoccupied square, but it can only be moved to an adjacent square and only one piece can be moved at a time. A piece can be moved to the left or to the right regardless of the direction of the arrow. However, if a piece is moved once in the direction of the arrow, one point is obtained, but if it is moved once in the opposite direction, one point is deducted. In addition, it is known that there is always a maximum number of points that can be scored, no matter what the starting situation is.
Given the number of squares and the situation of the pieces, write a program that calculates the maximum number of points that can be obtained. However, we assume that the squares are assigned numbers from 1 to $L$, starting from the left end of the column.
Input
The input is given in the following form.
$N$ $L$
$p_1$ $d_1$
$p_2$ $d_2$
:
$p_N$ $d_N$
The first line gives the number of pieces $N$ ($1 \leq N \leq 10^5$) and the number of squares $L$ ($N \leq L\leq 10^9$). The next $N$ lines give the number of squares where the pieces are placed $p_i$ ($1 \leq p_i\leq L$), and the direction of the arrows on the pieces $d_i$ (0 or 1). When $d_i$ is 0, the arrow is facing left, and when $d_i$ is 1, it is facing right. The same square number cannot be given (for $i \ne j$, $p_i \ne p_j$).
Output
Output the maximum number of points that can be obtained in a line.
Sample Input and Output
Sample Input 1
2 10
3 0
6 1
Sample Output 1
6
Sample Input 2
2 8
2 1
8 0
Sample Output 2
5
Sample Input 3
2 8
1 0
8 1
Sample Output 3
0

Submitted solutions:

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

using namespace std;

#define rep(i, l, n) for(int i = int(l); i < int(n); i++)
#define ll long long
#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(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[] = {0, -1, 0, 1, -1, -1, 1, 1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

void main_program() {
    int n; ll m; cin >> n >> m;
    pair<int, int> p[n];
    rep(i, 0, n) cin >> p[i].first >> p[i].second;
    sort(p, p + n);
    ll x[n]; int w[n];
    ll to[n];
    rep(i, 0, n) {
        x[i] = p[i].first;
        w[i] = p[i].second;
        to[i] = -1;
    }
    int cnt = 0, cur = 0, len = 0;
    rep(i, 0, n) {
        len++; cur++;
        if(w[i]) cnt--;
        else cnt++;
        if(cnt >= 0) {
            rep(j, 0, len) to[i - j] = cur - j;
            len = 0;
            cnt = 0;
        }
    }
    cnt = 0, cur = m + 1, len = 0;
    for(int i = n - 1; i >= 0; i--) {
        if(to[i] != -1) break;
        len++; cur--;
        if(w[i]) cnt++;
        else cnt--;
        if(cnt >= 0) {
            rep(j, 0, len) to[i + j] = cur + j;
            len = 0;
            cnt = 0;
        }
    }
    ll sum = 0;
    rep(i, 0, n) {
        if(w[i]) {
            sum += to[i] - x[i];
        }
        else {
            sum += x[i] - to[i];
        }
    }
    cout << sum << "\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: 6084
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#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 
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}

int main(){
    ll N,L;cin>>N>>L;
    vector<pair<ll,ll>>X(N);
    rep(i,N)cin>>X[i].fi>>X[i].se;
    sort(all(X));
    vector<ll>LS(N+1,0),RS(N+1,0);
    rep(i,N){
        ll k = 1;
        if(X[i].se==1)k=-1;
        LS[i+1]=LS[i]+k*(X[i].fi-i-1);
    }
    for(int i = N;i>0;i--){
        ll k = 1;
        if(X[i-1].se==0)k=-1;
        // cout<<k<<i-1<<X[i-1].se<<endl;
        RS[i-1]=RS[i]+k*(L-(N-i)-X[i-1].fi);
    }
    ll ans = 0;
    rep(i,N+1){
        // cout<<LS[i]<<" "<<RS[i]<<endl;
        ans = max(ans,LS[i]+RS[i]);
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 6124
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ll long long
const long long mod=998244353;
const long long hmod=46216567629137;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    int N;
    ll L;
    cin>>N>>L;
    pair<int,bool>pd[N+1];
    for(int i=1;i<=N;i++){
        cin>>pd[i].first>>pd[i].second;
    }
    sort(pd+1,pd+N+1);
    ll ans=-2e18;
    ll cnt=0;
    for(int i=N;i>=1;i--){
        ll mokuteki=L-(N-i);
        if(pd[i].second){
            cnt+=mokuteki-pd[i].first;
        }
        else{
            cnt-=mokuteki-pd[i].first;
        }
    }
    ans=cnt;
    for(int i=1;i<=N;i++){
        ll mokuteki=i;
        ll last=L-(N-i);
        if(pd[i].second){
            cnt-=last-mokuteki;
        }
        else{
            cnt+=last-mokuteki;
        }
        ans=max(ans,cnt);
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4104
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 1LL<<30
int main(){

    ll n,l,ans=0,sub;cin>>n>>l;sub=l;
    vector<pair<ll,ll>> dat(n);
    vector<ll> dpl(n+1,0),dpr(n+1,0);
    for(auto& i:dat)cin>>i.first>>i.second;
    sort(dat.begin(),dat.end());
    for(ll i=n-1;i>=0;i--){
        ans+=(sub-dat[i].first)*(dat[i].second?1:-1);
        sub--;
        dpr[i]=ans;
    }sub=1;
    ans=0;
    for(ll i=0;i<n;i++){
        ans+=(dat[i].first-sub)*(dat[i].second?-1:1);
        sub++;
        dpl[i+1]=ans;
    }ans=0;
    for(ll i=0;i<=n;i++){
        ans=max(ans,dpl[i]+dpr[i]);
    }
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 6080
final_rank: 18

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;
const int dy[] = { 0,0,1,-1 }, dx[] = { 1,-1,0,0 };
int main() {
    int n, l; cin >> n >> l;
    vector<pint> koma(n);
    rep(i, n)cin >> koma[i].first >> koma[i].second;
    sort(koma.begin(), koma.end());
    vector<ll> dp_l(n + 1), dp_r(n + 1);
    for (int i = 1; i <= n; ++i) {
        auto& [p, d] = koma[i - 1];
        dp_l[i] = dp_l[i - 1];
        if (d) dp_l[i] -= p - i;
        else  dp_l[i] += p - i;
    }
    for (int i = 1; i <= n; ++i) {
        auto& [p, d] = koma[n - i];
        dp_r[i] = dp_r[i - 1];
        if (d) dp_r[i] += l - i + 1 - p;
        else  dp_r[i] -= l - i + 1 - p;
    }
    ll ans = 0;
    rep(i, n + 1) {
        chmax(ans, dp_l[i] + dp_r[n - i]);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 5760
final_rank: 11

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

template <typename T>
using vc = vector<T>;
template <typename T>
using vv = vc<vc<T>>;

//-------------1.型系---------------
using ll = long long;
ll INF = 2e18;

using ld = long double;
using bl = bool;
using mint = modint998244353;
// using mint = modint1000000007;
//   using mint = modint;
//   mint::set_mod(m);

template <class T>
using pq = priority_queue<T, vc<T>>;
template <class T>
using pq_g = priority_queue<T, vc<T>, greater<T>>;
//-----------------------------------

//-------------2.配列系--------------

using pii = pair<int, int>;
using pll = pair<long long, long long>;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
template <class T>
istream &operator>>(istream &i, vc<T> &v)
{
    rep(j, size(v)) i >> v[j];
    return i;
}

using ar2 = array<ll, 2>;

//----------------------------------

//--------3.コード短縮化とか---------
const double PI = 3.141592653589793238;
const int inf = 1073741823;
const ll infl = 1LL << 60;
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string abc = "abcdefghijklmnopqrstuvwxyz";

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define drep(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define nfor(i, s, n) for (ll i = s; i < n; i++)
#define dfor(i, s, n) for (ll i = (s) - 1; i >= n; i--)

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YN                     \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    } // if(a==b)YN;

#define vc_unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define vc_rotate(v) rotate(v.begin(), v.begin() + 1, v.end());

#define pop_cnt(s) ll(popcount(uint64_t(s)))

#define next_p(v) next_permutation(v.begin(), v.end())

//-------------------------------

//---------4.グリッド系----------
vector<int> dx = {1, 0, -1, 0}; //  dx={1,1,0,-1,-1,-1,0,1};
vector<int> dy = {0, 1, 0, -1}; //  dy={0,1,1,1,0,-1,-1,-1};

bool out_grid(ll i, ll j, ll h, ll w = -1)
{
    if (w == -1)
    {
        w = h;
    }
    return (!(0 <= i && i < h && 0 <= j && j < w));
}

#define vvl_rotate(v)                                   \
    {                                                   \
        ll n = size(v);                                 \
        vvl nx(n, vl(n));                               \
        rep(i, n) rep(j, n) nx[j][n - i - 1] = v[i][j]; \
        swap(nx, v);                                    \
    } // 時計回りに90°回転
// #define vvl_rotate(v) {ll n = size(v);vvl
// nx(n,vl(n));rep(i,n)rep(j,n)nx[n-j-1][i]=v[i][j];swap(nx,v);}//反時計周りに90°回転

#define vs_rotate(v)                                    \
    {                                                   \
        ll n = size(v);                                 \
        vs nx(n, string(n, '.'));                       \
        rep(i, n) rep(j, n) nx[j][n - i - 1] = v[i][j]; \
        swap(nx, v);                                    \
    } // 文字列版 時計回りに90°回転
// #define vs_rotate(v) {ll n = size(v);vs
// nx(n,string(n,'.'));rep(i,n)rep(j,n)nx[n-j-1][i]=v[i][j];swap(nx,v);}//文字列版　反時計周りに90°回転

#define vvl_transpos(v)                         \
    {                                           \
        ll n = size(v);                         \
        vvl nx(n, vl(n));                       \
        rep(i, n) rep(j, n) nx[j][i] = v[i][j]; \
        swap(nx, v);                            \
    }
#define vs_transpos(v)                          \
    {                                           \
        ll n = size(v);                         \
        vs nx(n, string(n, '.'));               \
        rep(i, n) rep(j, n) nx[j][i] = v[i][j]; \
        swap(nx, v);                            \
    }

//--------------------------------

//-----------5.数学系--------------
#define euclid(x, y) ((x) * (x) + (y) * (y)) // ユークリッド距離 2乗のまま
#define manhattan(x1, x2, y1, y2) \
    (abs(x1 - x2) + abs(y1 - y2)) // マンハッタン距離 = |x1-x2|+|y1-y2|

template <class T>
T tousa_sum1(T l, T d, T r)
{ // 初項,公差,末項 で総和を求める
    T wide = (r - l) / d + 1;
    return (l + r) * wide / 2;
}
template <class T>
T tousa_sum2(T a, T d, T n)
{ // 初項,交差,項数 で総和を求める
    return (a * 2 + d * (n - 1)) * n / 2;
}
ll kousa_kousuu(ll l, ll r, ll d)
{ // 初項,末項,交差 で等差数列の項数を求める
    return (r - l) / d + 1;
}
mint touhi_sum(mint a, mint r,
               ll n)
{ // 初項,公比,項数で等比数列の総和を求める
    if (r == 1)
    {
        return a * n;
    }
    mint bunsi = a * (r.pow(n) - mint(1));
    mint bunbo = r - 1;
    return bunsi / bunbo;
}

ll nc2(ll x) { return x * (x - 1) / 2; }
ll nc3(ll x) { return x * (x - 1) * (x - 2) / 6; }

//----------------------------------------------

//-----------6.デバックや出力系------------------
void print(ld x) { printf("%.20Lf\n", x); }

#define print_vec(v)                   \
    {                                  \
        ll n = size(v);                \
        rep(i, n) cout << v[i] << " "; \
        cout << endl;                  \
    } // 一次元配列を出力する(改行なし)

#define vc_cout(v)                      \
    {                                   \
        ll n = size(v);                 \
        rep(i, n) cout << v[i] << endl; \
    } // 一次元配列を出力する(改行あり)
#define vv_cout(v)                                         \
    {                                                      \
        ll n = size(v);                                    \
        rep(i, n)                                          \
        {                                                  \
            rep(j, size(v[i])) { cout << v[i][j] << ' '; } \
            cout << endl;                                  \
        }                                                  \
    } // 二次元配列を出力する
//----------------------------------------------

int main()
{
    cout << fixed << setprecision(10);
    int n, m;
    cin >> n >> m;
    vector<pll> a(n);
    rep(i, n)
    {
        cin >> a[i].first >> a[i].second, a[i].first--;
        if (a[i].second == 1)
            a[i].second = -1;
        else
            a[i].second = 1;
    }
    sort(all(a));
    vector<ll> l(n), r(n);
    rep(i, n)
    {
        l[i] = (a[i].first - i) * a[i].second;
        r[i] = (m - a[i].first - (n - i)) * -a[i].second;
    }
    ll ans = accumulate(all(l), 0LL);
    ll sum = ans;
    drep(i, n)
    {
        sum += r[i] - l[i];
        chmax(ans, sum);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 6528
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pint = pair<int,int>;
using pll = pair<long long, long long>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i,n) for (int i = 1; i <= (int)(n); i++)
int inf = 2147483647; // おおよそ2*10^9
ll INF = 9223372036854775807; //おおよそ9*10^18
//ull UINF == おおよそ1.8*10^19
int main(){
    int n,l;
    cin >> n >> l;
    vector<pint> a(n);
    vector<int> cnt(2,0);
    bool check = true;
    rep(i,n){
        cin >> a[i].first >> a[i].second;
        a[i].first--;
    }
    sort(a.begin(),a.end());
    ll ans = 0;
    ll x = 0;
    rep(i,n){
        if(a[i].second){
            ans -= a[i].first - i;
        }else{
            ans += a[i].first - i;
        }
    }
    x = ans;
    rep(i,n){
        if(a[n-i-1].second){
            x += l - n;
        }else{
            x -= l - n;
        }
        ans = max(ans,x);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 3688
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    long long l;
    cin >> n >> l;
    vector<pair<long long,int>> p(n);
    for(int i=0;i<n;i++){
        cin >> p[i].first >> p[i].second;
    }
    sort(p.begin(),p.end());
    int left=0,right=n-1;
    long long ans=0;
    while(!p[left].second&&left<n){
        ans+=p[left].first-left-1;
        left++;
    }
    while(p[right].second&&right>=0){
        ans+=l-p[right].first-(n-right-1);
        right--;
    }
    int lcount=0,rcount=0;
    int q=left;
    while(left<right){
        while(p[left].second&&left<n){
            rcount++;
            left++;
        }
        while(!p[left].second&&left<n){
            lcount++;
            left++;
        }
        if(lcount>=rcount){
            for(int i=q;i<left;i++){
                if(p[i].second){
                    ans+=-1*(p[i].first-i-1);
                }else{
                    ans+=p[i].first-i-1;
                }
            }
            lcount=0;
            rcount=0;
            q=left;
        }
    }
    for(int i=right;i>=q;i--){
        if(p[i].second){
            ans+=l-p[i].first-(n-1-i);
        }else{
            ans+=-1*(l-p[i].first-(n-1-i));
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4608
final_rank: 10

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;}
#define pb emplace_back
#define all(a) (a).begin(),(a).end()
#define fi first
#define se second

const vector<int> dx = {1, 0, -1, 0};
const vector<int> dy = {0, 1, 0, -1};
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;}

int main() {
    int N, L; cin >> N >> L;
    vector<pii> p(N);
    for (auto& P: p) {
        cin >> P.fi >> P.se;
    }
    sort(all(p));
    
    vll Ld(N+1, 0), Rd(N+1, 0);
    for (int i = 0; i < N; i++) {
        Ld[i+1] = Ld[i];
        if (p[i].se==0) Ld[i+1] += p[i].fi-i-1;
        else Ld[i+1] -= p[i].fi-i-1;
    }
    for (int i = N-1; i >= 0; i--) {
        Rd[i] = Rd[i+1];
        if (p[i].se==1) Rd[i] += L+i-N+1-p[i].fi;
        else Rd[i] -= L+i-N+1-p[i].fi;
    }
    ll ans = 0;
    for (int i = 0; i < N+1; i++) chmax(ans, Ld[i]+Rd[i]);
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5348
final_rank: 14

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

using i64 = long long;
#define rep(i, a, b) for (int i = a; i < b; i++)

int main() {
    int n, l;
    cin >> n >> l;
    vector<pair<int, int>> pieces(n);
    rep (i, 0, n) {
        cin >> pieces[i].first >> pieces[i].second;
        pieces[i].first--;
    }

    sort(begin(pieces), end(pieces));

    i64 res = 0;
    i64 left = 0, right = 0;

    rep (mid, -1, n) {
        if (mid == -1) {
            rep (i, 0, n) {
                if (pieces[i].second == 0) right -= l - n + i - pieces[i].first;
                if (pieces[i].second == 1) right += l - n + i - pieces[i].first;
            }
            res = max(res, left + right);
            continue;
        }
        if (pieces[mid].second == 0) {
            right += l - n + mid - pieces[mid].second;
            left += pieces[mid].second - mid;
        }
        if (pieces[mid].second == 1) {
            right -= l - n + mid - pieces[mid].second;
            left -= pieces[mid].second - mid;
        }
        res = max(res, left + right);
    }
    cout << res << endl;
    return 0;
}
// converted!!
accuracy: 1
time_ms: 30
memory_kb: 3852
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define VVL vector<vector<long long>>
#define VL vector<long long>
#define rep(i,n) for(long long i = 0; i < n; i++)
#define Rrep(i,n) for(long long i = n-1; i >= 0; i--)
#define ld long double
const long long llmax = 9223372036854775807;
const int intmax = 2147483647;
const ll ll1 = 1;

int main(){
  //cout << fixed << setprecision(0);
  //指数表記になったときに弄って使う
  long long n, m;
  long long a, b;
  long long i, j, k;
  long long count = 0, ans = 0;
  string s, t;
  cin >> n >> m;
  vector<pair<ll,ll>> l(n);
  rep(i,n){
    cin >> l.at(i).first >> l.at(i).second;
  }
  sort(l.begin(),l.end());
  VL lg(n,0), rg(n,0);
  if(l.at(0).second == 0){
    lg.at(0) = l.at(0).first - 1;
  }else{
    lg.at(0) = (l.at(0).first - 1) * -1;
  }
  if(l.at(n-1).second == 1){
    rg.at(n-1) = m - l.at(n-1).first;
  }else{
    rg.at(n-1) = (m - l.at(n-1).first) * -1;
  }
  for(i = 1; i < n; i++){
    if(l.at(i).second == 0){
      lg.at(i) = lg.at(i-1) + (l.at(i).first - 1 - i);
    }else{
      lg.at(i) = lg.at(i-1) + (l.at(i).first - 1 - i) * -1;
    }
  }
  for(i = n-2; i >= 0; i--){
    if(l.at(i).second == 1){
      rg.at(i) = rg.at(i+1) + (m - l.at(i).first - n + i + 1);
    }else{
      rg.at(i) = rg.at(i+1) + (m - l.at(i).first - n + i + 1) *-1;
    }
  }/*testcodeなので後で消す
  rep(i,n){
    cout << lg.at(i) << ' ';
  }
  cout << endl;
  rep(i,n){
    cout << rg.at(i) << ' ';
  }
  cout << endl;*/
  ans = max(rg.at(0), ans);
  ans = max(lg.at(n-1), ans);
  for(i = 1; i < n; i++){
    ans = max(rg.at(i) + lg.at(i-1), ans);
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 6036
final_rank: 17

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

int main()
{
	long long n,l;
	cin >> n >> l;
	vector<pair<long long,long long>> a(n);
	for(long long i = 0ll;i < n;i++){
		cin >> a[i].first >> a[i].second;
	}
	sort(a.begin(),a.end());
	vector<long long> t(n,0ll);
	{
		long long now = 0ll;
		for(long long i = n - 1ll;i >= 0ll;i--){
			if(a[i].second == 0ll){
				now -= l - a[i].first - (n - 1ll - i);
			}else{
				now += l - a[i].first - (n - 1ll - i);
			}
			if(i != n - 1ll){
				t[i] = t[i + 1ll];
			}
			t[i] = max(t[i],now);
		}
	}
	long long ans = t[0ll];
	long long now = 0ll;
	for(long long i = 0ll;i < n;i++){
		if(a[i].second == 0ll){
			now += a[i].first - i - 1ll;
		}else{
			now -= a[i].first - i - 1ll;
		}
		if(i == n - 1ll){
			ans = max(ans,now);
		}else{
			ans = max(ans,now + t[i + 1ll]);
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 5172
final_rank: 13

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

#ifdef LOCAL
#include <debug_print.hpp>
#define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (static_cast<void>(0))
#endif

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int N, L;
    cin >> N >> L;
    vector<pair<int, int>> A(N);
    for (int i = 0; i < N; i++) cin >> A[i].first >> A[i].second, A[i].first--;

    // 位置でソート
    sort(A.begin(), A.end());

    // left[i] := [0, i) 番目までの矢印を全て左端に移動させるときのスコア
    vector<ll> left(N + 1);
    for (int i = 0; i < N; i++) {
        auto [p, d] = A[i];
        if (d == 0) {
            // 矢印が左向きのとき, スコアは p - i 増え
            left[i + 1] = left[i] + (p - i);
        }
        if (d == 1) {
            // 矢印が右向きのとき, スコアは p - i 減る
            left[i + 1] = left[i] - (p - i);
        }
    }
    // right[i] := [i, N) 番目までの矢印を全て右端に移動させるときのスコア
    vector<ll> right(N + 1);
    for (int i = N - 1; i >= 0; i--) {
        auto [p, d] = A[i];
        if (d == 0) {
            // 矢印が左向きのとき, スコアは L - 1 - (N - i - 1) - p 減る
            right[i] = right[i + 1] - (L - 1 - (N - i - 1) - p);
        }
        if (d == 1) {
            // 矢印が右向きのとき, スコアは L - 1 - (N - i - 1) - p 増える
            right[i] = right[i + 1] + (L - 1 - (N - i - 1) - p);
        }
    }

    ll ans = 0;
    for (int i = 0; i <= N; i++) ans = max(ans, left[i] + right[i]);
    cout << ans << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 5448
final_rank: 4

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

#ifdef LOCAL
#include <debug_print.hpp>
#define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (static_cast<void>(0))
#endif

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int N, L;
    cin >> N >> L;
    vector<int> p(N), d(N);
    for (int i = 0; i < N; i++) cin >> p[i] >> d[i], p[i]--;

    vector<int> id(N);
    iota(id.begin(), id.end(), 0);
    sort(id.begin(), id.end(), [&](int a, int b) { return p[a] < p[b]; });

    // left[i] := [0, i) 番目までの矢印を全て左端に移動させるときのスコア
    vector<ll> left(N + 1);
    for (int i = 0; i < N; i++) {
        if (d[id[i]] == 0) {
            // 矢印が左向きのとき, スコアは p[id[i]] - i 増える
            left[i + 1] = left[i] + (p[id[i]] - i);
        }
        if (d[id[i]] == 1) {
            // 矢印が右向きのとき, スコアは p[id[i]] - i 減る
            left[i + 1] = left[i] - (p[id[i]] - i);
        }
    }
    // right[i] := [i, N) 番目までの矢印を全て右端に移動させるときのスコア
    vector<ll> right(N + 1);
    for (int i = N - 1; i >= 0; i--) {
        if (d[id[i]] == 0) {
            // 矢印が左向きのとき, スコアは L - 1 - (N - i - 1) - p[id[i]] 減る
            right[i] = right[i + 1] - (L - 1 - (N - i - 1) - p[id[i]]);
        }
        if (d[id[i]] == 1) {
            // 矢印が右向きのとき, スコアは L - 1 - (N - i - 1) - p[id[i]] 増える
            right[i] = right[i + 1] + (L - 1 - (N - i - 1) - p[id[i]]);
        }
    }
    debug(left, right);

    ll ans = 0;
    for (int i = 0; i <= N; i++) ans = max(ans, left[i] + right[i]);
    cout << ans << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 5556
final_rank: 5

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 std::cout;
// shorten typenames
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>>;
// define CONSTANTS
constexpr double PI = 3.14159265358979323;
constexpr int INF = 100100111; constexpr ll INFL = 3300300300300300491LL;
float EPS = 1e-8; double EPSL = 1e-10;
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 * x || abs(x - y) < EPSL); }
template<> bool eq<float>(const float x, const float y) { return abs(x - y) < EPS * x; }
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;
// fasten io
struct Nyan { Nyan() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } nyan;
// define macros
#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,1,...,n-1
#define rep2(i, n) for(ll i = 0LL, i##_counter = 0LL; i##_counter < ll(n); ++(i##_counter), (i) = i##_counter) // i=0,1,...,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)) // i=s,s+1,...,t-1
#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) // i=s,s+step,...,<t
#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)) // iterate over all elements in v
#define smod(n, m) ((((n) % (m)) + (m)) % (m))
#define sdiv(n, m) (((n) - smod(n, m)) / (m))
#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> 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; }

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;
}
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; }
template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; }
int digit(ll x, int d=10) { int rev=0; while (x > 0) { rev++; x /= d;}; return rev; }
/**
 * @brief std.hpp
 * @docs docs/std/std.md
 */
//line 3 "/home/seekworser/.cpp_lib/competitive_library/competitive/math/bit.hpp"
namespace bit{
    bool lb(ll x) { return (x & 1); }
    bool get(ll x, int n) { return ((x >> n) & 1); }
    ll update(ll x, int n, bool b) { return (x & ~(1LL << n)) + (b << n); }
    ll lsb(ll x) { return -x & x; }
    int digit(ll x) { int rev=0; while(x) { rev++; x >>= 1; } return rev; }
    ll msb(ll x) { if (x==0) return 0; return 1LL << (digit(x) - 1); }
    int lsbd(ll x) {return digit(lsb(x)); }
    int count(ll x) { return __builtin_popcountll(x); }
    string to_string(ll x, bool rev=true) { stringstream ss; while (x) { ss << lb(x); x >>= 1;} string s(ss.str()); if (rev) reverse(all(s)); return s; }
}
/**
 * @brief ビット演算
 * @docs docs/math/bit.md
 */
//line 3 "/home/seekworser/.cpp_lib/competitive_library/competitive/std/io.hpp"
// overload operators (prototypes)
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);

// overload operators
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(); if (q.size()) os << " "; } 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 4 "answer.cpp"
int main() {
    ll n, m; input(n, m);
    vector<pair<ll,ll>> pd(n);
    rep(i, n) input(pd[i].first, pd[i].second);
    sort(all(pd));
    ll cur = 0;
    rep(i, n) {
        auto [p, d] = pd[i];
        if (d == 0) cur += p - (i+1);
        else cur -= p - (i+1);
    }
    ll ans = cur;
    rep(i, n-1, -1, -1) {
        auto [p, d] = pd[i];
        if (d == 0) {
            cur -= p - (i+1);
            cur -= (m - (n-1-i) - p);
        } else {
            cur += p - (i+1);
            cur += (m - (n-1-i) - p);
        }
        chmax(ans, cur);
        debug(i, cur);
    }
    print(ans);
}
accuracy: 1
time_ms: 10
memory_kb: 4616
final_rank: 3

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

int main() {
  int N, L;
  cin >> N >> L;
  vector<pair<long long, int>> pd(N);
  for(int i = 0; i < N; i++) {
    cin >> pd[i].first >> pd[i].second;
  }
  sort(pd.begin(), pd.end());
  long long score = 0;
  for(int i = 0; i < N; i++) {
    auto[p, d] = pd[i];
    score += (d == 0 ? 1 : -1)*(p-(i+1));
  }
  long long ans = max(0LL, score);
  for(int i = N-1; i >= 0; i--) {
    auto[p, d] = pd[i];
    score -= (d == 0 ? 1 : -1)*(p-(i+1));
    score += (d == 0 ? 1 : -1)*(p-(L-(N-i-1)));
    ans = max(ans, score);
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4680
final_rank: 12

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

int main() {
    int n, len;
    std::cin >> n >> len;
    std::vector<std::pair<int, int>> pieces;
    for (int i = 0; i < n; i++) {
        int p, d;
        std::cin >> p >> d;
        pieces.emplace_back(p, d);
    }
    std::sort(pieces.begin(), pieces.end());
    std::vector<int> stack;
    int diff = 0;
    long long ans = 0;
    for (auto [p, d]: pieces) {
        if (d == 0) {
            if (stack.empty()) {
                ans += p - diff - 1;
                diff++;
            } else {
                ans += p - diff - stack.back() - 1;
                stack.pop_back();
                diff += 2;
            }
        } else {
            stack.push_back(p - diff);
        }
    }
    while (!stack.empty()) {
        int p = stack.back();
        stack.pop_back();
        ans += len - diff - p;
        diff++;
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 30
memory_kb: 4016
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(void){
    long score=0;
    long a,b;
    cin>>a>>b;
    vector<vector<long> > e(a,vector<long> (3));
    for(int i=0;i<a;i+=1){
        cin>>e.at(i).at(0)>>e.at(i).at(1);
        e.at(i).at(1)=e.at(i).at(1)*2-1;
        e.at(i).at(2)=1;
    }
    sort(e.begin(),e.end());
    long min=1,max=b+1;
    for(int i=0;i<a;i+=1){
        if(e.at(i).at(0)!=-1){
        long l=min;
        long h=max;
        if(e.at(i).at(1)<0){
        score+=(l-e.at(i).at(0))*e.at(i).at(1);
            min+=e.at(i).at(2);
            e.at(i).at(0)=-1;
        }else{
         if(i!=a-1){
            h=e.at(i+1).at(0);
        }
        score+=(h-e.at(i).at(0)-e.at(i).at(2))*e.at(i).at(1);
        e.at(i).at(0)=h-e.at(i).at(2);
            if(e.at(i).at(0)+e.at(i).at(2)==max){
                max-=e.at(i).at(2);
            }else{
                e.at(i+1).at(1)+=e.at(i).at(1);
                e.at(i+1).at(2)+=e.at(i).at(2);
                e.at(i+1).at(0)=e.at(i).at(0);
            }
            e.at(i).at(0)=-1;
        }
    }
    }
    cout<<score<<endl;
}
accuracy: 1
time_ms: 50
memory_kb: 8240
final_rank: 20

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,n) for(long long i=a;i<n;i++)
#define repp(i,a,n) for(long long i=a;i>=n;i--)
#define so(z) sort(z.begin(),z.end())
#define sor(z) sort(z.begin(),z.end(),greater<long long>())
#define re(z) reverse(z.begin(),z.end())
#define su(a) substr(a)
#define sub(a,b) substr(a,b)
#define yess cout<<"YES"<<nn
#define noo cout<<"NO"<<nn
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define yesr cout<<"Yes"<<"\n";return(0)
#define nor cout<<"No"<<"\n";return(0)
#define fi first
#define se second
#define fr() front()
#define mp make_pair
#define pb push_back
#define nn "\n"
#define ll long long
#define mod 1000000007
#define IM INT_MAX
#define LM LONG_LONG_MAX
#define gcd(x,y) __gcd(x,y) //最大公約数
#define lcm(x,y) x/(__gcd(x,y))*y //最小公倍数
#define prev(a) prev_permutation(a.begin(),a.end()) //辞書前
#define next(a) next_permutation(a.begin(),a.end()) //辞書後
#define get(s) getline(cin,s) //　空白の空いたstringの入力
#define fixed(a) fixed<<setprecision(a) //小数点何桁か指定
#define ro(a) round(a) //四捨五入
#define ce(a) ceil(a) //切り上げ
#define up(a,v) upper_bound(a.begin(),a.end(),v)
#define lo(a,v) lower_bound(a.begin(),a.end(),v)
#define bi(a,v) binary_bound(a.begin(),a.end(),v)
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n,l,ans=LONG_LONG_MIN,b=0;
    cin>>n>>l;
    vector<pair<ll,ll>>a(n);
    rep(i,0,n){
        cin>>a[i].fi>>a[i].se;
        if(a[i].se==0) a[i].se=-1;
    }
    so(a);
    rep(i,0,n){
        b-=(a[i].fi-i-1)*a[i].se;
        /* if(a[i].se==0) b+=a[i].fi-i-1;
        else b-=a[i].fi-i-1;
        //cout<<b<<" ";
        a[i].fi=i+1; */
    }
    //cout<<nn;
    //cout<<b<<nn;
    ans=b;
    repp(i,n-1,0){
        b+=(l-n)*a[i].se;
        /* if(a[i].se==0) b-=l-n;
        else b+=l-n;
        */
        ans=max(ans,b); 
        //cout<<b<<" "<<ans<<nn;
    }
    cout<<ans<<nn;
    return(0);
}
accuracy: 1
time_ms: 10
memory_kb: 4524
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define INF INT64_MAX;
int main(void)
{
    LL n,l,ans=0,mx=-1,left=0,right=1;
    cin>>n>>l;
    vector< pair < LL , LL > > a;
    for(int i=0;i<n;i++){
        LL p,d;
        cin>>p>>d;
        a.push_back({p,d});
    } 
    sort(a.begin(),a.end());
    for(int i=0;i<n;i++){
        if(a[i].second==0){
            ans+=a[i].first-1-i;
            a[i].first=i+1;
        }
        else{
            ans-=a[i].first-1-i;
            a[i].first=i+1;
        }
        //cout<<ans<<endl;
    }
    mx=max(mx,ans);
    //cout<<"ans="<<ans<<endl;
    for(int i=n-1;i>=0;i--){
        //cout<<"a.first="<<a[i].first<<endl;
        //cout<<"a.second="<<a[i].second<<endl;
        if(a[i].second==1){
            //cout<<l-a[i].first-(n-1-i)<<endl;
            ans+=l-a[i].first-(n-1-i);
        }
        else{
            //cout<<l-a[i].first-(n-1-i)<<endl;
            ans-=l-a[i].first-(n-1-i);
        }
        //cout<<ans<<endl;
        mx=max(ans,mx);
    }
    cout<<mx<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 4820
final_rank: 16