There are $$$n$$$ bricks numbered from $$$1$$$ to $$$n$$$. Brick $$$i$$$ has a weight of $$$a_i$$$.Pak Chanek has $$$3$$$ bags numbered from $$$1$$$ to $$$3$$$ that are initially empty. For each brick, Pak Chanek must put it into one of the bags. After this, each bag must contain at least one brick.After Pak Chanek distributes the bricks, Bu Dengklek will take exactly one brick from each bag. Let $$$w_j$$$ be the weight of the brick Bu Dengklek takes from bag $$$j$$$. The score is calculated as $$$|w_1 - w_2| + |w_2 - w_3|$$$, where $$$|x|$$$ denotes the absolute value of $$$x$$$.It is known that Bu Dengklek will take the bricks in such a way that minimises the score. What is the maximum possible final score if Pak Chanek distributes the bricks optimally?

Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^4$$$) — the number of test cases. The following lines contain the description of each test case. The first line of each test case contains an integer $$$n$$$ ($$$3 \leq n \leq 2 \cdot 10^5$$$) — the number of bricks. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the weights of the bricks. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, output a line containing an integer representing the maximum possible final score if Pak Chanek distributes the bricks optimally.

NoteIn the first test case, one way of achieving a final score of $$$6$$$ is to do the following:   Put bricks $$$1$$$, $$$4$$$, and $$$5$$$ into bag $$$1$$$.  Put brick $$$3$$$ into bag $$$2$$$.  Put brick $$$2$$$ into bag $$$3$$$. If Pak Chanek distributes the bricks that way, a way Bu Dengklek can take the bricks is:   Take brick $$$5$$$ from bag $$$1$$$.  Take brick $$$3$$$ from bag $$$2$$$.  Take brick $$$2$$$ from bag $$$3$$$. The score is $$$|a_5 - a_3| + |a_3 - a_2| = |3 - 5| + |5 - 1| = 6$$$. It can be shown that Bu Dengklek cannot get a smaller score from this distribution.It can be shown that there is no other distribution that results in a final score bigger than $$$6$$$.

import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;

long solve(long[] a)
{
  assert(a.length >= 3);
  long ans = a[$ - 1] - a[0];
  return max(ans + a[1] - a[0], ans + (a[$ - 1] - a[$ - 2]));
}

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto n = readln.strip.to!int;
    auto a = readln.splitter.map!(to!long).array.sort.array;
    long best = solve(a);
    foreach (cut ; 0 .. n - 3 + 1) {
      best = max(best, solve(a[0 .. $ - cut]));
      best = max(best, solve(a[cut .. $]));
    }
    writeln(best);
  }
}

import std.stdio;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;
import std.functional;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void solve() {
    int n = read!int;
    auto a = readarray!long;
    a.sort!("a > b");
    long res1, res2;
    int minI = n - 1;
    foreach(i; 0 .. minI - 1) {
        if (abs(a[minI] - a[i]) + abs(a[i] - a[i+1]) > res1)
            res1 = abs(a[minI] - a[i]) + abs(a[i] - a[i+1]);
    }
    foreach(i; iota(n-1,1,-1).array) {
        if (abs(a[0] - a[i]) + abs(a[i] - a[i-1]) > res2)
            res2 = abs(a[0] - a[i]) + abs(a[i] - a[i-1]);
    }
    writeln(max(res1, res2));
}

void main() {
    int T = read!int;
    foreach (t; 0 .. T) solve();
}

import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		int res = 0;
		foreach (i; 2..n)
		{
			res = max (res, +(a[i] - a[i - 1] + a[i] - a[0]));
		}
		reverse (a);
		foreach (i; 2..n)
		{
			res = max (res, -(a[i] - a[i - 1] + a[i] - a[0]));
		}
		writeln (res);
	}
}

import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;

void main()
{
  foreach (casenum ; 1 .. readln.strip.to!int + 1) {
    auto n = readln.strip.to!int;
    auto a = readln.splitter.map!(to!long).array.sort.array;
    long ans = a[$ - 1] - a[0];
    ans = max(ans + a[1] - a[0], ans + (a[$ - 1] - a[$ - 2]));
    writeln(ans);
  }
}

import std.stdio;
import std.math;
import std.numeric;
import std.container;
import std.typecons;
import core.stdc.stdio : scanf;
import std.conv;
import std.array;
import std.string;
import std.range;
import std.algorithm;
import std.functional;

T read(T)() { return readln.chomp.to!T; }
T[] readarray(T)() { return readln.chomp.split(" ").map!(to!T).array; }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void solve() {
    int n = read!int;
    auto a = readarray!long;
    a.sort!("a > b");
    long res;
    int minI = n - 1;
    foreach(i; 0 .. minI - 1) {
        if (abs(a[minI] - a[i]) + abs(a[i] - a[i+1]) > res)
            res = abs(a[minI] - a[i]) + abs(a[i] - a[i+1]);
    }
    writeln(res);
}

void main() {
    int T = read!int;
    foreach (t; 0 .. T) solve();
}

import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		sort (a);
		writeln (a[$ - 1] - a[0] + max (a[$ - 1] - a[$ - 2],
		    a[1] - a[0]));
	}
}


Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n.Each man has at most one father but may have arbitrary number of sons.Man number A is considered to be the ancestor of the man number B if at least one of the following conditions is satisfied:   A = B;  the man number A is the father of the man number B;  there is a man number C, such that the man number A is his ancestor and the man number C is the father of the man number B. Of course, if the man number A is an ancestor of the man number B and A ≠ B, then the man number B is not an ancestor of the man number A.The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens.  A list of candidates is prepared, containing some (possibly all) of the n men in some order.  Each of the n men decides to give a gift.  In order to choose a person to give a gift to, man A looks through the list and picks the first man B in the list, such that B is an ancestor of A and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself.  If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone. This year you have decided to help in organizing celebration and asked each of the n men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above?

In the first line of the input two integers n and m (0 ≤ m < n ≤ 100 000) are given — the number of the men in the Sasha's family and the number of family relations in it respectively. The next m lines describe family relations: the (i + 1)th line consists of pair of integers pi and qi (1 ≤ pi, qi ≤ n, pi ≠ qi) meaning that the man numbered pi is the father of the man numbered qi. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father. The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n), ith of which means that the man numbered i wants to give a gift to the man numbered ai. It is guaranteed that for every 1 ≤ i ≤ n the man numbered ai is an ancestor of the man numbered i.

Print an integer k (1 ≤ k ≤ n) — the number of the men in the list of candidates, in the first line. Print then k pairwise different positive integers not exceeding n — the numbers of the men in the list in an order satisfying every of the men's wishes, one per line. If there are more than one appropriate lists, print any of them. If there is no appropriate list print  - 1 in the only line.

NoteThe first sample explanation:   if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3 = 1);  if there would be no 2 in the list then the second man wish would not be satisfied (a2 = 2);  if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (a2 = 2).  if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (a3 = 1).

#!/usr/bin/env rdmd

import std.container;
import std.range;
import std.stdio;

struct Node {
    int id;
    bool isTarget, used, hasParent;
    Node* trg;
    Array!(Node*) children;

    void dfs(Node* lastParent) {
        used = true;
        if (trg == &this || trg == lastParent) {
            auto nextParent = isTarget ? &this : lastParent;
            foreach (child; children)
                child.dfs(nextParent);
            if (isTarget) {
                order[0] = id;
                order = order[1 .. $];
            }
        } else
            ok = false;
    }
}

Node[100_000] _nodes;
int _order[100_000];
int[ ] order;
bool ok;

void main() {
    int n, m;
    while (readf("%d %d ", &n, &m) == 2) {
        auto nodes = _nodes[0 .. n];
        foreach (int i, ref node; nodes)
            node = Node(i);
        while (m--) {
            int p, q;
            readf("%d %d ", &p, &q);
            p--;
            q--;
            nodes[p].children.insertBack(&nodes[q]);
            nodes[q].hasParent = true;
        }
        int result = 0;
        foreach (ref node; nodes) {
            int t;
            readf("%d ", &t);
            t--;
            node.trg = &nodes[t];
            if (!nodes[t].isTarget) {
                nodes[t].isTarget = true;
                result++;
            }
        }
        order = _order[0 .. result];
        ok = true;
        foreach (ref node; nodes)
            if (!node.hasParent && !node.used) {
                node.dfs(null);
                if (!ok)
                    break;
            }
        if (ok) {
            assert(order.empty);
            writeln(result);
            foreach (id; _order[0 .. result])
                writeln(id + 1);
        } else
            write("-1\n");
    }
}

#!/usr/bin/env rdmd

import std.container;
import std.range;
import std.stdio;

struct Node {
    int id;
    bool isTarget, used;
    Node* /*parent,*/ trg;
    Array!(Node*) children;

    this(int id) { this.id = id; }

    void dfs(Node* lastParent) {
        used = true;
        if (trg == &this || trg == lastParent) {
            auto nextParent = isTarget ? &this : lastParent;
            foreach (child; children)
                child.dfs(nextParent);
            if (isTarget) {
                order[0] = id;
                order = order[1 .. $];
            }
        } else
            ok = false;
    }
}

Node[100_000] _nodes;
int _order[100_000];
int[ ] order;
bool ok;

void main() {
    int n, m;
    while (readf("%d %d ", &n, &m) == 2) {
        auto nodes = _nodes[0 .. n];
        foreach (int i, ref node; nodes)
            node = Node(i);
        while (m--) {
            int p, q;
            readf("%d %d ", &p, &q);
            p--;
            q--;
            //nodes[q].parent = &nodes[p];
            nodes[p].children.insertBack(&nodes[q]);
        }
        int result = 0;
        foreach (ref node; nodes) {
            int t;
            readf("%d ", &t);
            t--;
            node.trg = &nodes[t];
            if (!nodes[t].isTarget) {
                nodes[t].isTarget = true;
                result++;
            }
        }
        order = _order[0 .. result];
        ok = true;
        foreach (ref node; nodes)
            if (!node.used) {
                node.dfs(null);
                if (!ok)
                    break;
            }
        assert(order.empty);
        if (ok) {
            writeln(result);
            foreach (id; _order[0 .. result])
                writeln(id + 1);
        } else
            write("-1\n");
    }
}


There is a trampoline park with $$$n$$$ trampolines in a line. The $$$i$$$-th of which has strength $$$S_i$$$.Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice. If at the moment Pekora jumps on trampoline $$$i$$$, the trampoline will launch her to position $$$i + S_i$$$, and $$$S_i$$$ will become equal to $$$\max(S_i-1,1)$$$. In other words, $$$S_i$$$ will decrease by $$$1$$$, except of the case $$$S_i=1$$$, when $$$S_i$$$ will remain equal to $$$1$$$. If there is no trampoline in position $$$i + S_i$$$, then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position $$$i + S_i$$$ by the same rule as above.Pekora can't stop jumping during the pass until she lands at the position larger than $$$n$$$ (in which there is no trampoline). Poor Pekora!Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all $$$S_i$$$ to $$$1$$$. What is the minimum number of passes she needs to reduce all $$$S_i$$$ to $$$1$$$?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 5000$$$) — the number of trampolines. The second line of each test case contains $$$n$$$ integers $$$S_1, S_2, \dots, S_n$$$ ($$$1 \le S_i \le 10^9$$$), where $$$S_i$$$ is the strength of the $$$i$$$-th trampoline. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$5000$$$.

For each test case, output a single integer — the minimum number of passes Pekora needs to do to reduce all $$$S_i$$$ to $$$1$$$.

NoteFor the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.)  $$$[1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]$$$  $$$[1,\textbf{4},1,2,1,\textbf{2},1]$$$  $$$[1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]$$$  $$$[1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]$$$ For the second test case, the optimal series of passes is show below.  $$$[\textbf{2},3]$$$  $$$[1,\textbf{3}]$$$  $$$[1,\textbf{2}]$$$ For the third test case, all $$$S_i$$$ are already equal to $$$1$$$.

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto S = RDA;

		auto imos = new long[](n+1);
		auto a = new long[](n+1);
		foreach (i; 0..n)
		{
			auto d = S[i] - imos[i] - a[i];
			if (d >= 1)
				ans[ti] += d - 1;
			else
				a[i+1] += (-d) + 1;
			auto l = cast(int)(i + 2);
			auto r = cast(int)(i + S[i] + 1);
			if (l <= n)
				++imos[l];
			if (r <= n)
				--imos[r];
			imos[i+1] = min(imos[i+1] + imos[i], 10^^10);
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto s = readln.splitter.map !(to !(int)).array;
		s ~= 0;
		long res = 0;
		auto p = iota (n + 1).array;
		for (int i = 0; i < n; i++)
		{
			int add = max (0, s[i] - n + i);
			res += add;
			s[i] = max (0, s[i] - add);
			while (s[i] > 1)
			{
				for (int j = i; j < n; j += s[j] + 1)
				{
					if (s[j] <= 1)
					{
						p[j] = p[j + 1];
					}
					j = p[j];
					s[j] = max (0, s[j] - 1);
				}
				res += 1;
			}
		}
		writeln (res);
	}
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    auto t = readln.chomp.to!int;

    while (t--) {
        int n;
        readf("%s", &n);
        readln;

        auto a = readln.splitter.map!(to!int).array;

        debug { a.writeln; }

        auto incoming = new int[] (a.length + 1);
        incoming[] = 0;

        long ans = 0;
        foreach (i, e; a) {
            int mx = min(i + e, a.length.to!int - 1);
            foreach (step; i + 2 .. mx + 1) {
                ++incoming[step];
            }

            int starts = max(0, e - 1 - incoming[i]);
            ans += starts;

            int carryover = max(0, incoming[i] - (e-1));
            incoming[i+1] += carryover;

            debug { ans.writeln; }
        }

        debug { incoming.writeln; }

        ans.writeln;
    }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const numCases = readInt();
      foreach (caseId; 0 .. numCases) {
        const N = readInt();
        auto S = new long[N];
        foreach (i; 0 .. N) {
          S[i] = readLong();
        }

        auto dp = new long[N];
        long ans;
        foreach (i; 0 .. N) {
          long now = dp[i];
          if (S[i] - 1 >= now) {
            ans += (S[i] - 1) - now;
            now = S[i] - 1;
          }
          if (i + 1 < N) {
            dp[i + 1] += now - (S[i] - 1);
          }
          foreach (j; i + 2 .. N) {
            if (j <= i + S[i]) {
              dp[j] += 1;
            }
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto S = RDA;

		auto imos = new long[](n+1);
		foreach (i; 0..n)
		{
			auto d = S[i] - imos[i];
			if (d >= 1)
				ans[ti] += d - 1;
			else
				imos[i+1] += (-d) + 1;
			auto l = cast(int)(i + 2);
			auto r = cast(int)(i + S[i] + 1);
			if (l <= n)
				++imos[l];
			if (r <= n)
				--imos[r];
			imos[i+1] = min(imos[i+1] + imos[i], 10^^10);
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto S = RDA;

		auto imos = new long[](n+1);
		foreach (i; 0..n)
		{
			auto d = S[i] - imos[i];
			if (d > 1)
				ans[ti] += d - 1;
			else
				imos[i+1] += (-d) + 1;
			auto l = cast(int)(i + 2);
			auto r = cast(int)(i + S[i] + 1);
			if (l <= n)
				++imos[l];
			if (r <= n)
				--imos[r];
			imos[i+1] = min(imos[i+1] + imos[i], 10^^10);
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto S = RDA;

		auto imos = new long[](n+1);
		foreach (i; 0..n)
		{
			auto d = S[i] - imos[i];
			if (d > 1)
				ans[ti] += d - 1;
			else
				imos[i+1] += (-d) + 1;
			auto l = cast(int)(i + 2);
			auto r = cast(int)(i + S[i] + 1);
			if (l <= n)
				++imos[l];
			if (r <= n)
				--imos[r];
			imos[i+1] = min(imos[i+1] + imos[i], 10^^9);
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto S = RDA;

		auto imos = new long[](n+1);
		foreach (i; 0..n)
		{
			auto d = S[i] - imos[i];
			if (d > 1)
				ans[ti] += d - 1;
			else
				imos[i+1] += (-d) + 1;
			auto l = cast(int)(i + 2);
			auto r = cast(int)(i + S[i] + 1);
			if (l <= n)
				++imos[l];
			if (r <= n)
				--imos[r];
			imos[i+1] += imos[i];
		}
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	int sn = 0;
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		sn += n;
		if (sn > 5000)
		{
			assert (false);
		}
		auto s = readln.splitter.map !(to !(int)).array;
		if (s.length != n)
		{
			assert (false);
		}
		long res = 0;
		foreach (i; 0..n)
		{
			int add = max (0, s[i] - n + i);
			res += add;
			s[i] -= add;
			while (s[i] > 1)
			{
				for (int j = i; j < n; j += s[j] + 1)
				{
					s[j] = max (0, s[j] - 1);
				}
				res += 1;
				while (n > 0 && s[n - 1] <= 1)
				{
					n -= 1;
				}
			}
		}
		writeln (res);
	}
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.ascii, std.typecons;

void main()
{
    auto t = readln.chomp.to!int;

    while (t--) {
        int n;
        readf("%s", &n);
        readln;

        auto a = readln.splitter.map!(to!int).array;

        debug { a.writeln; }

        auto incoming = new int[] (a.length + 1);

        long ans = 0;
        foreach (i, e; a) {
            int inside = min(e, a.length - i - 1);
            foreach (step; (inside+1).iota.drop(2)) {
                ++incoming[i + step];
            }

            auto additional = max(0, e - 1 - incoming[i]);
            ans += additional;
        }

        ans.writeln;
    }
}


While walking down the street Vanya saw a label "Hide&Seek". Because he is a programmer, he used & as a bitwise AND for these two words represented as a integers in base 64 and got new word. Now Vanya thinks of some string s and wants to know the number of pairs of words of length |s| (length of s), such that their bitwise AND is equal to s. As this number can be large, output it modulo 109 + 7.To represent the string as a number in numeral system with base 64 Vanya uses the following rules:  digits from '0' to '9' correspond to integers from 0 to 9;  letters from 'A' to 'Z' correspond to integers from 10 to 35;  letters from 'a' to 'z' correspond to integers from 36 to 61;  letter '-' correspond to integer 62;  letter '_' correspond to integer 63.

The only line of the input contains a single word s (1 ≤ |s| ≤ 100 000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'.

Print a single integer — the number of possible pairs of words, such that their bitwise AND is equal to string s modulo 109 + 7.

NoteFor a detailed definition of bitwise AND we recommend to take a look in the corresponding article in Wikipedia.In the first sample, there are 3 possible solutions:   z&_ = 61&63 = 61 = z  _&z = 63&61 = 61 = z  z&z = 61&61 = 61 = z

import std.stdio;

int zeros(char c)
{
	int n = -1;
	if (c >= '0' && c <= '9') {
		n = c - '0';
	}
	else if (c >= 'A' && c <= 'Z') {
		n = c - 'A' + 10;
	}
	else if (c >= 'a' && c <= 'z') {
		n = c - 'a' + 36;
	}
	else if (c == '-') {
		n = 62;
	}
	else if (c == '_') {
		n = 63;
	}

	int result = 0;
	for (int i = 0; i < 6; i++) {
		if ((n & 1) == 0) {
			result++;
		}

		n >>= 1;
	}

	return result;
}

long solve(string s)
{
	long result = 1;
	for (int i = 0; i < s.length; i++) {
		int z = zeros(s[i]);
		for (int j = 0; j < z; j++) {
			result *= 3;
			result %= 1000000007;
		}
	}
	return result;
}

void main()
{
	string s;

	readf("%s\n", &s);
	writef("%d\n", solve(s));
}


Yaroslav has an array p = p1, p2, ..., pn (1 ≤ pi ≤ n), consisting of n distinct integers. Also, he has m queries:  Query number i is represented as a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).  The answer to the query li, ri is the number of pairs of integers q, w (li ≤ q, w ≤ ri) such that pq is the divisor of pw. Help Yaroslav, answer all his queries.

The first line contains the integers n and m (1 ≤ n, m ≤ 2·105). The second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n). The following m lines contain Yaroslav's queries. The i-th line contains integers li, ri (1 ≤ li ≤ ri ≤ n).

Print m integers — the answers to Yaroslav's queries in the order they appear in the input. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

import std.stdio, std.string, std.conv, std.typecons;
import std.algorithm, std.range, std.array, std.container;
import std.math, std.numeric;

alias Tuple!(int, "l", int, "r") Query;

class BIT {
    int n;
    int[] t;

    this(int n) {
        this.n = n;
        t = new int[n];
    }

    void inc(int x) {
        for (++x; x <= n; x += x & -x)
            ++t[x - 1];
    }

    int get(int x) {
        int res = 0;
        for (; x; x -= x & -x)
            res += t[x - 1];
        return res;
    }
}

void main() {
    int n, m;
    scanf(" %d %d", &n, &m);
    auto p = new int[n];
    foreach (i; 0..n) scanf(" %d", &p[i]);
    auto q = new Query[m];
    foreach (i; 0..m) scanf(" %d %d", &q[i].l, &q[i].r), --q[i].l;

    int ma = reduce!max(p);

    auto loc = new int[ma + 1];
    loc[] = -1;
    foreach (i; 0..n) loc[p[i]] = i;

    auto adj = new int[][n + 1];
    foreach (d; p) {
        int x = loc[d];
        for (int dd = d; dd <= ma; dd += d) {
            int y = loc[dd]; if (y == -1) continue;
            if (x < y) adj[y] ~= x;
            else adj[x] ~= y;
        }
    }

    auto ln = new int[][n + 1];
    foreach (i; 0..m) ln[q[i].r] ~= i;

    auto ans = new int[m];
    BIT t = new BIT(n);
    foreach (r; 0..n + 1) {
        foreach (i; ln[r]) ans[i] = t.get(r) - t.get(q[i].l);
        foreach (y; adj[r]) t.inc(y);
    }

    foreach (a; ans) printf("%d\n", a);
}

import std.stdio, std.string, std.conv, std.typecons;
import std.algorithm, std.range, std.array, std.container;
import std.math, std.numeric;

alias Tuple!(int, "l", int, "r") Query;

class BIT {
    int n;
    long[] t;

    this(int n) {
        this.n = n;
        t = new long[n];
    }

    void inc(int x) {
        for (++x; x <= n; x += x & -x)
            ++t[x - 1];
    }

    long get(int x) {
        long res = 0;
        for (; x; x -= x & -x)
            res += t[x - 1];
        return res;
    }
}

void main() {
    int n, m;
    int[] p;
    Query[] q;

    scanf(" %d %d", &n, &m);
    p = new int[n];
    foreach (i; 0..n) scanf(" %d", &p[i]);
    q = new Query[m];
    foreach (i; 0..m) scanf(" %d %d", &q[i].l, &q[i].r), --q[i].l;

    int ma = reduce!max(p);

    int[] loc = new int[ma + 1];
    loc[] = -1;
    foreach (i; 0..n) loc[p[i]] = i;

    int[][] adj = new int[][n + 1];
    foreach (d; 1..ma + 1) {
        for (int dd = d; dd <= ma; dd += d) {
            int x = loc[d], y = loc[dd];
            if (x == -1 || y == -1) continue;
            if (x < y) swap(x, y);
            adj[x] ~= y;
        }
    }

    int[][] ln = new int[][n + 1];
    foreach (i; 0..m) {
        ln[q[i].r] ~= i;
    }

    long[] ans = new long[m];
    BIT t = new BIT(n);
    foreach (r; 0..n + 1) {
        foreach (i; ln[r]) ans[i] = t.get(r) - t.get(q[i].l);
        foreach (y; adj[r]) t.inc(y);
    }

    foreach (a; ans) writeln(a);
}

import std.stdio, std.string, std.conv, std.typecons;
import std.algorithm, std.range, std.array, std.container;
import std.math, std.numeric;

alias Tuple!(int, "l", int, "r") Query;

class BIT {
    int n;
    int[] t;

    this(int n) {
        this.n = n;
        t = new int[n];
    }

    void inc(int x) {
        for (++x; x <= n; x += x & -x)
            ++t[x - 1];
    }

    int get(int x) {
        int res = 0;
        for (; x; x -= x & -x)
            res += t[x - 1];
        return res;
    }
}

void main() {
    int n, m;
    int[] p;
    Query[] q;

    scanf(" %d %d", &n, &m);
    p = new int[n];
    foreach (i; 0..n) scanf(" %d", &p[i]);
    q = new Query[m];
    foreach (i; 0..m) scanf(" %d %d", &q[i].l, &q[i].r), --q[i].l;

    int ma = reduce!max(p);

    int[] loc = new int[ma + 1];
    loc[] = -1;
    foreach (i; 0..n) loc[p[i]] = i;

    int[][] adj = new int[][n + 1];
    foreach (d; 1..ma + 1) {
        for (int dd = d; dd <= ma; dd += d) {
            int x = loc[d], y = loc[dd];
            if (x == -1 || y == -1) continue;
            if (x < y) swap(x, y);
            adj[x] ~= y;
        }
    }

    int[][] ln = new int[][n + 1];
    foreach (i; 0..m) {
        ln[q[i].r] ~= i;
    }

    int[] ans = new int[m];
    BIT t = new BIT(n);
    foreach (r; 0..n + 1) {
        foreach (i; ln[r]) ans[i] = t.get(r) - t.get(q[i].l);
        foreach (y; adj[r]) t.inc(y);
    }

    foreach (a; ans) printf("%d\n", a);
}