Here's my solution, small and simple for beginners

#include <iostream>using namespace std;int main() {int N, max, min, b;cin>>N>>b>>min>>max;int seq[N];for(int i = 0;i<N;i++)cin>>seq[i];int i = 0;while(i<N){int x = b + seq[i];int y = b - seq[i];if(x<=max && x>=min)b = x;else if(y<=max && y>=min)b = y;elseb= -1;i++;}