It's now 260 AD. Shapur, being extremely smart, became the King of Persia. He is now called Shapur, His majesty King of kings of Iran and Aniran.Recently the Romans declared war on Persia. They dreamed to occupy Armenia. In the recent war, the Romans were badly defeated. Now their senior army general, Philip is captured by Shapur and Shapur is now going to capture Valerian, the Roman emperor.Being defeated, the cowardly Valerian hid in a room at the top of one of his castles. To capture him, Shapur has to open many doors. Fortunately Valerian was too scared to make impenetrable locks for the doors.Each door has 4 parts. The first part is an integer number a. The second part is either an integer number b or some really odd sign which looks like R. The third one is an integer c and the fourth part is empty! As if it was laid for writing something. Being extremely gifted, after opening the first few doors, Shapur found out the secret behind the locks.c is an integer written in base a, to open the door we should write it in base b. The only bad news is that this R is some sort of special numbering system that is used only in Roman empire, so opening the doors is not just a piece of cake!Here's an explanation of this really weird number system that even doesn't have zero:Roman numerals are based on seven symbols: a stroke (identified with the letter I) for a unit, a chevron (identified with the letter V) for a five, a cross-stroke (identified with the letter X) for a ten, a C (identified as an abbreviation of Centum) for a hundred, etc.: I=1 V=5 X=10 L=50 C=100 D=500 M=1000Symbols are iterated to produce multiples of the decimal (1, 10, 100, 1, 000) values, with V, L, D substituted for a multiple of five, and the iteration continuing: I 1, II 2, III 3, V 5, VI 6, VII 7, etc., and the same for other bases: X 10, XX 20, XXX 30, L 50, LXXX 80; CC 200, DCC 700, etc. At the fourth and ninth iteration, a subtractive principle must be employed, with the base placed before the higher base: IV 4, IX 9, XL 40, XC 90, CD 400, CM 900.Also in bases greater than 10 we use A for 10, B for 11, etc.Help Shapur capture Valerian and bring peace back to Persia, especially Armenia.

The first line contains two integers a and b (2 ≤ a, b ≤ 25). Only b may be replaced by an R which indicates Roman numbering system. The next line contains a single non-negative integer c in base a which may contain leading zeros but its length doesn't exceed 103.  It is guaranteed that if we have Roman numerals included the number would be less than or equal to 300010 and it won't be 0. In any other case the number won't be greater than 101510.

Write a single line that contains integer c in base b. You must omit leading zeros.

NoteYou can find more information about roman numerals here: http://en.wikipedia.org/wiki/Roman_numerals

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], 10);
	};
	return tokens;
}

function main() {
	var tokens = tokenize(readline()),
		a10 = parseInt(tokens[0]), b = tokens[1],
		c = trim(readline()),
		digits = '0123456789ABCDEFGHIJKLMNO',
		digit2value = {}, value2digit = {};
	for (var i = 0; i < 25; ++i) {
		digit = digits.charAt(i);
		digit2value[digit] = i;
		value2digit[i] = digit;
	}
	var c10 = 0;
	for (var i = 0; i < c.length; ++i) {
		c10 = a10*c10 + digit2value[c.charAt(i)];
	}
	function roman(x) {
		if (x >= 1000) {
			return 'M'+roman(x-1000);
		} else if (x >= 900) {
			return 'CM'+roman(x-900);
		} else if (x >= 500) {
			return 'D'+roman(x-500);
		} else if (x >= 400) {
			return 'CD'+roman(x-400);
		} else if (x >= 100) {
			return 'C'+roman(x-100);
		} else if (x >= 90) {
			return 'XC'+roman(x-90);
		} else if (x >= 50) {
			return 'L'+roman(x-50);
		} else if (x >= 40) {
			return 'XL'+roman(x-40);
		} else if (x >= 10) {
			return 'X'+roman(x-10);
		} else if (x >= 9) {
			return 'IX'+roman(x-9);
		} else if (x >= 5) {
			return 'V'+roman(x-5);
		} else if (x >= 4) {
			return 'IV'+roman(x-4);
		} else if (x >= 1) {
			return 'I'+roman(x-1);
		} else {
			return '';
		}
	}
	if (b == 'R') {
		print(roman(c10));
	} else {
		if (c10 == 0) {
			print('0');
			return;
		}
		var x = c10, parts = [];
		while (x != 0) {
			var value = x%b;
			parts.push(value2digit[value]);
			x = (x-value)/b;
		}
		print(parts.reverse().join(''));
	}
}

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], 10);
	};
	return tokens;
}

function main() {
	var tokens = tokenize(readline()),
		a10 = parseInt(tokens[0]), b = tokens[1],
		c = trim(readline()),
		digits = '0123456789ABCDEFGHIJKLMNO',
		digit2value = {}, value2digit = {};
	for (var i = 0; i < 25; ++i) {
		digit = digits.charAt(i);
		digit2value[digit] = i;
		value2digit[i] = digit;
	}
	var c10 = 0;
	for (var i = 0; i < c.length; ++i) {
		c10 = a10*c10 + digit2value[c.charAt(i)];
	}
	var romanValues = { M: 1000, C: 100, D: 500, L: 50, X: 10, V: 5, I: 1 },
		romanList = [ { main: 'M', sub: 'C' },
					  { main: 'D', sub: 'C' },
					  { main: 'C', sub: 'X' },
					  { main: 'L', sub: 'X' },
					  { main: 'X', sub: 'I' },
					  { main: 'V', sub: 'I' } ];
	function roman(x) {
		for (var i = 0; i < romanList.length; ++i) {
			var mainChar = romanList[i].main, mainValue = romanValues[mainChar],
				subChar = romanList[i].sub, subValue = romanValues[subChar];
			if (x >= mainValue) {
				return mainChar + roman(x-mainValue);
			} else if (x >= mainValue-subValue) {
				return subChar + mainChar + roman(x-mainValue+subValue);
			}
		}
		if (x >= 1) {
			return 'I' + roman(x-1);
		}
		return '';
	}
	if (b == 'R') {
		print(roman(c10));
	} else {
		if (c10 == 0) {
			print('0');
			return;
		}
		var x = c10, parts = [];
		while (x != 0) {
			var value = x%b;
			parts.push(value2digit[value]);
			x = (x-value)/b;
		}
		print(parts.reverse().join(''));
	}
}

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], 10);
	};
	return tokens;
}

function main() {
	var tokens = tokenize(readline()),
		a10 = parseInt(tokens[0]), b = tokens[1],
		c = trim(readline()),
		digits = '0123456789ABCDEFGHIJKLMNO',
		digit2value = {}, value2digit = {};
	for (var i = 0; i < 25; ++i) {
		digit = digits.charAt(i);
		digit2value[digit] = i;
		value2digit[i] = digit;
	}
	var c10 = 0;
	for (var i = 0; i < c.length; ++i) {
		c10 = a10*c10 + digit2value[c.charAt(i)];
	}
	var romanValues = { M: 1000, C: 100, L: 50, X: 10, V: 5, I: 1 },
		romanList = [ { main: 'M', sub: 'C' },
					  { main: 'C', sub: 'X' },
					  { main: 'L', sub: 'X' },
					  { main: 'X', sub: 'I' },
					  { main: 'V', sub: 'I' } ];
	function roman(x) {
		for (var i = 0; i < romanList.length; ++i) {
			var mainChar = romanList[i].main, mainValue = romanValues[mainChar],
				subChar = romanList[i].sub, subValue = romanValues[subChar];
			if (x >= mainValue) {
				return mainChar + roman(x-mainValue);
			} else if (x >= mainValue-subValue) {
				return subChar + mainChar + roman(x-mainValue+subValue);
			}
		}
		if (x >= 1) {
			return 'I' + roman(x-1);
		}
		return '';
	}
	if (b == 'R') {
		print(roman(c10));
	} else {
		if (c10 == 0) {
			print('0');
			return;
		}
		var x = c10, parts = [];
		while (x != 0) {
			var value = x%b;
			parts.push(value2digit[value]);
			x = (x-value)/b;
		}
		print(parts.reverse().join(''));
	}
}

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], 10);
	};
	return tokens;
}

function main() {
	var tokens = tokenize(readline()),
		a10 = parseInt(tokens[0]), b = tokens[1],
		c = trim(readline()),
		digits = '0123456789ABCDEFGHIJKLMNO',
		digit2value = {}, value2digit = {};
	for (var i = 0; i < 25; ++i) {
		digit = digits.charAt(i);
		digit2value[digit] = i;
		value2digit[i] = digit;
	}
	var c10 = 0;
	for (var i = 0; i < c.length; ++i) {
		c10 = a10*c10 + digit2value[c.charAt(i)];
	}
	function roman(x) {
		if (x >= 1000) {
			return 'M'+roman(x-1000);
		} else if (x >= 900) {
			return 'CM'+roman(x-900);
		} else if (x >= 500) {
			return 'D'+roman(x-500);
		} else if (x >= 400) {
			return 'CD'+roman(x-400);
		} else if (x >= 100) {
			return 'C'+roman(x-100);
		} else if (x >= 90) {
			return 'XC'+roman(x-90);
		} else if (x >= 50) {
			return 'L'+roman(x-50);
		} else if (x >= 40) {
			return 'XL'+roman(x-40);
		} else if (x >= 10) {
			return 'X'+roman(x-10);
		} else if (x >= 9) {
			return 'IX'+roman(x-9);
		} else if (x >= 5) {
			return 'V'+roman(x-5);
		} else if (x >= 4) {
			return 'IV'+roman(x-4);
		} else if (x >= 1) {
			return 'I'+roman(x-1);
		} else {
			return '';
		}
	}
	if (b == 'R') {
		print(roman(c10));
	} else {
		var x = c10, parts = [];
		while (x != 0) {
			var value = x%b;
			parts.push(value2digit[value]);
			x = (x-value)/b;
		}
		print(parts.reverse().join(''));
	}
}

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], 10);
	};
	return tokens;
}

function main() {
	var tokens = tokenize(readline()),
		a10 = parseInt(tokens[0]), b = tokens[1],
		c = trim(readline()),
		digits = '0123456789ABCDEFGHIJKLMNO',
		digit2value = {}, value2digit = {};
	for (var i = 0; i < 25; ++i) {
		digit = digits.charAt(i);
		digit2value[digit] = i;
		value2digit[i] = digit;
	}
	var c10 = 0;
	for (var i = 0; i < c.length; ++i) {
		c10 = a10*c10 + digit2value[c.charAt(i)];
	}
	var romanValues = { M: 1000, C: 100, L: 50, X: 10, V: 5, I: 1 },
		romanList = [ { main: 'M', sub: 'C' },
					  { main: 'C', sub: 'X' },
					  { main: 'L', sub: 'X' },
					  { main: 'X', sub: 'I' },
					  { main: 'V', sub: 'I' } ];
	function roman(x) {
		for (var i = 0; i < romanList.length; ++i) {
			var mainChar = romanList[i].main, mainValue = romanValues[mainChar],
				subChar = romanList[i].sub, subValue = romanValues[subChar];
			if (x >= mainValue) {
				return mainChar + roman(x-mainValue);
			} else if (x >= mainValue-subValue) {
				return subChar + mainChar + roman(x-mainValue-subValue);
			}
		}
		return '';
	}
	if (b == 'R') {
		print(roman(c10));
	} else {
		if (c10 == 0) {
			print('0');
			return;
		}
		var x = c10, parts = [];
		while (x != 0) {
			var value = x%b;
			parts.push(value2digit[value]);
			x = (x-value)/b;
		}
		print(parts.reverse().join(''));
	}
}

main();


A penguin Rocher has $$$n$$$ sticks. He has exactly one stick with length $$$i$$$ for all $$$1 \le i \le n$$$.He can connect some sticks. If he connects two sticks that have lengths $$$a$$$ and $$$b$$$, he gets one stick with length $$$a + b$$$. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his set and can be used for the next connections.He wants to create the maximum number of sticks that have the same length. It is not necessary to make all sticks have the same length, some sticks can have the other length. How many sticks with the equal length he can create?

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case, the only line contains a single integer $$$n$$$ ($$$1 \le n \le 10^{9}$$$).

For each test case, print a single integer  — the answer to the problem.

NoteIn the third case, he can connect two sticks with lengths $$$1$$$ and $$$2$$$ and he will get one stick with length $$$3$$$. So, he will have two sticks with lengths $$$3$$$.In the fourth case, he can connect two sticks with lengths $$$1$$$ and $$$3$$$ and he will get one stick with length $$$4$$$. After that, he will have three sticks with lengths $$$\{2, 4, 4\}$$$, so two sticks have the same length, and one stick has the other length.

function solve() {
   var n = Number(read());
   write(Math.floor((n + 1) / 2));
}

function init() {
   var T;
   T = 1;
   T = Number(read());
   for (var t = 0; t < T; t++) {
      solve();
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         init();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', init);
   }
} else {
   init();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}

var tc = parseInt(readline());
while(tc--){
    var num = parseInt(readline());
    print(num%2==0 ? parseInt(num/2) : parseInt(num/2)+1);
}

var Tc = parseInt(readline());
while (Tc-- > 0) {
  var n = parseInt(readline());
  print(Math.floor((n + 1) / 2));
}

'use strict';

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();
    });

  main();
});

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

function main() {
  var T = readline();

  while (T-- > 0) {
    let n = readline();

    let res = 1 + parseInt((n - 1) / 2);

    console.log(res);
  }
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStd) => {
  inputString += inputStd;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}
function main() {
  let t = +readLine();
  while (t--) {
    let n = +readLine();
    if (n % 2 === 0) {
      console.log(Math.floor(n / 2));
    } else {
      console.log(Math.floor(n / 2) + 1);
    }
  }
}

//↓見なくていいよ！ここから--------------------------------------------------
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);
  console.error("\n");
  var start = new Date();
  Main();
  var end = new Date() - start;

  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();
		output[i] = Math.ceil(n / 2);
	}
	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[0] * 1;
  let i = 1;
  while (i <= testCases) {
    let n = data[i] * 1;
    console.log(Math.ceil(n / 2));
    i += 1;
  }
}

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let t = -1;

rl.on('line', (input) => {
    if (t === -1) {
        t = parseInt(input);
    } else {
        let tmp = parseInt(input);
        console.log(tmp % 2 === 0 ? tmp / 2 : tmp / 2 + 0.5);
    }
})

const process = require('process');

let inp = "";
process.stdin.on('data', chunk => inp += chunk);
process.stdin.on('end', () => {
    inp = inp.split('\n');
    let ic = 0;
    let ntest = +inp[ic++];
    for (let testcase = 0; testcase < ntest; ++testcase) {
        let n = +inp[ic++];
        console.log(Math.floor((n + 1) / 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')

    main()
})

function readLine() {
    return inputString[currentLine++]
}

const s2i = s => parseInt(s, 10)

const readLineOfInts = () => {
  const split = readLine().split(' ')
  const result = []
  for (let i = 0, l = split.length; i < l; i++) {
    result.push(s2i(split[i]));
  }
  return result
}

function main() {
  let T = s2i(readLine())
  let out = ''

  while (T--) {
    const n = s2i(readLine())

    out += Math.ceil(n/2) + '\n'
  }
  console.log(out)
}

'use strict';

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();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
/**
5
4 3
1 2 1 3
10 6
8 7 1 8 3 7 5 10 8 9
5 10
20 100 50 20 100500
10 25
24 24 24 24 24 24 24 24 24 24
8 8
1 2 3 4 5 6 7 8

**/

function gcd(a, b) {
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
};

function main() {
    let n = parseInt(readline());

    for(let r=0;r<n;r++) {
        solve();
    }

}

function solve() {
    let n = Number(readline());

    console.log(Math.ceil(n / 2));
}

const process = require('process');

let inp = "";
process.stdin.on('data', chunk => inp += chunk);
process.stdin.on('end', () => {
    inp = inp.split(/\s/);
    let ic = 0;
    let ntest = +inp[ic++];
    for (let testcase = 0; testcase < ntest; ++testcase) {
        let n = +inp[ic++];
        console.log(Math.floor((n + 1) / 2));
    }

});


You went to the store, selling $$$n$$$ types of chocolates. There are $$$a_i$$$ chocolates of type $$$i$$$ in stock.You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy $$$x_i$$$ chocolates of type $$$i$$$ (clearly, $$$0 \le x_i \le a_i$$$), then for all $$$1 \le j < i$$$ at least one of the following must hold:  $$$x_j = 0$$$ (you bought zero chocolates of type $$$j$$$) $$$x_j < x_i$$$ (you bought less chocolates of type $$$j$$$ than of type $$$i$$$) For example, the array $$$x = [0, 0, 1, 2, 10]$$$ satisfies the requirement above (assuming that all $$$a_i \ge x_i$$$), while arrays $$$x = [0, 1, 0]$$$, $$$x = [5, 5]$$$ and $$$x = [3, 2]$$$ don't.Calculate the maximum number of chocolates you can buy.

The first line contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$), denoting the number of types of chocolate. The next line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^9$$$), denoting the number of chocolates of each type.

Print the maximum number of chocolates you can buy.

NoteIn the first example, it is optimal to buy: $$$0 + 0 + 1 + 3 + 6$$$ chocolates.In the second example, it is optimal to buy: $$$1 + 2 + 3 + 4 + 10$$$ chocolates.In the third example, it is optimal to buy: $$$0 + 0 + 0 + 1$$$ chocolates.

var readline = require('readline')
var k=0
var n=0;
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('', (answer) => {
  n= answer
  rl.question('', (answer) => {
    x=answer.split(' ')
    for (var i = (n-1);i>=0;i--) {
      if(Number(x[i])<0) x[i] = 0
      if(Number(x[i-1])>Number(x[i]) || Number(x[i-1])==Number(x[i])) {
        x[i-1] = Number(x[i])-1
      }
    }
    x.forEach(function(i) {
      k+=+i
    })
    console.log(k)
    rl.close()
  })
})

const readline = require('readline');

const rl = readline.createInterface({
	input: process.stdin,
	setEncoding: 'utf8',
	terminal: false
});

const readInput = (function* () {
	const n = Number(yield);
	const s = (yield).trim().split(" ").map(Number);
	rl.close();
	let x = y = s[n - 1];
	for (let i = n - 2; i >= 0; i--) {
		if (y < 2) break;
		y = Math.min(s[i], y - 1);
		x += y;
	}
	console.log(x)
})();

readInput.next();

rl.on('line', (line) => {
	readInput.next(line);
});

var n=readline();
var a=readline().split(" ");
a=a.slice(0,n);
var curV=1000000001;
var res=0;
for(var i=n-1; i>=0; --i){
    curV=Math.max(0,Math.min(a[i],curV-1));
    res+=curV;
}
print(res);

var n = readline();
var a = readline().split(' ');
a=a.slice(0,n);

var curV=1000000001;
var res=0;
for(var i=n-1; i>=0; --i ){
    curV=Math.max(0,Math.min(curV-1, a[i]));
    res+=curV;
}
print(res);

const readline = require('readline');

const rl = readline.createInterface({
	input: process.stdin,
	setEncoding: 'utf8',
	terminal: false
});

const readInput = (function* () {
	const n = Number(yield);
	const s = (yield).trim().split(" ").map(Number);
	rl.close();
	let x = 0;
	let y = Infinity;
	for (let i = n - 1; i >= 0; i--) {
		if (s[i] < 2) {
			if (y > 1) x += s[i];
			break;
		}
		y = Math.min(s[i], y - 1);
		x += y;
	}
	console.log(x)
})();

readInput.next();

rl.on('line', (line) => {
	readInput.next(line);
});

const readline = require('readline');

const rl = readline.createInterface({
	input: process.stdin,
	setEncoding: 'utf8',
	terminal: false
});

const readInput = (function* () {
	const n = Number(yield);
	const s = (yield).trim().split(" ").map(Number);
	rl.close();
	let x = 0;
	let y = Infinity;
	for (let i = n - 1; i >= 0; i--) {
		if (s[i] < 2) {
			if (y > 1) x += s[i];
			break;
		}
		y = Math.max(s[i], y - 1);
		x += y;
	}
	console.log(x)
})();

readInput.next();

rl.on('line', (line) => {
	readInput.next(line);
});

const readline = require('readline');

const rl = readline.createInterface({
	input: process.stdin,
	setEncoding: 'utf8',
	terminal: false
});

const readInput = (function* () {
	const n = Number(yield);
	const s = (yield).trim().split(" ").map(Number);
	rl.close();
	let x = 0;
	let y = Infinity;
	for (let i = n - 1; i >= 0; i--) {
		if (s[i] < 2) {
			x += s[i];
			break;
		}
		y = Math.min(s[i], y - 1);
		x += y;
	}
	console.log(x)
})();

readInput.next();

rl.on('line', (line) => {
	readInput.next(line);
});

const readline = require('readline');

const rl = readline.createInterface({
	input: process.stdin,
	setEncoding: 'utf8',
	terminal: false
});

const readInput = (function* () {
	const n = Number(yield);
	const s = (yield).trim().split(" ").map(Number);
	rl.close();
	let x = y = s[n - 1];
	for (let i = n - 2; i >= 0; i--) {
		console.log('iteration', n - i,': y =', y);
		if (y < 2) break;
		y = Math.min(s[i], y - 1);
		x += y;
	}
	console.log(x)
})();

readInput.next();

rl.on('line', (line) => {
	readInput.next(line);
});

var n = readline();
var a = readline().split(' ');
var x = [];
var flag = false;
if(n<1 || n>200000) exit();

for(var i=0; i<n; i++){
    for(var j=i+1; j<n; j++){
        if(a[i]>=a[j]) flag = true;
    }
    if(flag) x.push(0);
    else x.push(Number(a[i]));

    flag = false;
}
print(x.reduce((a,b) => a+b));

var n = 5;
var a = "3 2 5 4 10".split(' ');
var x = [];
var flag = false;
if(n<1 || n>200000) exit();

for(var k=0; k<a.length; k++){
    a[k] = Number(a[k]);
}

for(var i=0; i<n; i++){
    if(a[i]==Math.min(...a.slice(i)) && a[i]!=Math.max(...a.slice(i))){
        x[i]=a[i];
    } else if(i==n-1){
        x[i]=Math.min(...a.slice(i));
    } else {
        x[i]=Math.min(...a.slice(i))-1;
    }
}

print(x.reduce((a,b)=>a+b));


You are given an array $$$a$$$ consisting of $$$n$$$ non-negative integers. You have to choose a non-negative integer $$$x$$$ and form a new array $$$b$$$ of size $$$n$$$ according to the following rule: for all $$$i$$$ from $$$1$$$ to $$$n$$$, $$$b_i = a_i \oplus x$$$ ($$$\oplus$$$ denotes the operation bitwise XOR).An inversion in the $$$b$$$ array is a pair of integers $$$i$$$ and $$$j$$$ such that $$$1 \le i < j \le n$$$ and $$$b_i > b_j$$$.You should choose $$$x$$$ in such a way that the number of inversions in $$$b$$$ is minimized. If there are several options for $$$x$$$ — output the smallest one.

First line contains a single integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$) — the number of elements in $$$a$$$. Second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.

Output two integers: the minimum possible number of inversions in $$$b$$$, and the minimum possible value of $$$x$$$, which achieves those number of inversions.

NoteIn the first sample it is optimal to leave the array as it is by choosing $$$x = 0$$$.In the second sample the selection of $$$x = 14$$$ results in $$$b$$$: $$$[4, 9, 7, 4, 9, 11, 11, 13, 11]$$$. It has $$$4$$$ inversions:  $$$i = 2$$$, $$$j = 3$$$;  $$$i = 2$$$, $$$j = 4$$$;  $$$i = 3$$$, $$$j = 4$$$;  $$$i = 8$$$, $$$j = 9$$$. In the third sample the selection of $$$x = 8$$$ results in $$$b$$$: $$$[0, 2, 11]$$$. It has no inversions.

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
// Array.prototype.rev = function (first, last) {
//   // for (let i = first; i < (first + last) / 2; i++) [this[i],this[last-]]
// }
Array.prototype.merge = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0, y = [].splice()
  for (let i = 29; i >= 0; i--) {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      b.merge(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  }

  log(sum, x);
}

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.set = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  For(29, 0, -1, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      b.rev(idau, cuoi);
      // b.merge(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  })

  log(sum, x);
}

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

function Arr(a, b, fn, init) {
  if (typeof (b) == 'function') return Arr(0, a, b, fn);
  if (b < a) return [];
  let arr = Array(b - a + 1).fill(0);
  if (init) init(arr);
  for (let i = a; i <= b; i++) 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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.set = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}
Array.prototype.for = function (a, b, fn) {
  let res
  for (let i = a; i <= b; i++) {
    if ((res = fn(this[i], i, this)) != null) return res;
  }
}
Array.prototype.forR = function (a, b, fn) {
  let res
  for (let i = a; i >= b; i--) {
    if ((res = fn(this[i], i, this)) != null) return res;
  }
}

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(n, (i, a) => i == 0 ? 1n : a[i - 1] * BigInt(i) % MOD);
// let rgt = Arr(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) => 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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  ForR(29, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      if (dau >= n) break;
      cuoi = For(dau + 1, n - 1, i => c[i] != c[i - 1] ? i : null) || n;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      a.for(dau, cuoi - 1, v => {
        if (v & (1 << i)) { s1++; b[--icuoi] = v; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = v };
      })
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      b.rev(idau, cuoi);
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else sum += ss0;
    [a, b] = [b, a];
  })

  log(sum, x);
}

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.merge = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = Float64Array.from($(n)), b = a.slice(), c = a.slice().fill(0), x = 0, sum = 0
  For(29, 0, -1, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      // b.rev(idau, cuoi);
      b.subarray(idau, cuoi).reverse();
      // b.merge(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  })

  log(sum, x);
}

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, 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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  for (let i = 29; i >= 0; i--) {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      // log(i, dau, cuoi, s, s1, idau, icuoi);
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    // log(a, b, c);
    [a, b] = [b, a];
  }

  log(sum, x);
}

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.set = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  For(29, 0, -1, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      // b.rev(idau, cuoi);
      b.set(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  })

  log(sum, x);
}

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.merge = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = Int32Array.from($(n)), b = a.slice(), c = a.slice().fill(0), x = 0, sum = 0
  For(29, 0, -1, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      // b.rev(idau, cuoi);
      b.subarray(idau, cuoi).reverse()
      // b.merge(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  })

  log(sum, x);
}

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.set = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}
Array.prototype.for = function (a, b, fn) {
  for (let i = a; i <= b; i++) fn(this[i], i, this);
}
Array.prototype.forR = function (a, b, fn) {
  for (let i = a; i >= b; i--) fn(this[i], i, this);
}

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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  For(29, 0, -1, i => {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      a.for(dau, cuoi - 1, v => {
        if (v & (1 << i)) { s1++; b[--icuoi] = v; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = v };
      })
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      b.rev(idau, cuoi);
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  })

  log(sum, x);
}

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, 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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = $(n), t = [a], x = 0, sum = 0
  for (let i = 29; i >= 0; i--) {
    let nt = [];
    let ss = 0, ss0 = 0, ss1 = 0;
    t.forEach(ta => {
      let nt0 = [], nt1 = []
      let s1 = 0, s = 0;
      ta.forEach(v => {
        if (v & (1 << i)) { s1++; nt1.push(v) }
        else { s += s1; nt0.push(v) };
      });
      ss0 += s;
      ss1 += s1 * (ta.length - s1) - s;
      ss += s1 * (ta.length - s1) - s - s;
      if (nt0.length) nt.push(nt0)
      if (nt1.length) nt.push(nt1)
    })
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    t = nt;
  }

  log(sum, x);
}

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, max];
}
Array.prototype.rev = function (first, last) {
  for (let i = first; i < (first + last) / 2; i++) {
    [this[i], this[last - 1 - i + first]] = [this[last - 1 - i + first], this[i]]
  }
}
Array.prototype.merge = function (pos, arr) {
  for (let i = pos; i < pos + arr.length; i++) {
    this[i] = arr[i - pos];
  }
}

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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0, y = [].splice()
  for (let i = 29; i >= 0; i--) {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      b.rev(idau, cuoi);
      // b.merge(idau, b.slice(idau, cuoi).reverse())
      // for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    [a, b] = [b, a];
  }

  log(sum, x);
}

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, 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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = $(n), b = Array(n), c = Array(n).fill(0), x = 0, sum = 0
  for (let i = 3; i >= 0; i--) {
    let dau = 0, cuoi;
    let ss = 0, ss0 = 0, ss1 = 0;
    while (1) {
      while (dau < n && c[dau] == c[dau - 1]) dau++;
      if (dau >= n) break;
      cuoi = dau + 1;
      while (cuoi < n && c[cuoi] == c[cuoi - 1]) cuoi++;
      let idau = dau;
      let icuoi = cuoi;
      let s1 = 0, s = 0;
      for (let j = dau; j < cuoi; j++) {
        if (a[j] & (1 << i)) { s1++; b[--icuoi] = a[j]; c[icuoi] += (1 << i) }
        else { s += s1; b[idau++] = a[j] };
      }
      // log(i, dau, cuoi, s, s1, idau, icuoi);
      ss0 += s;
      ss1 += s1 * ((cuoi - dau) - s1) - s;
      ss += s1 * ((cuoi - dau) - s1) - s - s;
      dau = cuoi;
      for (let j = idau; j < (cuoi + idau) / 2; j++) [b[j], b[cuoi - 1 - j + idau]] = [b[cuoi - 1 - j + idau], b[j]]
    }
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    // log(a, b, c);
    [a, b] = [b, a];
  }

  log(sum, x);
}

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 = 1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x < min) { min = x; index = i }
  }
  return [this[index], index, min];
}
Array.prototype.max = function (fn) {
  let max = -1e20, index = -1, x;
  for (let i = 0; i < this.length; i++) {
    x = fn ? fn(this[i], i, this) : this[i];
    if (x > max) { max = x; index = i }
  }
  return [this[index], index, 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 p = [2, 3, 5];
// for (let i = 6; i < 1e5; i += 6) {
//   let j, x = i + 1;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
//   x = i + 5;
//   for (j = 0; p[j] * p[j] <= x; j++) if (x % p[j] == 0) break;
//   if (x % p[j] != 0) p.push(x);
// }

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 = $(), a = $(n), t = [a], x = 0, sum = 0
  for (let i = 29; i >= 0; i--) {
    let nt = [];
    let ss0 = 0, ss1 = 0, ss = 0;
    t.forEach(ta => {
      let nt0 = [], nt1 = []
      let s1 = 0, s = 0;
      ta.forEach(v => {
        if (v & (1 << i)) { s1++; nt1.push(v) }
        else { s += s1; nt0.push(v) };
      });
      ss0 += s;
      ss1 += s1 * (ta.length - s1) - s;
      ss += ss1 - ss0;
      if (nt0.length) nt.push(nt0)
      if (nt1.length) nt.push(nt1)
    })
    if (ss < 0) {
      x += (1 << i);
      sum += ss1
    } else {
      sum += ss0
    }
    t = nt;
  }

  log(sum, x);
}


You are given a n × m field consisting only of periods ('.') and asterisks ('*'). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of '*'-cells. A right triangle is a triangle in which one angle is a right angle (that is, a 90 degree angle).

The first line contains two positive integer numbers n and m (1 ≤ n, m ≤ 1000). The following n lines consist of m characters each, describing the field. Only '.' and '*' are allowed.

Output a single number — total number of square triangles in the field. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

'use strict'

let lll = readline().split(' ').map(v => parseInt(v))
let N = lll[0]
let M = lll[1]

let f = []
while (lll = readline()) {
  f.push(lll.split(''))
}

let ar = []
let ac = []
for (let i = 0; i < N; i++) {
  ar.push([])
}
for (let i = 0; i < M; i++) {
  ac.push([])
}
for (let i = 0; i < N; i++) {
  for (let j = 0; j < M; j++) {
    if (f[i][j] == '*') {
      ar[i].push(j)
      ac[j].push(i)
    }
  }
}

let ts = 0
ar.forEach(r => {
  if (r.length < 2) return
  r.forEach(c => {
    ts += (ac[c].length - 1) * (r.length - 1)
  })
})

print(ts)


Anna is a girl so brave that she is loved by everyone in the city and citizens love her cookies. She is planning to hold a party with cookies. Now she has $$$a$$$ vanilla cookies and $$$b$$$ chocolate cookies for the party.She invited $$$n$$$ guests of the first type and $$$m$$$ guests of the second type to the party. They will come to the party in some order. After coming to the party, each guest will choose the type of cookie (vanilla or chocolate) to eat. There is a difference in the way how they choose that type:If there are $$$v$$$ vanilla cookies and $$$c$$$ chocolate cookies at the moment, when the guest comes, then  if the guest of the first type: if $$$v>c$$$ the guest selects a vanilla cookie. Otherwise, the guest selects a chocolate cookie.  if the guest of the second type: if $$$v>c$$$ the guest selects a chocolate cookie. Otherwise, the guest selects a vanilla cookie. After that:  If there is at least one cookie of the selected type, the guest eats one.  Otherwise (there are no cookies of the selected type), the guest gets angry and returns to home. Anna wants to know if there exists some order of guests, such that no one guest gets angry. Your task is to answer her question.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case, the only line contains four integers $$$a$$$, $$$b$$$, $$$n$$$, $$$m$$$ ($$$0 \le a,b,n,m \le 10^{18}, n+m \neq 0$$$).

For each test case, print the answer in one line. If there exists at least one valid order, print "Yes". Otherwise, print "No". You can print each letter in any case (upper or lower).

NoteIn the first test case, let's consider the order $$$\{1, 2, 2\}$$$ of types of guests. Then:  The first guest eats a chocolate cookie. After that, there are $$$2$$$ vanilla cookies and $$$1$$$ chocolate cookie.  The second guest eats a chocolate cookie. After that, there are $$$2$$$ vanilla cookies and $$$0$$$ chocolate cookies.  The last guest selects a chocolate cookie, but there are no chocolate cookies. So, the guest gets angry. So, this order can't be chosen by Anna.Let's consider the order $$$\{2, 2, 1\}$$$ of types of guests. Then:  The first guest eats a vanilla cookie. After that, there is $$$1$$$ vanilla cookie and $$$2$$$ chocolate cookies.  The second guest eats a vanilla cookie. After that, there are $$$0$$$ vanilla cookies and $$$2$$$ chocolate cookies.  The last guest eats a chocolate cookie. After that, there are $$$0$$$ vanilla cookies and $$$1$$$ chocolate cookie. So, the answer to this test case is "Yes".In the fifth test case, it is illustrated, that the number of cookies ($$$a + b$$$) can be equal to zero, but the number of guests ($$$n + m$$$) can't be equal to zero.In the sixth test case, be careful about the overflow of $$$32$$$-bit integer type.

const process = require('process');

let inp = "";
process.stdin.on('data', chunk => inp += chunk);
process.stdin.on('end', () => {
    inp = inp.split('\n');
    let ic = 0;
    for (let ntest = +inp[ic++]; ntest--; ) {
        let [a, b, n, m] = inp[ic++].split(' ').map(BigInt);
        if (n + m > a + b) {
            console.log("No");
            continue;
        }
        if (m > (a < b ? a : b)) {
            console.log("No");
            continue;
        }
        console.log("Yes");
    }
});

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')

    main()
})

function readLine() {
    return inputString[currentLine++]
}

const s2i = s => parseInt(s, 10)

const readLineOfBigInts = () => {
  const split = readLine().split(' ')
  const result = []
  for (let i = 0, l = split.length; i < l; i++) {
    result.push(BigInt(split[i]));
  }
  return result
}

function main() {
  let T = s2i(readLine())
  let out = ''

  while (T--) {
    const [a, b, n, m] = readLineOfBigInts()
    // a = vainilla
    // b = chocolate
    // n = tipo 1
    // m = tipo 2
    let v = a, c = b, t1 = n, t2 = m

    let result = 'Yes'

    if (v + c < t1 + t2) {
      // Más invitados que galletas
      result = 'No'
    } else if (v - c > t1) {
      // siempre v > c
      // t1 siempre come vainilla, t2 siempre come chocolate
      if (c < t2) {
        result = 'No'
      }
    } else if (c - v >= t1) {
      // siempre v <= c
      // t1 siempre come chocolate, t2 siempre come vainilla
      if (v < t2) {
        result = 'No'
      }
    } else {
      // t1 come galletas hasta que v == c
      if (v > c) {
        t1 -= v - c
        v = c
      } else {
        t1 -= c - v
        c = v
      }

      if (t2 > v && t1 < t2) {
        result = 'No'
      }
    }

    out += result + '\n'
  }

  console.log(out)
}

function f(a, b, n, m) {
   if (a >= m && b >= m) {
      return true;
   }
   return false;
}

function solve() {
   var abnm = read().split(' ').map(BigInt);
   var a = abnm[0];
   var b = abnm[1];
   var n = abnm[2];
   var m = abnm[3];
   if (f(a, b, n, m) && a + b >= n + m) {
      write('yes');
   } else {
      write('no');
   }
}

function init() {
   var T;
   T = 1;
   T = Number(read());
   for (var t = 0; t < T; t++) {
      solve();
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         init();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', init);
   }
} else {
   init();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}

function f(a, b, n, m) {
   if (a >= m && b >= m) {
      return true;
   }
   if (a >= b && a - n <= b && b + 1n >= m) {
      return true;
   }
   return false;
}

function solve() {
   var abnm = read().split(' ').map(BigInt);
   var a = abnm[0];
   var b = abnm[1];
   var n = abnm[2];
   var m = abnm[3];
   if (f(a, b, n, m) && a + b >= n + m) {
      write('yes');
   } else {
      write('no');
   }
}

function init() {
   var T;
   T = 1;
   T = Number(read());
   for (var t = 0; t < T; t++) {
      solve();
   }
}

var isNode = typeof console !== 'undefined';
var MEMORY = [];
var MEMORY_INDEX = 0;
if (isNode) {
   var fs = require('fs');
   var path = require('path');

   var inTextName;
   var outTextName;
   if (process.argv.length === 5) {
      inTextName = process.argv[3];
      outTextName = process.argv[4];
   }

   if (inTextName) {
      fs.readFile(path.join(__dirname, inTextName), 'utf8', (err, data) => {
         MEMORY = data.split('\r\n');
         init();
      });
   } else {
      var RL = require('readline').createInterface({
         input : process.stdin,
         output : process.stdout
      });

      RL.on('line', (line) => {
         MEMORY.push(line);
      });

      RL.on('close', init);
   }
} else {
   init();
}

function write(value) {
   isNode ? console.log(value) : print(value);
}
function read() {
   return isNode ? MEMORY[MEMORY_INDEX++] : readline();
}