Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly $$$1$$$ problem, during the second day — exactly $$$2$$$ problems, during the third day — exactly $$$3$$$ problems, and so on. During the $$$k$$$-th day he should solve $$$k$$$ problems.Polycarp has a list of $$$n$$$ contests, the $$$i$$$-th contest consists of $$$a_i$$$ problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly $$$k$$$ problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least $$$k$$$ problems that Polycarp didn't solve yet during the $$$k$$$-th day, then Polycarp stops his training.How many days Polycarp can train if he chooses the contests optimally?

The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of contests. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^5$$$) — the number of problems in the $$$i$$$-th contest.

Print one integer — the maximum number of days Polycarp can train if he chooses the contests optimally.

(function () {
    var read = {
        number: function () {
            return +readline();
        },
        arr: function (divider) {
            return readline().split(divider ? divider : '');
        },
        arrNumber: function (divider) {
            return readline()
                .split(divider ? divider : '')
                .map(item => +item);
        }
    }

    var n = read.number();
    var a = read.arrNumber(' ').sort((a,b) => a-b);

    var res = 0;
    var day = 1;

    for(var i = 0 ;i < n; i++) {
          if(a[i] >= day) {
              day++;
          }
    }
    print(day - 1);
}());

var n = readline();
var a = readline().split(' ').map(Number);

function comp(a, b){
	if(a > b) return 1;
	if(a < b) return -1;
	return 0;
}

var now = 1;
var k = 0;
a.sort(comp);
for (var i = 0; i < a.length; i++) {
	if(a[i] >= now){
		k++;
		now++;
	}
}
var b = [];
for (var i = 0; i < 1e6; i++) {
	b.shift(i);
}
print(k);

var n = readline();
var a = readline().split(' ').map(Number);

var b = new Array(3213213321);

function comp(a, b){
	if(a > b) return 1;
	if(a < b) return -1;
	return 0;
}

var now = 1;
var k = 0;
a.sort(comp);
for (var i = 0; i < a.length; i++) {
	if(a[i] >= now){
		k++;
		now++;
	}
}
var b = [];
for (var i = 0; i < 1e6; i++) {
	b.shift(i);
}
print(k);

var n = readline();
var a = readline().split(' ').map(Number);

function comp(a, b){
	if(a > b) return 1;
	if(a < b) return -1;
	return 0;
}

var now = 1;
var k = 0;
a.sort(comp);
for (var i = 0; i < a.length; i++) {
	if(a[i] >= now){
		k++;
		now++;
	}
}
var b = [];
for (var i = 0; i < 1e5; i++) {
	b.shift(i);
}
print(k);

var n = readline();
var a = readline().split(' ').map(Number);
function comp(a, b){
	if(a > b) return 1;
	if(a < b) return -1;
	return 0;
}

var now = 1;
var k = 0;
a.sort(comp);
for (var i = 0; i < a.length; i++) {
	if(a[i] >= now){
		k++;
		now++;
	}
}
print(k);

let lineCount = 0

require('readline').createInterface({
  input: process.stdin,
}).on('line', (line) => {
  if (lineCount++ !== 1) {
    return
  }
  const nums = line.split(' ').map(s => parseInt(s))
  nums.sort((a, b) => a - b)
  let result = 0
  nums.forEach((num) => {
    if (num > result) {
      result++
    }
  })
  process.stdout.write(result + '\n')
  process.exit()
})

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

    start()
    {
        process.stdin.resume();
        process.stdin.setEncoding("utf-8");

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount + 1)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a == b ? 0 : ( a < b ? -1 : 1));

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

let fs =  require('fs');
let input = fs.readFileSync(0, 'ascii')
    .split('\n')
    .map(s => s.trim().split(" ").map(Number))
;

let [n, args] = input;
let days = 0;
args.sort((a,b) => a - b);

args.forEach(num => {
    if (days < num) days++;
});
console.log(days);

'use strict';

function main() {
  let n = parseInt(next()), i = 0;
  let ar = [];
  for(i = 0; i < n; i++) {
    ar.push(parseInt(next()));
  }
  //ar.sort(); this is wrong sorts alphabetically on literals
  ar.sort((a, b) => a-b); // sort using compare function to sort integers -ve means a comes first
  let cur = 0;
  for(i = 0; i < n; i++) {
    if(ar[i] > cur)
      cur++;
  }
  println(cur);
}

//process.stdin.resume();
process.stdin.setEncoding('ascii');

let input = '';
process.stdin.on('data', (chunk) => input += chunk);

// next: reads next value from input.
let inputCursor = 0;
let next = () => {
  return inputCursor < input.length ? input[inputCursor++] : '';
};

//print: print one or more space seperated values.
let print = (...data) => {
  for(let i = 0; i < data.length; i++) {
    process.stdout.write(data[i] + (i < data.length - 1 ? ' ' : ''));
  }
};

//println: print one or more space seperated values then a line feed.
let println = (...data) => {
  print(...data);
  print('\n');
};

//after the the input is read.
process.stdin.on('end', () => {
    input = input.trim().split(/[\s]+/);
    main();
});

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

    start()
    {
        process.stdin.resume();
        process.stdin.setEncoding("utf-8");

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        // const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a > b);

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

    start()
    {
        process.stdin.resume();
        process.stdin.setEncoding("utf-8");

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a <= b);

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

    start()
    {
        process.stdin.resume();
        process.stdin.setEncoding("utf-8");

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a > b);

        if(nCount > 1000)
        {
            console.log(arrValues);
        }

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

    start()
    {
        process.stdin.resume();
        process.stdin.setEncoding("utf-8");

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a <= b);

        let nIndex = 1;
        arrValues.forEach(nValue => {
            if(nValue >= nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

    start()
    {
        process.stdin.resume();
        process.stdin.setEncoding("utf-8");

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a > b);

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

    start()
    {
        process.stdin.resume();
        process.stdin.setEncoding("utf-8");

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        // const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a <= b);

        console.log(arrValues);
        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

    start()
    {
        process.stdin.resume();
        process.stdin.setEncoding("utf-8");

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a == b ? 0 : ( a < b ? -1 : 1));

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

    start()
    {
        process.stdin.resume();
        process.stdin.setEncoding("utf-8");

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a == b ? 0 : ( a < b ? -1 : 1));

        if(nCount > 1000)
        {
            console.log(arrValues);
        }

        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

class CodeForcesNodeJS
{
    constructor()
    {
        this.stdin_input = "";
        this.arrInput = [];
        this.nInputIndex = 0;
    }

    start()
    {
        process.stdin.resume();
        process.stdin.setEncoding("utf-8");

        process.stdin.on("data", this.onData.bind(this));
        process.stdin.on("end", this.solveWrapper.bind(this));
    }

    onData(input)
    {
        this.stdin_input += input;
    }

    prepareInput(input)
    {
        this.arrInput = input.replace(/\n/g, " ").split(" ");
        this.nInputIndex = 0;
    }

    nextInput(strConvertType = null)
    {
        let _response = this.arrInput[this.nInputIndex];
        ++this.nInputIndex;

        if(typeof _response === "undefined")
        {
            throw new Error("End of input reached.");
        }

        if(typeof _response !== "string")
        {
            throw new Error("Internal Error: input at current step not an instance of string!");
        }

        switch(strConvertType)
        {
            case "Integer":
                _response = parseInt(_response, 10);
                break;

            case null:
                break;

            default:
                throw new Error("Unrecognized type for conversion!");
        }

        return _response;

    }

    async solveWrapper()
    {
        this.prepareInput(this.stdin_input);
        await this.solve().catch(console.error);
    }

    solve()
    {
        const nCount = this.nextInput("Integer");
        const arrValues = this.arrInput
            .slice(1, nCount)
            .map(strValue => parseInt(strValue, 10))
            .sort((a, b) => a > b);

        console.log(arrValues);
        let nIndex = 0;
        arrValues.forEach(nValue => {
            if(nValue > nIndex)
            {
                ++nIndex;
            }
        });
        process.stdout.write(nIndex + '\n');
    }
}

// process.stdout.write("YES\n");
const _solver = new CodeForcesNodeJS();
_solver.start();

let fs =  require('fs');
let input =  fs.readFileSync(0, 'ascii')
    .split('\n')
    .map(s => s.trim().split(" ").map(Number))
;

let [n, args] = input;
let days = 1;
let index = 1;
let length = args.length;
args.sort();
while (index < length) {
    if (args[index] >= days + 1) {
        days++;
    };
    index++;
}

console.log(days)

let fs =  require('fs');
let input = fs.readFileSync(0, 'ascii')
    .split('\n')
    .map(s => s.trim().split(" ").map(Number))
;

let [n, args] = input;
let days = 0;
args.sort();

args.forEach(num => {
    if (days < num) days++;
});
console.log(days);

'use strict';

function main() {
  let n = parseInt(next()), i = 0;
  let st = new Set();
  for(i = 0; i < n; i++) {
    st.add(parseInt(next()));
  }
  println(st.size);
}

//process.stdin.resume();
process.stdin.setEncoding('ascii');

let input = '';
process.stdin.on('data', (chunk) => input += chunk);

// next: reads next value from input.
let inputCursor = 0;
let next = () => {
  return inputCursor < input.length ? input[inputCursor++] : '';
};

//print: print one or more space seperated values.
let print = (...data) => {
  for(let i = 0; i < data.length; i++) {
    process.stdout.write(data[i] + (i < data.length - 1 ? ' ' : ''));
  }
};

//println: print one or more space seperated values then a line feed.
let println = (...data) => {
  print(...data);
  print('\n');
};

//after the the input is read.
process.stdin.on('end', () => {
    input = input.trim().split(/[\s]+/);
    main();
});

'use strict';

function main() {
  let n = parseInt(next()), i = 0;
  let ar = [];
  for(i = 0; i < n; i++) {
    ar.push(parseInt(next()));
  }
  console.log(ar);
  //ar.sort(); this is wrong sorts alphabetically on literals
  ar.sort((a, b) => a-b); // sort using compare function to sort integers -ve means a comes first
  console.log(ar);

  let cur = 0;
  for(i = 0; i < n; i++) {
    if(ar[i] > cur)
      cur++;
  }
  println(cur);
}

//process.stdin.resume();
process.stdin.setEncoding('ascii');

let input = '';
process.stdin.on('data', (chunk) => input += chunk);

// next: reads next value from input.
let inputCursor = 0;
let next = () => {
  return inputCursor < input.length ? input[inputCursor++] : '';
};

//print: print one or more space seperated values.
let print = (...data) => {
  for(let i = 0; i < data.length; i++) {
    process.stdout.write(data[i] + (i < data.length - 1 ? ' ' : ''));
  }
};

//println: print one or more space seperated values then a line feed.
let println = (...data) => {
  print(...data);
  print('\n');
};

//after the the input is read.
process.stdin.on('end', () => {
    input = input.trim().split(/[\s]+/);
    main();
});

'use strict';

function main() {
  let n = parseInt(next()), i = 0;
  let ar = [];
  for(i = 0; i < n; i++) {
    ar.push(parseInt(next()));
  }
  ar.sort();
  let cur = 0;
  for(i = 0; i < n; i++) {
    if(ar[i] > cur)
      cur++;
  }
  println(cur);
}

//process.stdin.resume();
process.stdin.setEncoding('ascii');

let input = '';
process.stdin.on('data', (chunk) => input += chunk);

// next: reads next value from input.
let inputCursor = 0;
let next = () => {
  return inputCursor < input.length ? input[inputCursor++] : '';
};

//print: print one or more space seperated values.
let print = (...data) => {
  for(let i = 0; i < data.length; i++) {
    process.stdout.write(data[i] + (i < data.length - 1 ? ' ' : ''));
  }
};

//println: print one or more space seperated values then a line feed.
let println = (...data) => {
  print(...data);
  print('\n');
};

//after the the input is read.
process.stdin.on('end', () => {
    input = input.trim().split(/[\s]+/);
    main();
});


Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.There is a set $$$S$$$ containing very important numbers on his dad's desk. The minute Johnny heard about it, he decided that it's a good idea to choose a positive integer $$$k$$$ and replace each element $$$s$$$ of the set $$$S$$$ with $$$s \oplus k$$$ ($$$\oplus$$$ denotes the exclusive or operation). Help him choose such $$$k$$$ that Johnny's dad will not see any difference after his son is done playing (i.e. Johnny will get the same set as before playing). It is possible that no such number exists. It is also possible that there are many of them. In such a case, output the smallest one. Note that the order of elements in a set doesn't matter, i.e. set $$$\{1, 2, 3\}$$$ equals to set $$$\{2, 1, 3\}$$$.Formally, find the smallest positive integer $$$k$$$ such that $$$\{s \oplus k | s \in S\} = S$$$ or report that there is no such number.For example, if $$$S = \{1, 3, 4\}$$$ and $$$k = 2$$$, new set will be equal to $$$\{3, 1, 6\}$$$. If $$$S = \{0, 1, 2, 3\}$$$ and $$$k = 1$$$, after playing set will stay the same.

In the first line of input, there is a single integer $$$t$$$ ($$$1 \leq t \leq 1024$$$), the number of test cases. In the next lines, $$$t$$$ test cases follow. Each of them consists of two lines.  In the first line there is a single integer $$$n$$$ ($$$1 \leq n \leq 1024$$$) denoting the number of elements in set $$$S$$$. Second line consists of $$$n$$$ distinct integers $$$s_i$$$ ($$$0 \leq s_i < 1024$$$), elements of $$$S$$$. It is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$1024$$$.

Print $$$t$$$ lines; $$$i$$$-th line should contain the answer to the $$$i$$$-th test case, the minimal positive integer $$$k$$$ satisfying the conditions or $$$-1$$$ if no such $$$k$$$ exists.

NoteIn the first test case, the answer is $$$1$$$ because it is a minimum positive integer and it satisfies all the conditions.

// const bg = require('big-integer')
process.stdin.resume()
process.stdin.setEncoding('utf-8')
const MOD1 = 1000000007

function alpha(c = 'A') {
  return [...{
    _b: c === 'A' ? 65 : 97,
    _i: -1,
    [Symbol.iterator]() {
      return this
    },
    next() {
      this._i++
      if (this._i === 26) return {done: true}
      else return {done: false, value: String.fromCharCode(this._i + this._b)}
    }
  }]
}

class rc {
  static construct() {
    this.input = ''
    this.ptr = 0
  }
  static readString() {
    return this.input[this.ptr++]
  }

  static readStrings() {
    return this.input[this.ptr++].split(' ')
  }

  static readInt() {
    return +this.readString()
  }

  static readInts() {
    return this.readStrings().map(a => +a)
  }

  static readBigInteger() {
    return bg(this.readString())
  }

  static readBigIntegers() {
    return this.readStrings().map(a => bg(a))
  }
}

function wr(...x) {
  console.log(...x)
}

function sort(a, inc = true) {
  if (inc) a.sort((a, b) => a - b)
  else a.sort((a, b) => b - a)
}

rc.construct()
// bc.construct()
process.stdin.on('data', (i) => {
  rc.input += i
})

process.stdin.on('end', () => {
  rc.input = rc.input.split('\n').map(a => a.replace(/(\r\n|\n|\r)/gm, ''))
  main()
})

function main() {
  let t = rc.readInts()
  while(t--) {
    const n = rc.readInt()
    const a1 = rc.readInts()
    const a3 = new Array(n)
    sort(a1)
    let ans = -1
    for(let i = 1; i <= 1024; i++) {
      for(let j = 0; j < n; j++) {
        a3[j] = (a1[j] ^ i)
      }
      sort(a3)
      if(check(n, a1, a3)) {
        ans = i
        break
      }
    }
    wr(ans)
    function check(n, a1, a3) {
      for(let j = 0; j < n; j++) {
        if(a1[j] !== a3[j]) return  false
      }
      return true
    }
  }
}

//↓見なくていいよ！ここから--------------------------------------------------
var read = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
var obj;
var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
  obj = init(inLine);
  myerr("-----start-----");
  var start = new Date();
  Main();
  var end = new Date() - start;
  myerr("----- end -----");
  myerr("time : " + (end) + "ms");
  myerr("memory : " + Math.round(process.memoryUsage().heapTotal / 1024) + "KB");
});
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
  var returnObj = {
    list : input, index : 0, max : input.length,
    hasNext : function(){return (this.index < this.max);},
    next : function(){if(!this.hasNext()){throw "ArrayIndexOutOfBoundsException これ以上ないよ";}else{var returnInput = this.list[this.index];this.index++;return returnInput;}}
  };
  return returnObj;
}
function myout(s){console.log(s);}
function myerr(s){console.error("debug:" + require("util").inspect(s,false,null));}
//[no]要素の扱い。数値型
//不明値、異常時:引数そのまま返す 1:数値へ変換
//2:半角SPで分割 4:半角SPで分割し、数値配列へ
//6:1文字で分割  7:1文字で分割し、数値配列へ
//8:半角SPで結合 9:改行で結合 0:文字なしで結合
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(" ");case 4:return i.split(" ").map(Number);case 6:return i.split("");case 7:return i.split("").map(Number);case 8:return i.join(" ");case 9:return i.join("\n");case 0:return i.join("");default:return i;}}catch(e){return i;}}

//↑見なくていいよ！ここまで--------------------------------------------------
function Main(){
	var t = nextInt();
	var output = new Array(t);
	for(var i = 0; i < t; i++){
		var N = nextInt();
		var list = nextIntArray();
		var tmp = JSON.parse(JSON.stringify(list));
		var set = new Set(tmp);
		for(var j = 1; j <= 1024; j++){
			var isNO = false;
			for(var k = 0; k < N; k++){
				if(!set.has(list[k] ^ j)){
					isNO = true;
					break;
				}
			}
			if(isNO){
				continue;
			}else{
				output[i] = j;
				break;
			}
		}
		if(output[i] == null){
			output[i] = -1;
		}
	}
	myout(myconv(output,9));
}

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

    let stdinInput = '';

    process.stdin.on('data', input => {
      stdinInput += input;
    })

    process.stdin.on('end', () => {
      main(stdinInput);
    })

    function main(data) {
    data = data.split('\n');
    const testCases = data.shift() * 2;
    for (let i = 1; i < testCases; i += 2) {
        let obj = {};
        let arr = data[i].slice(0,-1).split(' ');
        for (let j = 0; j < arr.length; j += 1) obj[arr[j]] = 1;
        let j = 1;
        while (j < 1025) {
            let bit = true;
            for (let k = 0; k < arr.length; k += 1) {
                let xor = arr[k] ^ j;
                if (!obj[xor]) {
                    bit = false;
                    break;
                }
            }
            if (bit) {
                console.log(j);
                break;
            }
            j += 1;
        }
        if (j === 1025) console.log(-1);
    }
}

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

    let stdinInput = '';

    process.stdin.on('data', input => {
      stdinInput += input;
    })

    process.stdin.on('end', () => {
      main(stdinInput);
    })

    function main(data) {
    data = data.split('\n');
    const testCases = data.shift() * 2;
    for (let i = 1; i < testCases; i += 2) {
        // let numEle = data[i].split(' ')[1] * 1;
        let obj = {};
        let arr = data[i].split(' ');
        for (let j = 0; j < arr.length; j += 1) obj[arr[j]] = 1;
        let j = 1;
        while (j < 1025) {
            let bit = true;
            for (let k = 0; k < arr.length; k += 1) {
                let xor = arr[k] ^ j;
                if (!obj[xor]) {
                    bit = false;
                    break;
                }
            }
            if (bit) {
                console.log(j);
                break;
            }
            j += 1;
        }
        if (j === 1025) console.log(-1);
    }
}


All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket. The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has n discount coupons, the i-th of them can be used with products with ids ranging from li to ri, inclusive. Today Fedor wants to take exactly k coupons with him.Fedor wants to choose the k coupons in such a way that the number of such products x that all coupons can be used with this product x is as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor!

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 3·105) — the number of coupons Fedor has, and the number of coupons he wants to choose. Each of the next n lines contains two integers li and ri ( - 109 ≤ li ≤ ri ≤ 109) — the description of the i-th coupon. The coupons can be equal.

In the first line print single integer — the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted. In the second line print k distinct integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the ids of the coupons which Fedor should choose. If there are multiple answers, print any of them.

NoteIn the first example if we take the first two coupons then all the products with ids in range [40, 70] can be bought with both coupons. There are 31 products in total.In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example.

/*
    About the solution, Note that if there is any answer exists with size X,
                        There is also such an answer that it's right bound is
                        on the one of the end boundaries of one of the coupons.

                ( Suppose input boundaries are [Li..Ri] )
        Proof : Suppose you have found an answer with size X that lies on [P..Q] and Q is not
                on the one of the end boundaries(Ri's) of some of the coupons.
                You can always move [P..Q] forward untill Q become equal to one of the Ri's.
*/

var z;
var n , k;
var a = [];
var p = [];

function readArray()
{
    return readline().split( ' ' ).map( Number );
}

function check( sz , which )
{
    // special case
    if ( sz == 0 ) return { ok : true , left : 0 , right : 0 , whichOnes : undefined };

    // status of each boundary : 0=not seen , 1=compeletely in the current boundary , 2=out of ...
    var f = new Array( n );
    for ( var i=0 ; i<n ; i++ ) f[i] = 0;

    var j=0 , i=0;          // i is moving forward and j is keeping distance sz from it
    var cnt = 0 , ret = 0;
    var found = false;
    var end = -8e9;
    var whichOnes = [];

    while ( i<2*n )
    {
        // find a closing bracket ... just closing brackets matter for i
        while ( p[i].type == +1 ) i++;

        // j keeps distance SZ from i
        while ( (j<2*n) && (p[i].pos - p[j].pos + 1) >= sz )
        {
            var id = p[j].id;
            if ( p[j].type == +1 )
            {
                if ( f[id] == 0 ) f[id] = 1 , cnt++;
            }
            j++;
        }

        // update the answer
        if ( cnt > ret )
        {
            ret = cnt;
            if ( ret >= k && found == false )
            {
                if ( which )
                {
                    var c = 0;
                    for ( var l=0 ; l<n && c<k ; l++ )
                        if ( f[l] == 1 )
                        {
                            c++;
                            whichOnes.push( l+1 );
                        }
                }

                found = true;
            }
        }

        var id = p[i].id;
        if ( f[id] == 0 ) f[id] = 2;
        else if ( f[id] == 1 ) f[id] = 2 , cnt--;
        i++;
    }

    return { ok : found , right : end , left : end - sz + 1 , whichOnes : whichOnes };
}

function main()
{
    // get input and prepare it for solving the problem
    z = readArray();
    n = z[0] , k = z[1];
    for ( var i=0 ; i<n ; i++ ) a[i] = readArray();

    for ( var i=0 ; i<n ; i++ )
    {
        p.push( { pos:a[i][0] , type:+1 , id:i } );
        p.push( { pos:a[i][1] , type:-1 , id:i } );
    }
    p.sort( function( fi , se ) {
        if ( fi.pos != se.pos ) return fi.pos - se.pos;
        if ( fi.type != se.type ) return fi.type==+1 ? -1 : +1;
        return fi.id - se.id;
    } );

    // Binary Search on the size
    var l = 0 , r = 2e9 + 5;
    while ( l < r )
    {
        var mid = Math.floor( ( l + r ) / 2 );
        if ( check( mid , false ).ok == true ) l = mid+1;
        else r = mid;
    }

    // Prepare answer for output
    var ans = check( r-1 , true );
    if ( ans.whichOnes == undefined )
    {
        ans.whichOnes = []
        for ( var i=0 ; i<k ; i++ )
            ans.whichOnes.push( i+1 );
    }

    print( r-1 );
    // print( ans.left + " " + ans.right );
    ans.whichOnes.forEach( function(x) { write( x + " " ); } );
    print();

} main();

var z;
var n , k;
var a = [];
var p = [];

function readArray()
{
    return readline().split( ' ' ).map( Number );
}

function check( sz )
{
    if ( sz == 0 ) return { ok : true , left : 0 , right : 0 , whichOnes : undefined };

    var f = new Array( n );
    for ( var i=0 ; i<n ; i++ ) f[i] = 0;

    var j=0 , i=0;
    var cnt = 0 , ret = 0;
    var found = false;
    var end = -8e9;
    var whichOnes = [];

    while ( i<2*n )
    {
        // find a closing bracket
        while ( p[i].type == +1 ) i++;

        while ( (j<2*n) && (p[i].pos - p[j].pos + 1) >= sz )
        {
            var id = p[j].id;
            if ( p[j].type == +1 )
            {
                if ( f[id] == 0 ) f[id] = 1 , cnt++;
            }
            j++;
        }

        if ( cnt > ret )
        {
            ret = cnt;
            if ( ret >= k && found == false )
            {
                var c = 0;
                for ( var l=0 ; l<n && c<k ; l++ )
                    if ( f[l] == 1 )
                    {
                        c++;
                        whichOnes.push( l+1 );
                    }

                found = true;
            }
        }

        var id = p[i].id;
        if ( f[id] == 0 ) f[id] = 2;
        else if ( f[id] == 1 ) f[id] = 2 , cnt--;
        i++;
    }

    return { ok : found , right : end , left : end - sz + 1 , whichOnes : whichOnes };
}

function main()
{
    z = readArray();
    n = z[0] , k = z[1];
    for ( var i=0 ; i<n ; i++ ) a[i] = readArray();

    for ( var i=0 ; i<n ; i++ )
    {
        p.push( { pos:a[i][0] , type:+1 , id:i } );
        p.push( { pos:a[i][1] , type:-1 , id:i } );
    }
    p.sort( function( fi , se ) {
        if ( fi.pos != se.pos ) return fi.pos - se.pos;
        if ( fi.type != se.type ) return fi.type==+1 ? -1 : +1;
        return fi.id - se.id;
    } );

    /*
    print();
    p.forEach( function( z ) {
        print( z.pos + " , " + z.type + " , " + z.id );
    } );
    print();
    */

    var l = 0 , r = 2e9 + 5;
    while ( l < r )
    {
        var mid = Math.floor( ( l + r ) / 2 );
        //print( l + " " + r + " " + mid );
        if ( check( mid ).ok == true ) l = mid+1;
        else r = mid;
    }

    var ans = check( r-1 );
    if ( ans.whichOnes == undefined )
    {
        ans.whichOnes = []
        for ( var i=0 ; i<k ; i++ )
            ans.whichOnes.push( i+1 );
    }

    print( r-1 );
    // print( ans.left + " " + ans.right );
    ans.whichOnes.forEach( function(x) { write( x + " " ); } );
    print();

} main();

var z;
var n , k;
var a = [];
var p = [];

function readArray()
{
    return readline().split( ' ' ).map( Number );
}

function check( sz )
{
    if ( sz == 0 ) return { ok : true , left : 0 , right : 0 , whichOnes : undefined };

    var f = new Array( n );
    for ( var i=0 ; i<n ; i++ ) f[i] = 0;

    var j=0 , i=0;
    var cnt = 0 , ret = 0;
    var found = false;
    var end = -8e9;
    var whichOnes = [];

    while ( i<2*n )
    {
        // find a closing bracket
        while ( p[i].type == +1 ) i++;

        while ( (j<2*n) && (p[i].pos - p[j].pos + 1) >= sz )
        {
            var id = p[j].id;
            if ( p[j].type == +1 )
            {
                if ( f[id] == 0 ) f[id] = 1 , cnt++;
            }
            j++;
        }

        if ( cnt > ret )
        {
            ret = cnt;
            if ( ret >= k && found == false )
            {
                for ( var l=0 ; l<n ; l++ )
                    if ( f[l] == 1 )
                        whichOnes.push( l+1 );

                found = true;
            }
        }

        var id = p[i].id;
        if ( f[id] == 0 ) f[id] = 2;
        else if ( f[id] == 1 ) f[id] = 2 , cnt--;
        i++;
    }

    return { ok : found , right : end , left : end - sz + 1 , whichOnes : whichOnes };
}

function main()
{
    z = readArray();
    n = z[0] , k = z[1];
    for ( var i=0 ; i<n ; i++ ) a[i] = readArray();

    for ( var i=0 ; i<n ; i++ )
    {
        p.push( { pos:a[i][0] , type:+1 , id:i } );
        p.push( { pos:a[i][1] , type:-1 , id:i } );
    }
    p.sort( function( fi , se ) {
        if ( fi.pos != se.pos ) return fi.pos - se.pos;
        if ( fi.type != se.type ) return fi.type==+1 ? -1 : +1;
        return fi.id - se.id;
    } );

    /*
    print();
    p.forEach( function( z ) {
        print( z.pos + " , " + z.type + " , " + z.id );
    } );
    print();
    */

    var l = 0 , r = 500;
    while ( l < r )
    {
        var mid = Math.floor( ( l + r ) / 2 );
        //print( l + " " + r + " " + mid );
        if ( check( mid ).ok == true ) l = mid+1;
        else r = mid;
    }

    var ans = check( r-1 );
    if ( ans.whichOnes == undefined )
    {
        ans.whichOnes = []
        for ( var i=0 ; i<k ; i++ )
            ans.whichOnes.push( i+1 );
    }

    print( r-1 );
    // print( ans.left + " " + ans.right );
    ans.whichOnes.forEach( function(x) { write( x + " " ); } );
    print();

} main();

var z;
var n , k;
var a = [];
var p = [];

function readArray()
{
    return readline().split( ' ' ).map( Number );
}

function check( sz )
{
    if ( sz == 0 ) return { ok : true , left : 0 , right : 0 , whichOnes : undefined };

    var f = new Array( n );
    for ( var i=0 ; i<n ; i++ ) f[i] = 0;

    var j=0 , i=0;
    var cnt = 0 , ret = 0;
    var found = false;
    var end = -8e9;
    var whichOnes = [];

    while ( i<2*n )
    {
        // find a closing bracket
        while ( p[i].type == +1 ) i++;

        while ( (j<2*n) && (p[i].pos - p[j].pos + 1) >= sz )
        {
            var id = p[j].id;
            if ( p[j].type == +1 )
            {
                if ( f[id] == 0 ) f[id] = 1 , cnt++;
            }
            j++;
        }

        if ( cnt > ret )
        {
            ret = cnt;
            if ( ret >= k && found == false )
            {
                for ( var l=0 ; l<n ; l++ )
                    if ( f[l] == 1 )
                        whichOnes.push( l+1 );

                found = true;
            }
        }

        var id = p[i].id;
        if ( f[id] == 0 ) f[id] = 2;
        else if ( f[id] == 1 ) f[id] = 2 , cnt--;
        i++;
    }

    return { ok : found , right : end , left : end - sz + 1 , whichOnes : whichOnes };
}

function main()
{
    z = readArray();
    n = z[0] , k = z[1];
    for ( var i=0 ; i<n ; i++ ) a[i] = readArray();

    for ( var i=0 ; i<n ; i++ )
    {
        p.push( { pos:a[i][0] , type:+1 , id:i } );
        p.push( { pos:a[i][1] , type:-1 , id:i } );
    }
    p.sort( function( fi , se ) {
        if ( fi.pos != se.pos ) return fi.pos - se.pos;
        if ( fi.type != se.type ) return fi.type==+1 ? -1 : +1;
        return fi.id - se.id;
    } );

    /*
    print();
    p.forEach( function( z ) {
        print( z.pos + " , " + z.type + " , " + z.id );
    } );
    print();
    */

    var l = 0 , r = 2e9 + 5;
    while ( l < r )
    {
        var mid = Math.floor( ( l + r ) / 2 );
        //print( l + " " + r + " " + mid );
        if ( check( mid ).ok == true ) l = mid+1;
        else r = mid;
    }

    var ans = check( r-1 );
    if ( ans.whichOnes == undefined )
    {
        ans.whichOnes = []
        for ( var i=0 ; i<k ; i++ )
            ans.whichOnes.push( i+1 );
    }

    print( r-1 );
    // print( ans.left + " " + ans.right );
    ans.whichOnes.forEach( function(x) { write( x + " " ); } );
    print();

} main();