import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
        int H = sc.nextInt();
        int W = sc.nextInt();
        char[][] S = new char[H][W];
        for(int i=0; i<H; i++){
            S[i] = sc.next().toCharArray();
        }
        //String S = sc.next();

        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                if(S[i][j]=='#'){
                    System.out.print(S[i][j]);
                    continue;
                }
                int count=0;
                if(i>0){
                    if(S[i-1][j]=='#'){
                        count++;
                    }
                    if(j>0 && S[i-1][j-1]=='#'){
                        count++;
                    }
                    if(j<W-1 && S[i-1][j+1]=='#'){
                        count++;
                    }
                }

                if(j>0 && S[i][j-1]=='#'){
                    count++;
                }
                if(j<W-1 && S[i][j+1]=='#'){
                    count++;
                }

                if(i<H-1){
                    if(S[i+1][j]=='#'){
                        count++;
                    }
                    if(j>0 && S[i+1][j-1]=='#'){
                        count++;
                    }
                    if(j<W-1 && S[i+1][j+1]=='#'){
                        count++;
                    }
                }

                System.out.print(count);
            }
            System.out.println();
        }

        }

        }

Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.

Print the strings after the process.

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310


import java.util.*;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int H = sc.nextInt();
		int W = sc.nextInt();

		String[][] S = new String[H][W];
		for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
            S[i][j] = sc.next();
            }
		}

		int count = 0;
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				count = 0;
				if(S[i][j].equals("#")) {
					continue;
				}

				if(j-1>=0) {
                    if(i-1>=0&&S[i-1][j-1].equals("#"))
                    count++;
                    if(S[i][j-1].equals("#"))
                    count++;
                    if(i+1<H&&S[i+1][j-1].equals("#"))
                    count++;
				}

                if(i-1>=0&&S[i-1][j].equals("#"))
                count++;
                if(i+1<H&&S[i+1][j].equals("#"))
                count++;

				if(j+1<W) {
                    if(i-1>=0&&S[i-1][j+1].equals("#"))
                    count++;
                    if(S[i][j+1].equals("#"))
                    count++;
                    if(i+1<H&&S[i+1][j+1].equals("#"))
                    count++;
				}

				S[i][j] = ""+count;
			}
		}

        for(int i=0; i<H; i++){
            System.out.print(String.join("",S[i]));
        }

	}
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


import java.util.*;

public class Main {

  public static void main (String[] args) {

    Scanner sc = new Scanner(System.in);

    int H = sc.nextInt();
    int W = sc.nextInt();

    String[][] S = new String[H][W];

    for (int i = 0; i < H; i++) {
      String s = sc.next();
      S[i] = s.split("");
    }

    sc.close();

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (S[i][j].equals(".")) {

          int count = 0;

          boolean checkU = true;
          boolean checkD = true;
          boolean checkR = true;
          boolean checkL = true;

          if (i == 0) {
            checkU = false;
          }
          if (i == H - 1){
            checkD = false;
          }
          if (j == 0) {
            checkL = false;
          }
          if (j == W - 1) {
            checkR = false;
          }

          if (checkU) {
            if (S[i-1][j].equals("#")){
              count++;
            }
          }

          if (checkD) {
            if (S[i+1][j].equals("#")){
              count++;
            }
          }

          if (checkL) {
            if (S[i][j-1].equals("#")){
              count++;
            }
          }

          if (checkR) {
            if (S[i][j+1].equals("#")){
              count++;
            }
          }

          if (checkU && checkL) {
            if (S[i-1][j-1].equals("#")){
              count++;
            }
          }

          if (checkU && checkR) {
            if (S[i-1][j+1].equals("#")){
              count++;
            }
          }

          if (checkD && checkL) {
            if (S[i+1][j-1].equals("#")){
              count++;
            }
          }

          if (checkD && checkR) {
            if (S[i+1][j+1].equals("#")){
              count++;
            }
          }

          S[i][j] = String.valueOf(count);

        }
      }
    }

    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        System.out.print(S[i][j]);
      }
      System.out.println("");
    }

  }

}


import java.util.Scanner;

class Main{
    public static void main(String[] args) {
        Scanner scn=new Scanner(System.in);
        int h=scn.nextInt();
        int w=scn.nextInt();
        char[][] map=new char[h][w];
        for(int i=0;i<h;i++){
            String text=scn.next();
            for(int j=0;j<w;j++){
                map[i][j]=text.charAt(j);
            }
        }
        System.out.println("a");
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                int cnt=0;
                if(i!=0){
                    if(map[i-1][j]=='#') cnt++;
                    if(j!=0){
                    if(map[i-1][j-1]=='#') cnt++;
                    }
                    if(j!=w-1){
                        if(map[i-1][j+1]=='#') cnt++;
                    }
                }
                if(i!=h-1){
                    if(map[i+1][j]=='#') cnt++;
                    if(j!=0){
                    if(map[i+1][j-1]=='#') cnt++;
                    }
                    if(j!=w-1){
                        if(map[i+1][j+1]=='#') cnt++;
                    }
                }
                if(j!=0){
                   if( map[i][j-1]=='#') cnt++;
                }
                if(j!=w-1){
                   if( map[i][j+1]=='#') cnt++;
                }
               if(map[i][j]!='#') map[i][j]=(char)(cnt+48);

            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                System.out.print(map[i][j]);
            }
            System.out.println();
        }
    }
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner((System.in));
		int H = sc.nextInt();
		int W = sc.nextInt();
		String[][] a = new String[H + 1][W + 1];

		for (int i = 0; i < H; i++) {
			int j = 0;
			String str = sc.next();
			str = str.replace(".", "0");
			String[] s = str.split("");
			for (String x : s) {
				a[i][j] = x;
				j++;
			}
		}

		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (a[i][j].equals("#")) {
					if (i != 0) {
						if (j != 0) {
							// 1
							if (!a[i - 1][j - 1].equals("#")) {
								a[i - 1][j - 1] = String.valueOf(Integer.valueOf(a[i - 1][j - 1]) + 1);
							}
						}
						// 2
						if (!a[i - 1][j].equals("#")) {
							a[i - 1][j] = String.valueOf(Integer.valueOf(a[i - 1][j]) + 1);
						}
						if (j != (W - 1)) {
							// 3
							if (!a[i - 1][j + 1].equals("#")) {
								a[i - 1][j + 1] = String.valueOf(Integer.valueOf(a[i - 1][j + 1]) + 1);
							}
						}
					}
					if (j != 0) {
						// 4
						if (!a[i][j - 1].equals("#")) {
							a[i][j - 1] = String.valueOf(Integer.valueOf(a[i][j - 1]) + 1);
						}
					}
					if (j != (W - 1)) {
						// 5
						if (!a[i][j + 1].equals("#")) {
							a[i][j + 1] = String.valueOf(Integer.valueOf(a[i][j + 1]) + 1);
						}
					}
					if (i != (H - 1)) {
						if (j != 0) {
							// 6
							if (!a[i + 1][j - 1].equals("#")) {
								a[i + 1][j - 1] = String.valueOf(Integer.valueOf(a[i + 1][j - 1]) + 1);
							}
						}
						// 7
						if (!a[i + 1][j].equals("#")) {
							a[i + 1][j] = String.valueOf(Integer.valueOf(a[i + 1][j]) + 1);
						}
						if (j != (W - 1)) {
							// 8
							if (!a[i + 1][j + 1].equals("#")) {
								a[i + 1][j + 1] = String.valueOf(Integer.valueOf(a[i + 1][j + 1]) + 1);
							}
						}
					}
				}
			}
		}

		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				System.out.print(a[i][j]);
			}
			System.out.println("");
		}

		sc.close();
	}
}


import java.util.Scanner;

public class Main
{
    int dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
    int dy[] = {1, -1, 1, 0, -1, 1, 0, -1};
    int h, w;
    int mp[][];
    String s[][];

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

    void run()
    {
	in();

	for(int i = 0; i < h; i++) {
	    for(int j = 0; j < w; j++) {

		if(s[i][j].equals(".")) continue;
		for(int k = 0; k < 8; k++) {
		    int mx = j + dx[k];
		    int my = i + dy[k];
		    if(mx < 0 || w <= mx || my < 0 || h <= my) continue;
		    mp[my][mx]++;
		}

	    }
	}

	out();
    }

    void in()
    {
	Scanner sc = new Scanner(System.in);

	h = sc.nextInt();
	w = sc.nextInt();
	mp = new int[51][51];
	s  = new String[51][51];

	for(int i = 0; i < h; i++) {
	    String line = sc.next();
	    for (int j = 0; j < w; j++)
		s[i][j] = String.valueOf(line.charAt(j));
	}
    }

    void out()
    {
	for(int i = 0; i < h; i++) {
	    for(int j = 0; j < w; j++) {
		if(s[i][j].equals("."))
		    System.out.print(mp[i][j]);
		else
		    System.out.print('#');
	    }

	    System.out.println();
	}
    }
}


import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;

public class Main {
	 private static String[][] map;

	public static void main(String[] args){
//		FastScanner sc = new FastScanner();
//		int x = sc.nextInt();
//		int y = sc.nextInt();
//		map = new String[x][y];
		int x =3;
		int y =3;
		for(int i=0; i<x;i++){
//			String line = sc.next();
			String line = "...##";
			for(int j=0; j<y; j++){
				map[i][j] = String.valueOf(line.charAt(j));
			}
		}
		for(int i=0;i<x;i++){
			StringBuilder sb = new StringBuilder();
			for(int j=0;j<y;j++){
				sb.append(check(i,j));
			}
			System.out.println(sb.toString());
		}

	}

	private static String check(int x, int y){
		int count = 0;
		String target;
		for(int i=-1; i<=1; i++){
			for(int j=-1;j<=1;j++){
				try {
					target = map[x+i][y+j];
					if(target.equals("#")){
						count++;
					}
				} catch (IndexOutOfBoundsException e){
					continue;
				}
			}
		}
		return String.valueOf(count);
	}

}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int h = scan.nextInt();
		int w = scan.nextInt();
		int[][] count = new int[h][w];
		int[] dx = {0, 0, 1, -1, 1, -1, 1, -1};
		int[] dy = {1, -1, 0, 0, -1, 1, 1, -1};
		String S = "";
		char[][] a = new char[h][w];
		for (int i = 0; i < h; i++) {
			S = scan.next();
			for (int j = 0; j < w; j++) {
				a[i][j] = S.charAt(j);
			}
		}
		int ny = 0;
		int nx = 0;

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (a[i][j] == '.') {
					for (int k = 0; k < 8; k++) {
						ny = i + dy[k];
						nx = j + dx[k];
						if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
						else {
							if (a[ny][nx] == '#') {
								count[i][j]++;
							}
						}
					}
				}
			}
		}
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (a[i][j] == '.') {
					a[i][j] = encode(count[i][j]);
				}
			}
		}
		String str[] = new String[h];
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				if (j == 0) {
					str[i] = String.valueOf(a[i][j]);
				} else {
					str[i] = str[i] + String.valueOf(a[i][j]);
				}
			}
		}
		for (int i = 0; i < h; i++) {
			System.out.println(str[i]);
		}
	}

	static private char encode(int num) {
		switch (num) {
		case 1:
			return '1';
		case 2:
			return '2';
		case 3:
			return '3';
		case 4:
			return '4';
		case 5:
			return '5';
		case 6:
			return '6';
		case 7:
			return '7';
		case 8:
			return '8';
		case 0:
		return '0';
		}
		return '0';
	}
}


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

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.solve(args);
    }

    public void solve(String[] args) {
        MyScanner scanner = new MyScanner();
        int H = scanner.nextInt();
        int W = scanner.nextInt();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < H; i++) {
            list.add(scanner.next());
        }
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                if (list.get(i).charAt(j) == '#') {
                    System.out.print("#");
                    continue;
                }
                int c = 0;
                if (i > 0 && j > 0 && list.get(i-1).charAt(j-1) == '#') {
                    c++;
                }
                if (i > 0 && list.get(i-1).charAt(j) == '#') {
                    c++;
                }
                if (i > 0 && j < W -1 && list.get(i-1).charAt(j+1) == '#') {
                    c++;
                }

                if (j > 0 && list.get(i).charAt(j-1) == '#') {
                    c++;
                }
                if (j < W -1 && list.get(i).charAt(j+1) == '#') {
                    c++;
                }

                if (i < H -1 && j > 0 && list.get(i+1).charAt(j-1) == '#') {
                    c++;
                }
                if (i < H -1 && list.get(i+1).charAt(j) == '#') {
                    c++;
                }
                if (i < H -1 && j < W -1 && list.get(i+1).charAt(j+1) == '#') {
                    c++;
                }
                System.out.print(c);
            }
            System.out.println("");
        }
    }

    private class MyScanner {
        String[] s;
        int i;
        BufferedReader br;
        String reg = " ";

        MyScanner () {
            s = new String[0];
            i = 0;
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public String next() {
            try {
                if (i < s.length) return s[i++];
                String line = br.readLine();
                while (line.equals("")) {
                    line = br.readLine();
                }
                s = line.split(reg, 0);
                i = 0;
                return s[i++];
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        public int nextInt() {
            try {
                return Integer.parseInt(next());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return -1;
        }

        public double nextDouble() {
            try {
                return Double.parseDouble(next());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return -1;
        }

        public long nextLong()  {
            try {
                return Long.parseLong(next());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return -1;
        }
    }
}


import java.util.Scanner;

public class Main {
    int h, w;
    char[][] cs;
    public static void main(String[] args) {
        Main m = new Main();
        m.read();
        m.solve();
    }

    private void read() {
        Scanner sc = new Scanner(System.in);
        h = sc.nextInt();
        w = sc.nextInt();
        cs = new char[h][];
        sc.nextLine();
        for (int i = 0; i < h; i++) {
            cs[i] = sc.nextLine().toCharArray();
        }
    }

    private void solve() {
        int[][] ans = new int[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                int cnt = 0;
                for (int dx = -1; dx <= 1; dx++) {
                    for (int dy = -1; dy <= 1; dy++) {
                        if (dx == dy && dx == 0)
                            continue;
                        int x = dx + j;
                        int y = dy + i;
                        if (x >= 0 && x < w && y >= 0 && y < h) {
                            if (cs[y][x] == '#')
                                cnt++;
                        }
                    }
                }
                ans[i][j] = cnt;
            }
        }

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (cs[i][j] == '#')
                    System.out.print('#');
                else
                    System.out.printf("%d", ans[i][j]);
            }
            System.out.println();
        }
    }
}


import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		// 整数の入力
		int n = sc.nextInt();
        int ans = 0;
        for (int i = 0; i < n; i++) {
          ans -= sc.nextInt() - sc.nextInt() - 1;
        }

		// 出力
		System.out.println(ans);
	}
}

Score : 200 points

Problem Statement

Joisino is working as a receptionist at a theater.

The theater has 100000 seats, numbered from 1 to 100000.

According to her memo, N groups of audiences have come so far, and the i-th group occupies the consecutive seats from Seat l_i to Seat r_i (inclusive).

How many people are sitting at the theater now?

Constraints

1≤N≤1000

1≤l_i≤r_i≤100000

No seat is occupied by more than one person.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
l_1 r_1
:
l_N r_N

Output

Print the number of people sitting at the theater.

Sample Input 1

1
24 30

Sample Output 1

7

There are 7 people, sitting at Seat 24,25,26,27,28,29 and 30.

Sample Input 2

2
6 8
3 3

Sample Output 2

4


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());

		int totalVisitor = 0;
		for (int i = 0; i < n; i++) {
			int l = sc.nextInt();
			int r = sc.nextInt();
			totalVisitor += r - l + 1;
		}

		System.out.println(totalVisitor);

		sc.close();
	}
}


import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;

public class Main {
	public static void main(String[] args) {
		FastScannerB sc = new FastScannerB(System.in);

		int N = sc.nextInt();
		int total = 0;
		for(int i = 0; i<N; i++){
			int L = sc.nextInt();
			int R = sc.nextInt();
			total += R - L + 1;
		}
		System.out.println(total);
	}
}

class FastScannerB{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public FastScannerB(InputStream stream)
    {
        this.stream = stream;
    }

    public int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars)
        {
            curChar = 0;
            try
            {
                numChars = stream.read(buf);
            } catch (IOException e)
            {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    public int nextInt()
    {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-')
        {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do
        {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public String next()
    {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do
        {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public double nextDouble() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        double res = 0;
        while (!isSpaceChar(c) && c != '.') {
            if (c == 'e' || c == 'E')
                return res * Math.pow(10, nextInt());
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        }
        if (c == '.') {
            c = read();
            double m = 1;
            while (!isSpaceChar(c)) {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                m /= 10;
                res += (c - '0') * m;
                c = read();
            }
        }
        return res * sgn;
    }

    public long nextLong() {
        int c = read();
        while (isSpaceChar(c))
            c = read();
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long res = 0;
        do {
            if (c < '0' || c > '9')
                throw new InputMismatchException();
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public boolean isSpaceChar(int c)
    {
        if (filter != null)
            return filter.isSpaceChar(c);
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public boolean isLineEndChar(int c)
    {
        if (filter != null)
            return filter.isSpaceChar(c);
        return c == '\n' || c == '\r' || c == -1;
    }

    public interface SpaceCharFilter
    {
        public boolean isSpaceChar(int ch);
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] array = sc.nextLine().toCharArray();

        Set<Character> list = new HashSet<Character>();
        for(Character c: array) {
            list.add(c);
        }

        for(char ch = 'a'; ch<='z'; ch++) {
            if(!list.contains(ch)) {
                System.out.println(ch);
                System.exit(0);
            }
        }
        System.out.println("None");
    }
}

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Find the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Constraints

1 \leq |S| \leq 10^5 (|S| is the length of string S.)

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Sample Input 1

atcoderregularcontest

Sample Output 1

b

The string atcoderregularcontest contains a, but does not contain b.

Sample Input 2

abcdefghijklmnopqrstuvwxyz

Sample Output 2

None

This string contains every lowercase English letter.

Sample Input 3

fajsonlslfepbjtsaayxbymeskptcumtwrmkkinjxnnucagfrg

Sample Output 3

d


import java.util.*;
import java.util.TreeMap;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    char[] s = sc.next().toCharArray();
    for (char c = 'a'; c <= 'z'; c++) {
      int flag = 1;
      for (int i = 0; i < s.length; i++) {
        if (s[i] == c) {
          flag = -1;
        }
      }
      if (flag == 1) {
        System.out.println(c);
        return;
      }
    }
    System.out.println("None");
  }
}


import java.util.*;

public class Main {
	public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String s=sc.next();
        String t="None";
        for(int i=97;i<123;i++){
            if(!s.contains(String.valueOf((char)i))){
                t=String.valueOf((char)i);
                break;
            }
        }
        System.out.println(t);
    }
}


import java.util.Scanner;

public class Main {
	public static void main(String[]args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		for(char c = 'a'; c <= 'z'; c++) {
			if(s.indexOf(c) == -1) {
				System.out.println(c);
				return;
			}
		}
		System.out.println("None");
	}

}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        String ans = "";

        char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        StringBuilder elem = new StringBuilder();

        for (int i = 0; i < alphabet.length; i++) {
            if (!s.contains(String.valueOf(alphabet[i]))) {
                elem.append(alphabet[i]);
                break;
            }
        }

        if (elem.length() == 0) {
            ans = "None";
        } else {
            ans = elem.toString();
        }

        System.out.println(ans);
    }
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		sc.close();
		for (int i = 97; i < 123; i++) {
			char c = (char) i;
			for (int j = 0; j < s.length(); j++) {
				char c2 = s.charAt(j);
				if (c == c2) {
					break;
				} else if (j + 1 == s.length() && c != c2) {
					System.out.println(c);
					return;
				}
			}
		}
		System.out.println("None");
	}
}


import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String s = scan.next();
        char[] arr = s.toCharArray();
        int len = s.length();
        if(len == 0){
            System.out.println("a");
            return;
        }
        HashSet<Character> set = new HashSet<>();
        for(int i=0;i<len;i++){
            set.add(arr[i]);
        }
        for(char x='a';x<='z';x++){
            if(!set.contains((Object)(x))){
                System.out.println(x);
                return;
            }
        }
        System.out.println("None");
    }
}


import java.util.*;
import java.lang.Math;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

	int n = sc.nextInt();
    int istack;
    int cnt = 0;
    int cnt2 = 0;
    int cnt3 = 0;
    int ef;
    boolean yon =true;

    for(int i = 0; i < n;i++) {
      istack = sc.nextInt();
      if(istack % 4 == 0) cnt++;
      else if(istack % 2 == 0) cnt2++;
      else cnt3++;
    }
   	cnt3++;

    if(cnt*2+1 >= cnt3) yon = false;

    if(yon) System.out.println("Yes");
    else System.out.println("No");

  }
}

Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


import java.io.*;
import java.math.*;
import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int N = input.nextInt();
		int T = 0;
		int F = 0; //Two and four
		for (int i = 0; i < N; i++) {
			int x = input.nextInt();
			if (x%4==0) F++;
			else if (x%2==0) T++;
		}
		if (T>1) N-=T; //multiples of two can be joined as one contiguous sub-array since 2^2 = 4
		N-=3;
		F--;
		N-=F*2;
		System.out.println(N<=0?"Yes":"No");
	}
}


import java.util.Scanner;

	public class Main {

		public static void main(String[] args) {

			Scanner sc = new Scanner(System.in);

			int n = sc.nextInt();
			int[] a = new int[n];
			int o = 0;
			int f = 0;

			for(int i=0;i<n;i++) {
				a[i] = sc.nextInt();
			}

			for(int i=0;i<n;i++) {
				if(a[i]%2!=0) {
					o++;
				}
				if(a[i]%4==0) {
					f++;
				}
			}

			if(o==0) {
				System.out.println("Yes");
				return;
			}

			if(o==f+1&&o+f==n&&f!=0) {
				System.out.println("Yes");
			}else if(o==f){
				System.out.println("Yes");
			}else {
				System.out.println("No");
			}

		}

	}


import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int four = 0;
		int two = 0;
		int others = 0;
		for (int i = 0; i < n; i++) {
			int a = sc.nextInt();
			if (a % 4 == 0) {
				four++;
			} else if (a % 2 == 0) {
				two++;
			} else {
				others++;
			}
		}
		if ((two >= 2 && four >= others) ||
			(two == 0 && four >= others - 1) ||
			(four >= others + two - 1)) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}
	}
}


import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	    int N = sc.nextInt();

	    long m = 1;

	    int count_4 =0;
	    int count_2=0;
	    int count_odd=0;
	    for (int i = 0; i<N; i++){
	    	m=sc.nextLong();
	    	if (m%4==0){
	    		count_4++;
	    	}
	    	else if (m%2==0){
	    		count_2++;
	    	}
	    	else
	    		count_odd++;
	    }
	    sc.close();

	    if (count_odd==0)
	    	System.out.println("Yes");

	    else if (count_2==0){
	    	if (count_4+1>=count_odd)
	    		System.out.println("Yes");
	    	else
	    		System.out.println("No");
	    }
	    else if (count_2!=0){
	    	if (count_4>=count_odd)
	    		System.out.println("Yes");
	    	else
	    		System.out.println("No");
	    }

	    else
	    	System.out.println("No");
	}

}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
	    int N = sc.nextInt();

	    long m = 1;

	    int count_4 =0;
	    int count_2=0;
	    int count_odd=0;
	    for (int i = 0; i<N; i++){
	    	m=sc.nextLong();
	    	if (m%4==0){
	    		count_4++;
	    	}
	    	else if (m%2==0){
	    		count_2++;
	    	}
	    	else
	    		count_odd++;
	    }
	    //System.out.println(count_odd+ ": "+count_2+": "+count_4);

	    if (count_odd==0)
	    	System.out.println("Yes");

	    else if (count_4==0)
	    	System.out.println("Yes");

	    else if (count_4+1>=count_odd)
	    	System.out.println("Yes");

	    else
	    	System.out.println("No");
	}

}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int N = reader.nextInt();
		String ans = "No";
		int[] all = new int[3];
		int[] arr = new int[N];
		for (int i = 0; i < N; i++) {
			arr[i] = reader.nextInt();
			int tmp = arr[i];
			for (int j = 1; j < 4; j++) {
				if (tmp % (2*j) != 0 || j == 3) {
					all[j-1]++;
					break;
				}
			}
		}
		if (all[2] > (all[0] + all[1] - 1)) {
			ans = "Yes";
		}
		if ((all[2] + all[1]/2) >= (all[0] +  all[1]%2)) {
			ans = "Yes";
		}

		System.out.println(ans);
		reader.close();

	}
}


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

// class Pair implements Comparable<Pair>{
//   long a;
//   int cnt;
//   public Pair(long i, int j){
//     this.a = i;
//     this.cnt = j;
//   }
//   public int compareTo(Pair p){
//     if(this.cnt>=2 && p.cnt >=2 && this.a<p.a) return -1;
//     if(this.cnt<2 && p.cnt>=2) return -1;
//     return 1;
//   }
// }

class Main{
  public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      int cnt4 = 0;
      int cnt2 = 0;
      int odd = 0;
      int N = Integer.parseInt(sc.next());
      for(int i = 0; i<N; i++){
        long a = Long.parseLong(sc.next());
        if(a%2 == 0){
          a/= 2;
          if(a%2 == 0) cnt4++;
          else cnt2++;
        }
        else odd++;
      }
      if(cnt2==0 && odd <= cnt4+1) System.out.println("Yes");
      else if(cnt2>0 && odd<=cnt4) System.out.println("Yes");
      else System.out.println("No");

  }
}


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

/**
 * @author baito
 */

public class Main
{
    static StringBuilder sb = new StringBuilder();
    static FastScanner sc = new FastScanner(System.in);
    static int INF = 12345678;
    static long LINF = 123456789123456789L;
    static long MINF = -123456789123456789L;
    static long MOD = 1000000007;
    static int[] y4 = {0, 1, 0, -1};
    static int[] x4 = {1, 0, -1, 0};
    static int[] y8 = {0, 1, 0, -1, -1, 1, 1, -1};
    static int[] x8 = {1, 0, -1, 0, 1, -1, 1, -1};
    static long[] F;//factorial
    static boolean[] isPrime;
    static int[] primes;
    static char[][] map;

    static int N, K;
    static int[] A;

    public static void main(String[] args)
    {
        N = sc.nextInt();
        A = sc.nextIntArray(N);
        int c4=0,c2 =0;
        for (int i = 0; i < N; i++)
        {
            if(A[i] %4==0)c4++;
            else if (A[i] %2==0)c2++;
        }
        int res = c4 * 3 + c2;
        if(res >= N){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }

    }

    public static long getHashKey(int a, int b)
    {
        return (long) a << 32 | b;
    }

    public static boolean isOutofIndex(int x, int y)
    {
        if (x < 0 || y < 0) return true;
        if (map[0].length <= x || map.length <= y) return true;
        return false;
    }

    public static void setPrimes()
    {
        int n = 100001;
        isPrime = new boolean[n];
        List<Integer> prs = new ArrayList<>();
        Arrays.fill(isPrime, true);
        isPrime[0] = isPrime[1] = false;
        for (int i = 2; i * i <= n; i++)
        {
            if (!isPrime[i]) continue;
            prs.add(i);
            for (int j = i * 2; j < n; j += i)
            {
                isPrime[j] = false;
            }
        }
        primes = new int[prs.size()];
        for (int i = 0; i < prs.size(); i++)
            primes[i] = prs.get(i);
    }

    public static void revSort(int[] a)
    {
        Arrays.sort(a);
        reverse(a);
    }

    public static void revSort(long[] a)
    {
        Arrays.sort(a);
        reverse(a);
    }

    public static int[][] copy(int[][] ar)
    {
        int[][] nr = new int[ar.length][ar[0].length];
        for (int i = 0; i < ar.length; i++)
            for (int j = 0; j < ar[0].length; j++)
                nr[i][j] = ar[i][j];
        return nr;
    }

    /**
     *
指定した値以上の先頭のインデクスを返す

     *
配列要素が０のときは、０が返る。

     *
     * @returnint ： 探索した値以上で、先頭になるインデクス
     */
    public static int lowerBound(final int[] arr, final int value)
    {
        int low = 0;
        int high = arr.length;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >>> 1) + low;    //(low + high) / 2 (オーバーフロー対策)
            if (arr[mid] < value)
            {
                low = mid + 1;
            }
            else
            {
                high = mid;
            }
        }
        return low;
    }

    /**
     *
指定した値より大きい先頭のインデクスを返す

     *
配列要素が０のときは、０が返る。

     *
     * @returnint ： 探索した値より上で、先頭になるインデクス
     */
    public static int upperBound(final int[] arr, final int value)
    {
        int low = 0;
        int high = arr.length;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >>> 1) + low;    //(low + high) / 2 (オーバーフロー対策)
            if (arr[mid] <= value)
            {
                low = mid + 1;
            }
            else
            {
                high = mid;
            }
        }
        return low;
    }

    //次の順列に書き換える、最大値ならfalseを返す
    public static boolean nextPermutation(int A[])
    {
        int len = A.length;
        int pos = len - 2;
        for (; pos >= 0; pos--)
        {
            if (A[pos]  1)
        {
            int mid = (ok + ng) / 2;
            if (A[mid] > A[pos]) ok = mid;
            else ng = mid;

        }

        swap(A, pos, ok);
        reverse(A, pos + 1, len - 1);

        return true;
    }

    //次の順列に書き換える、最小値ならfalseを返す
    public static boolean prevPermutation(int A[])
    {
        int len = A.length;
        int pos = len - 2;
        for (; pos >= 0; pos--)
        {
            if (A[pos] > A[pos + 1]) break;
        }
        if (pos == -1) return false;

        //posより小さい最大の数を二分探索
        int ok = pos + 1;
        int ng = len;
        while (Math.abs(ng - ok) > 1)
        {
            int mid = (ok + ng) / 2;
            if (A[mid] < A[pos]) ok = mid;
            else ng = mid;

        }

        swap(A, pos, ok);
        reverse(A, pos + 1, len - 1);

        return true;
    }

    //↓nCrをmod計算するために必要。　***factorial(N)を呼ぶ必要がある***
    static long ncr(int n, int r)
    {
        if (n < r) return 0;
        else if (r == 0) return 1;

        factorial(n);
        return F[n] / (F[n - r] * F[r]);
    }

    static long ncr2(int a, int b)
    {
        if (b == 0) return 1;
        else if (a < b) return 0;
        long res = 1;
        for (int i = 0; i < b; i++)
        {
            res *= a - i;
            res /= i + 1;
        }
        return res;
    }

    static long ncrdp(int n, int r)
    {
        if (n < r) return 0;
        long[][] dp = new long[n + 1][r + 1];
        for (int ni = 0; ni < n + 1; ni++)
        {
            dp[ni][0] = dp[ni][ni] = 1;
            for (int ri = 1; ri < ni; ri++)
            {
                dp[ni][ri] = dp[ni - 1][ri - 1] + dp[ni - 1][ri];
            }
        }
        return dp[n][r];
    }

    static long modNcr(int n, int r)
    {
        if (n < r) return 0;
        long result = F[n];
        result = result * modInv(F[n - r]) % MOD;
        result = result * modInv(F[r]) % MOD;
        return result;
    }

    public static long modSum(long... lar)
    {
        long res = 0;
        for (long l : lar)
            res = (res + l % MOD) % MOD;
        return res;
    }

    public static long modDiff(long a, long b)
    {
        long res = a - b;
        if (res < 0) res += MOD;
        res %= MOD;
        return res;
    }

    public static long modMul(long... lar)
    {
        long res = 1;
        for (long l : lar)
            res = (res * l) % MOD;
        if (res < 0) res += MOD;
        res %= MOD;
        return res;
    }

    public static long modDiv(long a, long b)
    {
        long x = a % MOD;
        long y = b % MOD;
        long res = (x * modInv(y)) % MOD;
        return res;
    }

    static long modInv(long n)
    {
        return modPow(n, MOD - 2);
    }

    static void factorial(int n)
    {
        F = new long[n + 1];
        F[0] = F[1] = 1;
//        for (int i = 2; i <= n; i++)
//        {
//            F[i] = (F[i - 1] * i) % MOD;
//        }
        //
        for (int i = 2; i <= 100000; i++)
        {
            F[i] = (F[i - 1] * i) % MOD;
        }
        for (int i = 100001; i <= n; i++)
        {
            F[i] = (F[i - 1] * i) % MOD;
        }
    }

    static long modPow(long x, long n)
    {
        long res = 1L;
        while (n > 0)
        {
            if ((n & 1) == 1)
            {
                res = res * x % MOD;
            }
            x = x * x % MOD;
            n >>= 1;
        }
        return res;
    }

    //↑nCrをmod計算するために必要

    static int gcd(int n, int r)
    {
        return r == 0 ? n : gcd(r, n % r);
    }

    static long gcd(long n, long r)
    {
        return r == 0 ? n : gcd(r, n % r);
    }

    static <T> void swap(T[] x, int i, int j)
    {
        T t = x[i];
        x[i] = x[j];
        x[j] = t;
    }

    static void swap(int[] x, int i, int j)
    {
        int t = x[i];
        x[i] = x[j];
        x[j] = t;
    }

    public static void reverse(int[] x)
    {
        int l = 0;
        int r = x.length - 1;
        while (l < r)
        {
            int temp = x[l];
            x[l] = x[r];
            x[r] = temp;
            l++;
            r--;
        }
    }

    public static void reverse(long[] x)
    {
        int l = 0;
        int r = x.length - 1;
        while (l < r)
        {
            long temp = x[l];
            x[l] = x[r];
            x[r] = temp;
            l++;
            r--;
        }
    }

    public static void reverse(int[] x, int s, int e)
    {
        int l = s;
        int r = e;
        while (l < r)
        {
            int temp = x[l];
            x[l] = x[r];
            x[r] = temp;
            l++;
            r--;
        }
    }

    static int length(int a)
    {
        int cou = 0;
        while (a != 0)
        {
            a /= 10;
            cou++;
        }
        return cou;
    }

    static int length(long a)
    {
        int cou = 0;
        while (a != 0)
        {
            a /= 10;
            cou++;
        }
        return cou;
    }

    static int countC2(char[][] a, char c)
    {
        int co = 0;
        for (int i = 0; i < a.length; i++)
            for (int j = 0; j < a[0].length; j++)
                if (a[i][j] == c) co++;
        return co;
    }

    static int countI(int[] a, int key)
    {
        int co = 0;
        for (int i = 0; i < a.length; i++)
            if (a[i] == key) co++;
        return co;
    }

    static int countI(int[][] a, int key)
    {
        int co = 0;
        for (int i = 0; i < a.length; i++)
            for (int j = 0; j < a[0].length; j++)
                if (a[i][j] == key) co++;
        return co;
    }

    static void fill(int[][] a, int v)
    {
        for (int i = 0; i < a.length; i++)
            for (int j = 0; j < a[0].length; j++)
                a[i][j] = v;
    }

    static void fill(long[][] a, long v)
    {
        for (int i = 0; i < a.length; i++)
            for (int j = 0; j < a[0].length; j++)
                a[i][j] = v;
    }

    static void fill(int[][][] a, int v)
    {
        for (int i = 0; i < a.length; i++)
            for (int j = 0; j < a[0].length; j++)
                for (int k = 0; k < a[0][0].length; k++)
                    a[i][j][k] = v;
    }

    static int max(int... a)
    {
        int res = Integer.MIN_VALUE;
        for (int i : a)
        {
            res = Math.max(res, i);
        }
        return res;
    }

    static int max(int[][] ar)
    {
        int res = Integer.MIN_VALUE;
        for (int i[] : ar)
            res = Math.max(res, max(i));
        return res;
    }

    static int min(int... a)
    {
        int res = Integer.MAX_VALUE;
        for (int i : a)
        {
            res = Math.min(res, i);
        }
        return res;
    }

    static int min(int[][] ar)
    {
        int res = Integer.MAX_VALUE;
        for (int i[] : ar)
            res = Math.min(res, min(i));
        return res;
    }

    static int sum(int[] a)
    {
        int cou = 0;
        for (int i : a)
            cou += i;
        return cou;
    }

    static int abs(int a)
    {
        return Math.abs(a);
    }

    static class FastScanner
    {

        private BufferedReader reader = null;
        private StringTokenizer tokenizer = null;

        public FastScanner(InputStream in)
        {
            reader = new BufferedReader(new InputStreamReader(in));
            tokenizer = null;
        }

        public String next()
        {
            if (tokenizer == null || !tokenizer.hasMoreTokens())
            {
                try
                {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        /*public String nextChar(){
            return (char)next()[0];
        }*/
        public String nextLine()
        {
            if (tokenizer == null || !tokenizer.hasMoreTokens())
            {
                try
                {
                    return reader.readLine();
                } catch (IOException e)
                {
                    throw new RuntimeException(e);
                }
            }

            return tokenizer.nextToken("\n");
        }

        public long nextLong()
        {
            return Long.parseLong(next());
        }

        public int nextInt()
        {
            return Integer.parseInt(next());
        }

        public double nextDouble()
        {
            return Double.parseDouble(next());
        }

        public int[] nextIntArray(int n)
        {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = nextInt();
            }
            return a;
        }

        public int[] nextIntArrayDec(int n)
        {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = nextInt() - 1;
            }
            return a;
        }

        public int[][] nextIntArray2(int h, int w)
        {
            int[][] a = new int[h][w];
            for (int hi = 0; hi < h; hi++)
            {
                for (int wi = 0; wi < w; wi++)
                {
                    a[hi][wi] = nextInt();
                }
            }
            return a;
        }

        public int[][] nextIntArray2Dec(int h, int w)
        {
            int[][] a = new int[h][w];
            for (int hi = 0; hi < h; hi++)
            {
                for (int wi = 0; wi < w; wi++)
                {
                    a[hi][wi] = nextInt() - 1;
                }
            }
            return a;
        }

        //複数の配列を受け取る
        public void nextIntArrays2ar(int[] a, int[] b)
        {
            for (int i = 0; i < a.length; i++)
            {
                a[i] = sc.nextInt();
                b[i] = sc.nextInt();
            }
        }

        public void nextIntArrays2arDec(int[] a, int[] b)
        {
            for (int i = 0; i < a.length; i++)
            {
                a[i] = sc.nextInt() - 1;
                b[i] = sc.nextInt() - 1;
            }
        }

        //複数の配列を受け取る
        public void nextIntArrays3ar(int[] a, int[] b, int[] c)
        {
            for (int i = 0; i < a.length; i++)
            {
                a[i] = sc.nextInt();
                b[i] = sc.nextInt();
                c[i] = sc.nextInt();
            }
        }

        //複数の配列を受け取る
        public void nextIntArrays3arDecLeft2(int[] a, int[] b, int[] c)
        {
            for (int i = 0; i < a.length; i++)
            {
                a[i] = sc.nextInt() - 1;
                b[i] = sc.nextInt() - 1;
                c[i] = sc.nextInt();
            }
        }

        public Integer[] nextIntegerArray(int n)
        {
            Integer[] a = new Integer[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = nextInt();
            }
            return a;
        }

        public char[] nextCharArray(int n)
        {
            char[] a = next().toCharArray();

            return a;
        }

        public char[][] nextCharArray2(int h, int w)
        {
            char[][] a = new char[h][w];
            for (int i = 0; i < h; i++)
            {
                a[i] = next().toCharArray();
            }
            return a;
        }

        //スペースが入っている場合
        public char[][] nextCharArray2s(int h, int w)
        {
            char[][] a = new char[h][w];
            for (int i = 0; i < h; i++)
            {
                a[i] = nextLine().replace(" ", "").toCharArray();
            }
            return a;
        }

        public char[][] nextWrapCharArray2(int h, int w, char c)
        {
            char[][] a = new char[h + 2][w + 2];
            //char c = '*';
            int i;
            for (i = 0; i < w + 2; i++)
                a[0][i] = c;
            for (i = 1; i < h + 1; i++)
            {
                a[i] = (c + next() + c).toCharArray();
            }
            for (i = 0; i < w + 2; i++)
                a[h + 1][i] = c;
            return a;
        }

        //スペースが入ってる時用
        public char[][] nextWrapCharArray2s(int h, int w, char c)
        {
            char[][] a = new char[h + 2][w + 2];
            //char c = '*';
            int i;
            for (i = 0; i < w + 2; i++)
                a[0][i] = c;
            for (i = 1; i < h + 1; i++)
            {
                a[i] = (c + nextLine().replace(" ", "") + c).toCharArray();
            }
            for (i = 0; i < w + 2; i++)
                a[h + 1][i] = c;
            return a;
        }

        public long[] nextLongArray(int n)
        {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
            {
                a[i] = nextLong();
            }
            return a;
        }

        public long[][] nextLongArray2(int h, int w)
        {
            long[][] a = new long[h][w];
            for (int hi = 0; hi < h; hi++)
            {
                for (int wi = 0; wi < h; wi++)
                {
                    a[h][w] = nextLong();
                }
            }
            return a;
        }
    }
}


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

class Main{
	public static void main(String[] args){
		solve();
	}
	public static void solve(){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i=0;i<n;i++){
			a[i] = sc.nextInt();
		}
		int[] num = new int[3];
		for(int i=0;i<n;i++){
			if(a[i]%4==0){
				num[0]++;
			}
			else if(a[i]%2==0){
				num[1]++;
			}
			else{
				num[2]++;
			}
		}
		if(num[2]>num[0] && num[1]!=0){
			System.out.println("No");
		}
		else if(num[2]!=0 && num[0]==0){
			System.out.println("No");
		}
		else{
			System.out.println("Yes");
		}
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = new int[3];
            for (int i = 0; i < n; ++i) {
                int x = in.nextInt();
                if (x % 4 == 0) {
                    ++a[2];
                } else if (x % 2 == 0) {
                    ++a[1];
                } else {
                    ++a[0];
                }
            }
            if (a[1] > 0) {
                out.println(a[2] >= a[0] ? "Yes" : "No");
            } else {
                out.println(Math.abs(a[2] - a[0]) > 1 ? "No" : "Yes");
            }
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.util.Scanner;

public class Main
{

	public static void main(String[] args)
	{
		// TODO Auto-ge
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			long x=sc.nextLong(),y=sc.nextLong(),z=sc.nextLong();
			if(y>z||x==1)System.out.println(0);
			else if(y==z)System.out.println(1);
			else System.out.println((x-2)*(z-y)+1);
		}
	}

}

Score : 200 points

Problem Statement

Snuke has N integers. Among them, the smallest is A, and the largest is B.
We are interested in the sum of those N integers. How many different possible sums there are?

Constraints

1 ≤ N,A,B ≤ 10^9

A and B are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of the different possible sums.

Sample Input 1

4 4 6

Sample Output 1

5

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.

Sample Input 2

5 4 3

Sample Output 2

0

Sample Input 3

1 7 10

Sample Output 3

0

Sample Input 4

1 3 3

Sample Output 4

1


import java.util.*;
public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      long n = sc.nextLong();
      long a = sc.nextLong();
      long b = sc.nextLong();
      if(a>=b)System.out.println(0);
      else System.out.println(b*(n-2)-a*(n-2)+1);
    }
}


import java.math.BigInteger;
import java.util.*;
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        long N = sc.nextLong();
        long A = sc.nextLong();
        long B = sc.nextLong();

        if (A > B || N < (A-B+1)) {
        	System.out.println(0);
        	return;
        }

        //処理
        N -= 2;

        String ans = BigInteger.valueOf(B).multiply(BigInteger.valueOf(N)).subtract(BigInteger.valueOf(A).multiply(BigInteger.valueOf(N))).add(BigInteger.ONE).toString(10);

        System.out.println(ans);
    }
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

    	Main main=new Main();

    	main.run();

    }

    void run() {
    	Scanner sc=new Scanner(System.in);

    	int N=sc.nextInt();
    	int A=sc.nextInt();
    	int B=sc.nextInt();

    	if(A>B || (N==1 && A!=B)) {
    		System.out.println("0");
    		return;
    	}

    	int lowbound=(N-1)*A+B;
    	int upeerbound=A+(N-1)*B;

    	int result=upeerbound-lowbound+1;

    	System.out.println(result);
    }

}


import java.util.Scanner;

public class Main {

	static Scanner sc = new Scanner(System.in);
    public void run() throws Exception {
    	//StringBuilder sb = new StringBuilder();

    	int n1 = sc.nextInt();
    	int n2 = sc.nextInt();
    	int n3 = sc.nextInt();
    	long anw = 0;
    	if(n1 == 1){
    		if(n2 > n3){
    			anw = 0;
    		}else if (n2 == n3){
    			long min = n2*(n1-1) + n3;
            	long max = n3*(n1-1) + n2;
            	anw = max - min + 1;
    		}
    	}else {
        	long min = n2*(n1-1) + n3;
        	long max = n3*(n1-1) + n2;
        	anw = max - min + 1;
    	}
    	System.out.println(anw);
    }
    public static void main(String[] args) throws Exception {
        new Main().run();
    }
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a = sc.nextInt();
		int b = sc.nextInt();
		int min = 0;
		int max = 0;
		int cnt = 0;

		if(n == 1) {
			if(a == b) {
				cnt++;
			}else if(a != b) {
				//なにもしない
			}
		}else if(n > 1) {
			if(a > b) {
				//なにもしない
			}else if(a < b){
				for(int i = 0 ; i < n ; i++) {
					min += a;
					max += b;
				}
				cnt = (max - b + a) - (min - a + b) + 1;
			}else if(a == b){
				cnt++;
			}
		}
		System.out.println(cnt);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskX solver = new TaskX();
		solver.solve(1, in, out);
		out.close();
	}

	static int INF = 1 << 30;
	static long LINF = 1L << 55;
	static int MOD = 1000000007;
	static int[] mh4 = { 0, -1, 1, 0 };
	static int[] mw4 = { -1, 0, 0, 1 };
	static int[] mh8 = { -1, -1, -1, 0, 0, 1, 1, 1 };
	static int[] mw8 = { -1, 0, 1, -1, 1, -1, 0, 1 };

	static class TaskX {

		public void solve(int testNumber, InputReader in, PrintWriter out) {

			int n = in.nextInt();
			long[] a = in.nextLongArray(3*n);
			PriorityQueue<Long> b = new PriorityQueue<Long>();
			PriorityQueue<Long> r = new PriorityQueue<Long>(Comparator.reverseOrder());
			long b_sum = 0, r_sum = 0;
			for (int i = 0; i < n; i++) {
				b_sum += a[i];
				b.add(a[i]);

				r_sum += a[3*n-i-1];
				r.add(a[3*n-i-1]);
			}

			long[] b_max = new long[n+1];
			long[] r_max = new long[n+1];
			b_max[0] = b_sum;
			r_max[0] = r_sum;

			for (int i = 1; i <= n; i++) {
				b.add(a[n+i-1]);
				b_max[i] += b_max[i-1] + a[n+i-1] - b.remove();
			}

			for (int i = 1; i <= n; i++) {
				r.add(a[2*n-i]);
				r_max[i] += r_max[i-1] + a[2*n-i] - r.remove();
			}

			long ans = -LINF;
			for (int i = 0; i <= n; i++) {
				ans = Math.max(ans, b_max[i] - r_max[n-i]);
			}

			out.println(ans);
		}
	}

	static class InputReader {
		BufferedReader in;
		StringTokenizer tok;

		public String nextString() {
			while (!tok.hasMoreTokens()) {
				try {
					tok = new StringTokenizer(in.readLine(), " ");
				} catch (IOException e) {
					throw new InputMismatchException();
				}
			}
			return tok.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(nextString());
		}

		public long nextLong() {
			return Long.parseLong(nextString());
		}

		public double nextDouble() {
			return Double.parseDouble(nextString());
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt();
			}
			return res;
		}

		public int[] nextIntArrayDec(int n) {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt() - 1;
			}
			return res;
		}

		public int[] nextIntArray1Index(int n) {
			int[] res = new int[n + 1];
			for (int i = 0; i < n; i++) {
				res[i + 1] = nextInt();
			}
			return res;
		}

		public long[] nextLongArray(int n) {
			long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong();
			}
			return res;
		}

		public long[] nextLongArrayDec(int n) {
			long[] res = new long[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextLong() - 1;
			}
			return res;
		}

		public long[] nextLongArray1Index(int n) {
			long[] res = new long[n + 1];
			for (int i = 0; i < n; i++) {
				res[i + 1] = nextLong();
			}
			return res;
		}

		public double[] nextDoubleArray(int n) {
			double[] res = new double[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextDouble();
			}
			return res;
		}

		public InputReader(InputStream inputStream) {
			in = new BufferedReader(new InputStreamReader(inputStream));
			tok = new StringTokenizer("");
		}
	}

}

Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

In the test set worth 300 points, N ≤ 1000.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a_1 are removed, a' will be (2, 3), which has a score of 2 - 3 = -1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a_2, a_3 and a_9 are removed, a' will be (8, 7, 4, 6, 5, 3), which has a score of (8 + 7 + 4) - (6 + 5 + 3) = 5.


import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();

        scanner.close();

        Pattern p = Pattern.compile("[A][A-Z]*[Z]");
        Matcher matcher = p.matcher(s);
        int max = 0;
        while (matcher.find()) {
            int length = matcher.group().length();
            if (max < length) {
                max = length;
            }
        }

        System.out.println(max);
    }
}

Score : 200 points

Problem Statement

Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).

Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.

Constraints

1 ≦ |s| ≦ 200{,}000

s consists of uppercase English letters.

There exists a substring of s that starts with A and ends with Z.

Input

The input is given from Standard Input in the following format:

s

Output

Print the answer.

Sample Input 1

QWERTYASDFZXCV

Sample Output 1

5

By taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z.

Sample Input 2

ZABCZ

Sample Output 2

4

Sample Input 3

HASFJGHOGAKZZFEGA

Sample Output 3

12


import java.util.Scanner;

public class Main{
  public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    String s=sc.next();
    int index1=s.indexOf("A");
    int index2=s.indexOf("Z");
    System.out.println(s.substring(index1,index2+1).length());
  }
}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();

        int positionA = Integer.MAX_VALUE;// Aは出来るだけ文字列の先頭で見つけたい
        int positionZ = 0;// Zは出来るだけ文字の後ろで見つけたい
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'A') {
                if (positionA > i) positionA = i;
            } else if (s.charAt(i) == 'Z') {
                if (positionZ < i) positionZ = i;
            }
        }

        System.out.println(positionZ-positionA+1);
    }
}


import java.util.Scanner;

public class Main {
	public static void main(String[]args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();

		int min = s.length();
		int max = 0;

		for(int i = 0; i < s.length(); i++) {
			if('A'==s.charAt(i)) {
				min = Math.min(min, i);
			}
			if('Z'==s.charAt(i)) {
				max = Math.max(max, i);
			}
		}
		System.out.println((max - min) + 1);

	}

}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		String s = sc.next();

		System.out.println(s.lastIndexOf('Z') - s.indexOf('A') + 1);

      	sc.close();
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String S = sc.next();
		sc.close();
		int l = 0;
		for(int i = 0; i < S.length(); i++) {
			if(S.charAt(i) == 'A') {
				l = i;
				break;
			}
		}
		int r = 0;
		for(int i = S.length() - 1; i >= 0; i--) {
			if(S.charAt(i) == 'Z') {
				r = i;
				break;
			}
		}
		System.out.println(r- l + 1);
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String s = scan.next();
		int a = s.indexOf("A");
		int z = s.lastIndexOf("Z");
		String r = s.substring(a, z+1);
		int ans = r.length();
		System.out.println(ans);
	}
}


import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    int firstA = line.indexOf("A");
    int lastZ = line.lastIndexOf("Z");
    line = line.substring(firstA, lastZ + 1);
    System.out.println(line.length());

  }
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();

		int firstIndex = s.indexOf('A');
		int lastIndex = s.lastIndexOf('Z');

		int result = lastIndex - firstIndex +1;
		System.out.println(result);

	}

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String input = in.nextLine();
        int size = input.codePointCount(0, input.length());

        boolean started = false;
        int count = 0;
        int result = 0;
        for(int i = 0; i < size; i++) {
            int c = input.codePointAt(i);

            count += 1;
            switch (c) {
                case 'A':
                    if (!started) {
                        count = 1;
                    }
                    started = true;
                    break;
                case 'Z':
                    result = count;
                    break;
            }
        }
        out.print(result);
    }
    public static final Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
    public static final PrintStream out = System.out;
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int s = sc.nextInt();

		int cnt = 0;
		for (int x = 0; x <= k && x <= s; x++) {
			for (int y = 0; y <= k && x + y <= s; y++) {
				if (s - x - y <= k) cnt++;
			}
		}

		System.out.println(cnt);

		sc.close();
	}

}

Score : 200 points

Problem Statement

You are given two integers K and S.

Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.

How many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

Constraints

2≤K≤2500

0≤S≤3K

K and S are integers.

Input

The input is given from Standard Input in the following format:

K S

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

Sample Input 1

2 2

Sample Output 1

6

There are six triples of X, Y and Z that satisfy the condition:

X = 0, Y = 0, Z = 2

X = 0, Y = 2, Z = 0

X = 2, Y = 0, Z = 0

X = 0, Y = 1, Z = 1

X = 1, Y = 0, Z = 1

X = 1, Y = 1, Z = 0

Sample Input 2

5 15

Sample Output 2

1

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.


import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;

public class Main {
	private static Scanner sc = new Scanner(System.in);
	//result = Math.pow(num1, num3)
	//StringBuffer str = new StringBuffer(hoge1);
	//String hoge2 = str.reverse().toString();
	//map.containsKey(A)

	//Map<String, Integer> map = new HashMap<String, Integer>(n);
	/*for ( キーのデータ型 key : マップの名前.keySet() ) {
		データのデータ型 data = マップの名前.get( key );

		// keyやdataを使った処理;
	}*/
	//int i = Integer.parseInt(s);
	//Queue<String> qq=new ArrayDeque<>(); //add,poll,peek
	//Deque<String> qq=new ArrayDeque<>();//push,pop,peek
	//ArrayList<Integer> cc = new ArrayList<>(N);
	//Collections.sort(list);
	//Array.sort(list);   cc.contains(tmp)
	//Arrays.asList(c).contains("a")
	//list.set(1,"walk");//1 1 2 3 5
	static long mod =1000000007;
	//static 	ArrayList<Integer> cc = new ArrayList<>(100);
	static int INF=1000000007;

	public static void main(String[] args) {
		//int N=sc.nextInt();for(int i=0;i<N;i++) {}
		//int a[]=new int[N];for(int i=0;i<N;i++) {}
		//Ae2+Be2=Ce2
		//StringBuffer str = new StringBuffer(hoge1);
		//for(int i=0;i<N;i++) {}
		int K=sc.nextInt();
		int S=sc.nextInt();

		int ans=0;

		for(int a=0;a<=K;a++) {
			for(int b=0;b<=K;b++) {
				if(a+b+K>=S&&a+b<=S)ans++;

			}
		}

		p(ans);
	}

	public static long modPow(long g,long N) {
		long T=1;
		for(int i=0;i<N;i++) {
			T*=g;
			T%=mod;
		}
		return T;
	}

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

	public static long gcd(long a, long b) {
        return b == 0 ? a: gcd(b, a % b);
	}
	static String nextPermutation(String s) {
		ArrayList<Character> list=new ArrayList<>();
		for(int i=0;i<s.length();i++) {
			list.add(s.charAt(i));
		}
		int pivotPos=-1;
		char pivot=0;
		for(int i=list.size()-2;i>=0;i--) {
			if(list.get(i)<list.get(i+1)) {
				pivotPos=i;
				pivot=list.get(i);
				break;
			}
		}
		if(pivotPos==-1&&pivot==0) {
			return "Final";
		}
		int L=pivotPos+1,R=list.size()-1;

		int minPos=-1;
		char min =Character.MAX_VALUE;

		for(int i=R;i>=L;i--) {
			if(pivot<list.get(i)) {
				if(list.get(i)<min) {
					min=list.get(i);
					minPos=i;
				}
			}
		}

		Collections.swap(list, pivotPos, minPos);
		Collections.sort(list.subList(L, R+1));

		StringBuilder sb=new StringBuilder();
		for(int i=0;i<list.size();i++) {
			sb.append(list.get(i));
		}
		return sb.toString();
	}

	static void p(String ans) {System.out.println(ans);};
	static void p(int ans) {System.out.println(ans);};
	static void p() {System.out.println();};
	static void p(long ans) {System.out.println(ans);};
	static void p(double ans) {System.out.println(ans);};

}


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

public class Main {
    public static void main(String args[]) throws IOException {
        Scan sc = new Scan();
        int k = sc.scanInt();
        int s = sc.scanInt();
        int res = 0;
        for(int i = 0; i <= k; ++i){
            if(i > s)
                break;
            for(int j = 0; j <= k; ++j){
                int s_ = s-(i+j);
                if(s_ < 0)
                    break;
                if(s_>=0 && s_<= k)
                    ++res;
            }
        }
        System.out.println(res);
    }
}

class Scan
{
    private byte[] buf=new byte[1024];
    private int index;
    private InputStream in;
    private int total;
    public Scan()
    {
        in=System.in;
    }
    public int scan()throws IOException
    {
        if(total<0)
        throw new InputMismatchException();
        if(index>=total)
        {
            index=0;
            total=in.read(buf);
            if(total<=0)
            return -1;
        }
        return buf[index++];
    }
    public int scanInt()throws IOException
    {
        int integer=0;
        int n=scan();
        while(isWhiteSpace(n))
        n=scan();
        int neg=1;
        if(n=='-')
        {
            neg=-1;
            n=scan();
        }
        while(!isWhiteSpace(n))
        {
            if(n>='0'&&n<='9')
            {
                integer*=10;
                integer+=n-'0';
                n=scan();
            }
            else throw new InputMismatchException();
        }
        return neg*integer;
    }
    public long scanLong() throws IOException {
        long integer=0;
        int n=scan();
        while(isWhiteSpace(n))
        n=scan();
        int neg=1;
        if(n=='-')
        {
            neg=-1;
            n=scan();
        }
        while(!isWhiteSpace(n))
        {
            if(n>='0'&&n<='9')
            {
                integer*=10;
                integer+=n-'0';
                n=scan();
            }
            else throw new InputMismatchException();
        }
        return neg*integer;
    }
    public double scanDouble()throws IOException
    {
        double doub=0;
        int n=scan();
        while(isWhiteSpace(n))
        n=scan();
        int neg=1;
        if(n=='-')
        {
            neg=-1;
            n=scan();
        }
        while(!isWhiteSpace(n)&&n!='.')
        {
            if(n>='0'&&n<='9')
            {
                doub*=10;
                doub+=n-'0';
                n=scan();
            }
            else throw new InputMismatchException();
        }
        if(n=='.')
        {
            n=scan();
            double temp=1;
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    temp/=10;
                    doub+=(n-'0')*temp;
                    n=scan();
                }
                else throw new InputMismatchException();
            }
        }
        return doub*neg;
    }
    public String scanString()throws IOException
    {
        StringBuilder sb=new StringBuilder();
        int n=scan();
        while(isWhiteSpace(n))
        n=scan();
        while(!isWhiteSpace(n))
        {
            sb.append((char)n);
            n=scan();
        }
        return sb.toString();
    }
    private boolean isWhiteSpace(int n)
    {
        if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
        return true;
        return false;
    }
}


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

public class Main {

	final static long MOD = 1000000007L;
	public static String[] Ws = null;
	public static int wsIndx = 0;
	public static BufferedReader in = null;
	public static ArrayList<int[]> list = new ArrayList<>();
	public static void main(String[] args) throws Exception {

		int K=nextInt();
		int S=nextInt();

		long ans = 0;
		for(int x=0;x<=K;x++){
			for(int y=0;y<=K;y++){
				int z=S-x-y;
				if(0<=z&&z<=K){
					ans++;
				}
			}
		}
		System.out.println(ans);

	}

	public static long[] disassemblyAlph(String s) {
		long[] alph = new long['z' - 'a' + 1];
		Arrays.fill(alph, 0);
		for (int i = 0; i < s.length(); i++) {
			alph[s.charAt(i) - 'a']++;
		}
		return alph;
	}

	public static String[] toStringArray(String s) {
		String[] tmp = new String[s.length()];
		char[] c = s.toCharArray();
		for (int i = 0; i < tmp.length; i++) {
			tmp[i] = String.valueOf(c[i]);
		}
		return tmp;
	}

	public static String concatStringArray(String[] s){
		StringBuffer sb = new StringBuffer();
		for(String x:s){
			sb.append(x);
		}
		return sb.toString();
	}

	public static void check() throws Exception{
		if(in == null){
			in = new BufferedReader(new InputStreamReader(System.in));
		}
		if(Ws==null || Ws.length<=wsIndx){
			Ws = in.readLine().split(" ");
			wsIndx=0;
		}
	}
	public static int nextInt()throws Exception{
		check();
		return Integer.parseInt(Ws[wsIndx++]);
	}

	public static long nextLong()throws Exception{
		check();
		return Long.parseLong(Ws[wsIndx++]);
	}

	public static String nextString()throws Exception{
		check();
		return Ws[wsIndx++];
	}

	public static int[] nextInts()throws Exception{
		check();
		int[] tmp = new int[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Integer.parseInt(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static long[] nextLongs()throws Exception{
		check();
		long[] tmp = new long[Ws.length];
		for(int i=0;i<tmp.length;i++){
			tmp[i]=Long.parseLong(Ws[i]);
		}
		wsIndx=Ws.length;
		return tmp;
	}

	public static String[] nextStrings()throws Exception{
		check();
		wsIndx=Ws.length;
		return Ws;
	}

}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int s = scanner.nextInt();
        int ans = 0;
        for (int x = 0; x <= k; x++) {
            for (int y = 0; y <= x; y++) {
                for (int z = 0; z <= y; z++) {
                    if (x + y + z == s) {
                        if (x == y && y == z) ans++;
                        else if (x == y || y == z || x == z) ans += 3;
                        else ans += 6;
                    }
                }
            }
        }
        System.out.println(ans);
    }

}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		SumOfThreeIntegers solver = new SumOfThreeIntegers();
		solver.readInput();
		solver.solve();
		solver.writeOutput();
	}

	static class SumOfThreeIntegers {
		private int k;
		private int s;
		private Scanner scanner;
		private int output;

		public SumOfThreeIntegers() {
			scanner = new Scanner(System.in);
			output = 0;
		}

		public void readInput() {
			k = scanner.nextInt();
			s = scanner.nextInt();
		}

		public void solve() {

			for(int x=0; x<=k;x++)
				for(int y=0; y<=k; y++)
					for(int z=0; z<=k; z++)
						if(x+y+z == s)
							output++;
		}

		public void writeOutput() {
			System.out.print(output);
		}

	}

}


import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		int k,s,x,y,z,n;
		n = 0;
		Scanner sc = new Scanner(System.in);
		k = sc.nextInt();
		s = sc.nextInt();
		for(x = 0; x <= k; x++) {
			for(y = 0; y <= k; y++) {
				z = s - x - y;
				if(0 <= z && z <= k) {
					n++;
				}
			}
		}
		System.out.println(n);
		sc.close();
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			int maxValue = scanner.nextInt();
			int S = scanner.nextInt();

			int cnt = 0;
			for (int x = 0; x <= maxValue; x++) {
				for (int y = 0; y <= maxValue; y++) {
					for (int z = 0; z <= maxValue; z++) {
						if (x + y + z == S)
							cnt++;
					}
				}
			}
			System.out.println(cnt);

		}
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int K = sc.nextInt();
		int S = sc.nextInt();

		int count = 0;
		for(int x=0; x<=K && x<=S; x++) {
			for(int y=0; y<=K && x+y<=S; y++) {
				for(int z=0; z<=K && x+y+z<=S; z++) {
					if(x+y+z == S) count++;
				}
			}
		}

		System.out.println(count);
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int K = sc.nextInt();
		int S = sc.nextInt();

		int count = 0;
		for(int x=0; x<=K; x++) {
			for(int y=0; y<=K; y++) {
				for(int z=0; z<=K; z++) {
					if(x+y+z == S) count++;
					if(x+y+z > S) break;
				}
				if(x+y > S) break;
			}
			if(x > S) break;
		}

		System.out.println(count);
	}
}


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int k = sc.nextInt();
        int s = sc.nextInt();

        int count = 0;
        for (int i = 0; i <= k; i++) {
            for (int j = 0; j <= k; j++) {
                int l = s - (i + j);
                if (l >= 0 && l <= k) {
                    count++;
                }
            }
        }

        System.out.println(count);
    }
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);

		int K = s.nextInt();
		int S = s.nextInt();

		int answer = 0;
		for (int x = 0; x <= K; x++) {
			if (x > S)
				break;
			for (int y = 0; y <= K; y++) {
				if (x + y > S)
					break;

				int z = S - x - y;
				if(z<=K) {
					answer ++;
				}
				/*
				for (int z = 0; z <= K; z++) {
					if (x + y + z > S)
						break;

					if(x+y+z == S) {
						answer++;
					}

				}
				*/
			}
		}

		System.out.println(answer);

	}

}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		try {
			int k = sc.nextInt();
			int s = sc.nextInt();

			int ans = 0;
			for (int a = 0; a <= k; a++) {
				if (a == s) {
					ans++;
					break;
				}
				int bc  = (int) Math.ceil((double) (s - a) / 2);
				if (bc <= k) {
					for (int b = 0; b <= k; b++) {
						if (s - a - b >= 0 && s - a - b <= k) {
							ans++;
						}
					}
				}
			}

			System.out.println(ans);

		} finally {
			sc.close();
		}
	}
}


import java.util.Scanner;

public class Main{
    public static void main(String[] args){
	Scanner scan = new Scanner(System.in);

	int K = scan.nextInt();
	int S = scan.nextInt();

	int count = 0;
	for(int i = 0; i <= K; i++){
	    for(int j = 0; j <= K; j++){
		for(int k = 0; k <= K; k++){
		    if(i+j+k == S){
			count++;
		    }
		}
	    }
	}

	System.out.println(count);
    }
}


import java.util.Scanner;
public class Main {

    public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
    	// 入力
    	int k = sc.nextInt();
    	int s = sc.nextInt();

    	int cnt = 0;

    	for (int x = 0; x <= k; x++){
        	for (int y = 0; y <= k; y++){
        		if((s - (x + y)) >= 0 && (s - (x + y)) <= k){
            		cnt++;
            	}
        	}
    	}

    	// 出力
    	System.out.println(cnt);
    }
}


import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int K = sc.nextInt();
    int S = sc.nextInt();

    int count = 0;

    for(int  X = 0 ; X <= K ; X ++){
      for(int  Y = 0 ; Y <= K ; Y ++){
        int Z = S - X - Y ;
        if( Z >= 0 && Z <= K ){
          count ++;
        }
      }
    }
    System.out.println(count);
  }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = "";
		int strN = 1; // 文字列を何行受け取るか
		for (int i = 0; i < strN; i++) {
			line += br.readLine() + " ";
		}
		String[] param = line.split(" ");
		AtoZString(param);

	}

	static void AtoZString(String[] param) { //2行、Iで1追加し、Dで1減らす、処理後の最大値を出力[1]
		int K = Integer.parseInt(param[0]);
		int S = Integer.parseInt(param[1]);

		int count = 0;

		for(int i = 0; i <= K; i++){
			for(int j = 0; j <= K; j++){
				int k = S - (i + j);
				if(k >= 0 && k <= K) count++;
			}
		}
		System.out.println(count);
	}
}


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

public class Main {

	public static void main(String[] args) throws java.lang.Exception {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskA solver = new TaskA();
		solver.solve(in, out);
		out.close();
	}
}

class TaskA {

	public void solve(InputReader in, PrintWriter out) {
		int k = in.nextInt(), s = in.nextInt();
		int x, y, z;
		int ans = 0;

		for (x=0; x<=k&&x<=s; ++x) {
			for (y=0; y<=k&&x+y<=s; ++y) {
				z = s - (x + y);
				if (z>=0 && z<=k)
					++ans;
			}
		}

		out.println(ans);
	}
}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer==null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public int nextInt()  {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}


import java.util.*;
public class Main {
 public static void main(String[] args){
  Scanner sc = new Scanner(System.in);
  int k = sc.nextInt();
  int s = sc.nextInt();
  int result=0;
  for(int x=0;x<=k;x++){
   for(int y=0;y<=k;y++){
if(x+y<=s&&x+y+k>=s){
result++;
}
}
}
System.out.println(result);
  }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author @MaxHeap
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TripletSum solver = new TripletSum();
        solver.solve(1, in, out);
        out.close();
    }

    static class TripletSum {
        public void solve(int testNumber, FastReader fs, PrintWriter pw) {
            int k = fs.nextInt(), Sum = fs.nextInt();
            int ans = 0;
            for (int i = 0; i <= k; i++) {
                for (int j = 0; j <= k; j++) {
                    int p = Sum - i - j;
                    if (p < 0 || p > k) continue;
                    ans += 1;
                }
            }
            pw.println(ans);
        }

    }

    static class FastReader {
        BufferedReader bf;
        StringTokenizer st;

        public FastReader(InputStream is) {
            bf = new BufferedReader(new InputStreamReader(is));
            st = null;
        }

        public String next() {
            try {
                while (st == null || !st.hasMoreTokens()) {
                    String line = bf.readLine();
                    if (line == null) return null;
                    st = new StringTokenizer(line);
                }
            } catch (Exception e) {
            }

            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.util.*;
public class Main {

 public static void main(String[] args) {
  Scanner no=new Scanner(System.in);
  String s=no.next();
  int y=s.length()-1;
  int i=y;
  for(i=y;i>=0;i--){
    if(s.charAt(i)=='m'){
      if(i-4<0){
        System.out.println("NO");
      }
      else{
       String r=s.substring(i-4,i+1);
       if(r.equals("dream")){
        i=i-4;
       }
       else{
        System.out.println("NO");
        break;
       }
      }
    }
    else if(s.charAt(i)=='e'){
     if(i-4<0){
       System.out.println("NO");
     }
     else{
      String r=s.substring(i-4,i+1);
      if(r.equals("erase")){
        i=i-4;
      }
     }
    }
    else if(s.charAt(i)=='r'){
     boolean t=false;
     boolean t1=false;
     String r="";
     String r1="";
     if(i-6>=0){
      r=s.substring(i-6,i+1);
      t=true;
     }
     if(i-5>=0){
      r1=s.substring(i-5,i+1);
      t1=true;
     }
      if(t==true&&r.equals("dreamer")){
        i=i-6;
      }
      else if(t1==true&&r1.equals("eraser")){
        i=i-5;
      }
      else{
        System.out.println("NO");
        break;
      }
    }
    else{
      System.out.println("NO");
      break;
    }
  }
  //System.out.println(i);
  if(i==-1){
    System.out.println("YES");
  }
 }

}

Score : 300 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

Append one of the following at the end of T: dream, dreamer, erase and eraser.

Constraints

1≦|S|≦10^5

S consists of lowercase English letters.

Input

The input is given from Standard Input in the following format:

S

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

Sample Input 1

erasedream

Sample Output 1

YES

Append erase and dream at the end of T in this order, to obtain S = T.

Sample Input 2

dreameraser

Sample Output 2

YES

Append dream and eraser at the end of T in this order, to obtain S = T.

Sample Input 3

dreamerer

Sample Output 3

NO


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String scStr = scanner.nextLine();
		int count = scStr.length() / 5;

		String dream = "dream";
		String dreamer = "dreamer";
		String erase = "erase";
		String eraser = "eraser";

		boolean result = false;
		boolean isEnd = false;

		for (int i = 0; i <= count; i++) {
			if (isEnd == true)
				break;
			if (scStr.endsWith(dreamer)) {
				scStr = scStr.substring(0,scStr.length()-7);
			} else if (scStr.endsWith(dream)) {
				scStr = scStr.substring(0,scStr.length()-5);
			} else if (scStr.endsWith(eraser)) {
				scStr = scStr.substring(0,scStr.length()-6);
			} else if (scStr.endsWith(erase)) {
				scStr = scStr.substring(0,scStr.length()-5);
			} else if (scStr.length() <= 7) {
				if (scStr.length() == 0) {
					result = true;
				} else if (scStr.length() == 7 && (scStr.endsWith(dreamer))) {
					result = true;
					isEnd = true;
				} else if (scStr.length() == 6 && (scStr.endsWith(eraser))) {
					result = true;
					isEnd = true;
				} else if (scStr.length() == 5 && (scStr.endsWith(dream) || scStr.endsWith(erase))) {
					result = true;
					isEnd = true;
				}else {
					isEnd = true;
				}
			}
		}
		if (result == true) {
			System.out.println("YES");
		}else {
			System.out.println("NO");
		}
	}

}