You are walking with your dog, and now you are at the promenade. The promenade can be represented as an infinite line. Initially, you are in the point $$$0$$$ with your dog. You decided to give some freedom to your dog, so you untied her and let her run for a while. Also, you watched what your dog is doing, so you have some writings about how she ran. During the $$$i$$$-th minute, the dog position changed from her previous position by the value $$$a_i$$$ (it means, that the dog ran for $$$a_i$$$ meters during the $$$i$$$-th minute). If $$$a_i$$$ is positive, the dog ran $$$a_i$$$ meters to the right, otherwise (if $$$a_i$$$ is negative) she ran $$$a_i$$$ meters to the left.During some minutes, you were chatting with your friend, so you don't have writings about your dog movement during these minutes. These values $$$a_i$$$ equal zero.You want your dog to return to you after the end of the walk, so the destination point of the dog after $$$n$$$ minutes should be $$$0$$$.Now you are wondering: what is the maximum possible number of different integer points of the line your dog could visit on her way, if you replace every $$$0$$$ with some integer from $$$-k$$$ to $$$k$$$ (and your dog should return to $$$0$$$ after the walk)? The dog visits an integer point if she runs through that point or reaches in it at the end of any minute. Point $$$0$$$ is always visited by the dog, since she is initially there.If the dog cannot return to the point $$$0$$$ after $$$n$$$ minutes regardless of the integers you place, print -1.

The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 3000; 1 \le k \le 10^9$$$) — the number of minutes and the maximum possible speed of your dog during the minutes without records. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^9 \le a_i \le 10^9$$$), where $$$a_i$$$ is the number of meters your dog ran during the $$$i$$$-th minutes (to the left if $$$a_i$$$ is negative, to the right otherwise). If $$$a_i = 0$$$ then this value is unknown and can be replaced with any integer from the range $$$[-k; k]$$$.

If the dog cannot return to the point $$$0$$$ after $$$n$$$ minutes regardless of the set of integers you place, print -1. Otherwise, print one integer — the maximum number of different integer points your dog could visit if you fill all the unknown values optimally and the dog will return to the point $$$0$$$ at the end of the walk.

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = 1//+lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const [n, k] = lines[l++].trim().split(' ').map(Number)
        const arr = lines[l++].trim().split(' ').map(Number)
        output[i] = solve(n, k, arr)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, k, arr) {
    // let zero = 0
    // let sum = 0
    // arr.forEach(x => {
    //     if (!x) zero++
    //     sum += x
    // })
    // const count = Math.ceil(Math.abs(sum) / k)
    // if (zero < count) return -1
    // const more = zero - count
    // const d = k * Math.floor(more / 2)
    let ans = -1
    for (let i = 0; i < n; i++) {
        ans = Math.max(ans, helper(n, k, arr, i))
    }
    return ans
}
function helper(n, k, arr, off) {
    const suf = []
    for (let j = n - 1; j >= 0; j--) {
        const i = (n + j - off) % n
        const now = arr[i] ? -arr[i] : k
        suf[j] = j < n - 1 ? suf[j + 1] + now : now
    }
// console.log('suf', suf)
    let sum = 0
    let ans = -Infinity
    for (let j = 0; j < n; j++) {
        const i = (n + j - off) % n
        if (arr[i]) {
            sum += arr[i]
        } else {
            const now = Math.min(k, (j + 1 < n ? suf[j + 1] : 0) - sum)
            if (now < -k) return -1
    // console.log(now)
            sum += now
            if (sum < 0) return -1
        }
        ans = Math.max(ans, sum)
    }
    if (sum) return -1
    // console.log(off, ans, sum)
    return ans + 1
}

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = 1//+lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const [n, k] = lines[l++].trim().split(' ').map(Number)
        const arr = lines[l++].trim().split(' ').map(Number)
        output[i] = solve(n, k, arr)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, k, arr) {
    // let zero = 0
    // let sum = 0
    // arr.forEach(x => {
    //     if (!x) zero++
    //     sum += x
    // })
    // const count = Math.ceil(Math.abs(sum) / k)
    // if (zero < count) return -1
    // const more = zero - count
    // const d = k * Math.floor(more / 2)
    let ans = -1
    for (let i = 0; i < n; i++) {
        ans = Math.max(ans, helper(n, k, arr, i))
    }
    return ans
}
function helper(n, k, arr, off) {
    const suf = []
    for (let j = n - 1; j >= 0; j--) {
        const i = (n + j - off) % n
        const now = arr[i] ? -arr[i] : k
        suf[j] = j < n - 1 ? suf[j + 1] + now : now
    }
// console.log('suf', suf)
    let sum = 0
    let ans = -Infinity
    for (let j = 0; j < n; j++) {
        const i = (n + j - off) % n
        if (arr[i]) {
            sum += arr[i]
        } else {
            const now = Math.min(k, (j + 1 < n ? suf[j + 1] : 0) - sum)
            if (now < -k) return -1
    // console.log(now)
            sum += now
            if (sum < 0) return -1
        }
        ans = Math.max(ans, sum)
    }
    // console.log(off, ans, sum)
    return ans + 1
}


Hemose was shopping with his friends Samez, AhmedZ, AshrafEzz, TheSawan and O_E in Germany. As you know, Hemose and his friends are problem solvers, so they are very clever. Therefore, they will go to all discount markets in Germany.Hemose has an array of $$$n$$$ integers. He wants Samez to sort the array in the non-decreasing order. Since it would be a too easy problem for Samez, Hemose allows Samez to use only the following operation:Choose indices $$$i$$$ and $$$j$$$ such that $$$1 \le i, j \le n$$$, and $$$\lvert i - j \rvert \geq x$$$. Then, swap elements $$$a_i$$$ and $$$a_j$$$.Can you tell Samez if there's a way to sort the array in the non-decreasing order by using the operation written above some finite number of times (possibly $$$0$$$)?

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 10^5)$$$. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ $$$(1 \leq x \leq n \leq 10^5)$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

For each test case, you should output a single string.  If Samez can sort the array in non-decreasing order using the operation written above, output "YES" (without quotes). Otherwise, output "NO" (without quotes). You can print each letter of "YES" and "NO" in any case (upper or lower).

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--) {
		let [n, x] = rna();
		let a = rna();
		let j = x - 1, i = n - x;

		let b = a.slice();
		b.sort((x, y) => x - y);

		let ans = true;
		for (let k = i; k <= j; k++) {
			ans = ans && a[k] == b[k];
		}

		console.log(ans ? 'YES' : 'NO');
	}
}

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
const solve = () => {
  let t = parseInt(readline());
  while (t--) {
    let [n, x] = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));
    let arr = readline()
      .trim()
      .split(" ")
      .map((cur) => parseInt(cur));

    let ans = true;
    for (let i = 0; i < n - 1; i++) {
      if (arr[i] > arr[i + 1]) {
        ans = false;
        break;
      }
    }
    if (ans) console.log("YES");
    else {
      ans = true;
      let narr = [...arr];
      narr.sort((a, b) => a - b);
      let bool = new Array(n).fill(false);
      for (let i = 0; i < n; i++) {
        if (!bool[i]) {
          for (let j = i + x; j < n; j++) {
            if (bool[j] === true) {
              bool[i] = true;
              break;
            }
            if (j - i >= x) {
              bool[i] = true;
              bool[j] = true;
            }
          }
        }
      }
      for (let i = 0; i < n; i++) {
        if (!bool[i]) {
          if (narr[i] !== arr[i]) {
            ans = false;
            break;
          }
        }
      }
      if (ans) console.log("YES");
      else console.log("NO");
    }
  }
};
solve();

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    for (let i = 0; i < t; i++) {
        const [n, x] = lines[l++].trim().split(' ').map(Number)
        const arr = lines[l++].trim().split(' ').map(Number)
        console.log(solve(n, x, arr))
    }
// })()
})

function solve(n, x, arr) {
    const sorted = arr.slice().sort((a, b) => a - b)
    for (let i = 0; i < arr.length; i++) {
        if (i + x >= n && i - x < 0 && arr[i] !== sorted[i]) {
            return 'NO'
        }
    }
    return 'YES'
}

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();
	let cnt = 0;
	while (t--) {
		let [n, x] = rna();
		let a = rna();

		cnt ++;
		if (cnt == 48) {
			console.log(n, x) ;
			console.log(a);
		}
		const k = n - x;

		if (2 * k >= n) {
			console.log('YES');
			continue;
		}

		let sorted = true;
		for (let i = k + 1; sorted && i < n - k; i++) {
			if (a[i] < a[i - 1]) sorted = false;
		}
		if (!sorted) {
			console.log('NO');
			continue;
		}

		let lcnt = 0, l = a[k];
		for (let i = 0; i < k; i++) {
			if (a[i] <= l) lcnt++;
			if (a[n-1-i] <= l) lcnt++;
		}

		ans = lcnt == k ? 'YES' : 'NO';
		console.log(ans);
	}
}

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--) {
		let [n, x] = rna();
		let a = rna();

		const k = n - x;

		if (2 * k >= n) {
			console.log('YES');
			continue;
		}

		let sorted = true;
		for (let i = k + 1; sorted && i < n - k;  i++) {
			if (a[i] < a[i - 1]) sorted = false;
		}
		if (!sorted) {
			console.log('NO');
			continue;
		}

		let lcnt = 0, l = a[k];
		for (let i = 0; i < k; i++) {
			if (a[i] <= l) lcnt++;
			if (a[n-1-i] <= l) lcnt++;
		}

		ans = lcnt == k ? 'YES' : 'NO';
		console.log(ans);
	}
}


You are given an undirected tree of $$$n$$$ vertices. Some vertices are colored blue, some are colored red and some are uncolored. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.You choose an edge and remove it from the tree. Tree falls apart into two connected components. Let's call an edge nice if neither of the resulting components contain vertices of both red and blue colors.How many nice edges are there in the given tree?

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the number of vertices in the tree. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 2$$$) — the colors of the vertices. $$$a_i = 1$$$ means that vertex $$$i$$$ is colored red, $$$a_i = 2$$$ means that vertex $$$i$$$ is colored blue and $$$a_i = 0$$$ means that vertex $$$i$$$ is uncolored. The $$$i$$$-th of the next $$$n - 1$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 \le v_i, u_i \le n$$$, $$$v_i \ne u_i$$$) — the edges of the tree. It is guaranteed that the given edges form a tree. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.

Print a single integer — the number of nice edges in the given tree.

NoteHere is the tree from the first example:  The only nice edge is edge $$$(2, 4)$$$. Removing it makes the tree fall apart into components $$$\{4\}$$$ and $$$\{1, 2, 3, 5\}$$$. The first component only includes a red vertex and the second component includes blue vertices and uncolored vertices.Here is the tree from the second example:  Every edge is nice in it.Here is the tree from the third example:  Edge $$$(1, 3)$$$ splits the into components $$$\{1\}$$$ and $$$\{3, 2\}$$$, the latter one includes both red and blue vertex, thus the edge isn't nice. Edge $$$(2, 3)$$$ splits the into components $$$\{1, 3\}$$$ and $$$\{2\}$$$, the former one includes both red and blue vertex, thus the edge also isn't nice. So the answer is 0.

var nums = readline().split(" ").map(function(x) { return parseInt(x); });
var n = nums[0];
var AAA = readline().split(" ").map(function(x) { return parseInt(x); });

var init_cnt = Array(3).fill(0);
for (var i=0; i<n; i++) init_cnt[AAA[i]]++;

var ms = Array(n).fill([]);
for (var i=0; i<n; i++) {
    var t = [];
    ms[i] = t;
}
var AA = [], BB = [];
for (var i=0; i<n-1; i++) {
	var qq = readline().split(" ").map(function(x) { return parseInt(x); });
	var a = qq[0], b = qq[1];
	a--; b--;
	ms[a].push(b);
	ms[b].push(a);
	AA.push(a);
	BB.push(b);
}

var sv = Array(n).fill(0);
var nnew = Array(n).fill(0);
var cnt = Array(n).fill([]);
for (var i=0; i<n; i++) {
	var t = Array(3).fill(0);
	cnt[i] = t;
}

var st = [];
st.push(0);
while (st.length > 0) {
	var v = st[st.length-1];
	nnew[v] = 1;
	var fnd = 0;
	for (var i=sv[v]; i<ms[v].length; i++)
		if (!nnew[ms[v][i]]) {
			sv[v] = i+1;
			st.push(ms[v][i]);
			fnd = 1;
			break;
		}
	if (fnd === 1) continue;
	sv[v] = ms[v].length;
	for (var i=0; i<ms[v].length; i++)
		for (var j=0; j<3; j++)
			cnt[v][j] += cnt[ms[v][i]][j];
	cnt[v][AAA[v]]++;
	st.pop();
}

// write(init_cnt,"\n");
// for (var i=0; i<n; i++) {
// 	write("V ",i," ");
// 	write(cnt[i],"\n");
// }

var ans = 0;
var used = Array(n).fill(0);
for (var i=0; i<n-1; i++) {
	var a = AA[i];
	var b = BB[i];

	var fnd = 0, sub = 0;
	if ((cnt[a][2] === 0 && cnt[a][1] === init_cnt[1]) ||
	    (cnt[a][1] === 0 && cnt[a][2] === init_cnt[2])) {
		fnd = 1;
		sub = a;
	}
	if (fnd === 1 && !used[sub]) {
		used[sub] = 1;
		ans++;
	}

	if ((cnt[b][2] === 0 && cnt[b][1] === init_cnt[1]) ||
	    (cnt[b][1] === 0 && cnt[b][2] === init_cnt[2])) {
		fnd = 1;
		sub = b;
	}
	if (fnd === 1 && !used[sub]) {
		used[sub] = 1;
		ans++;
	}
}

// if (n === 300000 && ans > 0) {
// 	write("init_cnt = ",init_cnt,"\n");
// 	write("sizes > 2");
// 	for (var i=0; i<n; i++)
// 		if (ms[i].length > 2)
// 			write(ms[i].length," ");
// 	for (var i=0; i<n-1; i++) {
// 		var a = AA[i];
// 		var b = BB[i];

// 		if ((cnt[a][2] === 0 && cnt[a][1] === init_cnt[1]) ||
// 		    (cnt[a][1] === 0 && cnt[a][2] === init_cnt[2]) ||
// 		    (cnt[b][2] === 0 && cnt[b][1] === init_cnt[1]) ||
// 		    (cnt[b][1] === 0 && cnt[b][2] === init_cnt[2]))
// 			write(" CNT_OK ", a," ",b," cnt_a = ", cnt[a],"[",AAA[a],"] cnt_b = ", cnt[b],"[",AAA[b],"]");
// 	}
// }

write(ans);

var nums = readline().split(" ").map(function(x) { return parseInt(x); });
var n = nums[0];
var AAA = readline().split(" ").map(function(x) { return parseInt(x); });

var init_cnt = Array(3).fill(0);
for (var i=0; i<n; i++) init_cnt[AAA[i]]++;

var e = Array(n).fill([]);
for (var i=0; i<n; i++) {
    var t = [];
    e[i] = t;
}
var AA = [], BB = [];
for (var i=0; i<n-1; i++) {
	var qq = readline().split(" ").map(function(x) { return parseInt(x); });
	var a = qq[0], b = qq[1];
	a--; b--;
	e[a].push(b);
	e[b].push(a);
	AA.push(a);
	BB.push(b);
}

var sv = Array(n).fill(0);   // saved positions of list search on "dfs" next call
var nnew = Array(n).fill(0);
var cnt = Array(n).fill([]);   // recalc answer array for each vertex
for (var i=0; i<n; i++) {
	var t = Array(3).fill(0);
	cnt[i] = t;
}
var st = [];   // stack
st.push(0);
while (st.length > 0) {
	var v = st[st.length-1];
	nnew[v] = 1;
	var fnd = 0;
	for (var i=sv[v]; i<e[v].length; i++)
		if (!nnew[e[v][i]]) {
			sv[v] = i+1;
			st.push(e[v][i]);
			fnd = 1;
			break;
		}
	if (fnd === 1) continue;
	sv[v] = e[v].length;
	for (var i=0; i<e[v].length; i++)
		for (var j=0; j<3; j++)
			cnt[v][j] += cnt[e[v][i]][j];
	cnt[v][AAA[v]]++;
	st.pop();
}

var ans = 0;
var used = Array(n).fill(0);
for (var i=0; i<n-1; i++) {
	var a = AA[i];
	var b = BB[i];

	var fnd = 0, sub = 0;
	if ((cnt[a][2] === 0 && cnt[a][1] === init_cnt[1]) ||
	    (cnt[a][1] === 0 && cnt[a][2] === init_cnt[2])) {
		fnd = 1;
		sub = a;
	}
	if (fnd === 1 && !used[sub]) {
		used[sub] = 1;
		ans++;
	}

	if ((cnt[b][2] === 0 && cnt[b][1] === init_cnt[1]) ||
	    (cnt[b][1] === 0 && cnt[b][2] === init_cnt[2])) {
		fnd = 1;
		sub = b;
	}
	if (fnd === 1 && !used[sub]) {
		used[sub] = 1;
		ans++;
	}
}

write(ans);

var nums = readline().split(" ").map(function(x) { return parseInt(x); });
var n = nums[0];
var AAA = readline().split(" ").map(function(x) { return parseInt(x); });

var init_cnt = Array(3).fill(0);
for (var i=0; i<n; i++) init_cnt[AAA[i]]++;

var ms = Array(n).fill([]);
for (var i=0; i<n; i++) {
    var t = [];
    ms[i] = t;
}
var AA = [], BB = [];
for (var i=0; i<n-1; i++) {
	var qq = readline().split(" ").map(function(x) { return parseInt(x); });
	var a = qq[0], b = qq[1];
	a--; b--;
	ms[a].push(b);
	ms[b].push(a);
	AA.push(a);
	BB.push(b);
}

var sv = Array(n).fill(0);
var nnew = Array(n).fill(0);
var cnt = Array(n).fill([]);
for (var i=0; i<n; i++) {
	var t = Array(3).fill(0);
	cnt[i] = t;
}

function getRandomInt(min, max) {
   min = Math.ceil(min);
   max = Math.floor(max);
   return Math.floor(Math.random() * (max - min + 1)) + min;
}

var st = [];
st.push(getRandomInt(0,n-1));
while (st.length > 0) {
	var v = st[st.length-1];
	nnew[v] = 1;
	var fnd = 0;
	for (var i=sv[v]; i<ms[v].length; i++)
		if (!nnew[ms[v][i]]) {
			sv[v] = i+1;
			st.push(ms[v][i]);
			fnd = 1;
			break;
		}
	if (fnd === 1) continue;
	sv[v] = ms[v].length + 1;
	for (var i=0; i<ms[v].length; i++)
		for (var j=0; j<3; j++)
			cnt[v][j] += cnt[ms[v][i]][j];
	cnt[v][AAA[v]]++;
	st.pop();
}

var ans = 0;
for (var i=0; i<n-1; i++) {
	var a = AA[i];
	var b = BB[i];

	if ((cnt[a][2] === 0 && cnt[a][1] === init_cnt[1]) ||
	    (cnt[a][1] === 0 && cnt[a][2] === init_cnt[2]) ||
	    (cnt[b][2] === 0 && cnt[b][1] === init_cnt[1]) ||
	    (cnt[b][1] === 0 && cnt[b][2] === init_cnt[2]))
		ans++;
}

write(ans);

var nums = readline().split(" ").map(function(x) { return parseInt(x); });
var n = nums[0];
var AAA = readline().split(" ").map(function(x) { return parseInt(x); });

var init_cnt = Array(3).fill(0);
for (var i=0; i<n; i++) init_cnt[AAA[i]]++;

var ms = Array(n).fill([]);
for (var i=0; i<n; i++) {
    var t = [];
    ms[i] = t;
}
var AA = [], BB = [];
for (var i=0; i<n-1; i++) {
	var qq = readline().split(" ").map(function(x) { return parseInt(x); });
	var a = qq[0], b = qq[1];
	a--; b--;
	ms[a].push(b);
	ms[b].push(a);
	AA.push(a);
	BB.push(b);
	//write("edge ",a," ",b,"\n");
}

//for (var i=0; i<n; i++) {
    //write(" vertex ",i,"  ");
    //for (var j=0; j<ms[i].length; j++) write(ms[i][j]," ");
    //write("\n");
//}

var sv = Array(n).fill(0);
var nnew = Array(n).fill(0);
var cnt = Array(n).fill([]);
for (var i=0; i<n; i++) {
	var t = Array(3).fill(0);
	cnt[i] = t;
}

var st = [];
st.push(0);
while (st.length > 0) {
//write("stk = ",st,"\n");
	var v = st[st.length-1];
	nnew[v] = 1;
	var fnd = 0;
	for (var i=sv[v]; i<ms[v].length; i++)
		if (!nnew[ms[v][i]]) {
		    //write("goto ",v," ",ms[v][i]);
			sv[v] = i+1;
			st.push(ms[v][i]);
			fnd = 1;
			break;
		}
	if (fnd === 1) continue;
	sv[v] = ms[v].length + 1;
	for (var i=0; i<ms[v].length; i++)
		for (var j=0; j<3; j++)
			cnt[v][j] += cnt[ms[v][i]][j];
	cnt[v][AAA[v]]++;
	//write(" ad to ", v, " ", a[v]);
	st.pop();
}

//write(init_cnt);
//write("\n\n");
//for (var i=0; i<n; i++) write(cnt[i],"\n");

var ans = 0;
for (var i=0; i<n-1; i++) {
	var a = AA[i];
	var b = BB[i];

	if ((cnt[a][2] === 0 && cnt[a][1] === init_cnt[1]) ||
	   (cnt[a][1] === 0 && cnt[a][2] === init_cnt[2]) ||
	   (cnt[b][2] === 0 && cnt[b][1] === init_cnt[1]) ||
	   (cnt[b][1] === 0 && cnt[b][2] === init_cnt[2]))
		ans++;
}

write(ans);

var nums = readline().split(" ").map(function(x) { return parseInt(x); });
var n = nums[0];
var AAA = readline().split(" ").map(function(x) { return parseInt(x); });

var init_cnt = Array(3).fill(0);
for (var i=0; i<n; i++) init_cnt[AAA[i]]++;

var ms = Array(n).fill([]);
for (var i=0; i<n; i++) {
    var t = [];
    ms[i] = t;
}
var AA = [], BB = [];
for (var i=0; i<n-1; i++) {
	var qq = readline().split(" ").map(function(x) { return parseInt(x); });
	var a = qq[0], b = qq[1];
	a--; b--;
	ms[a].push(b);
	ms[b].push(a);
	AA.push(a);
	BB.push(b);
}

var sv = Array(n).fill(0);
var nnew = Array(n).fill(0);
var cnt = Array(n).fill([]);
for (var i=0; i<n; i++) {
	var t = Array(3).fill(0);
	cnt[i] = t;
}

var st = [];
st.push(0);
while (st.length > 0) {
	var v = st[st.length-1];
	nnew[v] = 1;
	var fnd = 0;
	for (var i=sv[v]; i<ms[v].length; i++)
		if (!nnew[ms[v][i]]) {
			sv[v] = i+1;
			st.push(ms[v][i]);
			fnd = 1;
			break;
		}
	if (fnd === 1) continue;
	sv[v] = ms[v].length;
	for (var i=0; i<ms[v].length; i++)
		for (var j=0; j<3; j++)
			cnt[v][j] += cnt[ms[v][i]][j];
	cnt[v][AAA[v]]++;
	st.pop();
}

// write(init_cnt,"\n");
// for (var i=0; i<n; i++) {
// 	write("V ",i," ");
// 	write(cnt[i],"\n");
// }

var ans = 0;
for (var i=0; i<n-1; i++) {
	var a = AA[i];
	var b = BB[i];

	if ((cnt[a][2] === 0 && cnt[a][1] === init_cnt[1]) ||
	    (cnt[a][1] === 0 && cnt[a][2] === init_cnt[2]) ||
	    (cnt[b][2] === 0 && cnt[b][1] === init_cnt[1]) ||
	    (cnt[b][1] === 0 && cnt[b][2] === init_cnt[2]))
		ans++;
}

if (n === 300000 && ans > 0) {
	write("init_cnt = ",init_cnt,"\n");
	write("sizes > 2");
	for (var i=0; i<n; i++)
		if (ms[i].length > 2)
			write(ms[i].length," ");
	for (var i=0; i<n-1; i++) {
		var a = AA[i];
		var b = BB[i];

		if ((cnt[a][2] === 0 && cnt[a][1] === init_cnt[1]) ||
		    (cnt[a][1] === 0 && cnt[a][2] === init_cnt[2]) ||
		    (cnt[b][2] === 0 && cnt[b][1] === init_cnt[1]) ||
		    (cnt[b][1] === 0 && cnt[b][2] === init_cnt[2]))
			write(" CNT_OK ", a," ",b," cnt_a = ", cnt[a], " cnt_b = ", cnt[b]);
	}
}

write(ans);

var nums = readline().split(" ").map(function(x) { return parseInt(x); });
var n = nums[0];
var AAA = readline().split(" ").map(function(x) { return parseInt(x); });

var init_cnt = Array(3).fill(0);
for (var i=0; i<n; i++) init_cnt[AAA[i]]++;

var ms = Array(n).fill([]);
for (var i=0; i<n; i++) {
    var t = [];
    ms[i] = t;
}
var AA = [], BB = [];
for (var i=0; i<n-1; i++) {
	var qq = readline().split(" ").map(function(x) { return parseInt(x); });
	var a = qq[0], b = qq[1];
	a--; b--;
	ms[a].push(b);
	ms[b].push(a);
	AA.push(a);
	BB.push(b);
}

var sv = Array(n).fill(0);
var nnew = Array(n).fill(0);
var cnt = Array(n).fill([]);
for (var i=0; i<n; i++) {
	var t = Array(3).fill(0);
	cnt[i] = t;
}

var st = [];
st.push(0);
while (st.length > 0) {
	var v = st[st.length-1];
	nnew[v] = 1;
	var fnd = 0;
	for (var i=sv[v]; i<ms[v].length; i++)
		if (!nnew[ms[v][i]]) {
			sv[v] = i+1;
			st.push(ms[v][i]);
			fnd = 1;
			break;
		}
	if (fnd === 1) continue;
	sv[v] = ms[v].length;
	for (var i=0; i<ms[v].length; i++)
		for (var j=0; j<3; j++)
			cnt[v][j] += cnt[ms[v][i]][j];
	cnt[v][AAA[v]]++;
	st.pop();
}

// write(init_cnt,"\n");
// for (var i=0; i<n; i++) {
// 	write("V ",i," ");
// 	write(cnt[i],"\n");
// }

var ans = 0;
for (var i=0; i<n-1; i++) {
	var a = AA[i];
	var b = BB[i];

	if ((cnt[a][2] === 0 && cnt[a][1] === init_cnt[1]) ||
	    (cnt[a][1] === 0 && cnt[a][2] === init_cnt[2]) ||
	    (cnt[b][2] === 0 && cnt[b][1] === init_cnt[1]) ||
	    (cnt[b][1] === 0 && cnt[b][2] === init_cnt[2]))
		ans++;
}

if (n === 300000 && ans > 0) {
	write("init_cnt = ",init_cnt,"\n");
	write("sizes > 2");
	for (var i=0; i<n; i++)
		if (ms[i].length > 2)
			write(ms[i].length," ");
	for (var i=0; i<n-1; i++) {
		var a = AA[i];
		var b = BB[i];

		if ((cnt[a][2] === 0 && cnt[a][1] === init_cnt[1]) ||
		    (cnt[a][1] === 0 && cnt[a][2] === init_cnt[2]) ||
		    (cnt[b][2] === 0 && cnt[b][1] === init_cnt[1]) ||
		    (cnt[b][1] === 0 && cnt[b][2] === init_cnt[2]))
			write(" CNT_OK ", a," ",b," cnt_a = ", cnt[a],"[",AAA[a],"] cnt_b = ", cnt[b],"[",AAA[b],"]");
	}
}

write(ans);