Language: C++

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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

const int INF=1001001001001001001ll;

struct segtree{
    static const int SEG=1<<17;
    vint dat;
    segtree():dat(SEG*2,INF),put(SEG*2,INF){}
    inline void push(int k){
        chmin(dat[k],put[k]);
        if(k<SEG-1){
            chmin(put[k*2+1],put[k]);
            chmin(put[k*2+2],put[k]);
        }
    }
    void update(int a,int b,int x,int k=0,int l=0,int r=SEG){
        push(k);
        if(r<=a||b<=l)return;
        if(a<=l&&r<=b){
            put[k]=x;
            push(k);
            return;
        }
        update(a,b,x,k*2+1,l,(l+r)/2);
        update(a,b,x,k*2+2,(l+r)/2,r);
        dat[k]=min(dat[k*2+1],dat[k*2+2]);
    }
    int query(int a,int b,int k=0,int l=0,int r=SEG){
        push(k);
        if(r<=a||b<=l)return INF;
        if(a<=l&&r<=b)return dat[k];
        return min(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));
    }
};

int N,K;
int A[111111],B[111111];
int S[111111];

int to[20][111111],cost[20][111111];

signed main(){
    cin>>N>>K;
    rep(i,N){
        cin>>A[i]>>B[i];
        A[i]*=2;
    }
    rep(i,N){
        if(A[i]>K){
            cout<<-1<<endl;
            return 0;
        }
    }
    
    rep(i,N)S[i+1]=S[i]+A[i];
    vint lis;
    rep(i,N+1)lis.pb(S[i]%K);
    sort(all(lis));lis.erase(unique(all(lis)),lis.end());
    rep(i,20)to[i][N]=N;
    segtree seg;
    for(int i=N-1;i>=0;i--){
        int w=lower_bound(all(lis),S[i]%K)-lis.begin();
        int z=seg.qeury(w,w+1);
        if(z==INF){
            to[0][i]=N;
            cost[0][i]=sum[N]-sum[i]
        }
        else{
            to[0][i]=z;
            cost[0][i]=(sum[z]-sum[i])/K+1;
        }
        for(int j=0;j<19;j++){
            to[j+1][i]=to[j][to[j][i]];
            cost[j+1][i]=cost[j][i]+cost[j][to[j][i]];
        }
        if(B[i]==1){
            int l=lower_bound(all(lis),S[i]%K)-lis.begin();
            int r=lower_bound(all(lis),S[i+1]%K)-lis.begin();
            if(l<r){
                seg.update(l+1,r,i);
            }
            else{
                seg.update(l+1,lis.size(),i);
                seg.update(0,r,i);
            }
        }
    }
    
    rep(i,lis.size()){
        if(seg.qery(i,i+1)==INF){
            cout<<S[N]<<endl;
            return 0;
        }
    }
    assert(0);
    
    return 0;
}

Compiler output:
a.cc: In constructor 'segtree::segtree()':
a.cc:23:30: error: class 'segtree' does not have any field named 'put'
   23 |     segtree():dat(SEG*2,INF),put(SEG*2,INF){}
      |                              ^~~
a.cc: In member function 'void segtree::push(long long int)':
a.cc:25:22: error: 'put' was not declared in this scope; did you mean 'putw'?
   25 |         chmin(dat[k],put[k]);
      |                      ^~~
      |                      putw
a.cc: In member function 'void segtree::update(long long int, long long int, long long int, long long int, long long int, long long int)':
a.cc:35:13: error: 'put' was not declared in this scope; did you mean 'putw'?
   35 |             put[k]=x;
      |             ^~~
      |             putw
a.cc: In function 'int main()':
a.cc:78:19: error: 'struct segtree' has no member named 'qeury'; did you mean 'query'?
   78 |         int z=seg.qeury(w,w+1);
      |                   ^~~~~
      |                   query
a.cc:81:24: error: 'sum' was not declared in this scope
   81 |             cost[0][i]=sum[N]-sum[i]
      |                        ^~~
a.cc:85:25: error: 'sum' was not declared in this scope
   85 |             cost[0][i]=(sum[z]-sum[i])/K+1;
      |                         ^~~
a.cc:105:16: error: 'struct segtree' has no member named 'qery'; did you mean 'query'?
  105 |         if(seg.qery(i,i+1)==INF){
      |                ^~~~
      |                query