Submission

Status:

PPPPPPPPPP

Subtask/Task Score:

100/100

Score: 100

User: wleo.lwo

Problemset: ความลึก

Language: cpp

Time: 0.189 second

Submitted On: 2026-10-06 15:51:49

#include <iostream>
#include <vector>
using namespace std;

int main(){
    int n,q;
    cin >> n >> q;
    int depthnow = 0;
    int depthmax = 0;

    int keep = 0;

    vector<int> start(n+1,0);
    vector<int> best(n+1,0);

    for(int i=0;i<n;i++){
        int D,L;
        cin >> D >> L;

        if(D==1){
            depthnow++;
            start[depthnow] = keep;

            if(depthnow > depthmax){depthmax=depthnow;}
            keep+=L;
        }

        if(D==-1){
            int width;
            width = keep - start[depthnow];

            if(width > best[depthnow]){
                best[depthnow] = width;
            }
            depthnow--;
            keep+=L;
        }
    }

    best[1]=keep;



    for(int i=depthmax-1;i>=1;i--){
        if(best[i+1]>best[i]){
            best[i]=best[i+1];
        }
    }




    for(int i=0;i<q;i++){
        int w;
        cin >> w;

        int left = 1;
        int right = depthmax;
        int ans=0;

        while(left <= right){
            int m = (left+right)/2;
            if(best[m]>=w){
                ans = m;
                left=m+1;
            }
            else{
                right=m-1;
            }
        }
        cout << ans << "\n";
    }


    

    



    return 0;
}