Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in , which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maximum element of those that occur on the given segment exactly once. Help Sasha solve this problem.

The first line contains two positive integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n) — the number of array elements and the length of the segment.  Then follow n lines: the i-th one contains a single number ai ( - 109 ≤ ai ≤ 109).

Print n–k + 1 numbers, one per line: on the i-th line print of the maximum number of those numbers from the subarray ai ai + 1 … ai + k - 1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print "Nothing".

import std.stdio, std.string;
import std.random;

class Treap(T)
{
    class Node
    {
        Node left;
        Node right;
        T val;
        int priority;

        this()
        {
            left = right = null;
        }

        //int opCmp(Node node)
        //{
            //return this.priority - node.priority;
        //}

        int cmpPriority(int priority)
        {
            if (this.priority == priority) return 0;
            return this.priority < priority ? -1 : 1;
        }

        int cmpVal(T val)
        {
            if (this.val == val) return 0;
            return this.val < val ? -1 : 1;
        }
    }

    protected Node root;
    protected Xorshift rnd;
    protected int size;
    protected T minimal;
    protected T maximal;

    this()
    {
        root = null;
        rnd = Xorshift(1234567891);
        size = 0;
        minimal = T.max;
        maximal = T.min;
    }

    protected void leftRotate(ref Node node)
    {
        if (!node.right) return;
        auto rightNode = node.right;
        if (rightNode)
        {
            node.right = rightNode.left;
            rightNode.left = node;
            node = rightNode;
        }
    }

    protected void rightRotate(ref Node node)
    {
        if (!node.left) return;
        auto leftNode = node.left;
        if (leftNode)
        {
            node.left = leftNode.right;
            leftNode.right = node;
            node = leftNode;
        }
    }

    protected Node find(T val)
    {
        return _find(root, val);
    }

    protected Node _find(Node node, T val)
    {
        if (!node) return null;
        auto ret = node.cmpVal(val);
        if (ret == 0) return node;
        if (ret == -1) return _find(node.right, val);
        return _find(node.left, val);
    }

    public void insert(T val)
    {
        _insert(root, val);
    }

    protected void _insert(ref Node node, T val)
    {
        if (!node)
        {
            node = new Node();
            node.val = val;
            node.priority = this.rnd.front;
            this.rnd.seed(unpredictableSeed);
            ++ size;
            return;
        }
        auto ret = node.cmpVal(val);
        if (ret == 0) return;
        if (ret == 1)
        {
            _insert(node.left, val);
            if (node.left.priority > node.priority)
            {
                rightRotate(node);
            }
        }
        else if (ret == -1)
        {
            _insert(node.right, val);
            if (node.right.priority > node.priority)
            {
                leftRotate(node);
            }
        }
    }

    public void remove(T val)
    {
        _remove(root, val);
    }

    protected void _remove(ref Node node, T val)
    {
        if (!node) return;
        auto ret = node.cmpVal(val);
        if (ret == 0)
        {
            if (!node.left && !node.right)
            {
                node = null;
                -- size;
                return;
            }
            if (!node.left)
            {
                node = node.right;
                -- size;
                return;
            }
            if (!node.right)
            {
                node = node.left;
                -- size;
                return;
            }
            if (node.left.cmpPriority(node.right.priority) == 1)
            {
                rightRotate(node);
                _remove(node.right, val);
            }
            else
            {
                leftRotate(node);
                _remove(node.left, val);
            }
            return;
        }
        if (ret == -1)
        {
            _remove(node.right, val);
        }
        else
        {
            _remove(node.left, val);
        }
    }

    protected T _getMinimal(Node node)
    {
        if (!node) return minimal;
        if (!node.left) return node.val;
        return _getMinimal(node.left);
    }

    public T getMinimal()
    {
        return _getMinimal(root);
    }

    protected T _getMaximal(Node node)
    {
        if (!node) return maximal;
        if (!node.right) return node.val;
        return _getMaximal(node.right);
    }

    public T getMaximal()
    {
        return _getMaximal(root);
    }

    public void travel()
    {
        _travel(root);
    }

    protected void _travel(Node node)
    {
        if (!node) return;
        _travel(node.left);
        writeln(node.val);
        writeln(node.priority);
        _travel(node.right);
    }

    public void clear()
    {
        while (root)
        {
            remove(root.val);
        }
    }
}

void solve(int[] a, int k)
{
    auto n = cast(int)a.length;
    auto treap = new Treap!int();
    int[int] cnt;
    foreach (i; 0 .. n)
    {
        if (i >= k && a[i - k] != a[i])
        {
            -- cnt[a[i - k]];
            if (cnt[a[i - k]] == 1)
            {
                treap.insert(a[i - k]);
            }
            else
            {
                treap.remove(a[i - k]);
            }
        }
        if (i < k || a[i - k] != a[i])
        {
            if (!(a[i] in cnt))
            {
                cnt[a[i]] = 0;
            }
            ++ cnt[a[i]];
            if (cnt[a[i]] == 1)
            {
                treap.insert(a[i]);
            }
            else
            {
                treap.remove(a[i]);
            }
        }
        if (i >= k - 1)
        {
            if (treap.size > 0)
            {
                writeln(treap.getMaximal());
            }
            else
            {
                writeln("Nothing");
            }
        }
    }
}

int main(string[] args)
{
    int n, k;
    while (readf("%d %d\n", &n, &k) == 2)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf("%d\n", &a[i]);
        }
        solve(a, k);
    }
    return 0;
}

import std.stdio, std.string;
import std.container.rbtree;

void solve(int[] a, int k)
{
    auto n = cast(int)a.length;
    auto rbt = redBlackTree!int();
    int[int] cnt;
    foreach (i; 0 .. n)
    {
        if (i >= k && a[i - k] != a[i])
        {
            -- cnt[a[i - k]];
            if (cnt[a[i - k]] == 1)
            {
                rbt.insert(a[i - k]);
            }
            else
            {
                rbt.removeKey(a[i - k]);
            }
        }
        if (i < k || a[i - k] != a[i])
        {
            if (!(a[i] in cnt))
            {
                cnt[a[i]] = 0;
            }
            ++ cnt[a[i]];
            if (cnt[a[i]] == 1)
            {
                rbt.insert(a[i]);
            }
            else
            {
                rbt.removeKey(a[i]);
            }
        }
        if (i >= k - 1)
        {
            if (rbt.length > 0)
            {
                writeln(rbt.back());
            }
            else
            {
                writeln("Nothing");
            }
        }
    }
}

int main(string[] args)
{
    int n, k;
    while (readf("%d %d\n", &n, &k) == 2)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf("%d\n", &a[i]);
        }
        solve(a, k);
    }
    return 0;
}

import std.stdio, std.string;
import std.random;

class Treap(T)
{
    class Node
    {
        Node left;
        Node right;
        T val;
        int priority;

        this()
        {
            left = right = null;
        }

        //int opCmp(Node node)
        //{
            //return this.priority - node.priority;
        //}

        int cmpPriority(int priority)
        {
            if (this.priority == priority) return 0;
            return this.priority < priority ? -1 : 1;
        }

        int cmpVal(T val)
        {
            if (this.val == val) return 0;
            return this.val < val ? -1 : 1;
        }
    }

    protected Node root;
    protected Xorshift rnd;
    protected int size;
    protected T minimal;
    protected T maximal;

    this()
    {
        root = null;
        rnd = Xorshift(1234567891);
        size = 0;
        minimal = T.max;
        maximal = T.min;
    }

    protected void leftRotate(ref Node node)
    {
        if (!node.right) return;
        auto rightNode = node.right;
        if (rightNode)
        {
            node.right = rightNode.left;
            rightNode.left = node;
            node = rightNode;
        }
    }

    protected void rightRotate(ref Node node)
    {
        if (!node.left) return;
        auto leftNode = node.left;
        if (leftNode)
        {
            node.left = leftNode.right;
            leftNode.right = node;
            node = leftNode;
        }
    }

    protected Node find(T val)
    {
        return _find(root, val);
    }

    protected Node _find(Node node, T val)
    {
        if (!node) return null;
        auto ret = node.cmpVal(val);
        if (ret == 0) return node;
        if (ret == -1) return _find(node.right, val);
        return _find(node.left, val);
    }

    public void insert(T val)
    {
        _insert(root, val);
    }

    protected void _insert(ref Node node, T val)
    {
        if (!node)
        {
            node = new Node();
            node.val = val;
            node.priority = this.rnd.front;
            this.rnd.seed(unpredictableSeed);
            ++ size;
            return;
        }
        auto ret = node.cmpVal(val);
        if (ret == 0) return;
        ++ size;
        if (ret == 1)
        {
            _insert(node.left, val);
            if (node.left.priority > node.priority)
            {
                rightRotate(node);
            }
        }
        else if (ret == -1)
        {
            _insert(node.right, val);
            if (node.right.priority > node.priority)
            {
                leftRotate(node);
            }
        }
    }

    public void remove(T val)
    {
        _remove(root, val);
    }

    protected void _remove(ref Node node, T val)
    {
        if (!node) return;
        auto ret = node.cmpVal(val);
        if (ret == 0)
        {
            -- size;
            if (!node.left && !node.right)
            {
                node = null;
                return;
            }
            if (!node.left)
            {
                node = node.right;
                return;
            }
            if (!node.right)
            {
                node = node.left;
                return;
            }
            if (node.left.cmpPriority(node.right.priority) == 1)
            {
                rightRotate(node);
                _remove(node.right, val);
            }
            else
            {
                leftRotate(node);
                _remove(node.left, val);
            }
            return;
        }
        if (ret == -1)
        {
            _remove(node.right, val);
        }
        else
        {
            _remove(node.left, val);
        }
    }

    protected T _getMinimal(Node node)
    {
        if (!node) return minimal;
        if (!node.left) return node.val;
        return _getMinimal(node.left);
    }

    public T getMinimal()
    {
        return _getMinimal(root);
    }

    protected T _getMaximal(Node node)
    {
        if (!node) return maximal;
        if (!node.right) return node.val;
        return _getMaximal(node.right);
    }

    public T getMaximal()
    {
        return _getMaximal(root);
    }

    public void travel()
    {
        _travel(root);
    }

    protected void _travel(Node node)
    {
        if (!node) return;
        _travel(node.left);
        writeln(node.val);
        writeln(node.priority);
        _travel(node.right);
    }

    public void clear()
    {
        while (root)
        {
            remove(root.val);
        }
    }
}

void solve(int[] a, int k)
{
    auto n = cast(int)a.length;
    auto treap = new Treap!int();
    int[int] cnt;
    foreach (i; 0 .. n)
    {
        if (i >= k && a[i - k] != a[i])
        {
            -- cnt[a[i - k]];
            if (cnt[a[i - k]] == 1)
            {
                treap.insert(a[i - k]);
            }
            else
            {
                treap.remove(a[i - k]);
            }
        }
        if (i < k || a[i - k] != a[i])
        {
            if (!(a[i] in cnt))
            {
                cnt[a[i]] = 0;
            }
            ++ cnt[a[i]];
            if (cnt[a[i]] == 1)
            {
                treap.insert(a[i]);
            }
            else
            {
                treap.remove(a[i]);
            }
        }
        if (i >= k - 1)
        {
            if (treap.size > 0)
            {
                writeln(treap.getMaximal());
            }
            else
            {
                writeln("Nothing");
            }
        }
    }
}

int main(string[] args)
{
    int n, k;
    while (readf("%d %d\n", &n, &k) == 2)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf("%d\n", &a[i]);
        }
        solve(a, k);
    }
    return 0;
}

import std.stdio, std.string;
import std.random;

class Treap(T)
{
    class Node
    {
        Node left;
        Node right;
        T val;
        int priority;

        this()
        {
            left = right = null;
        }

        //int opCmp(Node node)
        //{
            //return this.priority - node.priority;
        //}

        int cmpPriority(int priority)
        {
            if (this.priority == priority) return 0;
            return this.priority < priority ? -1 : 1;
        }

        int cmpVal(T val)
        {
            if (this.val == val) return 0;
            return this.val < val ? -1 : 1;
        }
    }

    protected Node root;
    protected Xorshift rnd;
    protected int size;
    protected T minimal;
    protected T maximal;

    this()
    {
        root = null;
        rnd = Xorshift(1234567891);
        size = 0;
        minimal = T.max;
        maximal = T.min;
    }

    protected void leftRotate(ref Node node)
    {
        if (!node.right) return;
        auto rightNode = node.right;
        if (rightNode)
        {
            node.right = rightNode.left;
            rightNode.left = node;
            node = rightNode;
        }
    }

    protected void rightRotate(ref Node node)
    {
        if (!node.left) return;
        auto leftNode = node.left;
        if (leftNode)
        {
            node.left = leftNode.right;
            leftNode.right = node;
            node = leftNode;
        }
    }

    protected Node find(T val)
    {
        return _find(root, val);
    }

    protected Node _find(Node node, T val)
    {
        if (!node) return null;
        auto ret = node.cmpVal(val);
        if (ret == 0) return node;
        if (ret == -1) return _find(node.right, val);
        return _find(node.left, val);
    }

    public void insert(T val)
    {
        _insert(root, val);
    }

    protected void _insert(ref Node node, T val)
    {
        if (!node)
        {
            node = new Node();
            node.val = val;
            node.priority = this.rnd.front;
            this.rnd.seed(unpredictableSeed);
            ++ size;
            return;
        }
        auto ret = node.cmpVal(val);
        if (ret == 0) return;
        if (ret == 1)
        {
            _insert(node.left, val);
            if (node.left.priority > node.priority)
            {
                rightRotate(node);
            }
        }
        else if (ret == -1)
        {
            _insert(node.right, val);
            if (node.right.priority > node.priority)
            {
                leftRotate(node);
            }
        }
    }

    public void remove(T val)
    {
        _remove(root, val);
    }

    protected void _remove(ref Node node, T val)
    {
        if (!node) return;
        auto ret = node.cmpVal(val);
        if (ret == 0)
        {
            -- size;
            if (!node.left && !node.right)
            {
                node = null;
                return;
            }
            if (!node.left)
            {
                node = node.right;
                return;
            }
            if (!node.right)
            {
                node = node.left;
                return;
            }
            if (node.left.cmpPriority(node.right.priority) == 1)
            {
                rightRotate(node);
                _remove(node.right, val);
            }
            else
            {
                leftRotate(node);
                _remove(node.left, val);
            }
            return;
        }
        if (ret == -1)
        {
            _remove(node.right, val);
        }
        else
        {
            _remove(node.left, val);
        }
    }

    protected T _getMinimal(Node node)
    {
        if (!node) return minimal;
        if (!node.left) return node.val;
        return _getMinimal(node.left);
    }

    public T getMinimal()
    {
        return _getMinimal(root);
    }

    protected T _getMaximal(Node node)
    {
        if (!node) return maximal;
        if (!node.right) return node.val;
        return _getMaximal(node.right);
    }

    public T getMaximal()
    {
        return _getMaximal(root);
    }

    public void travel()
    {
        _travel(root);
    }

    protected void _travel(Node node)
    {
        if (!node) return;
        _travel(node.left);
        writeln(node.val);
        writeln(node.priority);
        _travel(node.right);
    }

    public void clear()
    {
        while (root)
        {
            remove(root.val);
        }
    }
}

void solve(int[] a, int k)
{
    auto n = cast(int)a.length;
    auto treap = new Treap!int();
    int[int] cnt;
    foreach (i; 0 .. n)
    {
        if (i >= k && a[i - k] != a[i])
        {
            -- cnt[a[i - k]];
            if (cnt[a[i - k]] == 1)
            {
                treap.insert(a[i - k]);
            }
            else
            {
                treap.remove(a[i - k]);
            }
        }
        if (i < k || a[i - k] != a[i])
        {
            if (!(a[i] in cnt))
            {
                cnt[a[i]] = 0;
            }
            ++ cnt[a[i]];
            if (cnt[a[i]] == 1)
            {
                treap.insert(a[i]);
            }
            else
            {
                treap.remove(a[i]);
            }
        }
        if (i >= k - 1)
        {
            if (treap.size > 0)
            {
                writeln(treap.getMaximal());
            }
            else
            {
                writeln("Nothing");
            }
        }
    }
}

int main(string[] args)
{
    int n, k;
    while (readf("%d %d\n", &n, &k) == 2)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf("%d\n", &a[i]);
        }
        solve(a, k);
    }
    return 0;
}

import std.stdio, std.string;
import std.container.rbtree;

void solve(int[] a, int k)
{
    auto n = cast(int)a.length;
    auto rbt = redBlackTree!int();
    int[int] cnt;
    foreach (i; 0 .. n)
    {
        if (i >= k && a[i - k] != a[i])
        {
            -- cnt[a[i - k]];
            if (cnt[a[i - k]] == 1)
            {
                rbt.insert(a[i - k]);
            }
        }
        if (i < k || a[i - k] != a[i])
        {
            if (!(a[i] in cnt))
            {
                cnt[a[i]] = 0;
            }
            ++ cnt[a[i]];
            if (cnt[a[i]] == 1)
            {
                rbt.insert(a[i]);
            }
            else
            {
                rbt.removeKey(a[i]);
            }
        }
        if (i >= k - 1)
        {
            if (rbt.length > 0)
            {
                writeln(rbt.back());
            }
            else
            {
                writeln("Nothing");
            }
        }
    }
}

int main(string[] args)
{
    int n, k;
    while (readf("%d %d\n", &n, &k) == 2)
    {
        auto a = new int[n];
        foreach (i; 0 .. n)
        {
            readf("%d\n", &a[i]);
        }
        solve(a, k);
    }
    return 0;
}


A string is called diverse if it contains consecutive (adjacent) letters of the Latin alphabet and each letter occurs exactly once. For example, the following strings are diverse: "fced", "xyz", "r" and "dabcef". The following string are not diverse: "az", "aa", "bad" and "babc". Note that the letters 'a' and 'z' are not adjacent.Formally, consider positions of all letters in the string in the alphabet. These positions should form contiguous segment, i.e. they should come one by one without any gaps. And all letters in the string should be distinct (duplicates are not allowed).You are given a sequence of strings. For each string, if it is diverse, print "Yes". Otherwise, print "No".

The first line contains integer $$$n$$$ ($$$1 \le n \le 100$$$), denoting the number of strings to process. The following $$$n$$$ lines contains strings, one string per line. Each string contains only lowercase Latin letters, its length is between $$$1$$$ and $$$100$$$, inclusive.

Print $$$n$$$ lines, one line per a string in the input. The line should contain "Yes" if the corresponding string is diverse and "No" if the corresponding string is not diverse. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable.

import std.stdio;
import std.string;

private int n;

private int good(string s)
{
    int[26] x;
    int i;
    for (i = 0; i < 26; i++)
        x[i] = 0;
    foreach (c; s)
        x[c - 'a']++;
    for (i = 0; i < 26 && !x[i]; i++) {}
    for (; i < 26 && x[i] == 1; i++) {}
    for (; i < 26 && !x[i]; i++) {}
    return i == 26;
}

void main()
{
    scanf("%d\n", &n);
    for (auto i = 0; i < n; i++)
        writeln(good(readln().strip()) ? "Yes" : "No");
}

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

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    while (N--) solve;
}

void solve() {
    auto S = readln.chomp.map!(a => (a - 'a').to!int).array;
    S.sort();
    bool ok = true;
    foreach (i; 0..S.length.to!int-1) {
        if (S[i] + 1 != S[i+1]) {
            ok = false;
        }
    }
    writeln(ok ? "Yes" : "No");
}


An identity permutation of length $$$n$$$ is an array $$$[1, 2, 3, \dots, n]$$$.We performed the following operations to an identity permutation of length $$$n$$$:  firstly, we cyclically shifted it to the right by $$$k$$$ positions, where $$$k$$$ is unknown to you (the only thing you know is that $$$0 \le k \le n - 1$$$). When an array is cyclically shifted to the right by $$$k$$$ positions, the resulting array is formed by taking $$$k$$$ last elements of the original array (without changing their relative order), and then appending $$$n - k$$$ first elements to the right of them (without changing relative order of the first $$$n - k$$$ elements as well). For example, if we cyclically shift the identity permutation of length $$$6$$$ by $$$2$$$ positions, we get the array $$$[5, 6, 1, 2, 3, 4]$$$;  secondly, we performed the following operation at most $$$m$$$ times: pick any two elements of the array and swap them. You are given the values of $$$n$$$ and $$$m$$$, and the resulting array. Your task is to find all possible values of $$$k$$$ in the cyclic shift operation.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n \le 3 \cdot 10^5$$$; $$$0 \le m \le \frac{n}{3}$$$). The second line contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, each integer from $$$1$$$ to $$$n$$$ appears in this sequence exactly once) — the resulting array. The sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.

For each test case, print the answer in the following way:   firstly, print one integer $$$r$$$ ($$$0 \le r \le n$$$) — the number of possible values of $$$k$$$ for the cyclic shift operation;  secondly, print $$$r$$$ integers $$$k_1, k_2, \dots, k_r$$$ ($$$0 \le k_i \le n - 1$$$) — all possible values of $$$k$$$ in increasing order.

NoteConsider the example:   in the first test case, the only possible value for the cyclic shift is $$$3$$$. If we shift $$$[1, 2, 3, 4]$$$ by $$$3$$$ positions, we get $$$[2, 3, 4, 1]$$$. Then we can swap the $$$3$$$-rd and the $$$4$$$-th elements to get the array $$$[2, 3, 1, 4]$$$;  in the second test case, the only possible value for the cyclic shift is $$$0$$$. If we shift $$$[1, 2, 3]$$$ by $$$0$$$ positions, we get $$$[1, 2, 3]$$$. Then we don't change the array at all (we stated that we made at most $$$1$$$ swap), so the resulting array stays $$$[1, 2, 3]$$$;  in the third test case, all values from $$$0$$$ to $$$2$$$ are possible for the cyclic shift:   if we shift $$$[1, 2, 3]$$$ by $$$0$$$ positions, we get $$$[1, 2, 3]$$$. Then we can swap the $$$1$$$-st and the $$$3$$$-rd elements to get $$$[3, 2, 1]$$$;  if we shift $$$[1, 2, 3]$$$ by $$$1$$$ position, we get $$$[3, 1, 2]$$$. Then we can swap the $$$2$$$-nd and the $$$3$$$-rd elements to get $$$[3, 2, 1]$$$;  if we shift $$$[1, 2, 3]$$$ by $$$2$$$ positions, we get $$$[2, 3, 1]$$$. Then we can swap the $$$1$$$-st and the $$$2$$$-nd elements to get $$$[3, 2, 1]$$$;   in the fourth test case, we stated that we didn't do any swaps after the cyclic shift, but no value of cyclic shift could produce the array $$$[1, 2, 3, 4, 6, 5]$$$.

import std.stdio;
import std.ascii;
import core.stdc.stdio;
import std.algorithm;
import std.array;
import std.range;

void main()
{
	int t = readInt!int;
	while (t--)
	{
		solve();
	}
}
void solve()
{
	int n = readInt!int;
	int m = readInt!int;
	auto p = new int[](n);
	foreach(ref pi; p) pi = readInt!int - 1;
	int[] kcnt = new int[](n);
	foreach(i, ref pi; p)
	{
		kcnt[((pi-cast(int)i)%n+n)%n]++;
	}
	int[] ks;
	foreach(k; 0 .. n)
	{
		if (kcnt[k] >= n/3)
		{
			debug writeln("trying k = ", k);
			int[] cycledP = new int[](n);
			foreach(i, ref ci; cycledP)
			{
				ci = p[((cast(int)i-k)%n+n)%n];
			}
			debug writeln("cycled is ", cycledP);
			int requiredSwaps = noSwaps(cycledP);
			debug writeln("requires ", requiredSwaps, " swaps");
			if (requiredSwaps <= m)
			{
				ks ~= k;
			}
		}
	}
	auto rks = new int[](ks.length);
	foreach(i, ref rksi; rks)
	{
		rksi = (n-ks[i])%n;
	}
	sort(rks);
	write(ks.length, " ");
	foreach(k; rks) write(k, " ");
	writeln;
}

int noSwaps(int[] p)
{
	bool[] visited = new bool[](p.length);
	void visit(int n)
	{
		assert(!visited[n]);
		visited[n] = true;
		if (!visited[p[n]]) visit(p[n]);
	}
	int g = 0;
	foreach(i; 0 .. cast(int)p.length)
	{
		if (!visited[i])
		{
			visit(i);
			g++;
		}
	}
	return cast(int)p.length - g;
}

/* INPUT ROUTINES */
int currChar;
static this()
{
	currChar = getchar();
}

char topChar()
{
	return cast(char) currChar;
}

void popChar()
{
	currChar = getchar();
}

auto readInt(T)()
{
	T num = 0;
	int sgn = 0;
	while (topChar.isWhite)
		popChar;
	while (topChar == '-' || topChar == '+')
	{
		sgn ^= (topChar == '-');
		popChar;
	}
	while (topChar.isDigit)
	{
		num *= 10;
		num += (topChar - '0');
		popChar;
	}
	if (sgn)
		return -num;
	return num;
}

string readString()
{
	string res = "";
	while (topChar.isWhite)
		popChar;
	while (!topChar.isWhite)
	{
		res ~= topChar;
		popChar;
	}
	return res;
}
/**MODULAR SYSTEM*/
struct Z(immutable long m)
{
	long rep;
	this(long num)
	{
		rep = num;
	}

	Z!m opBinary(string operator)(Z!m rhs)
	{
		static if (operator == "+")
		{
			long result = rhs.rep + this.rep;
			if (result >= m)
				result -= m;
			return Z!m(result);
		}
		else static if (operator == "-")
		{
			long result = this.rep - rhs.rep;
			if (result < 0)
				result += m;
			return Z!m(result);
		}
		else static if (operator == "*")
		{
			long result = this.rep * rhs.rep;
			if (result >= m)
				result %= m;
			return Z!m(result);
		}
		else
			static assert(text("Operator ", operator, " not supported"));
	}

	Z!m opBinary(string operator)(long exponent) if (operator == "^^")
	{
		assert(exponent >= 0);
		Z!m base = this;
		Z!m result = 1;
		while (exponent)
		{
			if (exponent & 1)
				result = result * base;
			base = base * base;
			exponent >>= 1;
		}
		return result;
	}

	invariant
	{
		assert(rep >= 0 && rep < m);
	}
}

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

bool canShift (int n, int m, int [] p, int shift)
{
        auto q = new int [n];
        foreach (i; 0..n)
        {
        	q[i] = (p[i] + n - shift) % n;
        }

	auto used = new bool [n];
	int ops = 0;
	foreach (i; 0..n)
	{
		if (!used[i] && q[i] != i)
		{
			used[i] = true;
			for (int j = q[i]; j != i; j = q[j])
			{
				used[j] = true;
				ops += 1;
			}
		}
	}

	return ops <= m;
}

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		int n, m;
		readf !(" %s %s") (n, m);
		readln;
		auto p = readln.splitter.map !(to !(int)).array;
		p[] -= 1;

		auto d = new int [n];
		foreach (i; 0..n)
		{
			d[(p[i] + n - i) % n] += 1;
		}

		int [] answer;
		foreach (shift; 0..n)
		{
			if (d[shift] + 2 * m >= n)
			{
				if (canShift (n, m, p, shift))
				{
					answer ~= (n - shift) % n;
				}
			}
		}
		sort (answer);

		writefln !("%s%( %s%)") (answer.length, answer);
	}
}


Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $$$a$$$ of $$$n$$$ non-negative integers.Dark created that array $$$1000$$$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) and replaces all missing elements in the array $$$a$$$ with $$$k$$$.Let $$$m$$$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $$$|a_i - a_{i+1}|$$$ for all $$$1 \leq i \leq n - 1$$$) in the array $$$a$$$ after Dark replaces all missing elements with $$$k$$$.Dark should choose an integer $$$k$$$ so that $$$m$$$ is minimized. Can you help him?

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n \leq 10^{5}$$$) — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-1 \leq a_i \leq 10 ^ {9}$$$). If $$$a_i = -1$$$, then the $$$i$$$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $$$n$$$ for all test cases does not exceed $$$4 \cdot 10 ^ {5}$$$.

Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $$$m$$$ and an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) that makes the maximum absolute difference between adjacent elements in the array $$$a$$$ equal to $$$m$$$. Make sure that after replacing all the missing elements with $$$k$$$, the maximum absolute difference between adjacent elements becomes $$$m$$$. If there is more than one possible $$$k$$$, you can print any of them.

NoteIn the first test case after replacing all missing elements with $$$11$$$ the array becomes $$$[11, 10, 11, 12, 11]$$$. The absolute difference between any adjacent elements is $$$1$$$. It is impossible to choose a value of $$$k$$$, such that the absolute difference between any adjacent element will be $$$\leq 0$$$. So, the answer is $$$1$$$.In the third test case after replacing all missing elements with $$$6$$$ the array becomes $$$[6, 6, 9, 6, 3, 6]$$$.  $$$|a_1 - a_2| = |6 - 6| = 0$$$;  $$$|a_2 - a_3| = |6 - 9| = 3$$$;  $$$|a_3 - a_4| = |9 - 6| = 3$$$;  $$$|a_4 - a_5| = |6 - 3| = 3$$$;  $$$|a_5 - a_6| = |3 - 6| = 3$$$. So, the maximum difference between any adjacent elements is $$$3$$$.

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;
T chmin(T)(ref T x, T y){ if(x > y) x = y; return x; } T chmax(T)(ref T x, T y){ if(x < y) x = y; return x; }

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	foreach(_; 0 .. rint){
		int n = rint;
		long[] as = rlong(n);
		long l = 1_000_000_000, r = 0;
		foreach(i; 0 .. n){
			if(as[i] >= 0) continue;
			else{
				if(i > 0 && as[i - 1] >= 0) l.chmin(as[i - 1]), r.chmax(as[i - 1]);
				if(i < n - 1 && as[i + 1] >= 0) l.chmin(as[i + 1]), r.chmax(as[i + 1]);
			}
		}
		log("n:", n, "as:", as, "l:", l, "r:", r);
		long ans = 0;
		foreach(i; 0 .. n - 1){
			if(as[i] >= 0 && as[i + 1] >= 0) ans.chmax(abs(as[i + 1] - as[i]));
		}
		ans.chmax((r - l + 1) / 2);

		writeln(ans, " ", (l + r) / 2);
	}
}

import std.container;
import std.algorithm;
import std.range;
import std.array;
import std.stdio;
import std.utf;
import std.math;
import std.typecons;

alias type = multi;
struct TestCase
{
  mixin prettyPrinting;

  long n;
  @("n") long[] a;

  void solve(long tc = -1)
  {
    auto adjacent = redBlackTree!long();
    foreach(i; 0 .. n)
      {
        if (a.at(i) == -1)
          {
            if (i - 1 >= 0 && a.at(i - 1) != -1)
              adjacent.insert(a.at(i - 1));
            if (i + 1 < n && a.at(i + 1) != -1)
              adjacent.insert(a.at(i + 1));
          }
      }
    if (adjacent.empty)
      {
        writeln(0, " ", 0);
      }
    else
      {
        auto k = (adjacent.front + adjacent.back) / 2;
        foreach(i; 0 .. n)
          if (a.at(i) == -1)
            a.at(i) = k;
        auto m = iota(0, n - 1).fold!((res, i) => max(res, abs(a.at(i) - a.at(i + 1))))(long.min);
        writeln(m, " ", k);
      }
  }
}

mixin template prettyPrinting()
{
  string toString()
  {
    alias structType = typeof(this);
    auto res = appender!(string)();
    res.put(structType.stringof);
    res.put("(");
    static foreach(fieldName; FieldNameTuple!structType)
      {
        res.put(text(" ", fieldName, ": ", mixin(fieldName)));
      }
    res.put(" )");
    res.put("\n");
    return res[];
  }
}

struct Graph(N)
{
  size_t size;
  this(S)(S size)
  {
    adjacencyList.length = cast(size_t) size;
    this.size = cast(size_t) size;
    this.visited = makeSlice!bool(size);
  }
  N[][] adjacencyList;
  alias ne = neighbours;
  N[] neighbours(N n)
  {
    return adjacencyList.at(n);
  }
  alias bcon = biconnect;
  void biconnect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
    adjacencyList.at(b) ~= a;
  }
  alias con = connect;
  void connect(N a, N b)
  {
    adjacencyList.at(a) ~= b;
  }
  void connect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        connect(edge[0], edge[1]);
      }
  }
  void biconnect(R)(R edges)
  {
    foreach(ref edge; edges)
      {
        biconnect(edge[0], edge[1]);
      }
  }
  alias deg = degree!long;
  auto degree(T)(N a)
  {
    return cast(T) adjacencyList.at(a).length;
  }

  auto visited = new bool[0];
  void dfs(Visitor)(long startNode)
  {
    auto visitor = Visitor.init;
    void internalDfs(long node)
    {
      visited.set(node, true);
      static if (is(typeof({visitor.n = node;})))
        visitor.n = node;
      static if (is(typeof({visitor.pre;})))
        visitor.pre;
      foreach(w; ne(node))
        if (!visited.at(w))
          {
            static if (is(typeof({visitor.w = w;})))
              visitor.w = w;
            static if (is(typeof(visitor.pren)))
              visitor.pren;
            internalDfs(w);
            static if (is(typeof(visitor.postn)))
              visitor.postn;
          }
      static if (is(typeof(visitor.post)))
        visitor.post;
    }
    internalDfs(startNode);
  }
  void dfs(long startNode)
  {
    struct DefVisitor {}
    dfs!DefVisitor(startNode);
  }
}

DList!string _words;

template baseType(W)
{
  static if (isDynamicArray!W)
    alias baseType = baseType!(typeof((delegate() {W w; return w[0];})()));
  else
    alias baseType = W;
}

auto makeSlice(E, W, T...)(W size, T otherSizes)
{
  static if (T.length == 0)
    {
      E[] res;
      res.length = cast(size_t) size;
      return res;
    }
  else
    {
      typeof(makeSlice!E(otherSizes))[] res;
      res.length = cast(size_t) size;
      foreach(ref row; res)
        row = makeSlice!E(otherSizes);
      return res;
    }
}

void read(T...)(ref T t)
{
  void basicTypeRead(W)(ref W w)
  {
    import std.stdio: readln;
    while(_words.empty)
      {
        import std.array: split;
        foreach(word; readln.split)
          {
            _words.insertBack(word);
          }
      }
    import std.conv: to;
    string word = _words.front;
    _words.removeFront;
    w = to!W(word);
  }
  void iterableRead(W)(ref W w)
  {
    foreach(ref element; w)
      read(element);
  }
  void tupleRead(W)(ref W w)
  {
    static foreach(j; 0 .. W.length)
      read(w[j]);
  }
  void structRead(W)(ref W readStruct)
  {
    static foreach(fieldName; FieldNameTuple!W)
      {
        with(readStruct)
          {
            alias fieldSymbol = mixin(W.stringof, ".", fieldName);
            alias typeOfField = typeof(mixin(fieldName));
            static if (hasUDA!(fieldSymbol, string))
              {
                static assert(isDynamicArray!(typeOfField)
                              , " A field with dimension initialization UDA must be a dynamic array.");
                enum uda = getUDAs!(fieldSymbol, string)[0];
                mixin(fieldName) = mixin(q{makeSlice!(baseType!typeOfField)(}, uda, q{)});
              }
            else static if (isDynamicArray!typeOfField && !is(typeOfField == string))
              {
                pragma(msg, "Warning: You probably want to add dimension to input ", fieldName);
              }
            read(mixin(fieldName));
          }
      }
  }
  static foreach(i; 0 .. T.length)
    {
      static if (isBasicType!(T[i]) || is(T[i] == string))
        basicTypeRead(t[i]);
      else static if (is(typeof({foreach(ref e; t[i]){}})))
        iterableRead(t[i]);
      else static if (isTuple!(T[i]))
        tupleRead(t[i]);
      else static if (isAggregateType!(T[i]))
        structRead(t[i]);
    }
}

auto next(T)()
{
  T t;
  read(t);
  return t;
}

size_t ind(T)(T t)
{
  return cast(size_t)t;
}

ref auto at(R, I)(ref R r, I i)
{
  return r[cast(size_t)i];
}

void set(R, I, V)(ref R r, I i, V v)
{
  r[cast(size_t)i] = v;
}

import std.conv;
import std.traits;
import std.meta;
int _loglevel = -1;
void _log(string functionName, argSeq...)()
{
  foreach(i; 0 .. _loglevel * 4)
    write(' ');
  write(functionName);
  static foreach(arg; argSeq)
    {
      write(" ", arg.stringof, " = ", arg);
    }
  writeln;
}
enum logCall =
  q{
    debug
    {
     {
     _loglevel++;
     alias _parameterTuple = ParameterIdentifierTuple!(mixin(split(__FUNCTION__, ".").back));
     static if (_parameterTuple.length > 0)
       mixin(`_log!(__FUNCTION__, `, [_parameterTuple].joiner(", ").array, `);`);
     else
       _log!(__FUNCTION__);
      }
     foreach(i; 0 .. _loglevel * 4) write(' ');
     writeln('{');
     scope(exit)
       {
         foreach(i; 0 .. _loglevel * 4) write(' ');
         writeln('}');
         _loglevel--;
       }
    }
};

void logSym(S...)(int line = __LINE__, string file = __FUNCTION__)
{
  debug
    {
      foreach(i; 0 .. _loglevel * 4) write(' ');
      write(file, "(", line, "):");
      static foreach(s; S)
        write(" ", s.stringof, " = ", s);
      writeln;

    }
}

Array!E makeArray(E, S)(S size, lazy E value)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  foreach(ref ai; a)
    ai = value();
  return a;
}
Array!E makeArray(E, S)(S size)
{
  auto a = Array!E();
  a.length = cast(size_t) size;
  return a;
}

template mem(alias F)
{
  ReturnType!F[Tuple!(Parameters!F)] table;
  ReturnType!F mem(Parameters!F args)
  {
    return table.require(tuple(args), F(args));
  }
}

string input(string names, string type = "long")
{
  return text(type, " ", names, "; read(", names, ");");
}

auto pmod(T, W)(T t, W w)
{
  return (t%w + w) % w;
}

void rep(S, F)(F f, S times)
{
  static if (is(typeof(f(i))))
    foreach(i; 0 .. times)
      f(i);
  else
    foreach(i; 0 .. times)
      f();
}

void multi()
{
  foreach(tc; 0 .. next!long)
    next!TestCase.solve(tc);
}

void single()
{
  auto tc = next!TestCase;
  tc.solve;
}

void main()
{
  type;
}

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 = 998244353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }

void main()
{
	auto t = RD!int;
	auto ans = new long[][](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;
		long b = long.max, c = long.min;
		foreach (i; 0..n)
		{
			if (a[i] != -1) continue;
			if (i != 0)
			{
				if (a[i-1] != -1)
				{
					b.chmin(a[i-1]);
					c.chmax(a[i-1]);
				}
			}
			if (i != n-1)
			{
				if (a[i+1] != -1)
				{
					b.chmin(a[i+1]);
					c.chmax(a[i+1]);
				}
			}
		}
		auto x = (b+c) / 2;
		auto y = max(0, x-b, c-x);
		foreach (i; 1..n)
		{
			if (a[i] != -1 && a[i-1] != -1)
			{
				y.chmax(abs(a[i] - a[i-1]));
			}
		}
		ans[ti] = [y, x];
	}

	foreach (e; ans)
	{
		writeln(e[0], " ", e[1]);
	}
	stdout.flush;
	debug readln;
}


This is a harder version of the problem. In this version, $$$n \le 50\,000$$$.There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even.You'd like to remove all $$$n$$$ points using a sequence of $$$\frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$.Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$\min(x_a, x_b) \le x_c \le \max(x_a, x_b)$$$, $$$\min(y_a, y_b) \le y_c \le \max(y_a, y_b)$$$, and $$$\min(z_a, z_b) \le z_c \le \max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$\frac{n}{2}$$$ snaps.

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 50\,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 \le x_i, y_i, z_i \le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide.

Output $$$\frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 \le a_i, b_i \le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them.

NoteIn the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed.

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 n = readln.chomp.to!int;

    Tuple!(int, int)[][int][int] mp;
    foreach (int i; 1 .. n+1) {
        auto v = readln.chomp.split.map!(to!int);
        auto x = v[0], y = v[1], z = v[2];

        mp[x][y] ~= tuple(z, i);
    }

    debug { mp.writeln; }

    Tuple!(int, int)[] ans;

    void takePairs(ref int[] arr, ref int[] leftOvers) {
        while (arr.length > 1) {
            auto idx1 = arr.back;
            arr.popBack();
            auto idx2 = arr.back;
            arr.popBack();
            ans ~= tuple(idx1, idx2);
        }

        if (arr.length == 1) {
            leftOvers ~= arr.back;
            arr.popBack();
        }
    }

    int[] lftx;
    foreach (xx; mp.keys.sort()) {
        int[] lfty;
        foreach (yy; mp[xx].keys.sort()) {
            auto values = mp[xx][yy].sort().map!(t => t[1]).array;
            takePairs(values, lfty);
        }

        takePairs(lfty, lftx);
    }

    int[] dummy;
    takePairs(lftx, dummy);

    ans.each!(t => writeln(t[0], ' ', t[1]));
}

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

immutable int NA = -1;

alias Point = Tuple !(int, q{x}, int, q{y}, int, q{z}, int, q{num});

Point [] p;
bool [] vis;
int [] [] res;
int n;

void loop (alias pred) ()
{
	int i = NA;
	int j = NA;
	foreach (k; 0..n)
	{
		if (!vis[k])
		{
			if (i == NA)
			{
				i = k;
			}
			else
			{
				j = k;
				if (pred (i, j))
				{
					res ~= [p[i].num,
					    p[j].num];
					vis[i] = true;
					vis[j] = true;
					i = NA;
					j = NA;
				}
				else
				{
					i = j;
					j = NA;
				}
			}
		}
	}
}

void main ()
{
	while (readf (" %s", &n) > 0)
	{
		p = new Point [n];
		foreach (i, ref q; p)
		{
			readf (" %s %s %s", &q.x, &q.y, &q.z);
			q.num = i + 1;
		}

		sort (p);
		vis = new bool [n];
		res = null;

		loop !((i, j) => p[i].x == p[j].x && p[i].y == p[j].y);
		loop !((i, j) => p[i].x == p[j].x);
		loop !((i, j) => true);
		writefln ("%(%(%s %)\n%)", res);
	}
}

import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0;
string rstring(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T rtype(T)(){ return rstring.to!T; } alias rint = rtype!int, rlong = rtype!long, rreal = rtype!real;
T[] rtypes(T)(int n){ return n.iota.map!(i => rtype!T()).array; } alias rint = rtypes!int, rlong = rtypes!long, rreal = rtypes!real;
T[][] rtypess(T)(int n, int m){ return n.iota.map!(i => rtypes!T(m)).array; } alias rint = rtypess!int, rlong = rtypess!long, rreal = rtypess!real;

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

void solve(){
	int n = rint;
	P[] ps;
	foreach(i; 0 .. n){
		ps ~= P(i, i + 1, rlong, rlong, rlong);
	}

	P[][long][long] pm;
	foreach(p; ps){
		if(p.x !in pm){
			P[][long] tmp;
			pm[p.x] = tmp;
		}
		if(p.y !in pm[p.x]) pm[p.x][p.y] = [];
		pm[p.x][p.y] ~= p;
	}

	int[][] ans;

	long[] xs = pm.keys;
	xs.sort();
	P superleft;
	bool hasSuperleft;
	foreach(x; xs){
		long[] ys = pm[x].keys;
		ys.sort();
		P left;
		bool hasLeft;
		foreach(y; ys){
			P[] pq = pm[x][y];
			pq.sort!isLess();
			foreach(i; 0 .. pq.length / 2){
				ans ~= [pq[i * 2].name, pq[i * 2 + 1].name];
			}
			if(pq.length % 2){
				if(hasLeft){
					ans ~= [pq[$ - 1].name, left.name];
					hasLeft = 0;
				}
				else{
					left = pq[$ - 1];
					hasLeft = 1;
				}
			}
		}
		if(hasLeft){
			if(hasSuperleft){
				ans ~= [left.name, superleft.name];
				hasLeft = 0, hasSuperleft = 0;
			}
			else{
				superleft = left;
				hasLeft = 0, hasSuperleft = 1;
			}
		}
	}

	foreach(an; ans) writeln(an[0], " ", an[1]);
}

struct P{
	int id, name;
	long x, y, z;
}
bool isLess(P a, P b){
	if(a.x != b.x) return a.x < b.x;
	if(a.y != b.y) return a.y < b.y;
	return a.z < b.z;
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, 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)); }

alias Pt = Tuple!(int, "x", int, "y", int, "z", int, "id");

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto P = new Pt[N];
      foreach (i; 0 .. N) {
        P[i].x = readInt();
        P[i].y = readInt();
        P[i].z = readInt();
        P[i].id = i;
      }

      sort(P);

      int[] ans;

      int[] as;
      for (int i = 0, j; i < N; i = j) {
        for (j = i; j < N && P[i].x == P[j].x; ++j) {}
        int[] bs;
        for (int k = i, l; k < j; k = l) {
          for (l = k; l < j && P[k].y == P[l].y; ++l) {}
          foreach (m; k .. k + (l - k) / 2 * 2) {
            ans ~= P[m].id;
          }
          if ((l - k) % 2 != 0) {
            bs ~= P[l - 1].id;
          }
        }
        const bsLen = cast(int)(bs.length);
        foreach (m; 0 .. bsLen / 2 * 2) {
          ans ~= bs[m];
        }
        if (bsLen % 2 != 0) {
          as ~= bs[$ - 1];
        }
      }
      assert(as.length % 2 == 0);
      ans ~= as;

      for (int i = 0; i < N; i += 2) {
        writeln(ans[i] + 1, " ", ans[i + 1] + 1);
      }
    }
  } catch (EOFException e) {
  }
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional;
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.typecons;

void main()
{
    int n;
    readf("%s", &n);
    readln;

    alias elem = Tuple!(int, int, int, int);
    elem[] arr;

    foreach (i; 1 .. n+1) {
        int x, y, z;
        readf("%s %s %s", &x, &y, &z);
        readln;

        arr ~= tuple(x, y, z, i);
    }

    arr.sort();

    foreach (cur; n.iota.stride(2)) {
        writeln(arr[cur][3], ' ', arr[cur+1][3]);
    }
}


Little Tanya decided to present her dad a postcard on his Birthday. She has already created a message — string s of length n, consisting of uppercase and lowercase English letters. Tanya can't write yet, so she found a newspaper and decided to cut out the letters and glue them into the postcard to achieve string s. The newspaper contains string t, consisting of uppercase and lowercase English letters. We know that the length of string t greater or equal to the length of the string s.The newspaper may possibly have too few of some letters needed to make the text and too many of some other letters. That's why Tanya wants to cut some n letters out of the newspaper and make a message of length exactly n, so that it looked as much as possible like s. If the letter in some position has correct value and correct letter case (in the string s and in the string that Tanya will make), then she shouts joyfully "YAY!", and if the letter in the given position has only the correct value but it is in the wrong case, then the girl says "WHOOPS".Tanya wants to make such message that lets her shout "YAY!" as much as possible. If there are multiple ways to do this, then her second priority is to maximize the number of times she says "WHOOPS". Your task is to help Tanya make the message.

The first line contains line s (1 ≤ |s| ≤ 2·105), consisting of uppercase and lowercase English letters — the text of Tanya's message. The second line contains line t (|s| ≤ |t| ≤ 2·105), consisting of uppercase and lowercase English letters — the text written in the newspaper. Here |a| means the length of the string a.

Print two integers separated by a space:   the first number is the number of times Tanya shouts "YAY!" while making the message,  the second number is the number of times Tanya says "WHOOPS" while making the message.

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

void main ()
{
	string s;
	while ((s = readln ().strip ()) != "")
	{
		string t = readln ().strip ();

		int [256] a;
		foreach (c; s)
		{
			a[c]++;
		}

		int [256] b;
		foreach (c; t)
		{
			b[c]++;
		}

		int ra = 0;
		int rb = 0;
		foreach (c; 0..256)
		{
			int d = min (a[c], b[c]);
			a[c] -= d;
			b[c] -= d;
			ra += d;
		}
		foreach (c; 'a'..'z' + 1)
		{
			auto e = c + 'A' - 'a';
			int d;
			d = min (b[c], a[e]);
			b[c] -= d;
			a[e] -= d;
			rb += d;
			d = min (b[e], a[c]);
			b[e] -= d;
			a[c] -= d;
			rb += d;
		}

		writeln (ra, ' ', rb);
	}
}


Stepan has the newest electronic device with a display. Different digits can be shown on it. Each digit is shown on a seven-section indicator like it is shown on the picture below.  So, for example, to show the digit 3 on the display, 5 sections must be highlighted; and for the digit 6, 6 sections must be highlighted. The battery of the newest device allows to highlight at most n sections on the display. Stepan wants to know the maximum possible integer number which can be shown on the display of his newest device. Your task is to determine this number. Note that this number must not contain leading zeros. Assume that the size of the display is enough to show any integer.

The first line contains the integer n (2 ≤ n ≤ 100 000) — the maximum number of sections which can be highlighted on the display.

Print the maximum integer which can be shown on the display of Stepan's newest device.

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n;
	for(int i = 0; i<1; i++)
		scanf("%d", &n);
	if (n%2==1)
	{
	    printf("7");
	    n-=3;
	}
	for (int i=0;i<n/2;i++)
	{
	    printf("1");
	}
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	if(n > 1) {
	if(n % 2 == 0) printf("1");
	else printf("7");
	for(;n>3;n-=2) printf("1");
	} else {
	printf("0");
	}
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	if(n == 2)
	{
	    printf("%d", 1);
	    return 0;
	}
	else if(n == 3)
	{
	    printf("%d", 7);
	    return 0;
	}
	else
	{
	    if(n % 2 == 1)
	    {
	        n = n - 3;
	        printf("%d", 7);
	    }
	    else
	    {
	        n = n - 2;
	        printf("%d", 1);
	    }

	    while (n > 0)
	    {
	        printf("%d", 1);
	        n = n - 2;
	    }
	}
	return 0;
}

import std.c.stdio;

int main()
{
    long n;

    scanf("%d", &n);

    long co = n/2;

    if(n%2 == 1){
        co--;
        printf("%d", 7);
    }
    for(long i=0;i<co;i++)
        printf("%d", 1);

    return 0;
}

module main;

import core.stdc.stdio;

int main(string[] argv) {
	int n = 0;
	scanf("%d", &n);
	if((n & 1) == 1) {
		n -= 3;
		putchar('7');
	}
	for(int i = 0; i < n; i += 2)
		putchar('1');
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{

int n, one, seven, a1, a7;

a1 = 1;
	a7 = 7;

scanf("%d", &n);

if (n % 2 == 1) {seven = 1; one = (n - 3) / 2;}

else {one = n / 2;}

for (int i = 0; i < seven; i++) printf("%d", a7);

for (int i = 0; i < one; i++) printf("%d", a1);

return 0;
}

import std.stdio;

void main()
{
    int n;
    scanf("%d", &n);
    if (n % 2 == 1) { write('7'); n -= 3; }
    for (; n > 0; n -= 2) write('1');
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	if (n%2 == 1)
		printf("%d", 7);
	else
		printf("%d", 1);

	for(int i = 0; i<n/2-1; i++)
		printf("1");

	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	int m = n / 2, k = n % 2;
	if(k == 1){
    printf("7");
    --m;
    }
	while(m--)printf("1");
	return 0;
}

module main;

import core.stdc.stdio;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
    if(n%2==1){
        printf("7");
    }else{
        printf("1");
    }
	for(int i = 0; i<n/2-1; i++)
		printf("1");
	return 0;
}

module main;
import std.c.stdio;
int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	if(n % 2 == 0)
	{
	   for(int i = 0; i < n / 2; i++)
	        printf("1");
	}
	else
	{
	    printf("7");
	    n -= 3;
	    for(int i = 0; i < n / 2; i++)
	        printf("1");
	}
	return 0;
}

module main;
import std.c.stdio;
int main(string[] args){
	int n; scanf("%d", &n);
	if(n % 2){
		printf("7");
		n -= 3;
	}
	for(int i = 0; i < n / 2; i++)
		printf("1");
	return 0;
}

module main;
import std.c.stdio;
int main(string[] argv)
{
	int n;
	scanf("%d",&n);
	int [] arr = new int[10];
	arr[0]=6;
	arr[1]=2;
	arr[2]=5;
	arr[3]=5;
	arr[4]=4;
	arr[5]=5;
	arr[6]=6;
	arr[7]=3;
	arr[8]=7;
	arr[9]=6;

	int len=n/2;
	for(int i=1;i<=len;i++)
	{
		int ok=-1;
		for(int j=9;j>-1;j--)
		{
			int req=n-arr[j];
			if( req>= (len-i)*2  )
			{
				if(ok==-1)ok=j;
			}
		}
		n=n-arr[ok];
		printf("%d",ok);
	}

	return 0;
}

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

void main()
{
    auto nums = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6];
    int n;
    scanf("%d", &n);
    int len = n / 2;

    while (len > 0)
    {
        foreach_reverse (i; 0..10)
            if ((len - 1) * 2 <= n - nums[i])
                {
                    write(i);
                    n -= nums[i];
                    break;
                }
        len = len - 1;
    }
}

import std.bigint,std.container,std.array,std.algorithm,std.conv,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
//import core.stdc.stdio;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;alias pair(X,Y)=Tuple!(X,"fi",Y,"se");
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;alias long lint;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;immutable int mod=1000000007;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
void main()
{
    int n;
read(n);
if(((n/2)*2) != n) {
write(7);
n = n - 3;
}
while(n>=2){
write(1);
n = n - 2;
}
}

import std.stdio;

void main()
{
    long x, i;
    readf(" %s", &x);
    if (x % 2 == 1)
    {
        write(7);
        x -= 3;
    }
    for (i = 0; i < x; i += 2)
        write(1);
}

module main;
import std.c.stdio;
int main(string[] argv)
{
    int number;
    scanf("%d",&number);
    if(number%2==0)
    {
        for(int i=1;i<=number/2;++i) printf("1");
    }
    else{
        printf("7");
        for(int i=1;i<=(number-3)/2;++i) printf("1");
    }
    return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n,k;
	scanf("%d %d", &n, &k);
	if(n%2==1)
	{
	    n-=3;
	    printf("7");
	}
	while(n>0)
	{
	    printf("1");
	    n-=2;
	}
	printf("\n");
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	for(;n>3;n-=2) printf("1");
	if(n == 3) printf("7");
	else printf("1");
	return 0;
}

module main;

import std.c.stdio;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	int m = n / 3, k = n % 3;
	if(k == 1){
	m--;
	if(m<0)m=0;
	k=2;
	} else
	if(k==2)k = 1;
	while(m--)printf("7");
	while(k--)printf("1");
	return 0;
}

module main;
import std.c.stdio;
int main(string[] args){
	int n; scanf("%d", &n);
	if(n % 2){
		printf("7");
		n -= 3;
	}
	for(int i = 0; i < n / 2; i++)
		printf("2");
	return 0;
}

module main;
import std.c.stdio;
int main(string[] argv)
{
	int n;
	scanf("%d",&n);
	int [] arr = new int[10];
	arr[0]=6;
	arr[1]=2;
	arr[2]=5;
	arr[3]=5;
	arr[4]=4;
	arr[5]=5;
	arr[6]=6;
	arr[7]=3;
	arr[8]=7;
	arr[9]=6;
	int len=n/2;
	for(int i=1;i<=len;i++)
	{
		int ok=-1;
		for(int j=9;j>-1;j--)
		{
			int req=n-arr[j];
			if( req>= (len-i)*2  )
			{
				if(ok==-1)ok=j;
			}
		}
		printf("%d",ok);
	}

	return 0;
}

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

void main()
{
    auto nums = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6];
    int n;
    scanf("%d", &n);
    int len = n / 2;

    while (len > 0)
    {
        foreach_reverse (i; 0..10)
            if ((len - 1) * 2 <= n - nums[i])
                {
                    write(i);
                    break;
                }
        len = len - 1;
    }
}

import std.bigint,std.container,std.array,std.algorithm,std.conv,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
import core.stdc.stdlib : system;
//import core.stdc.stdio;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;alias pair(X,Y)=Tuple!(X,"fi",Y,"se");
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;alias long lint;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;immutable int mod=1000000007;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	size_t lowb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(in T a,auto ref X g) if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
void main()
{
    int n;
read(n);
if(n%3 != 0) {
write(7);
n = n - 3;
}
while(n>=2){
write(1);
n = n - 2;
}
}


Polycarp has a strict daily schedule. He has n alarms set for each day, and the i-th alarm rings each day at the same time during exactly one minute.Determine the longest time segment when Polycarp can sleep, i. e. no alarm rings in that period. It is possible that Polycarp begins to sleep in one day, and wakes up in another.

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of alarms. Each of the next n lines contains a description of one alarm. Each description has a format "hh:mm", where hh is the hour when the alarm rings, and mm is the minute of that hour when the alarm rings. The number of hours is between 0 and 23, and the number of minutes is between 0 and 59. All alarm times are distinct. The order of the alarms is arbitrary. Each alarm starts ringing in the beginning of the corresponding minute and rings for exactly one minute (i. e. stops ringing in the beginning of the next minute). Polycarp can start sleeping instantly when no alarm is ringing, and he wakes up at the moment when some alarm starts ringing.

Print a line in format "hh:mm", denoting the maximum time Polycarp can sleep continuously. hh denotes the number of hours, and mm denotes the number of minutes. The number of minutes should be between 0 and 59. Look through examples to understand the format better.

NoteIn the first example there is only one alarm which rings during one minute of a day, and then rings again on the next day, 23 hours and 59 minutes later. Polycarp can sleep all this time.

import std.stdio;
import std.uni;
import std.string;
import core.stdc.stdio;
import std.array;
import std.algorithm;

int main(string[] argv)
{
	int n,k;
	scanf("%d", &n);
	int [] arr = new int[n];
	for(int i = 0; i<n; i++){
	    int h,m;
		scanf("%d:%d", &h,&m);
		h*=60;
		arr[i] = h+m;
	}
	arr.sort();
	int mx=0;
	for(int i=0;i<n-1;i++){
		int dif = arr[i+1] - arr[i] - 1;
		if(dif>mx)mx = dif;
	}
	int dif = 1440 - arr[n-1] + arr[0] - 1;
	if(dif>mx)mx=dif;
	if(n==1){
		printf("23:59\n");
	}
	if(n!=1){
	    if(mx/600==0){
	        printf("0");
	    }
		printf("%d:", mx/60);
		if((mx-mx/60*60)/10 == 0){
		    printf("0");
		}
		printf("%d\n",mx-mx/60*60);
	}
	return 0;
}

module main;
import core.stdc.stdio;
import std.algorithm;

int main(string[] argv)
{
    int n, ans = 0;
    scanf("%d", &n);
    int[] t = new int[n];
    for (int i = 0; i < n; ++i) {
        int h, m;
        scanf("%d:%d", &h, &m);
        t[i] = 60*h+m;
    }
    if (n == 1) {
        puts("23:59");
        return 0;
    }
    t.sort();

    for (int i = 1; i < n; ++i) {
        int v = t[i]-t[i-1]-1;
        if (v > ans) ans = v;
    }
    int tmp = 60*24+t[0]-t[n-1]-1;
    if (tmp > ans) ans = tmp;
    printf("%02d:%02d\n", ans/60, ans%60);

    return 0;
}

//Written by libra9z,Use D
module main;
import core.stdc.stdio;
import std.algorithm;
int max(int a,int b)
{
    if(a>b) return a;
	else return b;
}
int main()
{
    int n;
    scanf("%d",&n);
    if(n==1)
	{
        printf("23:59");
        return 0;
    }
    int [] arr=new int[n];
    for(int i=0;i<n;i++)
	{
        int a,b;
        scanf("%d:%d",&a,&b);
        arr[i]=a*60+b;
    }
    arr.sort();
    int ans=arr[0]+(24*60-arr[n-1]);
    for(int i=0;i<n-1;i++)
	{
        ans=max(ans,arr[i+1]-arr[i]);
    }
    ans--;
    printf("%02d:%02d",ans/60,ans%60);
    return 0;
}

//Written by libra9z,Use D
module main;
import core.stdc.stdio;
int max(int a,int b)
{
    if(a>b) return a;
	else return b;
}
int main()
{
    int n;
    scanf("%d",&n);
    if(n==1)
	{
        printf("23:59");
        return 0;
    }
    int [] arr=new int[105];
    for(int i=0;i<n;i++)
	{
        int a,b;
        scanf("%d:%d",&a,&b);
        arr[i]=a*60+b;
    }
    for(int i=0;i<n;i++)
	{
        for(int j=0;j<n;j++)
		{
            if(arr[i]<arr[j])
			{
                int t=arr[i];
                arr[i]=arr[j];
                arr[j]=t;
            }
        }
    }
    int ans=arr[0]+(24*60-arr[n-1]);
    for(int i=0;i<n-1;i++)
	{
        ans=max(ans,arr[i+1]-arr[i]);
    }
    ans--;
    printf("%02d:%02d",ans/60,ans%60);
    return 0;
}

//Written by libra9z,Use D
module main;
import core.stdc.stdio;
import std.algorithm;
int main()
{
    int n;
    scanf("%d",&n);
    if(n==1)
	{
        printf("23:59");
        return 0;
    }
    int [] arr=new int[n];
    for(int i=0;i<n;i++)
	{
        int a,b;
        scanf("%d:%d",&a,&b);
        arr[i]=a*60+b;
    }
    arr.sort();
    int ans=arr[0]+(24*60-arr[n-1]);
    for(int i=0;i<n-1;i++)
	{
        ans=max(ans,arr[i+1]-arr[i]);
    }
    ans--;
    printf("%02d:%02d",ans/60,ans%60);
    return 0;
}

module main;

import core.stdc.stdio ;

int main()
{
	int n,l,r,q,k=0,j=0,ans=0,low,mid,i;
	scanf("%d", &q);
	int [] a = new int[1000005];

    for(i=0;i<q;i++)
    {
        scanf("%d:%d",&l,&r);
         k=l*60+r;
        a[k]++;
        a[k+1440]++;
    }
    for(i=0;i<2879;i++){
    if(a[i]==0) j++;
    else {
        if(ans<j) ans=j;
        j=0;
    }
    }
    if(ans<j) ans=j;
    int hh=ans/60;
    int mm=ans%60;
    if(hh<10) printf("0%d:",hh);
    else printf("%d:",hh);

    if(mm<10) printf("0%d",mm);
    else printf("%d",mm);
	return 0;
}

module main;
import core.stdc.stdio;

int max(int a,int b){
    if(a>b) return a; else return b;
}

int main(){
    int n;
    scanf("%d",&n);
    if(n==1){
        printf("23:59");
        return 0;
    }

    int [] arr=new int[105];
    for(int i=0;i<n;i++){
        int a,b;
        scanf("%d:%d",&a,&b);
        //printf("%d %d",a,b);

        arr[i]=a*60+b;
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(arr[i]<arr[j]){
                int t=arr[i];
                arr[i]=arr[j];
                arr[j]=t;
            }
        }
    }

    // for(int i=0;i<n;i++){
    //     printf("%d ",arr[i]);
    // }

    int ans=arr[0]+(24*60-arr[n-1]);
    for(int i=0;i<n-1;i++){
        ans=max(ans,arr[i+1]-arr[i]);
    }
    ans--;
    printf("%02d:%02d",ans/60,ans%60);
    return 0;
}

module main;

import std.stdio;

int main(string[] argv)
{
	int n,k;
	scanf("%d", &n);
	int [] a = new int[1005];
	int [] b = new int[1005];
	int [] c = new int[1005];
	for(int i = 1; i<=n; i++)
		scanf("%d:%d", &a[i],&b[i]);
	for(int i=1;i<=n;i++)
	{

	c[i]=a[i]*60+b[i];//printf("%d ",c[i]);
	}
	for(int i=1;i<=n;i++)
	{
		int minn=100000000,pla=0,j;
		for(j=i;j<=n;j++)
		{
			if(c[j]<minn)
			{
				minn=c[j];
				pla=j;
			}
		}
		//printf("pla:%d",pla);
		int t=c[pla];
		c[pla]=c[i];
		c[i]=t;
	}
	int ans=0;
	for(int i=1;i<n;i++)
	{
	//printf("%d ",c[i]);
	if(c[i+1]-c[i]>ans) ans=c[i+1]-c[i];
	}
	if(n==1)
	{
		printf("23:59");
		return 0;
	}
	else
	{
	if(1440-c[n]+c[1]>ans) ans=1440-c[n]+c[1];
		ans--;
		printf("%02d:%02d",ans/60,ans%60);
	}
	return 0;
}

module main;

import std.c.stdio;

int dig(char c){
    c -= '0';
    return c <= 9 ? c : -1;
}

int main(string[] argv)
{
	char[] arr = new char[15];
    int t = 24 * 60;
    int[] a = new int[t];
    for(int i = 0; i < t; i++) a[i] = 0;
	int n;
	scanf("%d", &n);
	for(int i = 0; i<n; i++){

	    for(int j = 0; j < 15; j++) arr[j] = 0;
	    scanf("%s", &arr[0]);
	    //printf("%c\n", arr[0]);
	    int u = dig(arr[0]), uu = dig(arr[1]), v = dig(arr[3]), vv = dig(arr[4]);
	    //printf("%d\n", 60 * (10 * u + uu) + (10 * v + vv));
	    a[60 * (10 * u + uu) + (10 * v + vv)] = 1;
	}

	int mxmin = 1;
	for(int i = 0; i < t; i++){
	    // start at time i
	    if(a[i] != 1){
	        for(int j = 0; j < t; j++){
	            if(a[(i + j) % t] == 1){
	                if(mxmin < j) mxmin = j;
	                break;
	            }
	        }
	    }
	}

	int hrs = mxmin / 60;
	int mns = mxmin - 60 * hrs;

	if(hrs < 10) printf("%d", 0);
	printf("%d", hrs);

	printf(":");

	if(mns < 10) printf("%d", 0);
	printf("%d\n", mns);

	return 0;
}

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

int main(string[] argv)
{
	int n = to!int(readln.split[0] , 10);

	string[] s = new string[n];
	for(int i = 0;i < n;i++){
		s[i] = readln;
	}
	s.sort();
	int[] h = new int[n];
	int[] m = new int[n];
	for(int i = 0;i < n;i++){
	      auto x = s[i].split(':');
	     // writeln(x);
	      h[i] = to!int(x[0],10);
		m[i] = to!int(x[1].split('\n')[0],10);
// 		writeln(h[i] , m[i]);
	}
	int d = 0;
	for(int i = 0;i < n - 1;i++){
	      int x = 60 * (h[i + 1] - h[i]) + m[i + 1] - m[i] - 1;
	     // writeln(x);
	      if(d < x)d = x;
	}
	int x = 60 * (23 - h[n - 1]) + 60 - m[n - 1] + 60 * h[0] + m[0] - 1;
	if(d < x)d = x;
	int a = d % 60;
	d = (d - a)/60;
      if(d < 10){
            if(a < 10)writeln("0" , to!string(d) , ":0" , to!string(a));
            else writeln("0" , to!string(d) , ":" , to!string(a));
      }
	else {
	      if(a < 10)writeln(to!string(d) , ":0" , to!string(a));
	      else writeln(to!string(d) , ":" , to!string(a));
	}
	return 0;
}

module main;
import std.c.stdio;
import std.algorithm.sorting;

int n;
int[] ar = new int[1111];

void read()
{
    for(int i = 0 ; i < 1111 ; i ++) ar[i] = 2147483647;
	scanf("%d", &n);
	for(int i = 0 ; i < n ; i ++)
	{
	    char c;
		int h, m; scanf("%d%c%d", &h, &c, &m);
		ar[i] = (h * 60) + m;
	}
}

int solve()
{
	int res = 0;

	sort(ar);

	ar[n] = ar[0] + 24 * 60;
	for(int i = 1 ; i <= n ; i ++)
	{
		if(res < ar[i] - ar[i - 1] - 1)
		{
			res = ar[i] - ar[i - 1] - 1;
		}
	}
	return res;
}

void print(int s)
{
	int h = s / 60;
	int m = s % 60;

	if (h < 10)
		printf("0%d", h);
	else
		printf("%d", h);

	printf(":");

	if (m < 10)
		printf("0%d", m);
	else
		printf("%d", m);
}

int main(string[] argv)
{
	read();
	print(solve());
	return 0;
}

import std.stdio, std.array, std.conv, std.algorithm;

void main()
{
    auto max = 0;
    stdin.readln();
    auto arr = stdin
        .byLineCopy
        .array
        .sort!((a, b) => a < b) // descending order
        .map!(l => l.split(":"))
        .map!(l => to!int(l[0]) * 60 + to!int(l[1]));
    for (int i = 0; i < arr.length; i++) {
        if (i < arr.length - 1 && arr[i + 1] - arr[i] > max) max = arr[i + 1] - arr[i];
        if (i == arr.length - 1 && arr[0] + 1440 - arr[i] > max) max = arr[0] + 1440 - arr[i];
    }
    max = max - 1;
    printf("%02d:%02d", max / 60, max % 60);
}

import std.stdio;

int[3500] cnt;

int main() {
    int n;
    readf("%d\n", &n);

    for (int i = 0; i < n; i++) {
    	int h, m;
    	readf("%d:%d\n", &h, &m);
    	int x = h * 60 + m;
    	cnt[x] = 1;
    	cnt[x + 24 * 60] = 1;
    }

    int ans = 0, cur = 0;
    for (int i = 0; i < 2 * 24 * 60; i++) {
    	if (cnt[i] == 1) {
    		if (ans < cur) ans = cur;
    		cur = 0;
    	} else cur++;
    }

    if (ans < cur) ans = cur;
    printf("%02d:%02d", ans/60, ans % 60);

    return 0;
}

import std.stdio;

void main(string[] args) {
   int n;
   int[1440] time;
   scanf("%d", &n);
   for ( int i = 0; i < 1440; i++ ) {
      time[ i ] = 0;
   }
   int h, m;
   for ( int i = 0; i < n; i++ ) {
      scanf("%d:%d", &h, &m);
      time[h*60+m] = 1;
   }
   int mm=0;
   int cur=0;
   int cur1=0;
   int t = 0;
   for ( int i = 0; i < 1440; i++){
      if (time[i] == 0){
        cur = cur + 1;
      }
      else {
        if (t == 0){
            cur1 = cur;
            cur = 0;
            t = 1;
        }
        if (cur > mm){
            mm = cur;
        }
        cur = 0;
      }
   }
   if (cur+cur1 > mm){
        mm = cur+cur1;
    }
   writeln(mm/60/10,mm/60%10,":",mm%60/10,mm%60%10);
}

import std.stdio;

int[8888] cnt;

int main() {
    int n;

    readf("%d\n", &n);

    for (int i = 0; i < n - 1; i++) {
    	int h, m;

    	readf("%d:%d\n", &h, &m);
    	int x = h * 60 + m;
    	cnt[x] = 1;

    	cnt[x + 24 * 60] = 1;
    }

       	int h, m;

    	readf("%d:%d", &h, &m);
    	int x = h * 60 + m;
    	cnt[x] = 1;

    	cnt[x + 24 * 60] = 1;

    int ans = 0, cur = 0;

    for (int i = 0; i < 2 * 24 * 60; i++) {

    	if (cnt[i] == 1) {
    		if (ans < cur) {
    		        ans = cur;
    		    }
    		cur = 0;
    	} else cur++;

    }

    if (ans < cur) {
        ans = cur;
    }

    printf("%02d:%02d", ans/60, ans % 60);

    return 0;
}

module main;

import std.c.stdio;
import std.algorithm;

int main(string[] argv)
{
	int n;
	scanf("%d", &n);
	if (n == 1) {
		printf("23:59");
		return 0;
	}
	int cm = 0;
	int time = 0;
	int[] ms = new int[n];
	for (int i = 0; i < n; ++i) {
		getchar();
		int h = (getchar() - '0') * 10 + getchar() - '0';
		getchar();
		int m = (getchar() - '0') * 10 + getchar() - '0';
		ms[i] = 60 * h + m;
	}
	ms.sort();
	int dif = ms[0] + 24 * 60 - ms[n - 1];
	for (int i = 1; i < n; ++i) {
		if (ms[i] - ms[i - 1] > dif) {
			dif = ms[i] - ms[i - 1];
		}
	}
	--dif;
	int h1 = dif / 60;
	dif = dif - 60 * h1;
	printf("%d%d:%d%d", h1 / 10, h1 % 10, dif / 10, dif % 10);
	return 0;
}

module main;

import std.c.stdio;
import std.algorithm;
int main(string[] argv)
{
	int n, h,m,ot, z,a;
	ot = 0;
	scanf("%d", &n);

	int [] t = new int[n];

	for(int i = 0; i<n; i++)
	{
		scanf("%d:%d", &h, &m);
		t[i] = h*60+m;
	}
	t.sort();
	z=t[0]+24*60;
	for(int i = 1; i<n; i++)
	{
	    if (t[i]-t[i-1]-1>ot) ot=t[i]-t[i-1]-1;
	}
	if (z-t[n-1]-1>ot) ot=z-t[n-1]-1;
	a = ot/60;
	if (a<10) printf("0%d:",a);
	else printf("%d:",a);
	a = ot%60;
	if (a<10) printf("0%d",a);
	else printf("%d",a);
	return 0;
}

module main;
import std.c.stdio;

int main(string[] argv)
{
	int [] arr = new int[105];

	int n;
	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		int h, m;
		char c;
		scanf("%d%c%d", &h, &c, &m);
		int now = h * 60 + m;
		arr[i] = now;
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = n - 1; j >= i + 1; j--)
		{
			if (arr[j] < arr[j - 1])
			{
				int t = arr[j - 1];
				arr[j - 1] = arr[j];
				arr[j] = t;
			}
		}
	}
	arr[n] = arr[0] + 24 * 60;
	n++;

	int ans = 0;
	for (int i = 1; i < n; i++)
	{
		if (ans < arr[i] - arr[i - 1] - 1)
			ans = arr[i] - arr[i - 1] - 1;
	}

	int h = ans / 60, m = ans % 60;

	if (h < 10)
		printf("0%d", h);
	else
		printf("%d", h);
	printf(":");
	if (m < 10)
		printf("0%d", m);
	else
		printf("%d", m);

	return 0;
}

module main;

import std.c.stdio;
import std.algorithm;

int main (string[] argv)
{
	int n;
	scanf ("%d", &n);
	int [] arr = new int[n + 1];
	for (int i = 0, h, m; i < n; i++) {
		scanf("%d:%d", &h, &m);
		arr[i] = h * 60 + m;
	}
	arr[n] = 1000000000;
	sort (arr);
	arr[n] = arr[0] + 24 * 60;

	int ans = -1;
	for (int i = 1; i <= n; i++) {
		ans = max (ans, arr[i] - arr[i - 1] - 1);
	}
	printf ("%02d:%02d\n", ans / 60, ans % 60);

	return 0;
}

import std.stdio;
import std.uni;
import std.string;
import core.stdc.stdio;
import std.array;
import std.algorithm;

int main(string[] argv)
{
	int n,k;
	scanf("%d", &n);
	int [] arr = new int[n];
	for(int i = 0; i<n; i++){
	    int h,m;
		scanf("%d:%d", &h,&m);
		h*=60;
		arr[i] = h+m;
	}
	arr.sort();
	int mx=0;
	for(int i=0;i<n-1;i++){
		int dif = arr[i+1] - arr[i] - 1;
		if(dif>mx)mx = dif;
	}
	if(n==1){
		printf("23:59\n");
	}
	if(n!=1){
	    if(mx/600==0){
	        printf("0");
	    }
		printf("%d:", mx/60);
		if((mx-mx/60*60)/10 == 0){
		    printf("0");
		}
		printf("%d\n",mx-mx/60*60);
	}
	return 0;
}

import std.stdio;
import std.uni;
import std.string;
import core.stdc.stdio;
import std.array;
import std.algorithm;

int main(string[] argv)
{
	int n,k;
	scanf("%d", &n);
	int [] arr = new int[n];
	for(int i = 0; i<n; i++){
	    int h,m;
		scanf("%d:%d", &h,&m);
		h*=60;
		arr[i] = h+m;
	}
	arr.sort();
	int mx=0;
	for(int i=0;i<n-1;i++){
		int dif = arr[i+1] - arr[i] - 1;
		if(dif>mx)mx = dif;
	}
	int dif = 1320 - arr[n-1] + arr[0];
	if(dif>mx)mx=dif;
	if(n==1){
		printf("23:59\n");
	}
	if(n!=1){
	    if(mx/600==0){
	        printf("0");
	    }
		printf("%d:", mx/60);
		if((mx-mx/60*60)/10 == 0){
		    printf("0");
		}
		printf("%d\n",mx-mx/60*60);
	}
	return 0;
}

import std.stdio;
import std.uni;
import std.string;
import core.stdc.stdio;
import std.array;
import std.algorithm;

int main(string[] argv)
{
	int n,k;
	scanf("%d", &n);
	int [] arr = new int[n];
	for(int i = 0; i<n; i++){
	    int h,m;
		scanf("%d:%d", &h,&m);
		h*=60;
		arr[i] = h+m;
	}
	arr.sort();
	int mx=0;
	for(int i=0;i<n-1;i++){
		int dif = arr[i+1] - arr[i] - 1;
		if(dif>mx)mx = dif;
	}
	int dif = 1440 - arr[n-1] + arr[0];
	if(dif>mx)mx=dif;
	if(n==1){
		printf("23:59\n");
	}
	if(n!=1){
	    if(mx/600==0){
	        printf("0");
	    }
		printf("%d:", mx/60);
		if((mx-mx/60*60)/10 == 0){
		    printf("0");
		}
		printf("%d\n",mx-mx/60*60);
	}
	return 0;
}

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

int main(string[] argv)
{
	int n,k;
	scanf("%d", &n);
	int [] arr = new int[n];
	for(int i = 0; i<n; i++){
	    int h,m;
		scanf("%d:%d", &h,&m);
		h*=60;
		arr[i] = h+m;
	}
	arr.sort();
	int mx=0;
	for(int i=0;i<n-1;i++){
		int dif = arr[i+1] - arr[i] - 1;
		if(dif>mx)mx = dif;
	}
	int dif = arr[n-1] - arr[0] - 1;
	if(dif>mx)mx=dif;
	if(n==1){
		printf("23:59\n");
	}
	if(n!=1){
	    if(mx/600==0){
	        printf("0");
	    }
		printf("%d:", mx/60);
		if((mx-mx/60*60)/10 == 0){
		    printf("0");
		}
		printf("%d\n",mx-mx/60*60);
	}
	return 0;
}

module main;

import core.stdc.stdio ;

int main()
{
	int n,l,r,q,k=0,j=0,ans=0,low,mid,i;
	scanf("%d", &q);
	int [] a = new int[1000005];

    for(i=0;i<q;i++)
    {
        scanf("%d:%intd",&l,&r);
         k=l*60+r;
        a[k]++;
        a[k+1440]++;
    }
    for(i=0;i<2880;i++){
    if(a[i]==0) j++;
    else {
        if(ans<j) ans=j;
        j=0;
    }
    }
    if(ans<j) ans=j;
    int hh=ans/60;
    int mm=ans%60;
    if(hh<10) printf("0%d:",hh);
    else printf("%d:",hh);

    if(mm<10) printf("0%d",mm);
    else printf("%d",mm);
	return 0;
}

module main;

import core.stdc.stdio ;

int main()
{
	int n,l,r,q,k=0,j=0,ans=0,low,mid,i;
	scanf("%d", &q);
	int [] a = new int[1000005];

    for(i=0;i<q;i++)
    {
        scanf("%d:%intd",&l,&r);
         k=l*60+r;
        a[k]++;
        a[k+1440]++;
    }
    for(i=0;i<2880;i++){
    if(a[i]==0) j++;
    else {
        if(ans<j) ans=j;
        j=0;
    }
    }
    if(ans<j) ans=j;
    printf("%d:%d",ans/60,ans%60);
	return 0;
}

module main;
import core.stdc.stdio;

int max(int a,int b){
    if(a>b) return a; else return b;
}

int main(){
    int n;
    scanf("%d",&n);
    if(n==1){
        printf("23:59");
        return 0;
    }

    int [] arr=new int[105];
    for(int i=0;i<n;i++){
        int a,b;
        scanf("%d:%d",&a,&b);
        //printf("%d %d",a,b);

        arr[i]=a*60+b;
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(arr[i]<arr[j]){
                int t=arr[i];
                arr[i]=arr[j];
                arr[j]=t;
            }
        }
    }

    // for(int i=0;i<n;i++){
    //     printf("%d ",arr[i]);
    // }

    int ans=arr[0]+(24*60-arr[n-1])-1;
    for(int i=0;i<n-1;i++){
        ans=max(ans,arr[i+1]-arr[i]);
    }
    ans--;
    printf("%02d:%02d",ans/60,ans%60);
    return 0;
}

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

int main(string[] argv)
{
	int n = to!int(readln.split[0] , 10);

	string[] s = new string[n];
	for(int i = 0;i < n;i++){
		s[i] = readln;
	}
	s.sort();
	int[] h = new int[n];
	int[] m = new int[n];
	for(int i = 0;i < n;i++){
	      auto x = s[i].split(':');
	     // writeln(x);
	      h[i] = to!int(x[0],10);
		m[i] = to!int(x[1].split('\n')[0],10);
// 		writeln(h[i] , m[i]);
	}
	int d = 0;
	for(int i = 0;i < n - 1;i++){
	      int x = 60 * (h[i + 1] - h[i]) + m[i + 1] - m[i] - 1;
	     // writeln(x);
	      if(d < x)d = x;
	}
	int x = 60 * (23 - h[n - 1]) + 60 - m[n - 1] + 60 * h[0] + m[0] - 1;
	if(d < x)d = x;
	int a = d % 60;
	d = (d - a)/60;
// 	writeln(d);
// 	writeln(a);
	writeln(to!string(d) , ":" , to!string(a));
	return 0;
}

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

int main(string[] argv)
{
	int n = to!int(readln.split[0] , 10);

	string[] s = new string[n];
	for(int i = 0;i < n;i++){
		s[i] = readln;
	}
	s.sort();
	int[] h = new int[n];
	int[] m = new int[n];
	for(int i = 0;i < n;i++){
	      auto x = s[i].split(':');
	     // writeln(x);
	      h[i] = to!int(x[0],10);
		m[i] = to!int(x[1].split('\n')[0],10);
// 		writeln(h[i] , m[i]);
	}
	int d = 0;
	for(int i = 0;i < n - 1;i++){
	      int x = 60 * (h[i + 1] - h[i]) + m[i + 1] - m[i] - 1;
	     // writeln(x);
	      if(d < x)d = x;
	}
	int x = 60 * (23 - h[n - 1]) + 60 - m[n - 1] + 60 * h[0] + m[0] - 1;
	if(d < x)d = x;
	int a = d % 60;
	d = (d - a)/60;
// 	writeln(d);
// 	writeln(a);
      if(d < 10)writeln("0" , to!string(d) , ":" , to!string(a));
	else writeln(to!string(d) , ":" , to!string(a));
	return 0;
}

module main;
import std.c.stdio;
import std.algorithm.sorting;

int n;
int[] ar = new int[1111];

void read()
{
	scanf("%d", &n);
	for(int i = 0 ; i < n ; i ++)
	{
		int h, m; scanf("%d %d", &h, &m);
		ar[i] = (h * 60) + m;
	}
}

int solve()
{
	int res = 0;
	ar.sort();
	ar[n] = ar[0] + 24 * 60;
	for(int i = 1 ; i <= n ; i ++)
		if(res < ar[i] - ar[i - 1] - 1)
			res = ar[i] - ar[i - 1] - 1;
	return res;
}

void print(int s)
{
	int h = s / 60;
	int m = s % 60;

	if (h < 10)
		printf("0%d", h);
	else
		printf("%d", h);

	printf(":");

	if (m < 10)
		printf("0%d", m);
	else
		printf("%d", m);
}

int main(string[] argv)
{
	read();
	print(solve());
	return 0;
}

module main;
import std.c.stdio;

int main(string[] argv)
{
	int [] arr = new int[105];

	int n;
	scanf("%d", &n);

	for (int i = 0; i < n; i++)
	{
		int h, m;
		char c;
		scanf("%d%c%d", &h, &c, &m);
		int now = h * 60 + m;
		arr[i] = now;
	}

	for (int i = 0; i < n; i++)
	{
		for (int j = n - 1; j >= i + 1; j--)
		{
			if (arr[j] < arr[j - 1])
			{
				int t = arr[j - 1];
				arr[j - 1] = arr[j];
				arr[j] = t;
			}
		}
	}
	arr[n] = arr[0] + 24 * 60;
	n++;

	int ans = 0;
	for (int i = 1; i < n; i++)
	{
		if (ans < arr[i] - arr[i - 1] - 1)
			ans = arr[i] - arr[i - 1] - 1;
	}

	int h = ans / 60, m = ans % 60;
	printf("%d%c%d", h, ':', m);

	return 0;
}


You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.Let volume vj of barrel j be equal to the length of the minimal stave in it.  You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.

The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 105, 1 ≤ n·k ≤ 105, 0 ≤ l ≤ 109). The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.

Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.

NoteIn the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].In the second example you can form the following barrels: [10], [10].In the third example you can form the following barrels: [2, 5].In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.

import std.stdio;
import std.range;
import std.array;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.container;
import std.typecons;
import std.random;
import std.csv;
import std.regex;
import std.math;
import core.time;
import std.ascii;
import std.digest.sha;
import std.outbuffer;
import std.numeric;
import std.bigint;

void main()
{
	int n, k, l;
	readln.chomp.split.tie(n, k, l);
	long[] a = readln.chomp.split.map!(to!long).array;
	alias Tree = RedBlackTree!(long, "a < b", true);
	Tree left = new Tree();
	Tree right = new Tree();
	long target = a.reduce!min;
	foreach (i, v; a) {
		if (v <= target + l) {
			left.insert(v);
		} else {
			right.insert(v);
		}
	}
	debug verbose(left);
	debug verbose(right);
	bool ok = true;
	long sum = 0;
	while (right.length) {
		if (left.length == 0) {
			ok = false;
			break;
		}
		int t = 0;
		for (;t < k - 1 && right.length > 0;) {
			right.removeBack;
			++t;
		}
		long v = left.back;
		for (;t < k && left.length > 0;) {
			v = left.back;
			left.removeBack;
			++t;
		}
		if (t < k) {
			ok = false;
			break;
		}
		sum += v;
	}
	while (left.length) {
		int t = 0;
		long v = left.back;
		for (;t < k && left.length > 0;) {
			v = left.back;
			left.removeBack;
			++t;
		}
		if (t < k) {
			ok = false;
			break;
		}
		sum += v;
	}
	if (ok) {
		sum.writeln;
	} else {
		0.writeln;
	}
}

void tie(R, Args...)(R arr, ref Args args)
		if (isRandomAccessRange!R || isArray!R)
in
{
	assert (arr.length == args.length);
}
body
{
	foreach (i, ref v; args) {
		alias T = typeof(v);
		v = arr[i].to!T;
	}
}

void verbose(Args...)(in Args args)
{
	stderr.write("[");
	foreach (i, ref v; args) {
		if (i) stderr.write(", ");
		stderr.write(v);
	}
	stderr.writeln("]");
}

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

void main()
{
    uint n, k, le;
    readf( "%s %s %s", &n, &k, &le );
    readln;
    auto a = readln.split.map!(to!int).array;
    a.sort();

    auto overLimit = Array!uint();
    ulong ans = 0;
    for ( int i = 0; i < n*k; i += k ) {
        ans += a[ i ];
        if ( a[ 0 ] + le < a[ i ] ) overLimit ~= a[ i ];
        debug { writeln(i); }
    }

    auto candidates = Array!uint();
    for ( int i = 0; i < n*k && a[ i ] <= a[ 0 ] + le; ++i ) {
        if ( i % k == 0 ) continue;

        candidates ~= a[ i ];
    }

    debug { writeln(a); writeln(ans); writeln(overLimit.array); writeln(candidates.array); }

    while ( !overLimit.empty() ) {
        if ( candidates.empty() ) {
            writeln( 0 );
            return;
        }

        ans -= overLimit.back - candidates.back;
        overLimit.removeBack();
        candidates.removeBack();
    }

    writeln ( ans );
}