Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size n × m and consists of blocks of size 1 × 1.Each block of the labyrinth has a button that rotates all blocks 90 degrees clockwise. Each block rotates around its center and doesn't change its position in the labyrinth. Also, each block has some number of doors (possibly none). In one minute, Theseus can either push the button in order to rotate all the blocks 90 degrees clockwise or pass to the neighbouring block. Theseus can go from block A to some neighbouring block B only if block A has a door that leads to block B and block B has a door that leads to block A.Theseus found an entrance to labyrinth and is now located in block (xT, yT) — the block in the row xT and column yT. Theseus know that the Minotaur is hiding in block (xM, yM) and wants to know the minimum number of minutes required to get there.Theseus is a hero, not a programmer, so he asks you to help him.

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in labyrinth, respectively. Each of the following n lines contains m characters, describing the blocks of the labyrinth. The possible characters are:   «+» means this block has 4 doors (one door to each neighbouring block);  «-» means this block has 2 doors — to the left and to the right neighbours;  «|» means this block has 2 doors — to the top and to the bottom neighbours;  «^» means this block has 1 door — to the top neighbour;  «>» means this block has 1 door — to the right neighbour;  «<» means this block has 1 door — to the left neighbour;  «v» means this block has 1 door — to the bottom neighbour; «L» means this block has 3 doors — to all neighbours except left one;  «R» means this block has 3 doors — to all neighbours except right one;  «U» means this block has 3 doors — to all neighbours except top one;  «D» means this block has 3 doors — to all neighbours except bottom one; «*» means this block is a wall and has no doors.  Left, right, top and bottom are defined from representing labyrinth as a table, where rows are numbered from 1 to n from top to bottom and columns are numbered from 1 to m from left to right. Next line contains two integers — coordinates of the block (xT, yT) (1 ≤ xT ≤ n, 1 ≤ yT ≤ m), where Theseus is initially located. Last line contains two integers — coordinates of the block (xM, yM) (1 ≤ xM ≤ n, 1 ≤ yM ≤ m), where Minotaur hides. It's guaranteed that both the block where Theseus starts and the block where Minotaur is hiding have at least one door. Theseus and Minotaur may be initially located at the same block.

If Theseus is not able to get to Minotaur, then print -1 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the block where Minotaur is hiding.

NoteAssume that Theseus starts at the block (xT, yT) at the moment 0.

(function () {
	function Queue (amount) {
		if(!(this instanceof Queue)) {
			return new Queue(amount);
		}
		amount = amount||1024;
		this.amount = amount;
		this.bg = 0;
		this.ed = 0;
		this.arr = [];
	}
	Queue.prototype = {
		constractor: Queue,
		isEmpty: function () {
			return (this.bg === this.ed);
		},
		doubleSize: function () {
			var target = [],
				o = this.arr,
				i = this.bg,
				j = this.ed,
				l = this.amount;
			target.push(o[i])
			i=(i+1)%l;
			for(;i!=j;i=(i+1)%l)
				target.push(o[i]);
			this.amount *= 2;
			this.bg = 0;
			this.ed = target.length;
			this.arr = target;
			//print(this.arr);
		},
		push: function (o) {
			this.arr[this.ed] = o;
			this.ed = (this.ed+1)%this.amount;
			if(this.isEmpty()) {
				this.doubleSize();
			}
		},
		pop: function () {
			var idx = this.bg;
			this.bg = (this.bg+1)%this.amount;
			return this.arr[idx];
		}
	}
	var rotater = (function () {
		var _door = [
			['*'],
			['^','>','v','<'],
			['-','|'],
			['L','U','R','D'],
			['+']
		];
		var _todoor = {},
			i,
			j,
			l;
		for(i=0;i<5;++i) {
			l = _door[i].length;
			for(j=0;j<l;++j) {
				_todoor[_door[i][j]] = {
					x: i,
					y: j,
					l: l
				};
			}
		}
		function rotateOne (c, t) {
			t = (t===undefined?1:t);
			var di = _todoor[c];
			return _door[di.x][(di.y+t)%di.l];
		}
		return {
			rotateOne: rotateOne
		}
	})();
	var steps = [
		[0,1],
		[1,0],
		[0,-1],
		[-1,0]
	];
	var rules = (function () {
		var right = ['+','-','>','L','U','D'];
		var down = ['+','|','v','L','R','U'];
		var left = ['+','-','<','D','R','U'];
		var up = ['+','|','^','D','R','L'];
		function lg (c, d) {
			for(var i=d.length-1;i>=0;--i) {
				if(d[i]===c)
					return true;
			}
			return false;
		}
		return [
			function (a,b) {
				return lg(a,right)&&lg(b,left);
			},
			function (a,b) {
				return lg(a,down)&&lg(b,up);
			},
			function (a,b) {
				return lg(a,left)&&lg(b,right);
			},
			function (a,b) {
				return lg(a,up)&&lg(b,down);
			}
		];
	})();
	function readlineInt () {
		return readline().split(' ').map(function (e) {
			return parseInt(e);
		});
	}
	var line = readlineInt();
	var n = line[0],
		m = line[1];
	var vec = [];
	for(var i=0;i<n;++i)
		vec.push(readline());
	var st = readlineInt();
	var ed = readlineInt();
	st = st.map(function (e) {
		return e-1;
	});
	ed = ed.map(function (e) {
		return e-1;
	});

	st.push(0);
	var qu = new Queue();
	var used = [];
	used.length = n*m*4;
	qu.push({
		p: st,
		l: 0
	});
	used[(st[0]*m+st[1])*4+st[2]] = true;

	var cur;
	var nxt;
	var ans = -1;
	var i,j,k,x,xx,ii,jj;
	while(!qu.isEmpty()) {
		cur = qu.pop();
		  // print("i:"+cur.p[0]);
		  // print("j:"+cur.p[1]);
		  // print("x:"+cur.p[2]);
		  // print("l:"+cur.l);
		if(cur.p[0] === ed[0] && cur.p[1] === ed[1]) {
			qu = [];
			ans = cur.l;
			break;
		}
		i = cur.p[0];
		j = cur.p[1];
		x = cur.p[2];
		for(k=0;k<4;++k){
			ii = i + steps[k][0];
			jj = j + steps[k][1];
			if(ii>=0&&ii<n&&jj>=0&&jj<m&&!used[(ii*m+jj)*4+x]) {
				if(rules[k](rotater.rotateOne(vec[i][j],x),rotater.rotateOne(vec[ii][jj],x))) {
					qu.push({
						p: [ii,jj,x],
						l: cur.l+1
					});
					used[(ii*m+jj)*4+x] = true;
				}
			}
		}
		xx = (x+1)%4;
		if(!used[(i*m+j)*4+xx]) {
			qu.push({
				p: [i,j,xx],
				l: cur.l+1
			});
			used[(i*m+j)*4+xx] = true;
		}
	}
	print(ans);
})();

(function () {
	function Queue (amount) {
		if(!(this instanceof Queue)) {
			return new Queue(amount);
		}
		amount = amount||1024;
		this.amount = amount;
		this.bg = 0;
		this.ed = 0;
		this.arr = [];
	}
	Queue.prototype = {
		constractor: Queue,
		isEmpty: function () {
			return (this.bg === this.ed);
		},
		doubleSize: function () {
			var target = [],
				o = this.arr,
				i = this.bg,
				j = this.ed,
				l = this.amount;
			target.push(o[i]);
			i=(i+1)%l;
			for(;i!=j;i=(i+1)%l)
				target.push(o[i]);
			this.amount *= 2;
			this.bg = 0;
			this.ed = target.length;
			this.arr = target;
			//print(this.arr);
		},
		push: function (o) {
			this.arr[this.ed] = o;
			this.ed = (this.ed+1)%this.amount;
			if(this.isEmpty()) {
				this.doubleSize();
			}
		},
		pop: function () {
			var idx = this.bg;
			this.bg = (this.bg+1)%this.amount;
			return this.arr[idx];
		}
	};
	var rotater = (function () {
		var _door = [
			['*'],
			['^','>','v','<'],
			['-','|'],
			['L','U','R','D'],
			['+']
		];
		var _todoor = {},
			i,
			j,
			l;
		for(i=0;i<5;++i) {
			l = _door[i].length;
			for(j=0;j<l;++j) {
				_todoor[_door[i][j]] = {
					x: i,
					y: j,
					l: l
				};
			}
		}
		function rotateOne (c, t) {
			t = (t===undefined?1:t);
			var di = _todoor[c];
			return _door[di.x][(di.y+t)%di.l];
		}
		return {
			rotateOne: rotateOne
		};
	})();
	var steps = [
		[0,1],
		[1,0],
		[0,-1],
		[-1,0]
	];
	var rules = (function () {
		var right = ['+','-','>','L','U','D'];
		var down = ['+','|','v','L','R','U'];
		var left = ['+','-','<','D','R','U'];
		var up = ['+','|','^','D','R','L'];
		function lg (c, d) {
			for(var i=d.length-1;i>=0;--i) {
				if(d[i]===c)
					return true;
			}
			return false;
		}
		return [
			function (a,b) {
				return lg(a,right)&&lg(b,left);
			},
			function (a,b) {
				return lg(a,down)&&lg(b,up);
			},
			function (a,b) {
				return lg(a,left)&&lg(b,right);
			},
			function (a,b) {
				return lg(a,up)&&lg(b,down);
			}
		];
	})();
	function readlineInt () {
		return readline().split(' ').map(function (e) {
			return parseInt(e);
		});
	}
	var line = readlineInt();
	var n = line[0],
		m = line[1];
	var vec = [];
	for(var i=0;i<n;++i)
		vec.push(readline());
	var st = readlineInt();
	var ed = readlineInt();
	st = st.map(function (e) {
		return e-1;
	});
	ed = ed.map(function (e) {
		return e-1;
	});

	st.push(0);
	var qu = new Queue();
	var L = n*m*4;
	var used = [];
	for( i=0; i<L;++i)
		used.push(false);
	qu.push({
		p: st,
		l: 0
	});
	used[(st[0]*m+st[1])*4+st[2]] = true;

	var cur;
	var nxt;
	var ans = -1;
	var j,k,x,xx,ii,jj;
	while(!qu.isEmpty()) {
		cur = qu.pop();
		  // print("i:"+cur.p[0]);
		  // print("j:"+cur.p[1]);
		  // print("x:"+cur.p[2]);
		  // print("l:"+cur.l);
		if(cur.p[0] === ed[0] && cur.p[1] === ed[1]) {
			qu = [];
			ans = cur.l;
			break;
		}
		i = cur.p[0];
		j = cur.p[1];
		x = cur.p[2];
		for(k=0;k<4;++k){
			ii = i + steps[k][0];
			jj = j + steps[k][1];
			if(ii>=0&&ii<n&&jj>=0&&jj<m&&!used[(ii*m+jj)*4+x]) {
				if(rules[k](rotater.rotateOne(vec[i][j],x),rotater.rotateOne(vec[ii][jj],x))) {
					qu.push({
						p: [ii,jj,x],
						l: cur.l+1
					});
					used[(ii*m+jj)*4+x] = true;
				}
			}
		}
		xx = (x+1)%4;
		if(!used[(i*m+j)*4+xx]) {
			qu.push({
				p: [i,j,xx],
				l: cur.l+1
			});
			used[(i*m+j)*4+xx] = true;
		}
	}
	print(ans);
})();

(function () {
	function Queue (amount) {
		if(!(this instanceof Queue)) {
			return new Queue(amount);
		}
		amount = amount||1024;
		this.amount = amount;
		this.bg = 0;
		this.ed = 0;
		this.arr = [];
	}
	Queue.prototype = {
		constractor: Queue,
		isEmpty: function () {
			return (this.bg === this.ed);
		},
		doubleSize: function () {
			var target = [],
				o = this.arr,
				i = this.bg,
				j = this.ed,
				l = this.amount;
			target.push(o[i])
			i=(i+1)%l;
			for(;i!=j;i=(i+1)%l)
				target.push(o[i]);
			this.amount *= 2;
			this.bg = 0;
			this.ed = target.length;
			this.arr = target;
			//print(this.arr);
		},
		push: function (o) {
			this.arr[this.ed] = o;
			this.ed = (this.ed+1)%this.amount;
			if(this.isEmpty()) {
				this.doubleSize();
			}
		},
		pop: function () {
			var idx = this.bg;
			this.bg = (this.bg+1)%this.amount;
			return this.arr[idx];
		}
	}
	var rotater = (function () {
		var _door = [
			['*'],
			['^','>','v','<'],
			['-','|'],
			['L','U','R','D'],
			['+']
		];
		var _todoor = {},
			i,
			j,
			l;
		for(i=0;i<5;++i) {
			l = _door[i].length;
			for(j=0;j<l;++j) {
				_todoor[_door[i][j]] = {
					x: i,
					y: j,
					l: l
				};
			}
		}
		function rotateOne (c, t) {
			t = (t===undefined?1:t);
			var di = _todoor[c];
			return _door[di.x][(di.y+t)%di.l];
		}
		return {
			rotateOne: rotateOne
		}
	})();
	var steps = [
		[0,1],
		[1,0],
		[0,-1],
		[-1,0]
	];
	var rules = (function () {
		var right = ['+','-','>','L','U','D'];
		var down = ['+','|','v','L','R','U'];
		var left = ['+','-','<','D','R','U'];
		var up = ['+','|','^','D','R','L'];
		function lg (c, d) {
			for(var i=d.length-1;i>=0;--i) {
				if(d[i]===c)
					return true;
			}
			return false;
		}
		return [
			function (a,b) {
				return lg(a,right)&&lg(b,left);
			},
			function (a,b) {
				return lg(a,down)&&lg(b,up);
			},
			function (a,b) {
				return lg(a,left)&&lg(b,right);
			},
			function (a,b) {
				return lg(a,up)&&lg(b,down);
			}
		];
	})();
	function readlineInt () {
		return readline().split(' ').map(function (e) {
			return parseInt(e);
		});
	}
	var line = readlineInt();
	var n = line[0],
		m = line[1];
	var vec = [];
	for(var i=0;i<n;++i)
		vec.push(readline());
	var st = readlineInt();
	var ed = readlineInt();
	st = st.map(function (e) {
		return e-1;
	});
	ed = ed.map(function (e) {
		return e-1;
	});

	st.push(0);
	var qu = new Queue();
	var L = n*m*4;
	var used = [];
	for(var i=0; i<L;++i)
		used[i] = false;
	qu.push({
		p: st,
		l: 0
	});
	used[(st[0]*m+st[1])*4+st[2]] = true;

	var cur;
	var nxt;
	var ans = -1;
	var j,k,x,xx,ii,jj;
	while(!qu.isEmpty()) {
		cur = qu.pop();
		  // print("i:"+cur.p[0]);
		  // print("j:"+cur.p[1]);
		  // print("x:"+cur.p[2]);
		  // print("l:"+cur.l);
		if(cur.p[0] === ed[0] && cur.p[1] === ed[1]) {
			qu = [];
			ans = cur.l;
			break;
		}
		i = cur.p[0];
		j = cur.p[1];
		x = cur.p[2];
		for(k=0;k<4;++k){
			ii = i + steps[k][0];
			jj = j + steps[k][1];
			if(ii>=0&&ii<n&&jj>=0&&jj<m&&!used[(ii*m+jj)*4+x]) {
				if(rules[k](rotater.rotateOne(vec[i][j],x),rotater.rotateOne(vec[ii][jj],x))) {
					qu.push({
						p: [ii,jj,x],
						l: cur.l+1
					});
					used[(ii*m+jj)*4+x] = true;
				}
			}
		}
		xx = (x+1)%4;
		if(!used[(i*m+j)*4+xx]) {
			qu.push({
				p: [i,j,xx],
				l: cur.l+1
			});
			used[(i*m+j)*4+xx] = true;
		}
	}
	print(ans);
})();

(function () {
	function Queue (amount) {
		if(!(this instanceof Queue)) {
			return new Queue(amount);
		}
		amount = amount||1024;
		this.amount = amount;
		this.bg = 0;
		this.ed = 0;
		this.arr = [];
	}
	Queue.prototype = {
		constractor: Queue,
		isEmpty: function () {
			return (this.bg === this.ed);
		},
		doubleSize: function () {
			var target = [],
				o = this.arr,
				i = this.bg,
				j = this.ed,
				l = this.amount;
			target.push(o[i])
			i=(i+1)%l;
			for(;i!=j;i=(i+1)%l)
				target.push(o[i]);
			this.amount *= 2;
			this.bg = 0;
			this.ed = target.length;
			this.arr = target;
			//print(this.arr);
		},
		push: function (o) {
			this.arr[this.ed] = o;
			this.ed = (this.ed+1)%this.amount;
			if(this.isEmpty()) {
				this.doubleSize();
			}
		},
		pop: function () {
			var idx = this.bg;
			this.bg = (this.bg+1)%this.amount;
			return this.arr[idx];
		}
	}
	var rotater = (function () {
		var _door = [
			['*'],
			['^','>','v','<'],
			['-','|'],
			['L','U','R','D'],
			['+']
		];
		var _todoor = {},
			i,
			j,
			l;
		for(i=0;i<5;++i) {
			l = _door[i].length;
			for(j=0;j<l;++j) {
				_todoor[_door[i][j]] = {
					x: i,
					y: j,
					l: l
				};
			}
		}
		function rotateOne (c, t) {
			t = (t===undefined?1:t);
			var di = _todoor[c];
			return _door[di.x][(di.y+t)%di.l];
		}
		return {
			rotateOne: rotateOne
		}
	})();
	var steps = [
		[0,1],
		[1,0],
		[0,-1],
		[-1,0]
	];
	var rules = (function () {
		var right = ['+','-','>','L','U','D'];
		var down = ['+','|','v','L','R','U'];
		var left = ['+','-','<','D','R','U'];
		var up = ['+','|','^','D','R','L'];
		function lg (c, d) {
			for(var i=d.length-1;i>=0;--i) {
				if(d[i]===c)
					return true;
			}
			return false;
		}
		return [
			function (a,b) {
				return lg(a,right)&&lg(b,left);
			},
			function (a,b) {
				return lg(a,down)&&lg(b,up);
			},
			function (a,b) {
				return lg(a,left)&&lg(b,right);
			},
			function (a,b) {
				return lg(a,up)&&lg(b,down);
			}
		];
	})();
	function readlineInt () {
		return readline().split(' ').map(function (e) {
			return parseInt(e);
		});
	}
	var line = readlineInt();
	var n = line[0],
		m = line[1];
	var vec = [];
	for(var i=0;i<n;++i)
		vec.push(readline());
	var st = readlineInt();
	var ed = readlineInt();
	st = st.map(function (e) {
		return e-1;
	});
	ed = ed.map(function (e) {
		return e-1;
	});

	st.push(0);
	var qu = new Queue();
	var L = n*m*4;
	var used = [];
	for(var i=0; i<L;++i)
		used[i] = 0;
	qu.push({
		p: st,
		l: 0
	});
	used[(st[0]*m+st[1])*4+st[2]] = 1;

	var cur;
	var nxt;
	var ans = -1;
	var i,j,k,x,xx,ii,jj;
	while(!qu.isEmpty()) {
		cur = qu.pop();
		  // print("i:"+cur.p[0]);
		  // print("j:"+cur.p[1]);
		  // print("x:"+cur.p[2]);
		  // print("l:"+cur.l);
		if(cur.p[0] === ed[0] && cur.p[1] === ed[1]) {
			qu = [];
			ans = cur.l;
			break;
		}
		i = cur.p[0];
		j = cur.p[1];
		x = cur.p[2];
		for(k=0;k<4;++k){
			ii = i + steps[k][0];
			jj = j + steps[k][1];
			if(ii>=0&&ii<n&&jj>=0&&jj<m&&!used[(ii*m+jj)*4+x]) {
				if(rules[k](rotater.rotateOne(vec[i][j],x),rotater.rotateOne(vec[ii][jj],x))) {
					qu.push({
						p: [ii,jj,x],
						l: cur.l+1
					});
					used[(ii*m+jj)*4+x] = 1;
				}
			}
		}
		xx = (x+1)%4;
		if(!used[(i*m+j)*4+xx]) {
			qu.push({
				p: [i,j,xx],
				l: cur.l+1
			});
			used[(i*m+j)*4+xx] = 1;
		}
	}
	print(ans);
})();

(function () {
	function Queue (amount) {
		if(!(this instanceof Queue)) {
			return new Queue(amount);
		}
		amount = amount||1024;
		this.amount = amount;
		this.bg = 0;
		this.ed = 0;
		this.arr = [];
	}
	Queue.prototype = {
		constractor: Queue,
		isEmpty: function () {
			return (this.bg === this.ed);
		},
		doubleSize: function () {
			var target = [],
				o = this.arr,
				i = this.bg,
				j = this.ed,
				l = this.amount;
			target.push(o[i])
			i=(i+1)%l;
			for(;i!=j;i=(i+1)%l)
				target.push(o[i]);
			this.amount *= 2;
			this.bg = 0;
			this.ed = target.length;
			this.arr = target;
			//print(this.arr);
		},
		push: function (o) {
			this.arr[this.ed] = o;
			this.ed = (this.ed+1)%this.amount;
			if(this.isEmpty()) {
				this.doubleSize();
			}
		},
		pop: function () {
			var idx = this.bg;
			this.bg = (this.bg+1)%this.amount;
			return this.arr[idx];
		}
	}
	var rotater = (function () {
		var _door = [
			['*'],
			['^','>','v','<'],
			['-','|'],
			['L','U','R','D'],
			['+']
		];
		var _todoor = {},
			i,
			j,
			l;
		for(i=0;i<5;++i) {
			l = _door[i].length;
			for(j=0;j<l;++j) {
				_todoor[_door[i][j]] = {
					x: i,
					y: j,
					l: l
				};
			}
		}
		function rotateOne (c, t) {
			t = (t===undefined?1:t);
			var di = _todoor[c];
			return _door[di.x][(di.y+t)%di.l];
		}
		return {
			rotateOne: rotateOne
		}
	})();
	var steps = [
		[0,1],
		[1,0],
		[0,-1],
		[-1,0]
	];
	var rules = (function () {
		var right = ['+','-','>','L','U','D'];
		var down = ['+','|','v','L','R','U'];
		var left = ['+','-','<','D','R','U'];
		var up = ['+','|','^','D','R','L'];
		function lg (c, d) {
			for(var i=d.length-1;i>=0;--i) {
				if(d[i]===c)
					return true;
			}
			return false;
		}
		return [
			function (a,b) {
				return lg(a,right)&&lg(b,left);
			},
			function (a,b) {
				return lg(a,down)&&lg(b,up);
			},
			function (a,b) {
				return lg(a,left)&&lg(b,right);
			},
			function (a,b) {
				return lg(a,up)&&lg(b,down);
			}
		];
	})();
	function readlineInt () {
		return readline().split(' ').map(function (e) {
			return parseInt(e);
		});
	}
	var line = readlineInt();
	var n = line[0],
		m = line[1];
	var vec = [];
	for(var i=0;i<n;++i)
		vec.push(readline());
	var st = readlineInt();
	var ed = readlineInt();
	st = st.map(function (e) {
		return e-1;
	});
	ed = ed.map(function (e) {
		return e-1;
	});

	st.push(0);
	var qu = new Queue();
	var used = new Array(n*m*4);
	//used.length = ;
	qu.push({
		p: st,
		l: 0
	});
	used[(st[0]*m+st[1])*4+st[2]] = true;

	var cur;
	var nxt;
	var ans = -1;
	var i,j,k,x,xx,ii,jj;
	while(!qu.isEmpty()) {
		cur = qu.pop();
		  // print("i:"+cur.p[0]);
		  // print("j:"+cur.p[1]);
		  // print("x:"+cur.p[2]);
		  // print("l:"+cur.l);
		if(cur.p[0] === ed[0] && cur.p[1] === ed[1]) {
			qu = [];
			ans = cur.l;
			break;
		}
		i = cur.p[0];
		j = cur.p[1];
		x = cur.p[2];
		for(k=0;k<4;++k){
			ii = i + steps[k][0];
			jj = j + steps[k][1];
			if(ii>=0&&ii<n&&jj>=0&&jj<m&&!used[(ii*m+jj)*4+x]) {
				if(rules[k](rotater.rotateOne(vec[i][j],x),rotater.rotateOne(vec[ii][jj],x))) {
					qu.push({
						p: [ii,jj,x],
						l: cur.l+1
					});
					used[(ii*m+jj)*4+x] = true;
				}
			}
		}
		xx = (x+1)%4;
		if(!used[(i*m+j)*4+xx]) {
			qu.push({
				p: [i,j,xx],
				l: cur.l+1
			});
			used[(i*m+j)*4+xx] = true;
		}
	}
	print(ans);
})();

(function () {
	function Queue (amount) {
		if(!(this instanceof Queue)) {
			return new Queue(amount);
		}
		amount = amount||2048;
		this.amount = amount;
		this.bg = 0;
		this.ed = 0;
		this.arr = [];
	}
	Queue.prototype = {
		constractor: Queue,
		isEmpty: function () {
			return (this.bg === this.ed);
		},
		doubleSize: function () {
			var target = [],
				o = this.arr,
				i = this.bg,
				j = this.ed,
				l = this.amount;
			target.push(o[i])
			i=(i+1)%l;
			for(;i!=j;i=(i+1)%l)
				target.push(o[i]);
			this.amount *= 2;
			this.bg = 0;
			this.ed = target.length;
			this.arr = target;
			//print(this.arr);
		},
		push: function (o) {
			this.arr[this.ed] = o;
			this.ed = (this.ed+1)%this.amount;
			if(this.isEmpty()) {
				this.doubleSize();
			}
		},
		pop: function () {
			var idx = this.bg;
			this.bg = (this.bg+1)%this.amount;
			return this.arr[idx];
		}
	}
	var rotater = (function () {
		var _door = [
			['*'],
			['^','>','v','<'],
			['-','|'],
			['L','U','R','D'],
			['+']
		];
		var _todoor = {},
			i,
			j,
			l;
		for(i=0;i<5;++i) {
			l = _door[i].length;
			for(j=0;j<l;++j) {
				_todoor[_door[i][j]] = {
					x: i,
					y: j,
					l: l
				};
			}
		}
		function rotateOne (c, t) {
			t = (t===undefined?1:t);
			var di = _todoor[c];
			return _door[di.x][(di.y+t)%di.l];
		}
		return {
			rotateOne: rotateOne
		}
	})();
	var steps = [
		[0,1],
		[1,0],
		[0,-1],
		[-1,0]
	];
	var rules = (function () {
		var right = ['+','-','>','L','U','D'];
		var down = ['+','|','v','L','R','U'];
		var left = ['+','-','<','D','R','U'];
		var up = ['+','|','^','D','R','L'];
		function lg (c, d) {
			for(var i=d.length-1;i>=0;--i) {
				if(d[i]===c)
					return true;
			}
			return false;
		}
		return [
			function (a,b) {
				return lg(a,right)&&lg(b,left);
			},
			function (a,b) {
				return lg(a,down)&&lg(b,up);
			},
			function (a,b) {
				return lg(a,left)&&lg(b,right);
			},
			function (a,b) {
				return lg(a,up)&&lg(b,down);
			}
		];
	})();
	function readlineInt () {
		return readline().split(' ').map(function (e) {
			return parseInt(e);
		});
	}
	var line = readlineInt();
	var n = line[0],
		m = line[1];
	var vec = [];
	for(var i=0;i<n;++i)
		vec.push(readline());
	var st = readlineInt();
	var ed = readlineInt();
	st = st.map(function (e) {
		return e-1;
	});
	ed = ed.map(function (e) {
		return e-1;
	});

	st.push(0);
	var qu = new Queue();
	var used = [];
	used.length = n*m*4;
	qu.push({
		p: st,
		l: 0
	});
	used[(st[0]*m+st[1])*4+st[2]] = true;

	var cur;
	var nxt;
	var ans = -1;
	var i,j,k,x,xx,ii,jj;
	while(!qu.isEmpty()) {
		cur = qu.pop();
		  // print("i:"+cur.p[0]);
		  // print("j:"+cur.p[1]);
		  // print("x:"+cur.p[2]);
		  // print("l:"+cur.l);
		if(cur.p[0] === ed[0] && cur.p[1] === ed[1]) {
			qu = [];
			ans = cur.l;
			break;
		}
		i = cur.p[0];
		j = cur.p[1];
		x = cur.p[2];
		for(k=0;k<4;++k){
			ii = i + steps[k][0];
			jj = j + steps[k][1];
			if(ii>=0&&ii<n&&jj>=0&&jj<m&&!used[(ii*m+jj)*4+x]) {
				if(rules[k](rotater.rotateOne(vec[i][j],x),rotater.rotateOne(vec[ii][jj],x))) {
					qu.push({
						p: [ii,jj,x],
						l: cur.l+1
					});
					used[(ii*m+jj)*4+x] = true;
				}
			}
		}
		xx = (x+1)%4;
		if(!used[(i*m+j)*4+xx]) {
			qu.push({
				p: [i,j,xx],
				l: cur.l+1
			});
			used[(i*m+j)*4+xx] = true;
		}
	}
	print(ans);
})();


You are given a sequence of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$.You have to construct two sequences of integers $$$b$$$ and $$$c$$$ with length $$$n$$$ that satisfy:  for every $$$i$$$ ($$$1\leq i\leq n$$$) $$$b_i+c_i=a_i$$$  $$$b$$$ is non-decreasing, which means that for every $$$1<i\leq n$$$, $$$b_i\geq b_{i-1}$$$ must hold  $$$c$$$ is non-increasing, which means that for every $$$1<i\leq n$$$, $$$c_i\leq c_{i-1}$$$ must hold You have to minimize $$$\max(b_i,c_i)$$$. In other words, you have to minimize the maximum number in sequences $$$b$$$ and $$$c$$$.Also there will be $$$q$$$ changes, the $$$i$$$-th change is described by three integers $$$l,r,x$$$. You should add $$$x$$$ to $$$a_l,a_{l+1}, \ldots, a_r$$$. You have to find the minimum possible value of $$$\max(b_i,c_i)$$$ for the initial sequence and for sequence after each change.

The first line contains an integer $$$n$$$ ($$$1\leq n\leq 10^5$$$). The secound line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\leq i\leq n$$$, $$$-10^9\leq a_i\leq 10^9$$$). The third line contains an integer $$$q$$$ ($$$1\leq q\leq 10^5$$$). Each of the next $$$q$$$ lines contains three integers $$$l,r,x$$$ ($$$1\leq l\leq r\leq n,-10^9\leq x\leq 10^9$$$), desribing the next change.

Print $$$q+1$$$ lines. On the $$$i$$$-th ($$$1 \leq i \leq q+1$$$) line, print the answer to the problem for the sequence after $$$i-1$$$ changes.

NoteIn the first test:  The initial sequence $$$a = (2, -1, 7, 3)$$$. Two sequences $$$b=(-3,-3,5,5),c=(5,2,2,-2)$$$ is a possible choice.  After the first change $$$a = (2, -4, 4, 0)$$$. Two sequences $$$b=(-3,-3,5,5),c=(5,-1,-1,-5)$$$ is a possible choice.  After the second change $$$a = (2, -4, 6, 2)$$$. Two sequences $$$b=(-4,-4,6,6),c=(6,0,0,-4)$$$ is a possible choice.

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $()
  let a = $(n)
  let d = Arr(1, n - 1, 1, i => a[i] - a[i - 1]);
  let a0 = a[0];
  let k = d.map(v => v > 0 ? v : 0).sum();
  out.push(Math.ceil((a0 + k) / 2) + 0);
  let q = $()
  while (q--) {
    let [l, r, x] = $(3)
    if (l == 1) a0 += x;
    else {
      k -= max(d[l - 1], 0);
      d[l - 1] += x;
      k += max(d[l - 1], 0);
    }
    if (r < n) {
      k -= max(d[r], 0);
      d[r] -= x;
      k += max(d[r], 0);
    }
    out.push(Math.ceil((a0 + k) / 2) + 0);
  }
  log(out.join('\n'))
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $()
  let a = $(n)
  let d = Arr(1, n - 1, 1, i => a[i] - a[i - 1]);
  let a0 = a[0];
  let k = d.map(v => v > 0 ? v : 0).sum();
  out.push(Math.ceil((a0 + k) / 2) + '');
  let q = $()
  while (q--) {
    let [l, r, x] = $(3)
    if (l == 1) a0 += x;
    else {
      k -= max(d[l - 1], 0);
      d[l - 1] += x;
      k += max(d[l - 1], 0);
    }
    if (r < n) {
      k -= max(d[r], 0);
      d[r] -= x;
      k += max(d[r], 0);
    }
    out.push(Math.ceil((a0 + k) / 2) + '');
  }
  log(out.join('\n'))
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $()
  let a = $(n)
  let d = Arr(1, n - 1, 1, i => a[i] - a[i - 1]);
  let a0 = a[0];
  let k = d.map(v => v > 0 ? v : 0).sum();
  log(Math.ceil((a0 + k) / 2) + '');
  let q = $()
  while (q--) {
    let [l, r, x] = $(3)
    if (l == 1) a0 += x;
    else {
      k -= max(d[l - 1], 0);
      d[l - 1] += x;
      k += max(d[l - 1], 0);
    }
    if (r < n) {
      k -= max(d[r], 0);
      d[r] -= x;
      k += max(d[r], 0);
    }
    log(Math.ceil((a0 + k) / 2) + '');
  }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    D();
});

function readline() {
    return inputString[currentLine++];
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = parseInt(a[i]);

    return a;
}

function pi(x){
    return parseInt(x);
}

function D(){
    let n = pi(readline());
    let a = ti(readline().split(' '));
    let negSum = 0;
    let posSum = 0;
    if(n === 1){
        if(a[0] % 2 === 0)
            console.log(a[0]/2);
        else
            console.log(Math.ceil(a[0]/2));

        let q = pi(readline());
        while(q > 0){
            q--;
            let [l,r,x] = ti(readline().split(' '));
            a[0] += x;

            if(a[0] % 2 === 0){
                let res = a[0]/2;
                console.log(res === -0 ? 0 : res);
            }
            else{
                let res = Math.ceil(a[0]/2);
                console.log(res === -0 ? 0 : res);
            }
        }

        return;
    }

    let max = 0;
    let d = new Array(n);
    for(let i = 1; i < n; i++){
        d[i-1] = a[i] - a[i-1];
        max += Math.max(0, d[i-1]);
    }

    console.log(Math.ceil((a[0] + max) / 2));
    let q = pi(readline());
    while(q > 0){
        q--;
        let [l, r, x] = ti(readline().split(' '));

        if(l-1 !== 0){
            if(d[l-2] > 0)
                max -= d[l-2];
            d[l-2] += x;
            if(d[l-2] > 0)
                max += d[l-2];
        }else{
            a[l-1] += x;
        }

        if(r < n){
            if(d[r-1] > 0)
                max -= d[r-1];
            d[r-1] += -x;
            if(d[r-1] > 0)
                max += d[r-1];
        }

        console.log(Math.ceil((a[0] + max) / 2));
    }
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $()
  let a = $(n)
  let a0 = a[0];
  let k = Arr(1, n - 1, 1, i => max(a[i] - a[i - 1], 0)).sum();
  log(Math.ceil((a0 + k) / 2));
  let q = $()
  while (q--) {
    let [l, r, x] = $(3)
    if (l == 1) a0 += x;
    else {
      k -= max(a[l - 1] - a[l - 2], 0);
      k += max(a[l - 1] + x - a[l - 2], 0);
    }
    if (r < n) {
      k -= max(a[r] - a[r - 1], 0);
      k += max(a[r] - a[r - 1] - x, 0);
    }
    log(Math.ceil((a0 + k) / 2));
  }
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $()
  let a = $(n)
  let d = Arr(1, n - 1, 1, i => a[i] - a[i - 1]);
  let a0 = a[0];
  let k = d.map(v => v > 0 ? v : 0).sum();
  out.push((a0 + k) / 2 | 0);
  let q = $()
  while (q--) {
    let [l, r, x] = $(3)
    if (l == 1) a0 += x;
    else {
      k -= max(d[l - 1], 0);
      d[l - 1] += x;
      k += max(d[l - 1], 0);
    }
    if (r < n) {
      k -= max(d[r], 0);
      d[r] -= x;
      k += max(d[r], 0);
    }
    out.push((a0 + k) / 2 | 0);
  }
  log(out.join('\n'))
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $()
  let a = $(n)
  let d = Arr(1, n - 1, 1, i => a[i] - a[i - 1]);
  let a0 = a[0];
  let k = d.map(v => v > 0 ? v : 0).sum();
  let kq = a0 + k;
  out.push((kq + (kq > 0)) / 2 | 0);
  let q = $()
  while (q--) {
    let [l, r, x] = $(3)
    if (l == 1) a0 += x;
    else {
      k -= max(d[l - 1], 0);
      d[l - 1] += x;
      k += max(d[l - 1], 0);
    }
    if (r < n) {
      k -= max(d[r], 0);
      d[r] -= x;
      k += max(d[r], 0);
    }
    kq = a0 + k;
    out.push((kq + (kq > 0)) / 2 | 0);
  }
  log(out.join('\n'))
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $()
  let a = $(n)
  let a0 = a[0];
  let k = Arr(1, n - 1, 1, i => max(a[i] - a[i - 1], 0)).sum();
  log(Math.ceil((a0 + k) / 2) + '');
  let q = $()
  while (q--) {
    let [l, r, x] = $(3)
    if (l == 1) a0 += x;
    else {
      k -= max(a[l - 1] - a[l - 2], 0);
      k += max(a[l - 1] + x - a[l - 2], 0);
    }
    if (r < n) {
      k -= max(a[r] - a[r - 1], 0);
      k += max(a[r] - a[r - 1] - x, 0);
    }
    log(Math.ceil((a0 + k) / 2) + '');
  }
}

function For(a, b, step, fn) {
  let res;
  if (step > 0) {
    for (let i = a; i <= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  } else {
    for (let i = a; i >= b; i += step) {
      if ((res = fn(i)) != null) return res;
    }
  }
}

function Arr(a, b, step, fn, init) {
  if (!step) {
    return a < b
      ? Array(b - a + 1).fill(0).map((v, i) => fn(a + i))
      : Array(a - b + 1).fill(0).map((v, i) => fn(a - i))
  }
  let arr = Array(Math.max(a, b) + 1).fill(0);
  if (init) init(arr);
  if (step > 0) for (let i = a; i <= b; i += step) arr[i] = fn(i, arr);
  else for (let i = a; i >= b; i += step) arr[i] = fn(i, arr);
  return arr;
}

Object.defineProperty(Object.prototype, '$k', {
  value: function () { return Object.keys(this) }
});
Object.defineProperty(Object.prototype, '$v', {
  value: function () { return Object.values(this) }
});
Object.defineProperty(Object.prototype, '$kv', {
  value: function () { return Object.entries(this) }
});
Array.prototype.sum = function (fn) { return this.reduce((p, v) => p + (fn ? fn(v) : v), 0) }
Array.prototype.uniq = function (fn) {
  var set = new Set();
  return this.filter(v => {
    var newValue = !set.has(fn ? fn(v) : v);
    set.add(fn ? fn(v) : v);
    return newValue;
  });
}
Array.prototype.toObj = function (fn) {
  return this.map(v => fn(v)).reduce((p, v) => {
    p[v[0]] = v[1];
    return p;
  }, {})
}
Array.prototype.min = function (fn) {
  let min = Math.min(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == min : v == min);
  return [this[i], i, min];
}
Array.prototype.max = function (fn) {
  let max = Math.max(...this.map((v, i) => fn ? fn(v, i) : v));
  let i = this.findIndex((v, i) => fn ? fn(v, i) == max : v == max);
  return [this[i], i, max];
}

function gcd(a, b) {
  if (!b) return a;
  return gcd(b, a % b);
}
function mod(a, n, m) {
  if (n == 0n) return 1n;
  if (n % 2n == 0n) return mod(a * a % m, n / 2n, m);
  return a * mod(a, n - 1n, m) % m;
}
function extendedEuclid(a, b) {
  if (!b) return [1, 0];
  else {
    let [x, y] = extendedEuclid(b, a % b);
    return [y, x - (a / b | 0) * y];
  }
}
// [m] = extendedEuclid(a, 998244353);
// let MOD = 998244353n;
// let gt = Arr(0, n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(0, n, (i, a) => i <= 1 ? 1n : MOD - MOD / BigInt(i) * a[998244353 % i] % MOD);
// for (let i = 2; i < n; i++) rgt[i] = rgt[i] * rgt[i - 1] % MOD;
// let nCk = (n, k) => {
//   return gt[n] * rgt[k] * rgt[n - k] % MOD;
// }
// for (let i = dm[l.l]; i <= n + n; i += i & -i) s[i]++; // start from index 1

let input = '';
process.stdin.on('data', c => input += c);
process.stdin.on('end', main);
function main() {
  let inp = input.split(/\s+/).map(v => +v), _i = 0, $ = n => n != null ? inp.slice(_i, _i += n) : inp[_i++],
    out = [], log = console.log, asc = (a, b) => a - b, desc = (a, b) => b - a, min = Math.min, max = Math.max, abs = Math.abs;
  let n = $()
  let a = $(n)
  let d = Arr(1, n - 1, 1, i => a[i] - a[i - 1]);
  let a0 = a[0];
  let k = d.map(v => v > 0 ? v : 0).sum();
  out.push((a0 + k + (a0 + k > 0)) / 2 | 0);
  let q = $()
  while (q--) {
    let [l, r, x] = $(3)
    if (l == 1) a0 += x;
    else {
      k -= max(d[l - 1], 0);
      d[l - 1] += x;
      k += max(d[l - 1], 0);
    }
    if (r < n) {
      k -= max(d[r], 0);
      d[r] -= x;
      k += max(d[r], 0);
    }
    out.push((a0 + k + (a0 + k > 0)) / 2 | 0);
  }
  log(out.join('\n'))
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    D();
});

function readline() {
    return inputString[currentLine++];
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = parseInt(a[i]);

    return a;
}

function pi(x){
    return parseInt(x);
}

function D(){
    let n = pi(readline());
    let a = ti(readline().split(' '));
    let negSum = 0;
    let posSum = 0;
    if(n === 1){
        if(a[0] % 2 === 0)
            console.log(a[0]/2);
        else
            console.log(Math.ceil(a[0]/2));

        let q = pi(readline());
        while(q > 0){
            q--;
            let [l,r,x] = ti(readline().split(' '));
            a[0] += x;

            if(a[0] % 2 === 0){
                let res = a[0]/2;
                console.log(res === -0 ? 0 : res);
            }
            else{
                let res = Math.ceil(a[0]/2);
                console.log(res === -0 ? 0 : res);
            }
        }

        return;
    }

    for(let i = 1; i < n; i++){
        if(a[i] < a[i-1]){
            negSum += (a[i] - a[i-1]);
        }else{
            posSum += (a[i] - a[i-1]);
        }
    }

    let getMaxB = (maxC) => {
        let minB = a[0] - maxC;
        return posSum + minB;
    }

    console.log(Math.ceil((a[0] + posSum) / 2));
    let q = pi(readline());
    while(q > 0){
        q--;
        let [l, r, x] = ti(readline().split(' '));

        if(l-1 !== 0){
            let y = a[l-1] + x;
            if(a[l-2] > y){
                negSum += x;
            }else{
                posSum += x;
            }
        }

        if(r !== n){
            let y = a[r-1] + x;
            if(a[r-2] > y){
                negSum += x;
            }else{
                posSum += x;
            }
        }

        console.log(Math.ceil((a[0] + posSum) / 2));
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    D();
});

function readline() {
    return inputString[currentLine++];
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = parseInt(a[i]);

    return a;
}

function pi(x){
    return parseInt(x);
}

function D(){
    let n = pi(readline());
    let a = ti(readline().split(' '));
    let negSum = 0;
    let posSum = 0;
    if(n === 1){
        if(a[0] % 2 === 0)
            console.log(a[0]/2);
        else
            console.log(Math.ceil(a[0]/2));

        let q = pi(readline());
        while(q > 0){
            q--;
            let [l,r,x] = ti(readline().split(' '));
            a[0] += x;

            if(a[0] % 2 === 0){
                let res = a[0]/2;
                console.log(res === -0 ? 0 : res);
            }
            else{
                let res = Math.ceil(a[0]/2);
                console.log(res === -0 ? 0 : res);
            }
        }

        return;
    }

    for(let i = 1; i < n; i++){
        if(a[i] < a[i-1]){
            negSum += (a[i] - a[i-1]);
        }else{
            posSum += (a[i] - a[i-1]);
        }
    }

    let getMaxB = (maxC) => {
        let minB = a[0] - maxC;
        return posSum + minB;
    }

    let findRes = () => {
        let st = -1000000000;
        let en = 1000000000;
        while(st <= en){
            let maxC = Math.floor((st+en)/2);
            let maxB = getMaxB(maxC);
            if(st === en){
                return st;
            }

            if(maxC === maxB){
                return maxC;
            }else if(maxC > maxB){
                en = maxC;
            }else{
                st = maxC+1;
            }
        }
    }

    console.log(findRes());
    let q = pi(readline());
    while(q > 0){
        q--;
        let [l, r, x] = ti(readline().split(' '));

        if(l-1 !== 0){
            let y = a[l-1] + x;
            if(a[l-2] > y){
                negSum += x;
            }else{
                posSum += x;
            }
        }

        if(r !== n){
            let y = a[r-1] + x;
            if(a[r-2] > y){
                negSum += x;
            }else{
                posSum += x;
            }
        }

        console.log(findRes());
    }
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    D();
});

function readline() {
    return inputString[currentLine++];
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = parseInt(a[i]);

    return a;
}

function pi(x){
    return parseInt(x);
}

function D(){
    let n = pi(readline());
    let a = ti(readline().split(' '));
    let negSum = 0;
    let posSum = 0;
    if(n === 1){
        if(a[0] % 2 === 0)
            console.log(a[0]/2);
        else
            console.log(Math.ceil(a[0]/2));

        let q = pi(readline());
        while(q > 0){
            q--;
            let [l,r,x] = ti(readline().split(' '));
            a[0] += x;

            if(a[0] % 2 === 0){
                let res = a[0]/2;
                console.log(res === -0 ? 0 : res);
            }
            else{
                let res = Math.ceil(a[0]/2);
                console.log(res === -0 ? 0 : res);
            }
        }

        return;
    }

    for(let i = 1; i < n; i++){
        if(a[i] < a[i-1]){
            negSum += (a[i] - a[i-1]);
        }else{
            posSum += (a[i] - a[i-1]);
        }
    }

    let getMaxB = (maxC) => {
        let minB = a[0] - maxC;
        return posSum + minB;
    }

    console.log(Math.ceil((a[0] + posSum) / 2));
    let q = pi(readline());
    while(q > 0){
        q--;
        let [l, r, x] = ti(readline().split(' '));

        if(l-1 !== 0){
            if(a[l-2] > a[l-1]){
                negSum -= (a[l-1]-a[l-2]);
            }else{
                posSum -= (a[l-1]-a[l-2]);
            }
            a[l-1] += x;
            if(a[l-2] > a[l-1]){
                negSum += (a[l-1]-a[l-2]);
            }else{
                posSum += (a[l-1]-a[l-2]);
            }
        }

        if(r < n){
            if(a[r-1] > a[r]){
                negSum -= (a[r]-a[r-1]);
            }else{
                posSum -= (a[r]-a[r-1]);
            }
            a[r-1] += x;
            if(a[r-1] > a[r]){
                negSum += (a[r]-a[r-1]);
            }else{
                posSum += (a[r]-a[r-1]);
            }
        }

        console.log(Math.ceil((a[0] + posSum) / 2));
    }
}


Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:  Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end.  Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers. Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday!

The first line contains integer n (1 ≤ n ≤ 105) — the number of Inna's commands. Then n lines follow, describing Inna's commands. Each line consists an integer:   Integer a (1 ≤ a ≤ 105) means that Inna gives Dima a command to add number a into one of containers.  Integer 0 shows that Inna asks Dima to make at most three extractions from different containers.

Each command of the input must correspond to one line of the output — Dima's action. For the command of the first type (adding) print one word that corresponds to Dima's choice:   pushStack — add to the end of the stack;  pushQueue — add to the end of the queue;  pushFront — add to the beginning of the deck;  pushBack — add to the end of the deck.  For a command of the second type first print an integer k (0 ≤ k ≤ 3), that shows the number of extract operations, then print k words separated by space. The words can be:   popStack — extract from the end of the stack;  popQueue — extract from the beginning of the line;  popFront — extract from the beginning from the deck;  popBack — extract from the end of the deck.  The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers. The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them.

0
pushStack
1 popStack
pushStack
pushQueue
2 popStack popQueue
pushStack
pushQueue
pushFront
3 popStack popQueue popFront

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i]);
	};
	return tokens;
}

function maximizeSum(commands) {
	var numbers = [];
	for (var i = 0; i < commands.length; ++i) {
		if (commands[i] != 0) {
			numbers.push(commands[i]);
			continue;
		}
		if (numbers.length == 0) {
			print(0);
		}
		else if (numbers.length == 1) {
			print('pushStack');
			print('1 popStack');
		}
		else if (numbers.length == 2) {
			print('pushStack');
			print('pushQueue');
			print('2 popStack popQueue');
		}
		else {
			var best = numbers.slice(0);
			best.sort(function(a, b) {
				return b-a;
			});
			best = best.slice(0, 3);
			best.contains = function(x) {
				return best.indexOf(x) != -1;
			};
			best.remove = function(x) {
				best.splice(best.indexOf(x), 1);
			}

			var pos = 0;
			function advance(message) {
				print(message);
				++pos;
			}

			while (!best.contains(numbers[pos])) {
				advance('pushStack');
			}
			best.remove(numbers[pos]);
			advance('pushQueue');

			while (!best.contains(numbers[pos])) {
				advance('pushStack');
			}
			best.remove(numbers[pos]);
			advance('pushStack');

			while (!best.contains(numbers[pos])) {
				advance('pushQueue');
			}
			best.remove(numbers[pos]);
			advance('pushFront');

			while (pos < numbers.length) {
				advance('pushBack');
			}
			print('3 popQueue popStack popFront');
		}
		numbers = [];
	}
	for (i = 0; i < numbers.length; ++i) {
		print('pushStack');
	}
}

function main() {
	var commandNum = parseInt(readline());
	var commands = [];
	for (var i = 0; i < commandNum; ++i) {
		commands.push(parseInt(readline()));
	}
	maximizeSum(commands);
}

main();

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i]);
	};
	return tokens;
}

function maximizeSum(commands) {
	var numbers = [];
	for (var i = 0; i < commands.length; ++i) {
		if (commands[i] != 0) {
			numbers.push(commands[i]);
		}
		else if (numbers.length == 0) {
			print(0);
		}
		else if (numbers.length < 3) {
			var words = [numbers.length, 'popStack'];
			print('pushStack');
			if (numbers.length == 2) {
				print('pushQueue');
				words.push('popQueue');
			}
			print(words.join(' '));
			numbers = [];
		}
		else {
			var best = numbers.slice(0);
			best.sort(function(a, b) {
				return a-b;
			});
			best = best.slice(0, 3);
			best.contains = function(x) {
				return best.indexOf(x) != -1;
			};
			var pos = 0;
			function advance(message) {
				print(message);
				++pos;
			}
			while (!best.contains(numbers[pos])) {
				advance('pushStack');
			}
			advance('pushQueue');
			while (!best.contains(numbers[pos])) {
				advance('pushStack');
			}
			advance('pushStack');
			while (!best.contains(numbers[pos])) {
				advance('pushQueue');
			}
			advance('pushFront');
			while (pos < numbers.length) {
				advance('pushBack');
			}
			print('3 popStack popQueue popFront');
			numbers = [];
		}
	}
}

function main() {
	var commandNum = parseInt(readline());
	var commands = [];
	for (var i = 0; i < commandNum; ++i) {
		commands.push(parseInt(readline()));
	}
	maximizeSum(commands);
}

main();

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i]);
	};
	return tokens;
}

function maximizeSum(commands) {
	var numbers = [];
	for (var i = 0; i < commands.length; ++i) {
		if (commands[i] != 0) {
			numbers.push(commands[i]);
		}
		else if (numbers.length == 0) {
			print(0);
		}
		else if (numbers.length < 3) {
			var words = [numbers.length, 'popStack'];
			print('pushStack');
			if (numbers.length == 2) {
				print('pushQueue');
				words.push('popQueue');
			}
			print(words.join(' '));
			numbers = [];
		}
		else {
			var best = numbers.slice(0);
			best.sort(function(a, b) {
				return a-b;
			});
			best = best.slice(0, 3);
			best.contains = function(x) {
				return best.indexOf(x) != -1;
			};
			var pos = 0;
			function advance(message) {
				print(message);
				++pos;
			}
			while (!best.contains(numbers[pos])) {
				advance('pushStack');
			}
			advance('pushQueue');
			while (!best.contains(numbers[pos])) {
				advance('pushStack');
			}
			advance('pushStack');
			while (!best.contains(numbers[pos])) {
				advance('pushQueue');
			}
			advance('pushFront');
			while (pos < numbers.length) {
				advance('pushBack');
			}
			print('3 popStack popQueue popFront');
			numbers = [];
		}
	}
	for (i = 0; i < numbers.length; ++i) {
		print('pushStack');
	}
}

function main() {
	var commandNum = parseInt(readline());
	var commands = [];
	for (var i = 0; i < commandNum; ++i) {
		commands.push(parseInt(readline()));
	}
	maximizeSum(commands);
}

main();

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i]);
	};
	return tokens;
}

function maximizeSum(commands) {
	var numbers = [];
	for (var i = 0; i < commands.length; ++i) {
		if (commands[i] != 0) {
			numbers.push(commands[i]);
			continue;
		}
		if (numbers.length == 0) {
			print(0);
		}
		else if (numbers.length == 1) {
			print('pushStack');
			print('1 popStack');
		}
		else if (numbers.length == 2) {
			print('pushStack');
			print('pushQueue');
			print('2 popStack popQueue');
		}
		else {
			var best = numbers.slice(0);
			best.sort(function(a, b) {
				return b-a;
			});
			best = best.slice(0, 3);
			best.contains = function(x) {
				return best.indexOf(x) != -1;
			};

			var pos = 0;
			function advance(message) {
				print(message);
				++pos;
			}

			while (!best.contains(numbers[pos])) {
				advance('pushStack');
			}
			advance('pushQueue');

			while (!best.contains(numbers[pos])) {
				advance('pushStack');
			}
			advance('pushStack');

			while (!best.contains(numbers[pos])) {
				advance('pushQueue');
			}
			advance('pushFront');

			while (pos < numbers.length) {
				advance('pushBack');
			}
			print('3 popQueue popStack popFront');
		}
		numbers = [];
	}
	for (i = 0; i < numbers.length; ++i) {
		print('pushStack');
	}
}

function main() {
	var commandNum = parseInt(readline());
	var commands = [];
	for (var i = 0; i < commandNum; ++i) {
		commands.push(parseInt(readline()));
	}
	maximizeSum(commands);
}

main();

function trim(s) {
	return s.replace(/^\s+|\s+$/gm, '');
}

function tokenize(s) {
	return trim(s).split(/\s+/);
}

function tokenizeIntegers(s) {
	var tokens = tokenize(s);
	for (var i = 0; i < tokens.length; i += 1) {
		tokens[i] = parseInt(tokens[i]);
	};
	return tokens;
}

function maximizeSum(commands) {
	var numbers = [];
	for (var i = 0; i < commands.length; ++i) {
		if (commands[i] != 0) {
			numbers.push(commands[i]);
		}
		else if (numbers.length == 0) {
			print(0);
		}
		else if (numbers.length < 3) {
			var words = [numbers.length, 'popStack'];
			print('pushStack');
			if (numbers.length == 2) {
				print('pushQueue');
				words.push('popQueue');
			}
			print(words.join(' '));
			numbers = [];
		}
		else {
			var best = numbers.slice(0);
			best.sort(function(a, b) {
				return b-a;
			});
			best = best.slice(0, 3);
			best.contains = function(x) {
				return best.indexOf(x) != -1;
			};
			var pos = 0;
			function advance(message) {
				print(message);
				++pos;
			}
			while (!best.contains(numbers[pos])) {
				advance('pushStack');
			}
			advance('pushQueue');
			while (!best.contains(numbers[pos])) {
				advance('pushStack');
			}
			advance('pushStack');
			while (!best.contains(numbers[pos])) {
				advance('pushQueue');
			}
			advance('pushFront');
			while (pos < numbers.length) {
				advance('pushBack');
			}
			print('3 popStack popQueue popFront');
			numbers = [];
		}
	}
	for (i = 0; i < numbers.length; ++i) {
		print('pushStack');
	}
}

function main() {
	var commandNum = parseInt(readline());
	var commands = [];
	for (var i = 0; i < commandNum; ++i) {
		commands.push(parseInt(readline()));
	}
	maximizeSum(commands);
}

main();


Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.This time Miroslav laid out $$$n$$$ skewers parallel to each other, and enumerated them with consecutive integers from $$$1$$$ to $$$n$$$ in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number $$$i$$$, it leads to turning $$$k$$$ closest skewers from each side of the skewer $$$i$$$, that is, skewers number $$$i - k$$$, $$$i - k + 1$$$, ..., $$$i - 1$$$, $$$i + 1$$$, ..., $$$i + k - 1$$$, $$$i + k$$$ (if they exist). For example, let $$$n = 6$$$ and $$$k = 1$$$. When Miroslav turns skewer number $$$3$$$, then skewers with numbers $$$2$$$, $$$3$$$, and $$$4$$$ will come up turned over. If after that he turns skewer number $$$1$$$, then skewers number $$$1$$$, $$$3$$$, and $$$4$$$ will be turned over, while skewer number $$$2$$$ will be in the initial position (because it is turned again).As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all $$$n$$$ skewers with the minimal possible number of actions. For example, for the above example $$$n = 6$$$ and $$$k = 1$$$, two turnings are sufficient: he can turn over skewers number $$$2$$$ and $$$5$$$.Help Miroslav turn over all $$$n$$$ skewers.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 1000$$$, $$$0 \leq k \leq 1000$$$) — the number of skewers and the number of skewers from each side that are turned in one step.

The first line should contain integer $$$l$$$ — the minimum number of actions needed by Miroslav to turn over all $$$n$$$ skewers. After than print $$$l$$$ integers from $$$1$$$ to $$$n$$$ denoting the number of the skewer that is to be turned over at the corresponding step.

NoteIn the first example the first operation turns over skewers $$$1$$$, $$$2$$$ and $$$3$$$, the second operation turns over skewers $$$4$$$, $$$5$$$, $$$6$$$ and $$$7$$$.In the second example it is also correct to turn over skewers $$$2$$$ and $$$5$$$, but turning skewers $$$2$$$ and $$$4$$$, or $$$1$$$ and $$$5$$$ are incorrect solutions because the skewer $$$3$$$ is in the initial state after these operations.

var x = readline().split(" ").map(x => Number(x));
var n = x[0];
var k = x[1];

var maxFlip = 2 * k + 1;

var ans = [];
if (n % maxFlip === 0) {
  for (var i = k + 1; i <= n; i += maxFlip) {
    ans.push(i);
  }
} else {
  var d = n % maxFlip;
  var f = Math.max(1, k + 1 - d / 2);
  for (var i = 1 + Math.floor(d / 2); i <= n; i += maxFlip) {
    ans.push(i);
  }
}
print(ans.length);
print(ans.join(' '));

/******/ (function(modules) { // webpackBootstrap
/******/ 	// The module cache
/******/ 	var installedModules = {};
/******/
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/
/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId]) {
/******/ 			return installedModules[moduleId].exports;
/******/ 		}
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			i: moduleId,
/******/ 			l: false,
/******/ 			exports: {}
/******/ 		};
/******/
/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ 		// Flag the module as loaded
/******/ 		module.l = true;
/******/
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/
/******/
/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;
/******/
/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;
/******/
/******/ 	// define getter function for harmony exports
/******/ 	__webpack_require__.d = function(exports, name, getter) {
/******/ 		if(!__webpack_require__.o(exports, name)) {
/******/ 			Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ 		}
/******/ 	};
/******/
/******/ 	// define __esModule on exports
/******/ 	__webpack_require__.r = function(exports) {
/******/ 		if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ 			Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ 		}
/******/ 		Object.defineProperty(exports, '__esModule', { value: true });
/******/ 	};
/******/
/******/ 	// create a fake namespace object
/******/ 	// mode & 1: value is a module id, require it
/******/ 	// mode & 2: merge all properties of value into the ns
/******/ 	// mode & 4: return value when already ns object
/******/ 	// mode & 8|1: behave like require
/******/ 	__webpack_require__.t = function(value, mode) {
/******/ 		if(mode & 1) value = __webpack_require__(value);
/******/ 		if(mode & 8) return value;
/******/ 		if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ 		var ns = Object.create(null);
/******/ 		__webpack_require__.r(ns);
/******/ 		Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ 		if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ 		return ns;
/******/ 	};
/******/
/******/ 	// getDefaultExport function for compatibility with non-harmony modules
/******/ 	__webpack_require__.n = function(module) {
/******/ 		var getter = module && module.__esModule ?
/******/ 			function getDefault() { return module['default']; } :
/******/ 			function getModuleExports() { return module; };
/******/ 		__webpack_require__.d(getter, 'a', getter);
/******/ 		return getter;
/******/ 	};
/******/
/******/ 	// Object.prototype.hasOwnProperty.call
/******/ 	__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "";
/******/
/******/
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = "./sources/hackerrank/algorithms/practice/strings/highest-value-palindrome-copy/index.js");
/******/ })
/************************************************************************/
/******/ ({

/***/ "./helpers/input-manager.js":
/*!**********************************!*\
  !*** ./helpers/input-manager.js ***!
  \**********************************/
/*! exports provided: InputManager */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "InputManager", function() { return InputManager; });
const fs = __webpack_require__(/*! fs */ "fs");

class InputManager {
    static getInput() {
        return new Promise((resolve) => {
            let input = '';

            if (fs.existsSync('./input.txt')) {
                input = fs.readFileSync('./input.txt').toString();
                resolve(this.parseInputString(input));
            } else {
                process.stdin.resume();
                process.stdin.setEncoding('utf-8');
                process.stdin.on('data', inputStdin => {
                    input += inputStdin;
                });

                process.stdin.on('end', () => {
                    resolve(this.parseInputString(input));
                });
            }
        });
    }

    static parseInputString(input) {
        return input.replace(/\s*$/, '')
            .split('\n')
            .map(str => str.replace(/\s*$/, ''));
    }
}

/***/ }),

/***/ "./helpers/solution-manager.js":
/*!*************************************!*\
  !*** ./helpers/solution-manager.js ***!
  \*************************************/
/*! exports provided: SolutionManager */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "SolutionManager", function() { return SolutionManager; });
class SolutionManager {
    static printSolution(solutionFn) {
        let result = solutionFn();
        if (typeof result !== 'undefined') {
            if (Array.isArray(result)) {
                result.forEach(v => console.log(v));
            } else {
                console.log(result);
            }
        }
    }
}

/***/ }),

/***/ "./sources/hackerrank/algorithms/practice/strings/highest-value-palindrome-copy/index.js":
/*!***********************************************************************************************!*\
  !*** ./sources/hackerrank/algorithms/practice/strings/highest-value-palindrome-copy/index.js ***!
  \***********************************************************************************************/
/*! no exports provided */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _helpers_solution_manager__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../../../../../helpers/solution-manager */ "./helpers/solution-manager.js");
/* harmony import */ var _helpers_input_manager__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../../../../../helpers/input-manager */ "./helpers/input-manager.js");

_helpers_input_manager__WEBPACK_IMPORTED_MODULE_1__["InputManager"].getInput().then((input) => _helpers_solution_manager__WEBPACK_IMPORTED_MODULE_0__["SolutionManager"].printSolution(() => {
    let [n, k] = input.shift().split(' ').map(v => Number(v));
    let maxTurn = k * 2 + 1;
    let remainder = n % maxTurn;

    let i = remainder === 0 || remainder > maxTurn / 2 ? k : 0;
    let moves = [i];
    while (i + maxTurn <= n) {
        i += maxTurn;

        if (i === n && k === 0) {
            break;
        }

        moves.push(i);
    }

    console.log(moves.length);
    console.log(moves.map(v => v + 1).join(' '));
}));

/***/ }),

/***/ "fs":
/*!*********************!*\
  !*** external "fs" ***!
  \*********************/
/*! no static exports found */
/***/ (function(module, exports) {

module.exports = require("fs");

/***/ })

/******/ });
//# sourceMappingURL=index.built.js.map

var x = readline().split(" ").map(x => Number(x));
var n = x[0];
var k = x[1];

var maxFlip = 2 * k + 1;

if (n <= maxFlip) {
  print(1);
  print(Math.min(n, k + 1));
} else {
  var d = n % maxFlip;
  var f = Math.max(1, Math.floor(k + 1 - d / 2));
  var ans = [];
  for (var i = f; i <= n; i += maxFlip) {
    ans.push(i);
  }
  print(ans.length);
  print(ans.join(' '));
}

var x = readline().split(" ").map(x => Number(x));
var n = x[0];
var k = x[1];

var maxFlip = 2 * k + 1;

if (n <= maxFlip) {
  print(1);
  print(Math.min(n, k + 1));
} else {
  var d = n % maxFlip;
  var f = Math.max(1, k + 1 - d / 2);
  var ans = [];
  for (var i = f; i <= n; i += maxFlip) {
    ans.push(i);
  }
  print(ans.length);
  print(ans.join(' '));
}

var x = readline().split(" ").map(x => Number(x));
var n = x[0];
var k = x[1];

var maxFlip = 2 * k + 1;

if (n <= maxFlip) {
  print(1);
  print(Math.min(n, k + 1));
} else {
  var d = n % maxFlip;
  var f = Math.max(1, k + 1 - d);
  var ans = [];
  for (var i = f; i < n; i += maxFlip) {
    ans.push(i);
  }
  print(ans.length);
  print(ans.join(' '));
}

var x = readline().split(" ").map(x => Number(x));
var n = x[0];
var k = x[1];

var maxFlip = 2 * k + 1;

if (n <= maxFlip) {
  print(1);
  print(Math.min(n, k + 1));
} else {
  var d = n % maxFlip;
  var f = Math.max(1, k + 1 - d);
  var ans = [];
  for (var i = f; i <= n; i += maxFlip) {
    ans.push(i);
  }
  print(ans.length);
  print(ans.join(' '));
}


Ksusha the Squirrel is standing at the beginning of a straight road, divided into n sectors. The sectors are numbered 1 to n, from left to right. Initially, Ksusha stands in sector 1. Ksusha wants to walk to the end of the road, that is, get to sector n. Unfortunately, there are some rocks on the road. We know that Ksusha hates rocks, so she doesn't want to stand in sectors that have rocks.Ksusha the squirrel keeps fit. She can jump from sector i to any of the sectors i + 1, i + 2, ..., i + k. Help Ksusha! Given the road description, say if she can reach the end of the road (note, she cannot stand on a rock)?

The first line contains two integers n and k (2 ≤ n ≤ 3·105, 1 ≤ k ≤ 3·105). The next line contains n characters — the description of the road: the i-th character equals ".", if the i-th sector contains no rocks. Otherwise, it equals "#". It is guaranteed that the first and the last characters equal ".".

Print "YES" (without the quotes) if Ksusha can reach the end of the road, otherwise print "NO" (without the quotes).

var input = readline().split(" ").map(x => parseInt(x));
var n = input[0];
var k = input[1];
var road = readline().split("");
var temp = 0;
var prev;
var output = 'YES';
for(var e of road){
    if(prev && prev === e && e=='#'){
        temp += 1;
    }else if(e=='#'){
        temp = 1;
    }
    if(temp === k){
        output = "NO";
        break;
    }
    prev = e;
}
print(output);

var input = readline().split(" ").map(x => parseInt(x));
var n = input[0];
var k = input[1];
var road = readline().split(" ");
var temp = 0;
var prev;
for(var e of road){
    if(prev && prv === e && e=='#'){
        temp += 1;
        if(temp === k){
            print("NO");
            break;
        }
    }else if(e=='#'){
        temp = 1;
    }
    prev = e;
}

print("YES");

var input = readline().split(" ").map(x => parseInt(x));
var n = input[0];
var k = input[1];
var road = readline().split(" ");
var temp = 0;
var prev;
for(var e of road){
    if(prev && prev === e && e=='#'){
        temp += 1;
        if(temp === k){
            print("NO");
            break;
        }
    }else if(e=='#'){
        temp = 1;
    }
    prev = e;
}

print("YES");

var input = readline().split(" ").map(x => parseInt(x));
var n = input[0];
var k = input[1];
var road = readline().split(" ");
var temp = 0;
var prev;
for(var e of road){
    if(prev && prev === e && e=='#'){
        temp += 1;
    }else if(e=='#'){
        temp = 1;
    }
    if(temp === k){
        print("NO");
        break;
    }
    prev = e;
}

print("YES");

var input = readline().split(" ").map(x => parseInt(x));
var n = input[0];
var k = input[1];
var road = readline().split(" ");
var temp = 0;
for(var e of road){
    if(e=='#'){
        temp += 1;
        if(temp === k){
            print("NO");
            break;
        }
    }
}

print("YES");


Keshi is throwing a party and he wants everybody in the party to be happy.He has $$$n$$$ friends. His $$$i$$$-th friend has $$$i$$$ dollars. If you invite the $$$i$$$-th friend to the party, he will be happy only if at most $$$a_i$$$ people in the party are strictly richer than him and at most $$$b_i$$$ people are strictly poorer than him.Keshi wants to invite as many people as possible. Find the maximum number of people he can invite to the party so that every invited person would be happy.

The first line contains a single integer $$$t$$$ $$$(1\le t\le 10^4)$$$ — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1\le n\le 2 \cdot 10^5)$$$ — the number of Keshi's friends. The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ $$$(0 \le a_i, b_i < n)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case print the maximum number of people Keshi can invite.

NoteIn the first test case, he invites the first and the second person. If he invites all of them, the third person won't be happy because there will be more than $$$1$$$ person poorer than him.

var dataitr;
var stdin_input = "";
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { dataitr = stdin_input.split('\n').values(); main(); });

const rl = () => dataitr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		const a = [], b = [];

		for (let i = 0; i < n; i++) {
			[a[i], b[i]] = rna();
		}

		function possible (x) {
			let cnt = 0;
			for (let i = 0; i < n; i++) {
				if (b[i] >= cnt && a[i] >= x - cnt - 1)
					cnt++;
			}
			return cnt >= x;
		}

		let low = 0, high = n + 1;
		while (high - low > 1) {
			//console.log({low, high})
			const mid = (low + high) >> 1;
			if (possible(mid)) {
				low = mid;
			} else {
				high = mid;
			}
			//console.log({mid})
		}

		const ans = low;

		console.log(ans);
	}
}