A. Unusual Competitionstime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputA bracketed sequence is called correct (regular) if by inserting "+" and "1" you can get a well-formed mathematical expression from it. For example; sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not.The teacher gave Dmitry's class a very strange task — she asked every student to come up with a sequence of arbitrary length, consisting only of opening and closing brackets. After that all the students took turns naming the sequences they had invented. When Dima's turn came, he suddenly realized that all his classmates got the correct bracketed sequence, and whether he got the correct bracketed sequence, he did not know.Dima suspects now that he simply missed the word "correct" in the task statement, so now he wants to save the situation by modifying his sequence slightly. More precisely, he can the arbitrary number of times (possibly zero) perform the reorder operation.The reorder operation consists of choosing an arbitrary consecutive subsegment (substring) of the sequence and then reordering all the characters in it in an arbitrary way. Such operation takes ll nanoseconds, where ll is the length of the subsegment being reordered. It's easy to see that reorder operation doesn't change the number of opening and closing brackets. For example for "))((" he can choose the substring ")(" and do reorder ")()(" (this operation will take 22 nanoseconds).Since Dima will soon have to answer, he wants to make his sequence correct as fast as possible. Help him to do this, or determine that it's impossible.InputThe first line contains a single integer nn (1≤n≤1061≤n≤106) — the length of Dima's sequence.The second line contains string of length nn, consisting of characters "(" and ")" only.OutputPrint a single integer — the minimum number of nanoseconds to make the sequence correct or "-1" if it is impossible to do so.ExamplesInputCopy8
))((())(
OutputCopy6
InputCopy3
(()
OutputCopy-1
NoteIn the first example we can firstly reorder the segment from first to the fourth character; replacing it with "()()"; the whole sequence will be "()()())(". And then reorder the segment from the seventh to eighth character; replacing it with "()". In the end the sequence will be "()()()()"; while the total time spent is 4+2=64+2=6 nanoseconds.

import java.io.*;

import java.util.*;

public class cf {

    static class FastReader {

        BufferedReader br;

        StringTokenizer st;

        public FastReader() {

            br = new BufferedReader(new InputStreamReader(System.in));

        }

        String next() {

            while (st == null || !st.hasMoreElements()) {

                try {

                    st = new StringTokenizer(br.readLine());

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            return st.nextToken();

        }

        int nextInt() {

            return Integer.parseInt(next());

        }

        long nextLong() {

            return Long.parseLong(next());

        }

        double nextDouble() {

            return Double.parseDouble(next());

        }

        float nextFloat() {

            return Float.parseFloat(next());

        }

        String nextLine() {

            String str = "";

            try {

                str = br.readLine();

            } catch (IOException e) {

                e.printStackTrace();

            }

            return str;

        }

        void readArr(int[] ar, int n) {

            for (int i = 0; i < n; i++) {

                ar[i] = nextInt();

            }

        }

    }

    public static long gcd(long a, long b) {

        if (b == 0) {

            return a;

        }

        return gcd(b, a % b);

    }

    public static boolean binary_search(long[] a, long k) {

        long low = 0;

        long high = a.length - 1;

        long mid = 0;

        while (low <= high) {

            mid = low + (high - low) / 2;

            if (a[(int) mid] == k) {

                return true;

            } else if (a[(int) mid] < k) {

                low = mid + 1;

            } else {

                high = mid - 1;

            }

        }

        return false;

    }

    public static int bSearchDiff(long[] a, int low, int high) {

        int mid = low + ((high - low) / 2);

        int hight = high;

        int lowt = low;

        while (lowt < hight) {

            mid = lowt + (hight - lowt) / 2;

            if (a[high] - a[mid] <= 5) {

                hight = mid;

            } else {

                lowt = mid + 1;

            }

        }

        return lowt;

    }

    public static long lowerbound(long a[], long ddp) {

        long low = 0;

        long high = a.length;

        long mid = 0;

        while (low < high) {

            mid = low + (high - low) / 2;

            // if (a[(int) mid] == ddp) {

            // return mid;

            // }

            if (a[(int) mid] <= ddp) {

                low = mid + 1;

            } else {

                high = mid;

            }

        }

        // if(low + 1 < a.length && a[(int)low + 1] <= ddp){

        // low++;

        // }

        if (low == a.length && low != 0) {

            low--;

            return low;

        }

        if (a[(int) low] > ddp && low != 0) {

            low--;

        }

        return low;

    }

    public static long lowerbound(long a[], long ddp, long factor) {

        long low = 0;

        long high = a.length;

        long mid = 0;

        while (low < high) {

            mid = low + (high - low) / 2;

            if ((a[(int) mid] + (mid * factor)) == ddp) {

                return mid;

            }

            if ((a[(int) mid] + (mid * factor)) < ddp) {

                low = mid + 1;

            } else {

                high = mid;

            }

        }

        // if(low + 1 < a.length && a[(int)low + 1] <= ddp){

        // low++;

        // }

        if (low == a.length && low != 0) {

            low--;

            return low;

        }

        if (a[(int) low] > ddp - (low * factor) && low != 0) {

            low--;

        }

        return low;

    }

    public static long lowerbound(List<Long> a, long ddp) {

        long low = 0;

        long high = a.size();

        long mid = 0;

        while (low < high) {

            mid = low + (high - low) / 2;

            if (a.get((int) mid) == ddp) {

                return mid;

            }

            if (a.get((int) mid) < ddp) {

                low = mid + 1;

            } else {

                high = mid;

            }

        }

        // if(low + 1 < a.length && a[(int)low + 1] <= ddp){

        // low++;

        // }

        if (low == a.size() && low != 0) {

            low--;

            return low;

        }

        if (a.get((int) low) > ddp && low != 0) {

            low--;

        }

        return low;

    }

    public static long lowerboundforpairs(pair a[], double pr) {

        long low = 0;

        long high = a.length;

        long mid = 0;

        while (low < high) {

            mid = low + (high - low) / 2;

            if (a[(int) mid].w <= pr) {

                low = mid + 1;

            } else {

                high = mid;

            }

        }

        // if(low + 1 < a.length && a[(int)low + 1] <= ddp){

        // low++;

        // }

        // if(low == a.length && low != 0){

        // low--;

        // return low;

        // }

        // if(a[(int)low].w > pr && low != 0){

        // low--;

        // }

        return low;

    }

    public static long upperbound(long a[], long ddp) {

        long low = 0;

        long high = a.length;

        long mid = 0;

        while (low < high) {

            mid = low + (high - low) / 2;

            if (a[(int) mid] <= ddp) {

                low = mid + 1;

            } else {

                high = mid;

            }

        }

        if (low == a.length) {

            return a.length - 1;

        }

        return low;

    }

    public static long upperbound(ArrayList<Long> a, long ddp) {

        long low = 0;

        long high = a.size();

        long mid = 0;

        while (low < high) {

            mid = low + (high - low) / 2;

            if (a.get((int) mid) <= ddp) {

                low = mid + 1;

            } else {

                high = mid;

            }

        }

        return low;

    }

    // public static class pair implements Comparable<pair> {

    // long w;

    // long h;

    // public pair(long w, long h) {

    // this.w = w;

    // this.h = h;

    // }

    // public int compareTo(pair b) {

    // if (this.w != b.w)

    // return (int) (this.w - b.w);

    // else

    // return (int) (this.h - b.h);

    // }

    // }

    public static class pairs {

        char w;

        int h;

        public pairs(char w, int h) {

            this.w = w;

            this.h = h;

        }

    }

    public static class pair {

        long w;

        long h;

        public pair(long w, long h) {

            this.w = w;

            this.h = h;

        }

        @Override

        public int hashCode() {

            return Objects.hash(w, h);

        }

        @Override

        public boolean equals(Object o) {

            if (o == this) {

                return true;

            }

            if (!(o instanceof pair)) {

                return false;

            }

            pair c = (pair) o;

            return Long.compare(this.w, c.w) == 0 && Long.compare(this.h, h) == 0;

        }

    }

    public static class trinary {

        long a;

        long b;

        long c;

        public trinary(long a, long b, long c) {

            this.a = a;

            this.b = b;

            this.c = c;

        }

    }

    public static pair[] sortpair(pair[] a) {

        Arrays.sort(a, new Comparator<pair>() {

            public int compare(pair p1, pair p2) {

                if (p1.w != p2.w) {

                    return (int) (p1.w - p2.w);

                }

                return (int) (p1.h - p2.h);

            }

        });

        return a;

    }

    public static trinary[] sortpair(trinary[] a) {

        Arrays.sort(a, new Comparator<trinary>() {

            public int compare(trinary p1, trinary p2) {

                if (p1.b != p2.b) {

                    return (int) (p1.b - p2.b);

                } else if (p1.c != p2.c) {

                    return (int) (p1.c - p2.c);

                }

                return (int) (p1.a - p2.a);

            }

        });

        return a;

    }

    public static void sort(long[] arr) {

        ArrayList<Long> a = new ArrayList<>();

        for (long i : arr) {

            a.add(i);

        }

        Collections.sort(a);

        for (int i = 0; i < a.size(); i++) {

            arr[i] = a.get(i);

        }

    }

    public static void sortForObjecttypes(pair[] arr) {

        ArrayList<pair> a = new ArrayList<>();

        for (pair i : arr) {

            a.add(i);

        }

        Collections.sort(a, new Comparator<pair>() {

            @Override

            public int compare(pair a, pair b) {

                return (int) (a.h - b.h);

            }

        });

        for (int i = 0; i < a.size(); i++) {

            arr[i] = a.get(i);

        }

    }

    public static boolean ispalindrome(String s) {

        long i = 0;

        long j = s.length() - 1;

        boolean is = false;

        while (i < j) {

            if (s.charAt((int) i) == s.charAt((int) j)) {

                is = true;

                i++;

                j--;

            } else {

                is = false;

                return is;

            }

        }

        return is;

    }

    public static long power(long base, long pow, long mod) {

        long result = base;

        long temp = 1;

        while (pow > 1) {

            if (pow % 2 == 0) {

                result = ((result % mod) * (result % mod)) % mod;

                pow /= 2;

            } else {

                temp = temp * base;

                pow--;

            }

        }

        result = ((result % mod) * (temp % mod));

        // System.out.println(result);

        return result;

    }

    public static long sqrt(long n) {

        long res = 1;

        long l = 1, r = (long) 10e9;

        while (l <= r) {

            long mid = (l + r) / 2;

            if (mid * mid <= n) {

                res = mid;

                l = mid + 1;

            } else

                r = mid - 1;

        }

        return res;

    }

    public static boolean is[] = new boolean[10001];

    public static int a[] = new int[10001];

    public static Vector<Integer> seiveOfEratosthenes() {

        Vector<Integer> listA = new Vector<>();

        for (int i = 2; i * i <= a.length; i++) {

            if (a[i] != 1) {

                for (long j = i * i; j < a.length; j += i) {

                    a[(int) j] = 1;

                }

            }

        }

        for (int i = 2; i < a.length; i++) {

            if (a[i] == 0) {

                is[i] = true;

                listA.add(i);

            }

        }

        return listA;

    }

    public static Vector<Integer> ans = seiveOfEratosthenes();

    public static long sumOfDigits(long n) {

        long ans = 0;

        while (n != 0) {

            ans += n % 10;

            n /= 10;

        }

        return ans;

    }

    public static long gcdTotal(long a[]) {

        long t = a[0];

        for (int i = 1; i < a.length; i++) {

            t = gcd(t, a[i]);

        }

        return t;

    }

    public static ArrayList<Long> al = new ArrayList<>();

    public static void makeArr() {

        long t = 1;

        while (t <= 10e17) {

            al.add(t);

            t *= 2;

        }

    }

    public static boolean isBalancedBrackets(String s) {

        if (s.length() == 1) {

            return false;

        }

        Stack<Character> sO = new Stack<>();

        int id = 0;

        while (id < s.length()) {

            if (s.charAt(id) == '(') {

                sO.add('(');

            }

            if (s.charAt(id) == ')') {

                if (!sO.isEmpty()) {

                    sO.pop();

                } else {

                    return false;

                }

            }

            id++;

        }

        if (sO.isEmpty()) {

            return true;

        }

        return false;

    }

    public static void solve(FastReader sc, PrintWriter w, StringBuilder sb) throws Exception {

        int n = sc.nextInt();

        String s = sc.nextLine();

        int openings[] = new int[n + 1];

        int closings[] = new int[n + 1];

        for (int i = 0; i < n; i++) {

            if (s.charAt(i) == ')') {

                closings[i + 1] = closings[i] + 1;

                openings[i + 1] = openings[i];

            } else {

                openings[i + 1] = openings[i] + 1;

                closings[i + 1] = closings[i];

            }

        }

        if (openings[n] != closings[n]) {

            sb.append(-1);

            return;

        }

        int ans = 0;

        int prev = 0;

        for (int i = 0; i < n; i++) {

            if (openings[i + 1] == closings[i + 1]) {

                if (!isBalancedBrackets(s.substring(prev, i + 1))) {

                    ans += i - prev + 1;

                    prev = i + 1;

                } else {

                    prev = i + 1;

                }

            }

        }

        sb.append(ans);

    }

    public static void main(String[] args) throws Exception {

        FastReader sc = new FastReader();

        PrintWriter w = new PrintWriter(System.out);

        StringBuilder sb = new StringBuilder();

        // long o = sc.nextLong();

        // // makeArr();

        // while (o > 0) {

        solve(sc, w, sb);

        // o--;

        // }

        System.out.print(sb.toString());

        w.close();

    }

}


C. Frog Jumpstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a frog staying to the left of the string s=s1s2…sns=s1s2…sn consisting of nn characters (to be more precise, the frog initially stays at the cell 00). Each character of ss is either 'L' or 'R'. It means that if the frog is staying at the ii-th cell and the ii-th character is 'L', the frog can jump only to the left. If the frog is staying at the ii-th cell and the ii-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell 00.Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.The frog wants to reach the n+1n+1-th cell. The frog chooses some positive integer value dd before the first jump (and cannot change it later) and jumps by no more than dd cells at once. I.e. if the ii-th character is 'L' then the frog can jump to any cell in a range [max(0,i−d);i−1][max(0,i−d);i−1], and if the ii-th character is 'R' then the frog can jump to any cell in a range [i+1;min(n+1;i+d)][i+1;min(n+1;i+d)].The frog doesn't want to jump far, so your task is to find the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it can jump by no more than dd cells at once. It is guaranteed that it is always possible to reach n+1n+1 from 00.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.The next tt lines describe test cases. The ii-th test case is described as a string ss consisting of at least 11 and at most 2⋅1052⋅105 characters 'L' and 'R'.It is guaranteed that the sum of lengths of strings over all test cases does not exceed 2⋅1052⋅105 (∑|s|≤2⋅105∑|s|≤2⋅105).OutputFor each test case, print the answer — the minimum possible value of dd such that the frog can reach the cell n+1n+1 from the cell 00 if it jumps by no more than dd at once.ExampleInputCopy6
LRLRRLL
L
LLR
RRRR
LLLLLL
R
OutputCopy3
2
3
1
7
1
NoteThe picture describing the first test case of the example and one of the possible answers:In the second test case of the example; the frog can only jump directly from 00 to n+1n+1.In the third test case of the example, the frog can choose d=3d=3, jump to the cell 33 from the cell 00 and then to the cell 44 from the cell 33.In the fourth test case of the example, the frog can choose d=1d=1 and jump 55 times to the right.In the fifth test case of the example, the frog can only jump directly from 00 to n+1n+1.In the sixth test case of the example, the frog can choose d=1d=1 and jump 22 times to the right.

/*****  --->         :)    Vijender  Srivastava      (:       <---       *****/

import java.util.*;

import java.lang.*;

import java.io.*;

public class Main

{

    static FastReader sc =new FastReader();

    static PrintWriter out=new PrintWriter(System.out);

    static long mod=(long)998244353;

    static StringBuilder sb = new StringBuilder();

    /* start */

    public static void main(String [] args)

    {

        int testcases = 1;

        testcases = i();

        // calc();

        while(testcases-->0)

        {

            solve();

        }

        out.flush();

        out.close();

    }

    static void solve()

    {

        String s = "R"+sc.next()+"R";

        int i=0,max = 0;

        for(int j=1;j<s.length();j++)

        {

            if(s.charAt(j)=='R')

            {

                max = max(max,j-i);

                i = j;

            }

        }

        pl(max);

    }

    /* end */

    static class FastReader

    {

        BufferedReader br;

        StringTokenizer st;

        public FastReader()

        {

            br = new BufferedReader(new

                     InputStreamReader(System.in));

        }

        String next()

        {

            while (st == null || !st.hasMoreElements())

            {

                try

                {

                    st = new StringTokenizer(br.readLine());

                }

                catch (IOException  e)

                {

                    e.printStackTrace();

                }

            }

            return st.nextToken();

        }

        int nextInt()

        {

            return Integer.parseInt(next());

        }

        long nextLong()

        {

            return Long.parseLong(next());

        }

        double nextDouble()

        {

            return Double.parseDouble(next());

        }

        String nextLine()

        {

            String str = "";

            try

            {

                str = br.readLine();

            }

            catch (IOException e)

            {

                e.printStackTrace();

            }

            return str;

        }

    }

    // print code start

    static void p(Object o)

    {

        out.print(o);

    }

    static void pl(Object o)

    {

        out.println(o);

    }

    static void pl()

    {

        out.println("");

    }

    // print code end //

    static int i() {

        return sc.nextInt();

    }

    static String s() {

        return sc.next();

    }

    static long l() {

        return sc.nextLong();

    }

    static char[] inputC()

    {

        String s = sc.nextLine();

        return s.toCharArray();

    }

    static int[] input(int n) {

        int A[]=new int[n];

           for(int i=0;i<n;i++) {

               A[i]=sc.nextInt();

           }

        return A;

    }

    static long[] inputL(int n) {

        long A[]=new long[n];

           for(int i=0;i<n;i++) {

               A[i]=sc.nextLong();

           }

           return A;

    }

    static int[][] input(int n,int m) {

        int a[][] = new int[n][m];

        for(int i=0;i<n;i++) a[i] = input(m);

        return a;

    }

    static long[][] inputL(int n,int m)

    {

        long a[][] = new long[n][m];

        for(int i=0;i<n;i++) a[i] = inputL(m);

        return a;

    }

    static long[] putL(long a[]) {

        long A[]=new long[a.length];

           for(int i=0;i<a.length;i++) {

               A[i]=a[i];

           }

           return A;

    }

    static String[] inputS(int n) {

        String A[]=new String[n];

           for(int i=0;i<n;i++) {

               A[i]=sc.next();

           }

           return A;

    }

    static int gcd(int a, int b)

     {

         if (a == 0)

             return b;

         return gcd(b % a, a);

     }

     static long gcd(long a, long b)

     {

         if (a == 0)

             return b;

         return gcd(b % a, a);

     }

     static int lcm(int a, int b)

    {

        return (a / gcd(a, b)) * b;

    }

     static String reverse(String s) {

        StringBuffer p=new StringBuffer(s);

        p.reverse();

        return p.toString();

     }

     static int min(int a,int b) {

        return Math.min(a, b);

    }

    static int min(int a,int b,int c) {

        return Math.min(a, Math.min(b, c));

    }

    static int min(int a,int b,int c,int d) {

        return Math.min(a, Math.min(b, Math.min(c, d)));

    }

    static int max(int a,int b) {

        return Math.max(a, b);

    }

    static int max(int a,int b,int c) {

        return Math.max(a, Math.max(b, c));

    }

    static int max(int a,int b,int c,int d) {

        return Math.max(a, Math.max(b, Math.max(c, d)));

    }

    static long min(long a,long b) {

        return Math.min(a, b);

    }

    static long min(long a,long b,long c) {

        return Math.min(a, Math.min(b, c));

    }

    static long min(long a,long b,long c,long d) {

        return Math.min(a, Math.min(b, Math.min(c, d)));

    }

    static long max(long a,long b) {

        return Math.max(a, b);

    }

    static long max(long a,long b,long c) {

        return Math.max(a, Math.max(b, c));

    }

    static long max(long a,long b,long c,long d) {

        return Math.max(a, Math.max(b, Math.max(c, d)));

    }

    static long sum(int A[]) {

        long sum=0;

        for(int i : A) {

            sum+=i;

        }

        return sum;

    }

    static long sum(long A[]) {

        long sum=0;

        for(long i : A) {

            sum+=i;

        }

        return sum;

    }

    static long mod(long x) {

          return ((x%mod + mod)%mod);

    }

    static long sum_mod(long x,long y)

    {

        return mod(mod(x)+mod(y));

    }

    public static long ncr_mod(long n,long r){

        long top=fac_mod(n);

        long bottom=mul_mod(fac_mod(r),fac_mod(n-r));

        long ans=mul_mod(top,power_mod(bottom,mod-2));

        return ans;

    }

    public static long power_mod(long a,long b){

        if(b==0)return 1;

        if(b==1)return a;

        long res=1;

        while(b>0){

            if((b&1)==1) res=mul_mod(res,a);

            a=mul_mod(a,a);

            b/=2;

        }

        return res;

    }

    public static long fac_mod(long n){

        if(n==0)return 1;

        return mul_mod(fac_mod(n-1),n);

    }

    static long mul_mod(long x,long y)

    {

        return mod(mod(x)*mod(y));

    }

    static long power(long x, long y)

    {

	if(y==0)

		return 1;

	if(x==0)

		return 0;

    long res = 1;

    while (y > 0) {

        if (y % 2 == 1)

            res = (res * x) ;

        y = y >> 1;

        x = (x * x);

    }

    return res;

    }

    static boolean prime(int n)

	    {

	        if (n <= 1)

	            return false;

	        if (n <= 3)

	            return true;

	        if (n % 2 == 0 || n % 3 == 0)

	            return false;

	        double sq=Math.sqrt(n);

	        for (int i = 5; i <= sq; i = i + 6)

	            if (n % i == 0 || n % (i + 2) == 0)

	                return false;

	        return true;

	    }

     static boolean prime(long n)

	    {

	        if (n <= 1)

	            return false;

	        if (n <= 3)

	            return true;

	        if (n % 2 == 0 || n % 3 == 0)

	            return false;

	        double sq=Math.sqrt(n);

	        for (int i = 5; i <= sq; i = i + 6)

	            if (n % i == 0 || n % (i + 2) == 0)

	                return false;

	        return true;

	    }

        static long[] sort(long a[]) {

            ArrayList<Long> arr = new ArrayList<>();

            for(long i : a) {

                arr.add(i);

            }

            Collections.sort(arr);

            for(int i = 0; i < arr.size(); i++) {

                a[i] = arr.get(i);

            }

            return a;

        }

        static int[] sort(int a[])

        {

            ArrayList<Integer> arr = new ArrayList<>();

            for(Integer i : a) {

                arr.add(i);

            }

            Collections.sort(arr);

            for(int i = 0; i < arr.size(); i++) {

                a[i] = arr.get(i);

            }

            return a;

        }

        static int[] sortInd(int a[])

        {

            int n = a.length;

            int ii[] = new int[n];

            for(int i=0;i<n;i++)ii[i] = i;

            ii = Arrays.stream(ii).boxed().sorted((i,j)->a[i]-a[j]).mapToInt($->$).toArray();

            return ii;

        }

        //pair class

        private static class Pair implements Comparable<Pair> {

            long first, second;

            public Pair(long f, long s) {

                first = f;

                second = s;

            }

            @Override

            public int compareTo(Pair p) {

                if (first < p.first)

                    return 1;

                else if (first > p.first)

                    return -1;

                else {

                    if (second > p.second)

                        return 1;

                    else if (second < p.second)

                        return -1;

                    else

                        return 0;

                }

            }

        }

}


A. Fast Food Restauranttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTired of boring office work; Denis decided to open a fast food restaurant.On the first day he made aa portions of dumplings, bb portions of cranberry juice and cc pancakes with condensed milk.The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules:  every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes);  each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk;  all visitors should receive different sets of dishes. What is the maximum number of visitors Denis can feed?InputThe first line contains an integer tt (1≤t≤5001≤t≤500) — the number of test cases to solve.Each of the remaining tt lines contains integers aa, bb and cc (0≤a,b,c≤100≤a,b,c≤10) — the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.OutputFor each test case print a single integer — the maximum number of visitors Denis can feed.ExampleInputCopy71 2 10 0 09 1 72 2 32 3 23 2 24 4 4OutputCopy3045557NoteIn the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.

import java.util.Scanner;

public class CF0712{

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int testCases = scan.nextInt();

		while(testCases>0){

			int a = scan.nextInt();

			int b = scan.nextInt();

			int c = scan.nextInt();

			int counter = 0;

			if(b>a){

				int temp = a;

				a = b;

				b = temp;

			}

			if(c>b){

				int temp = b;

				b = c;

				c = temp;

			}

			if(b>a){

				int temp = a;

				a = b;

				b = temp;

			}

			if(a>0){

				counter++;

				a--;

			}if(b>0){

				counter++;

				b--;

			}if(c>0){

				counter++;

				c--;

			}if(a>0 && b>0){

				counter++;a--;b--;

			}if(a>0 && c>0){

				counter++;a--;c--;

			}if(b>0 && c>0){

				counter++; b--;c--;

			}if(a>0 && b>0 && c>0){

				counter++;

			}

			System.out.println(counter);

			testCases--;

		}

	}

}


C. Perfect Keyboardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp wants to assemble his own keyboard. Layouts with multiple rows are too complicated for him — his keyboard will consist of only one row; where all 2626 lowercase Latin letters will be arranged in some order.Polycarp uses the same password ss on all websites where he is registered (it is bad, but he doesn't care). He wants to assemble a keyboard that will allow to type this password very easily. He doesn't like to move his fingers while typing the password, so, for each pair of adjacent characters in ss, they should be adjacent on the keyboard. For example, if the password is abacaba, then the layout cabdefghi... is perfect, since characters a and c are adjacent on the keyboard, and a and b are adjacent on the keyboard. It is guaranteed that there are no two adjacent equal characters in ss, so, for example, the password cannot be password (two characters s are adjacent).Can you help Polycarp with choosing the perfect layout of the keyboard, if it is possible?InputThe first line contains one integer TT (1≤T≤10001≤T≤1000) — the number of test cases.Then TT lines follow, each containing one string ss (1≤|s|≤2001≤|s|≤200) representing the test case. ss consists of lowercase Latin letters only. There are no two adjacent equal characters in ss.OutputFor each test case, do the following:  if it is impossible to assemble a perfect keyboard, print NO (in upper case, it matters in this problem);  otherwise, print YES (in upper case), and then a string consisting of 2626 lowercase Latin letters — the perfect layout. Each Latin letter should appear in this string exactly once. If there are multiple answers, print any of them. ExampleInputCopy5
ababa
codedoca
abcda
zxzytyz
abcdefghijklmnopqrstuvwxyza
OutputCopyYES
bacdefghijklmnopqrstuvwxyz
YES
edocabfghijklmnpqrstuvwxyz
NO
YES
xzytabcdefghijklmnopqrsuvw
NO

import java.lang.*;
import java.util.*;
import java.io.*;

public class practice{

    static int components;

    private static int find(int[] parent, int x){

        if (!(x>=0 && x<parent.length))
            return -1;

        if (parent[x] < 0){
            return x;
        }

        return (parent[x] = find(parent, parent[x]));
    }

    private static void union(int[] parent, int x1, int x2){

        int r1 = find(parent, x1), r2 = find(parent, x2);

        if (r1 == r2)
            return;

        components--;

        if (parent[r1] <= parent[r2]){
            parent[r1] += parent[r2];
            parent[r2] = r1;
        }else{
            parent[r2] += parent[r1];
            parent[r1] = r2;
        }

    }

    private static void union1(int[] parent, int[] cost, int x1, int x2){

        int r1 = find(parent, x1), r2 = find(parent, x2);

        if (r1 == r2)
            return;

        if (cost[r1] >= cost[r2]){
            parent[r1] += parent[r2];
            parent[r2] = r1;
        }else{
            parent[r2] += parent[r1];
            parent[r1] = r2;
        }

    }

    private static boolean check(int[] parent, int x1, int x2){
        int r1 = find(parent, x1), r2 = find(parent, x2);

        if (r1 == r2){
            return false;
        }

        return true;
    }

    private static int dfs(List<List<Integer>> adj, int idx){

        if (adj.get(idx).size() == 0){
            return 1;
        }

        List<Integer> nei = adj.get(idx);

        int count = 0;

        for (int ne : nei){
            count += dfs(adj, ne);
        }

        return count;

    }

    public static class Pair{
        private int idx;
        private int sum;

        Pair(int idx, int sum){
            this.idx = idx;
            this.sum = sum;
        }
    }

    private static int rec(int v, int[] dp){
        if (v == 0){
            return 0;
        }

        if (dp[v] != -1){
            return dp[v];
        }

        return (dp[v]=1+Math.min(rec((v+1)%32768, dp), rec((2*v)%32768, dp)));

    }

    static class Node {

        int idx, level;

        public Node(int v, int d) {
            this.idx = v;
            this.level = d;
        }

    }
    static boolean vis[] = new boolean[100005];

    public static void main(String args[]) throws IOException{

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //StringTokenizer st = new StringTokenizer(br.readLine());

        int t = Integer.parseInt(br.readLine().trim());

        while (t-->0){
            String s = br.readLine().trim();

            if (s.length() == 1){
                System.out.println("YES");
                StringBuilder sb = new StringBuilder();

                boolean[] visited = new boolean[26];

                visited[s.charAt(0)-'a'] = true;

                sb.append(s.charAt(0));

                for (int i=0 ; i<26 ; i++){
                    if (!visited[i]){
                        sb.append((char)(i+'a'));
                    }
                }

                System.out.println(sb.toString());

                continue;
            }

            Set<Integer>[] adj = new HashSet[26];

            for (int i=0 ; i<26 ; i++){
                adj[i] = new HashSet<>();
            }

            char[] arr = s.toCharArray();

            boolean check = true;

            for (int i=1 ; i<s.length() ; i++){
                int u = s.charAt(i)-'a';
                int v = s.charAt(i-1)-'a';

                adj[u].add(v);
                adj[v].add(u);

                if (adj[u].size()>=3 || adj[v].size()>=3){
                    check = false;
                }
            }

            if (!check){
                System.out.println("NO");
                continue;
            }

            int start_idx = -1;

            for (int i=0 ; i<26 ; i++){
                if (adj[i].size() == 1){
                    start_idx = i;
                }
            }

            if (start_idx == -1){
                System.out.println("NO");
                continue;
            }

            boolean[] visited = new boolean[26];

            StringBuilder sb = new StringBuilder();

            LinkedList<Integer> q = new LinkedList<>();
            q.offer(start_idx);

            while(!q.isEmpty()){
                int size = q.size();

                while (size-->0){
                    int tmp = q.poll();

                    sb.append((char)(tmp+'a'));

                    visited[tmp] = true;

                    for (int next : adj[tmp]){
                        if (!visited[next]){
                            q.offer(next);
                        }
                    }
                }
            }

            for(int i=0 ; i<26 ; i++){
                if(!visited[i]){
                    sb.append((char)(i+'a'));
                }
            }

            System.out.println("YES");
            System.out.println(sb.toString());
        }
    }

}


A. Grade Allocationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputnn students are taking an exam. The highest possible score at this exam is mm. Let aiai be the score of the ii-th student. You have access to the school database which stores the results of all students.You can change each student's score as long as the following conditions are satisfied:   All scores are integers  0≤ai≤m0≤ai≤m  The average score of the class doesn't change. You are student 11 and you would like to maximize your own score.Find the highest possible score you can assign to yourself such that all conditions are satisfied.InputEach test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤2001≤t≤200). The description of the test cases follows.The first line of each test case contains two integers nn and mm (1≤n≤1031≤n≤103, 1≤m≤1051≤m≤105)  — the number of students and the highest possible score respectively.The second line of each testcase contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤m0≤ai≤m)  — scores of the students.OutputFor each testcase, output one integer  — the highest possible score you can assign to yourself such that both conditions are satisfied._ExampleInputCopy2
4 10
1 2 3 4
4 5
1 2 3 4
OutputCopy10
5
NoteIn the first case; a=[1,2,3,4]a=[1,2,3,4], with average of 2.52.5. You can change array aa to [10,0,0,0][10,0,0,0]. Average remains 2.52.5, and all conditions are satisfied.In the second case, 0≤ai≤50≤ai≤5. You can change aa to [5,1,1,3][5,1,1,3]. You cannot increase a1a1 further as it will violate condition 0≤ai≤m0≤ai≤m.

import java.util.Scanner;

public class CF{

	public static void main(String[] args){

		Scanner scan = new Scanner(System.in);

		int testCas = scan.nextInt();

		while(testCas>0){

			int arrayLength = scan.nextInt();

			int maxPossibleScore = scan.nextInt();

			int totalSumOfScores = 0;

			for(int i=0; i<arrayLength; i++){

				totalSumOfScores += scan.nextInt();

			}

			if(totalSumOfScores<=maxPossibleScore){

				System.out.println(totalSumOfScores);

			}else{

				System.out.println(maxPossibleScore);

			}

			testCas--;

		}

	}

}


D. Ehab the Xorcisttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven 2 integers uu and vv, find the shortest array such that bitwise-xor of its elements is uu, and the sum of its elements is vv.InputThe only line contains 2 integers uu and vv (0≤u,v≤1018)(0≤u,v≤1018).OutputIf there's no array that satisfies the condition, print "-1". Otherwise:The first line should contain one integer, nn, representing the length of the desired array. The next line should contain nn positive integers, the array itself. If there are multiple possible answers, print any.ExamplesInputCopy2 4
OutputCopy2
3 1InputCopy1 3
OutputCopy3
1 1 1InputCopy8 5
OutputCopy-1InputCopy0 0
OutputCopy0NoteIn the first sample; 3⊕1=23⊕1=2 and 3+1=43+1=4. There is no valid array of smaller length.Notice that in the fourth sample the array is empty.

	import java.io.*;

	import java.lang.*;

	import java.util.*;

	public class ComdeFormces {

		static int x[]= {0,-1,1,1,1,1,-1,-1};

		static int y[]= {-1,0,0,0,1,-1,1,-1};

//		static int dp[][][];

		static boolean ans;

		static int cnt;

		public static void main(String[] args) throws Exception{

			// TODO Auto-generated method stub

		FastReader sc=new FastReader();

		BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));

	//		 OutputStream out = new BufferedOutputStream ( System.out );

//	    int t=sc.nextInt();

		int tc=1;

        while(tc--!=0) {

        	 long xor=sc.nextLong();

        	 long sum=sc.nextLong();

        	 int max=0;

        	 long p=1;

        	 long tsum=sum;

        	 boolean anss=true;

        	 for(int i=60;i>=0 && tsum>=0;i--) {

                 if(((xor&(p<<i))>0?1:0)>0) {

                	 tsum-=(p<<i);

                 }

        	 }

             for(int i=60;i>=0 && tsum>=0;i--) {

//            	 System.out.println(p<<i);

                   long count=tsum/(p<<i);

                   if(count%2!=0)count--;

                   int shift=(((xor&(p<<i))>0?1:0)>0)?1:0;

                   max=Math.max(max, (int)(count+shift));

                   tsum-=(count*(p<<i));

             }

             if(!anss || tsum!=0)log.write("-1\n");

             else {

             long ans[]=new long[max];

             tsum=sum;

             for(int i=60;i>=0;i--) {

            	 if(((xor&(p<<i))>0?1:0)>0) {

                 ans[0]+=(p<<i);

                 tsum-=(p<<i);

            	 }

             }

             for(int i=60;i>=0;i--) {

            	 long count=tsum/(p<<i);

            	 if(count%2!=0)count--;

            	 int shift=(((xor&(p<<i))>0?1:0)>0)?1:0;

            	 for(int j=shift;j<count+shift;j++) {

            		 ans[j]+=(p<<i);

            	 }

            	 tsum-=(count*(p<<i));

             }

             log.write(max+"\n");

        	 for(int j=0;j<max;j++) {

        		 log.write(ans[j]+" ");

        	 }

        	 log.write("\n");

             }

        	  log.flush();

        }

		}

static int eval(ArrayList<ArrayList<Integer>> ar,int a[],int src,int xor,int pr) {

  int xr=a[src];

  for(int k:ar.get(src)) {

	  if(k==pr)continue;

	  xr^=eval(ar,a,k,xor,src);

  }

  if(xr==(xor^xr))ans=true;

  return xr;

}

static int eval2(ArrayList<ArrayList<Integer>> ar,int a[],int src,int xor,int pr) {

	int xr=a[src];

	for(int k:ar.get(src)) {

		 if(k==pr)continue;

		  xr^=eval2(ar,a,k,xor,src);

	}

	if(xr==xor) {

		cnt++;

		return 0;

	}

	else return xr;

}

public static int m=(int)(998244353);

public static int mul(int a, int b) {

	return ((a%m)*(b%m))%m;

}

public static long mul(long a, long b) {

	return ((a%m)*(b%m))%m;

}

public static int add(int a, int b) {

	return ((a%m)+(b%m))%m;

}

public static long add(long a, long b) {

	return ((a%m)+(b%m))%m;

}

public static long sub(long a,long b) {

return ((a%m)-(b%m)+m)%m;

}

static long gcd(long a,long b) {

	if(b==0)return a;

	else return gcd(b,a%b);

}

static int gcd(int a,int b) {

	if(b==0)return a;

	else return gcd(b,a%b);

}

	static long ncr(int n, int r){

	    if(r>n-r)r=n-r;

	    long ans=1;

	    long m=(int)(1e9+7);

	    for(int i=0;i<r;i++){

	          ans=ans*(n-i);

	          ans/=i+1;

	    }

	    return ans;

	}

public static class num{

	long v;

}

static long gcd(long a,long b,num x,num y) {

	if(b==0) {

		x.v=1;

		y.v=0;

		return a;

	}

	num x1=new num();

	num y1=new num();

	long ans=gcd(b,a%b,x1,y1);

	x.v=y1.v;

	y.v=x1.v-(a/b)*y1.v;

	return ans;

}

static long inverse(long b,long m) {

	num x=new num();

	num y=new num();

	long gc=gcd(b,m,x,y);

	if(gc!=1) {

		return -1;

	}

	return (x.v%m+m)%m;

}

static long div(long a,long b,long m) {

	a%=m;

	if(inverse(b,m)==-1)return a/b;

	 return (inverse(b,m)*a)%m;

}

public static class trip{

	int a;

	int b;

	long c;

	int d;

	public trip(int a,int b,long  c,int d) {

		this.a=a;

		this.b=b;

		this.c=c;

		this.d=d;

	}

}

static void mergesort(int[] a,int start,int end) {

		if(start>=end)return ;

		int mid=start+(end-start)/2;

		mergesort(a,start,mid);

		mergesort(a,mid+1,end);

		merge(a,start,mid,end);

	}

static void merge(int[] a, int start,int mid,int end) {

		int ptr1=start;

		int ptr2=mid+1;

		int b[]=new int[end-start+1];

		int i=0;

		while(ptr1<=mid && ptr2<=end) {

			if(a[ptr1]<=a[ptr2]) {

				b[i]=a[ptr1];

				ptr1++;

				i++;

			}

			else {

				b[i]=a[ptr2];

				ptr2++;

				i++;

			}

		}

		while(ptr1<=mid) {

			b[i]=a[ptr1];

			ptr1++;

			i++;

		}

		while(ptr2<=end) {

			b[i]=a[ptr2];

			ptr2++;

			i++;

		}

		for(int j=start;j<=end;j++) {

			a[j]=b[j-start];

		}

	}

	public static class FastReader {

			BufferedReader b;

			StringTokenizer s;

			public FastReader() {

				b=new BufferedReader(new InputStreamReader(System.in));

			}

			String next() {

				while(s==null ||!s.hasMoreElements()) {

					try {

						s=new StringTokenizer(b.readLine());

					}

					catch(IOException e) {

						e.printStackTrace();

					}

				}

				return s.nextToken();

			}

			public int nextInt() {

				return Integer.parseInt(next());

			}

			public long nextLong() {

				return Long.parseLong(next());

			}

			public double nextDouble() {

				return Double.parseDouble(next());

			}

			String nextLine() {

				String str="";

				try {

					str=b.readLine();

				}

				catch(IOException e) {

					e.printStackTrace();

				}

				return str;

			}

			boolean hasNext() {

			    if (s != null && s.hasMoreTokens()) {

			        return true;

			    }

			    String tmp;

			    try {

			        b.mark(1000);

			        tmp = b.readLine();

			        if (tmp == null) {

			            return false;

			        }

			        b.reset();

			    } catch (IOException e) {

			        return false;

			    }

			    return true;

			}

	}

	public static class pair{

		int a;

        int b;

		public pair(int a,int b) {

			this.a=a;

			this.b=b;

		}

		@Override

		public String toString() {

			return "{"+this.a+" "+this.b+"}";

			}

	}

		static long pow(long a, long pw) {

			long temp;

			if(pw==0)return 1;

			temp=pow(a,pw/2);

			if(pw%2==0)return mul(temp,temp);

			return mul(a,mul(temp,temp));

		}

		public static int md=998244353;

		static int pow(int a, int pw) {

			int temp;

			if(pw==0)return 1;

			temp=pow(a,pw/2);

			if(pw%2==0)return temp*temp;

			return a*temp*temp;

		}

}


A. Erasing Zeroestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string ss. Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase?InputThe first line contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases.Then tt lines follow, each representing a test case. Each line contains one string ss (1≤|s|≤1001≤|s|≤100); each character of ss is either 0 or 1.OutputPrint tt integers, where the ii-th integer is the answer to the ii-th testcase (the minimum number of 0's that you have to erase from ss).ExampleInputCopy3
010011
0
1111000
OutputCopy2
0
0
NoteIn the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111).

import java.util.*;

public class waytoolong {

		public static void main(String args[]) {

			Scanner s=new Scanner(System.in);

			int t=s.nextInt();

			for(int i=0;i<t;i++) {

				int c=0;

				String sa=s.next();

				for(int j=sa.indexOf('1');j<sa.lastIndexOf('1');j++) {

					if(sa.charAt(j)=='0') {

						c++;

					}

				}

				System.out.println(c);

}

}

}


import java.io.*;

import java.util.*;

public class Aq {

	public static void main(String[] args) throws NumberFormatException, IOException {

		BufferedReader br = new BufferedReader (new InputStreamReader (System.in));

		int numberSets = Integer.parseInt(br.readLine());

		for (int nos = 0; nos < numberSets; nos++) {

			String s = br.readLine();

			int x1 = s.indexOf('1');

			int x2 = s.lastIndexOf('1');

			if (x1 == -1) {

				System.out.println(0);

				continue;

			}

			int counter = 0;

			for (int i = x1; i < x2; i++) {

				if (s.charAt(i)=='0') {

					counter++;

				}

			}

			System.out.println(counter);

		}

	}

}


C. Two Arraystime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and mm. Calculate the number of pairs of arrays (a,b)(a,b) such that:  the length of both arrays is equal to mm;  each element of each array is an integer between 11 and nn (inclusive);  ai≤biai≤bi for any index ii from 11 to mm;  array aa is sorted in non-descending order;  array bb is sorted in non-ascending order. As the result can be very large, you should print it modulo 109+7109+7.InputThe only line contains two integers nn and mm (1≤n≤10001≤n≤1000, 1≤m≤101≤m≤10).OutputPrint one integer – the number of arrays aa and bb satisfying the conditions described above modulo 109+7109+7.ExamplesInputCopy2 2
OutputCopy5
InputCopy10 1
OutputCopy55
InputCopy723 9
OutputCopy157557417
NoteIn the first test there are 55 suitable arrays:   a=[1,1],b=[2,2]a=[1,1],b=[2,2];  a=[1,2],b=[2,2]a=[1,2],b=[2,2];  a=[2,2],b=[2,2]a=[2,2],b=[2,2];  a=[1,1],b=[2,1]a=[1,1],b=[2,1];  a=[1,1],b=[1,1]a=[1,1],b=[1,1].

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.PrintWriter;

public class Naive {

    static class FastScanner

    {

        //I don't understand how this works lmao

        private int BS = 1 << 16;

        private char NC = (char) 0;

        private byte[] buf = new byte[BS];

        private int bId = 0, size = 0;

        private char c = NC;

        private double cnt = 1;

        private BufferedInputStream in;

        public FastScanner() {

            in = new BufferedInputStream(System.in, BS);

        }

        public FastScanner(String s) {

            try {

                in = new BufferedInputStream(new FileInputStream(new File(s)), BS);

            } catch (Exception e) {

                in = new BufferedInputStream(System.in, BS);

            }

        }

        private char getChar() {

            while (bId == size) {

                try {

                    size = in.read(buf);

                } catch (Exception e) {

                    return NC;

                }

                if (size == -1) return NC;

                bId = 0;

            }

            return (char) buf[bId++];

        }

        public int nextInt() {

            return (int) nextLong();

        }

        public int[] nextInts(int N) {

            int[] res = new int[N];

            for (int i = 0; i < N; i++) {

                res[i] = (int) nextLong();

            }

            return res;

        }

        public long[] nextLongs(int N) {

            long[] res = new long[N];

            for (int i = 0; i < N; i++) {

                res[i] = nextLong();

            }

            return res;

        }

        public long nextLong() {

            cnt = 1;

            boolean neg = false;

            if (c == NC) c = getChar();

            for (; (c < '0' || c > '9'); c = getChar()) {

                if (c == '-') neg = true;

            }

            long res = 0;

            for (; c >= '0' && c <= '9'; c = getChar()) {

                res = (res << 3) + (res << 1) + c - '0';

                cnt *= 10;

            }

            return neg ? -res : res;

        }

        public double nextDouble() {

            double cur = nextLong();

            return c != '.' ? cur : cur + nextLong() / cnt;

        }

        public double[] nextDoubles(int N) {

            double[] res = new double[N];

            for (int i = 0; i < N; i++) {

                res[i] = nextDouble();

            }

            return res;

        }

        public String next() {

            StringBuilder res = new StringBuilder();

            while (c <= 32) c = getChar();

            while (c > 32) {

                res.append(c);

                c = getChar();

            }

            return res.toString();

        }

        public String nextLine() {

            StringBuilder res = new StringBuilder();

            while (c <= 32) c = getChar();

            while (c != '\n') {

                res.append(c);

                c = getChar();

            }

            return res.toString();

        }

        public boolean hasNext() {

            if (c > 32) return true;

            while (true) {

                c = getChar();

                if (c == NC) return false;

                else if (c > 32) return true;

            }

        }

    }

    public static void main(String[] args) {

        FastScanner in = new FastScanner();

        int n = in.nextInt(); // 1 <= a[i] <= n

        int m = in.nextInt(); // 1 <= i <= m

        PrintWriter out = new PrintWriter(System.out);

        out.println(getCountOfValidArrays(n, m));

        out.close();

    }

    private static long add(long a, long b) {

        return (a + b) % MOD;

    }

    private static final long MOD = (long) (1e9 + 7);

    private static long getCountOfValidArrays(int n, int m) {

        long[][] dp = new long[2 * m + 1][n + 1]; // dp[i][j] = # of valid arrays of size 'i' ending with 'j'

        dp[0][0] = 1;

        for (int i = 1; i <= 2 * m; i++) {

            for (int j = 1; j <= n; j++) {

                for (int k = 0; k <= j; k++) {

                    dp[i][j] = add(dp[i][j], dp[i - 1][k]);

                }

            }

        }

        long ans = 0;

        for (int i = 0; i <= n; i++) {

            ans = add(ans, dp[2 * m][i]);

        }

        return ans;

    }

}


E. 1-Trees and Queriestime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputGildong was hiking a mountain; walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree?Then he found that such tree-like graphs are called 1-trees. Since Gildong was bored of solving too many tree problems, he wanted to see if similar techniques in trees can be used in 1-trees as well. Instead of solving it by himself, he's going to test you by providing queries on 1-trees.First, he'll provide you a tree (not 1-tree) with nn vertices, then he will ask you qq queries. Each query contains 55 integers: xx, yy, aa, bb, and kk. This means you're asked to determine if there exists a path from vertex aa to bb that contains exactly kk edges after adding a bidirectional edge between vertices xx and yy. A path can contain the same vertices and same edges multiple times. All queries are independent of each other; i.e. the added edge in a query is removed in the next query.InputThe first line contains an integer nn (3≤n≤1053≤n≤105), the number of vertices of the tree.Next n−1n−1 lines contain two integers uu and vv (1≤u,v≤n1≤u,v≤n, u≠vu≠v) each, which means there is an edge between vertex uu and vv. All edges are bidirectional and distinct.Next line contains an integer qq (1≤q≤1051≤q≤105), the number of queries Gildong wants to ask.Next qq lines contain five integers xx, yy, aa, bb, and kk each (1≤x,y,a,b≤n1≤x,y,a,b≤n, x≠yx≠y, 1≤k≤1091≤k≤109) – the integers explained in the description. It is guaranteed that the edge between xx and yy does not exist in the original tree.OutputFor each query, print "YES" if there exists a path that contains exactly kk edges from vertex aa to bb after adding an edge between vertices xx and yy. Otherwise, print "NO".You can print each letter in any case (upper or lower).ExampleInputCopy5
1 2
2 3
3 4
4 5
5
1 3 1 2 2
1 4 1 3 2
1 4 1 3 3
4 2 3 3 9
5 2 3 3 9
OutputCopyYES
YES
NO
YES
NO
NoteThe image below describes the tree (circles and solid lines) and the added edges for each query (dotted lines).  Possible paths for the queries with "YES" answers are:   11-st query: 11 – 33 – 22  22-nd query: 11 – 22 – 33  44-th query: 33 – 44 – 22 – 33 – 44 – 22 – 33 – 44 – 22 – 33

import java.io.*;

import java.util.*;

import static java.lang.Math.*;

import static java.util.Arrays.*;

public class cf1304e_2 {

    public static void main(String[] args) throws IOException {

        int n = ri();

        dep = new int[n];

        anc = new int[n][18];

        List<List<Integer>> g = rg(n, n - 1);

        binary_lifting_dfs(g, 0, -1);

        int q = ri();

        while (q --> 0) {

            int x = rni() - 1, y = ni() - 1, a = ni() - 1, b = ni() - 1, k = ni(), d;

            d = dist(a, b);

            if (k >= d && (k - d) % 2 == 0) {

                prY();

                continue;

            }

            d = dist(a, x) + 1 + dist(y, b);

            if (k >= d && (k - d) % 2 == 0) {

                prY();

                continue;

            }

            d = dist(a, y) + 1 + dist(x, b);

            if (k >= d && (k - d) % 2 == 0) {

                prY();

                continue;

            }

            prN();

        }

        close();

    }

    // initialization: dep[n], anc[n][lg(n)], fill(anc, -1)

    static int dep[], anc[][];

    static int dist(int a, int b) {

        int lca = lca(a, b);

        return (dep[a] - dep[lca]) + (dep[b] - dep[lca]);

    }

    static int lca(int a, int b) {

        if (dep[a] < dep[b]) {

            int __swap = a;

            a = b;

            b = __swap;

        }

        for(int i = anc[a].length - 1; i >= 0; --i) {

            if(dep[a] - (1 << i) >= dep[b]) {

                a = anc[a][i];

            }

        }

        if(a == b) {

            return a;

        } else {

            for(int i = anc[a].length - 1; i >= 0; --i) {

                if(anc[a][i] != anc[b][i]) {

                    a = anc[a][i];

                    b = anc[b][i];

                }

            }

            return anc[a][0];

        }

    }

    // anc should be initialized to all -1s

    static void binary_lifting_dfs(List<? extends Collection<Integer>> g, int i, int p) {

        if (p == -1) {

            dep[i] = 0;

        } else {

            dep[i] = dep[p] + 1;

        }

        anc[i][0] = p;

        for (int j = 1; j < anc[i].length && anc[i][j - 1] >= 0; ++j) {

            anc[i][j] = anc[anc[i][j - 1]][j - 1];

        }

        for (int n : g.get(i)) {

            if (n != p) {

                binary_lifting_dfs(g, n, i);

            }

        }

    }

    static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));

    static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));

    static StringTokenizer input;

    static Random __rand = new Random();

    // references

    // IBIG = 1e9 + 7

    // IMAX ~= 2e9

    // LMAX ~= 9e18

    // constants

    static final int IBIG = 1000000007;

    static final int IMAX = 2147483647;

    static final int IMIN = -2147483648;

    static final long LMAX = 9223372036854775807L;

    static final long LMIN = -9223372036854775808L;

    // math util

    static int minof(int a, int b, int c) {return min(a, min(b, c));}

    static int minof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}

    static long minof(long a, long b, long c) {return min(a, min(b, c));}

    static long minof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return min(x[0], x[1]); if(x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] < min) min = x[i]; return min;}

    static int maxof(int a, int b, int c) {return max(a, max(b, c));}

    static int maxof(int... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}

    static long maxof(long a, long b, long c) {return max(a, max(b, c));}

    static long maxof(long... x) {if(x.length == 1) return x[0]; if(x.length == 2) return max(x[0], x[1]); if(x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for(int i = 1; i < x.length; ++i) if(x[i] > max) max = x[i]; return max;}

    static int powi(int a, int b) {if(a == 0) return 0; int ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}

    static long powl(long a, int b) {if(a == 0) return 0; long ans = 1; while(b > 0) {if((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}

    static int fli(double d) {return (int)d;}

    static int cei(double d) {return (int)ceil(d);}

    static long fll(double d) {return (long)d;}

    static long cel(double d) {return (long)ceil(d);}

    static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}

    static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}

    static int lcm(int a, int b) {return a * b / gcf(a, b);}

    static long lcm(long a, long b) {return a * b / gcf(a, b);}

    static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}

    static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}

    // array util

    static void reverse(int[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}

    static void reverse(long[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}

    static void reverse(double[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}

    static void reverse(char[] a) {for(int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}

    static void shuffle(int[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}

    static void shuffle(long[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}

    static void shuffle(double[] a) {int n = a.length - 1; for(int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}

    static void rsort(int[] a) {shuffle(a); sort(a);}

    static void rsort(long[] a) {shuffle(a); sort(a);}

    static void rsort(double[] a) {shuffle(a); sort(a);}

    static int[] copy(int[] a) {int[] ans = new int[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}

    static long[] copy(long[] a) {long[] ans = new long[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}

    static double[] copy(double[] a) {double[] ans = new double[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}

    static char[] copy(char[] a) {char[] ans = new char[a.length]; for(int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}

    // graph util

    static List<List<Integer>> g(int n) {List<List<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new ArrayList<>()); return g;}

    static List<Set<Integer>> sg(int n) {List<Set<Integer>> g = new ArrayList<>(); for(int i = 0; i < n; ++i) g.add(new HashSet<>()); return g;}

    static void c(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v); g.get(v).add(u);}

    static void cto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).add(v);}

    static void dc(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v); g.get(v).remove(u);}

    static void dcto(List<? extends Collection<Integer>> g, int u, int v) {g.get(u).remove(v);}

    // input

    static void r() throws IOException {input = new StringTokenizer(rline());}

    static int ri() throws IOException {return Integer.parseInt(rline());}

    static long rl() throws IOException {return Long.parseLong(rline());}

    static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for(int i = 0; i < n; ++i) a[i] = ni(); return a;}

    static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for(int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}

    static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for(int i = 0; i < n; ++i) a[i] = nl(); return a;}

    static char[] rcha() throws IOException {return rline().toCharArray();}

    static String rline() throws IOException {return __in.readLine();}

    static String n() {return input.nextToken();}

    static int rni() throws IOException {r(); return ni();}

    static int ni() {return Integer.parseInt(n());}

    static long rnl() throws IOException {r(); return nl();}

    static long nl() {return Long.parseLong(n());}

    static double rnd() throws IOException {r(); return nd();}

    static double nd() {return Double.parseDouble(n());}

    static List<List<Integer>> rg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}

    static void rg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}

    static List<List<Integer>> rdg(int n, int m) throws IOException {List<List<Integer>> g = g(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}

    static void rdg(List<List<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}

    static List<Set<Integer>> rsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1); return g;}

    static void rsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) c(g, rni() - 1, ni() - 1);}

    static List<Set<Integer>> rdsg(int n, int m) throws IOException {List<Set<Integer>> g = sg(n); for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1); return g;}

    static void rdsg(List<Set<Integer>> g, int m) throws IOException {for(int i = 0; i < m; ++i) cto(g, rni() - 1, ni() - 1);}

    // output

    static void pr(int i) {__out.print(i);}

    static void prln(int i) {__out.println(i);}

    static void pr(long l) {__out.print(l);}

    static void prln(long l) {__out.println(l);}

    static void pr(double d) {__out.print(d);}

    static void prln(double d) {__out.println(d);}

    static void pr(char c) {__out.print(c);}

    static void prln(char c) {__out.println(c);}

    static void pr(char[] s) {__out.print(new String(s));}

    static void prln(char[] s) {__out.println(new String(s));}

    static void pr(String s) {__out.print(s);}

    static void prln(String s) {__out.println(s);}

    static void pr(Object o) {__out.print(o);}

    static void prln(Object o) {__out.println(o);}

    static void prln() {__out.println();}

    static void pryes() {__out.println("yes");}

    static void pry() {__out.println("Yes");}

    static void prY() {__out.println("YES");}

    static void prno() {__out.println("no");}

    static void prn() {__out.println("No");}

    static void prN() {__out.println("NO");}

    static void pryesno(boolean b) {__out.println(b ? "yes" : "no");};

    static void pryn(boolean b) {__out.println(b ? "Yes" : "No");}

    static void prYN(boolean b) {__out.println(b ? "YES" : "NO");}

    static void prln(int... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}

    static void prln(long... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}

    static void prln(double... a) {for(int i = 0, len = a.length - 1; i < len; __out.print(a[i]), __out.print(' '), ++i); __out.println(a[a.length - 1]);}

    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for(int i = 0; i < n; __out.print(iter.next()), __out.print(' '), ++i); if(n >= 0) __out.println(iter.next()); else __out.println();}

    static void h() {__out.println("hlfd"); flush();}

    static void flush() {__out.flush();}

    static void close() {__out.close();}

}


A. Dead Pixeltime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputScreen resolution of Polycarp's monitor is a×ba×b pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates (x,y)(x,y) (0≤x<a,0≤y<b0≤x<a,0≤y<b). You can consider columns of pixels to be numbered from 00 to a−1a−1, and rows — from 00 to b−1b−1.Polycarp wants to open a rectangular window of maximal size, which doesn't contain the dead pixel. The boundaries of the window should be parallel to the sides of the screen.Print the maximal area (in pixels) of a window that doesn't contain the dead pixel inside itself.InputIn the first line you are given an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test. In the next lines you are given descriptions of tt test cases.Each test case contains a single line which consists of 44 integers a,b,xa,b,x and yy (1≤a,b≤1041≤a,b≤104; 0≤x<a0≤x<a; 0≤y<b0≤y<b) — the resolution of the screen and the coordinates of a dead pixel. It is guaranteed that a+b>2a+b>2 (e.g. a=b=1a=b=1 is impossible).OutputPrint tt integers — the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn't contain the dead pixel.ExampleInputCopy6
8 8 0 0
1 10 0 3
17 31 10 4
2 1 0 0
5 10 3 9
10 10 4 8
OutputCopy56
6
442
1
45
80
NoteIn the first test case; the screen resolution is 8×88×8, and the upper left pixel is a dead pixel. Here you can see one of two possible layouts of the maximal window.

import java.util.Scanner;

public class App {

    private static Scanner fs;

    public static void solve() {

        int a=fs.nextInt();

        int b=fs.nextInt();

        int x=fs.nextInt();

        int y=fs.nextInt();

        int ans=0;

        ans=Math.max(x*b,(a-x-1)*b);

        ans=Math.max(ans,Math.max(y*a,(b-y-1)*a));

        System.out.println(ans);

    }

    public static void main(String[] args) {

        fs = new Scanner(System.in);

        int t=fs.nextInt();

        while (t>0) {

            solve();

            t--;

        }

        fs.close();

    }

}


F1. Animal Observation (easy version)time limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe only difference between easy and hard versions is the constraint on kk.Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The color of one camera is red, and the other one's color is blue.Gildong is going to take videos for nn days, starting from day 11 to day nn. The forest can be divided into mm areas, numbered from 11 to mm. He'll use the cameras in the following way:   On every odd day (11-st, 33-rd, 55-th, ...), bring the red camera to the forest and record a video for 22 days.  On every even day (22-nd, 44-th, 66-th, ...), bring the blue camera to the forest and record a video for 22 days.  If he starts recording on the nn-th day with one of the cameras, the camera records for only one day. Each camera can observe kk consecutive areas of the forest. For example, if m=5m=5 and k=3k=3, he can put a camera to observe one of these three ranges of areas for two days: [1,3][1,3], [2,4][2,4], and [3,5][3,5].Gildong got information about how many animals will be seen in each area each day. Since he would like to observe as many animals as possible, he wants you to find the best way to place the two cameras for nn days. Note that if the two cameras are observing the same area on the same day, the animals observed in that area are counted only once.InputThe first line contains three integers nn, mm, and kk (1≤n≤501≤n≤50, 1≤m≤2⋅1041≤m≤2⋅104, 1≤k≤min(m,20)1≤k≤min(m,20)) – the number of days Gildong is going to record, the number of areas of the forest, and the range of the cameras, respectively.Next nn lines contain mm integers each. The jj-th integer in the i+1i+1-st line is the number of animals that can be seen on the ii-th day in the jj-th area. Each number of animals is between 00 and 10001000, inclusive.OutputPrint one integer – the maximum number of animals that can be observed.ExamplesInputCopy4 5 2
0 2 1 1 0
0 0 3 1 2
1 0 4 3 1
3 3 0 0 4
OutputCopy25
InputCopy3 3 1
1 2 3
4 5 6
7 8 9
OutputCopy31
InputCopy3 3 2
1 2 3
4 5 6
7 8 9
OutputCopy44
InputCopy3 3 3
1 2 3
4 5 6
7 8 9
OutputCopy45
NoteThe optimal way to observe animals in the four examples are as follows:Example 1:   Example 2:   Example 3:   Example 4:

//package round620;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintWriter;

import java.util.ArrayDeque;

import java.util.Arrays;

import java.util.Deque;

import java.util.InputMismatchException;

public class F2 {

	InputStream is;

	PrintWriter out;

	String INPUT = "";

	void solve()

	{

		int n = ni(), m = ni(), K = ni();

		int[][] a = new int[n][];

		for(int i = 0;i < n;i++){

			a[i] = na(m);

		}

		long[][] cum = new long[n][m+1];

		for(int i = 0;i < n;i++){

			for(int j = 0;j < m;j++){

				cum[i][j+1] = cum[i][j] + a[i][j];

			}

		}

		long[][] rcum = new long[n][m+1];

		for(int i = 0;i < n;i++){

			for(int j = 0;j < m;j++){

				rcum[i][j+1] = rcum[i][j] + a[i][m-1-j];

			}

		}

		long[] dp = new long[m-K+1];

		for(int i = 0;i < m-K+1;i++){

			dp[i] = cum[0][i+K] - cum[0][i];

		}

		for(int i = 1;i < n;i++){

			dp = trans(dp, cum[i], rcum[i] ,K);

		}

		long ans = 0;

		for(long v : dp){

			ans = Math.max(ans, v);

		}

		out.println(ans);

	}

	long[] trans(long[] dp, long[] cum, long[] rcum, int K)

	{

		int n = cum.length-1;

		for(int i = 0;i < n-K+1;i++){

			dp[i] += cum[i+K] - cum[i];

		}

//		tr(dp, cum, rcum);

		long[] ndp = new long[n-K+1];

		Arrays.fill(ndp, Long.MIN_VALUE / 2);

		// 2321

		// 000346

		go(ndp, dp, cum, K);

		rev_(ndp);

		rev_(dp);

		go(ndp, dp, rcum, K);

		rev_(ndp);

//		tr(ndp);

		return ndp;

	}

	public static long[] rev_(long[] a)

	{

		for(int i = 0, j = a.length-1;i < j;i++,j--){

			long c = a[i]; a[i] = a[j]; a[j] = c;

		}

		return a;

	}

	void go(long[] ndp, long[] dp, long[] cum, int K)

	{

		int n = cum.length-1;

		long flatmax = Long.MIN_VALUE / 2;

		Deque<long[]> nonflat = new ArrayDeque<>();

		for(int i = 0;i < n-K+1;i++){

			while(!nonflat.isEmpty() && nonflat.peekLast()[1] < dp[i]-cum[i+K]){

				nonflat.pollLast();

			}

			nonflat.add(new long[]{i, dp[i]-cum[i+K]});

			while(!nonflat.isEmpty() && nonflat.peekFirst()[0] < i-K){

				nonflat.pollFirst();

			}

			if(i-K >= 0){

				flatmax = Math.max(flatmax, dp[i-K]);

			}

//			tr(flatmax, nonflat.peekFirst());

			ndp[i] = Math.max(ndp[i], flatmax + cum[i+K] - cum[i]);

			if(!nonflat.isEmpty()){

				ndp[i] = Math.max(ndp[i], nonflat.peekFirst()[1] + cum[i] + cum[i+K] - cum[i]);

			}

		}

	}

	void run() throws Exception

	{

		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());

		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();

		solve();

		out.flush();

		tr(System.currentTimeMillis()-s+"ms");

	}

	public static void main(String[] args) throws Exception { new F2().run(); }

	private byte[] inbuf = new byte[1024];

	public int lenbuf = 0, ptrbuf = 0;

	private int readByte()

	{

		if(lenbuf == -1)throw new InputMismatchException();

		if(ptrbuf >= lenbuf){

			ptrbuf = 0;

			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }

			if(lenbuf <= 0)return -1;

		}

		return inbuf[ptrbuf++];

	}

	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }

	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

	private double nd() { return Double.parseDouble(ns()); }

	private char nc() { return (char)skip(); }

	private String ns()

	{

		int b = skip();

		StringBuilder sb = new StringBuilder();

		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')

			sb.appendCodePoint(b);

			b = readByte();

		}

		return sb.toString();

	}

	private char[] ns(int n)

	{

		char[] buf = new char[n];

		int b = skip(), p = 0;

		while(p < n && !(isSpaceChar(b))){

			buf[p++] = (char)b;

			b = readByte();

		}

		return n == p ? buf : Arrays.copyOf(buf, p);

	}

	private char[][] nm(int n, int m)

	{

		char[][] map = new char[n][];

		for(int i = 0;i < n;i++)map[i] = ns(m);

		return map;

	}

	private int[] na(int n)

	{

		int[] a = new int[n];

		for(int i = 0;i < n;i++)a[i] = ni();

		return a;

	}

	private int ni()

	{

		int num = 0, b;

		boolean minus = false;

		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));

		if(b == '-'){

			minus = true;

			b = readByte();

		}

		while(true){

			if(b >= '0' && b <= '9'){

				num = num * 10 + (b - '0');

			}else{

				return minus ? -num : num;

			}

			b = readByte();

		}

	}

	private long nl()

	{

		long num = 0;

		int b;

		boolean minus = false;

		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));

		if(b == '-'){

			minus = true;

			b = readByte();

		}

		while(true){

			if(b >= '0' && b <= '9'){

				num = num * 10 + (b - '0');

			}else{

				return minus ? -num : num;

			}

			b = readByte();

		}

	}

	private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

	private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }

}


A. Mind Controltime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou and your n−1n−1 friends have found an array of integers a1,a2,…,ana1,a2,…,an. You have decided to share it in the following way: All nn of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.You are standing in the mm-th position in the line. Before the process starts, you may choose up to kk different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.Suppose that you're doing your choices optimally. What is the greatest integer xx such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to xx?Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.InputThe input consists of multiple test cases. The first line contains a single integer tt (1≤t≤10001≤t≤1000)  — the number of test cases. The description of the test cases follows.The first line of each test case contains three space-separated integers nn, mm and kk (1≤m≤n≤35001≤m≤n≤3500, 0≤k≤n−10≤k≤n−1)  — the number of elements in the array, your position in line and the number of people whose choices you can fix.The second line of each test case contains nn positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109)  — elements of the array.It is guaranteed that the sum of nn over all test cases does not exceed 35003500.OutputFor each test case, print the largest integer xx such that you can guarantee to obtain at least xx.ExampleInputCopy4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
OutputCopy8
4
1
1
NoteIn the first test case; an optimal strategy is to force the first person to take the last element and the second person to take the first element.  the first person will take the last element (55) because he or she was forced by you to take the last element. After this turn the remaining array will be [2,9,2,3,8][2,9,2,3,8];  the second person will take the first element (22) because he or she was forced by you to take the first element. After this turn the remaining array will be [9,2,3,8][9,2,3,8];  if the third person will choose to take the first element (99), at your turn the remaining array will be [2,3,8][2,3,8] and you will take 88 (the last element);  if the third person will choose to take the last element (88), at your turn the remaining array will be [9,2,3][9,2,3] and you will take 99 (the first element). Thus, this strategy guarantees to end up with at least 88. We can prove that there is no strategy that guarantees to end up with at least 99. Hence, the answer is 88.In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with 44.

import java.util.*;

// import java.lang.*;

import java.io.*;

//           THIS TEMPLATE MADE BY AKSH BANSAL.

public class Solution {

    static class FastReader {

        BufferedReader br;

        StringTokenizer st;

        public FastReader() {

            br = new BufferedReader(new InputStreamReader(System.in));

        }

        String next() {

            while (st == null || !st.hasMoreElements()) {

                try {

                    st = new StringTokenizer(br.readLine());

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            return st.nextToken();

        }

        int nextInt() {

            return Integer.parseInt(next());

        }

        long nextLong() {

            return Long.parseLong(next());

        }

        double nextDouble() {

            return Double.parseDouble(next());

        }

        String nextLine() {

            String str = "";

            try {

                str = br.readLine();

            } catch (IOException e) {

                e.printStackTrace();

            }

            return str;

        }

    }

    private static boolean[] isPrime;

    private static void primes(){

        int num = (int)1e6; // PRIMES FROM 1 TO NUM

        isPrime = new boolean[num];

        for (int i = 2; i< isPrime.length; i++) {

           isPrime[i] = true;

        }

        for (int i = 2; i< Math.sqrt(num); i++) {

           if(isPrime[i] == true) {

              for(int j = (i*i); j<num; j = j+i) {

                 isPrime[j] = false;

              }

           }

        }

    }

    static void sort(int a[]){

        ArrayList<Integer> arr=new ArrayList<>();

        for(int i=0;i<a.length;i++)

        arr.add(a[i]);

        Collections.sort(arr);

        for(int i=0;i<a.length;i++)

        a[i]=arr.get(i);

    }

    static void sort(long a[]){

        ArrayList<Long> arr=new ArrayList<>();

        for(int i=0;i<a.length;i++)

        arr.add(a[i]);

        Collections.sort(arr);

        for(int i=0;i<a.length;i++)

        a[i]=arr.get(i);

    }

    private static long gcd(long a, long b){

        if(b==0)return a;

        return gcd(b,a%b);

    }

    private static long pow(long x,long y){

        if(y==0)return 1;

        long temp = pow(x, y/2);

        if(y%2==1){

            return x*temp*temp;

        }

        else{

            return temp*temp;

        }

    }

    // static ArrayList<Integer>[] adj;

    // static void getAdj(int n,int q, FastReader sc){

    //     adj = new ArrayList[n+1];

    //     for(int i=1;i<=n;i++){

    //         adj[i] = new ArrayList<>();

    //     }

    //     for(int i=0;i<q;i++){

    //         int a = sc.nextInt();

    //         int b = sc.nextInt();

    //         adj[a].add(b);

    //         adj[b].add(a);

    //     }

    // }

    static PrintWriter out;

    static FastReader sc ;

    public static void main(String[] args) throws IOException {

        sc = new FastReader();

        out = new PrintWriter(System.out);

        // primes();

        // ________________________________

        int t = sc.nextInt();

        StringBuilder output = new StringBuilder();

        while (t-- > 0) {

            int n= sc.nextInt();

            int m= sc.nextInt();

            int k= sc.nextInt();

            int[] arr =new int[n];

            for(int i=0;i<n;i++){

                arr[i] =sc.nextInt();

            }

            output.append(solver(arr, n, m, k)).append("\n");

        }

        out.println(output);

        // _______________________________

        // int n = sc.nextInt();

        // out.println(solver());

        // ________________________________

        out.flush();

    }

    public static int solver(int[]arr, int n, int m, int k) {

        if(k>=m-1){

            int max = 0;

            for(int i=0;i<m;i++){

                max = Math.max(max, arr[i]);

            }

            for(int i=n-1;i>=n-m;i--){

                max = Math.max(max, arr[i]);

            }

            return max;

        }

        else{

            int best = -1;

            for(int kk=0;kk<=k;kk++){

                int min =Integer.MAX_VALUE;

                for(int i=0;i<m-k;i++){

                    // System.out.println(arr[kk+i]+"_"+kk+"_"+ (arr[n+1-k+kk-m+k+i]));

                    int temp = Math.max(arr[kk+i], arr[n-k+kk-m+k+i]);

                    min = Math.min(min, temp);

                    // System.out.println(kk+"__"+ min);

                }

                best = Math.max(best, min);

            }

            return best;

        }

    }

}


B. Array Sharpeningtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou're given an array a1,…,ana1,…,an of nn non-negative integers.Let's call it sharpened if and only if there exists an integer 1≤k≤n1≤k≤n such that a1<a2<…<aka1<a2<…<ak and ak>ak+1>…>anak>ak+1>…>an. In particular, any strictly increasing or strictly decreasing array is sharpened. For example:  The arrays [4][4], [0,1][0,1], [12,10,8][12,10,8] and [3,11,15,9,7,4][3,11,15,9,7,4] are sharpened;  The arrays [2,8,2,8,6,5][2,8,2,8,6,5], [0,1,1,0][0,1,1,0] and [2,5,6,9,8,8][2,5,6,9,8,8] are not sharpened. You can do the following operation as many times as you want: choose any strictly positive element of the array, and decrease it by one. Formally, you can choose any ii (1≤i≤n1≤i≤n) such that ai>0ai>0 and assign ai:=ai−1ai:=ai−1.Tell if it's possible to make the given array sharpened using some number (possibly zero) of these operations.InputThe input consists of multiple test cases. The first line contains a single integer tt (1≤t≤15 0001≤t≤15 000)  — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer nn (1≤n≤3⋅1051≤n≤3⋅105).The second line of each test case contains a sequence of nn non-negative integers a1,…,ana1,…,an (0≤ai≤1090≤ai≤109).It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053⋅105.OutputFor each test case, output a single line containing "Yes" (without quotes) if it's possible to make the given array sharpened using the described operations, or "No" (without quotes) otherwise.ExampleInputCopy10
1
248618
3
12 10 8
6
100 11 15 9 7 8
4
0 1 1 0
2
0 0
2
0 1
2
1 0
2
1 1
3
0 1 0
3
1 0 1
OutputCopyYes
Yes
Yes
No
No
Yes
Yes
Yes
Yes
No
NoteIn the first and the second test case of the first test; the given array is already sharpened.In the third test case of the first test; we can transform the array into [3,11,15,9,7,4][3,11,15,9,7,4] (decrease the first element 9797 times and decrease the last element 44 times). It is sharpened because 3<11<153<11<15 and 15>9>7>415>9>7>4.In the fourth test case of the first test, it's impossible to make the given array sharpened.

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

public class ArraySharpening {

    public static void main(String[] args) throws IOException {

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        PrintWriter pr=new PrintWriter(System.out);

        int t=Integer.parseInt(br.readLine());

        while(t!=0){

            solve(br,pr);

            t--;

        }

        pr.flush();

        pr.close();

    }

    public static void solve(BufferedReader br,PrintWriter pr) throws IOException{

        int n=Integer.parseInt(br.readLine());

        int[] nums=new int[n];

        String[] temp=br.readLine().split(" ");

        for(int i=0;i<n;i++){

            nums[i]=Integer.parseInt(temp[i]);

        }

        boolean[] canIncreaseLeft=new boolean[n];

        canIncreaseLeft[0]=true;

        for(int i=1;i<n;i++){

            if(canIncreaseLeft[i-1]==false){

                canIncreaseLeft[i]=false;

                continue;

            }

            if(nums[i]>=i){

                canIncreaseLeft[i]=true;

            }

            else{

                canIncreaseLeft[i]=false;

            }

        }

        boolean[] canDecreaseRight=new boolean[n];

        canDecreaseRight[n-1]=true;

        for(int i=n-2;i>=0;i--){

            if(canDecreaseRight[i+1]==false){

                canDecreaseRight[i]=false;

                continue;

            }

            if(nums[i]>=n-1-i){

                canDecreaseRight[i]=true;

            }

            else{

                canDecreaseRight[i]=false;

            }

        }

        if(canDecreaseRight[0]||canIncreaseLeft[n-1]){

            pr.println("Yes");

            return;

        }

        for(int i=1;i<n-1;i++){

            if(canIncreaseLeft[i]&&canDecreaseRight[i]){

                pr.println("Yes");

                return;

            }

        }

        pr.println("No");

    }

}