import java.util.*;
import java.lang.*;
class Counter<T> extends TreeMap<T,Long>{
    public Counter(){
        super();
    }
    public Counter(List<T> list){
        super();
        for(T e: list) this.addOne(e);
    }
    public Long count(Object elm){
        return getOrDefault(elm,0L);
    }
    public void add(T elm, long amount){
        if(!this.containsKey(elm)) put(elm, amount);
        else replace(elm, get(elm)+amount);
        if(this.count(elm)==0) this.remove(elm);
    }
    public void addOne(T elm){
        this.add(elm, 1);
    }
    public void removeOne(T elm){
        this.add(elm, -1);
    }
    public void removeAll(T elm){
        this.add(elm, this.count(elm));
    }
    public static<T> Counter<T> merge(Counter<T> a, Counter<T> b){
        Counter<T> c = new Counter<>();
        for(T x: a.keySet()) c.add(x, a.count(x));
        for(T y: b.keySet()) c.add(y, b.count(y));
        return c;
    }
}
class MathLib{
    public static long divCeil(long a, long b){
        return (a+b-1)/b;
    }
    public static long power(long a, long x){
        if(x<0) return 0;
        if(x==0) return 1;
        if(x==1) return a;
        if(x%2==0){
            long half = power(a,x/2);
            return half*half;
        }
        return a*power(a,x-1);
    }

    public static long gcd(long a, long b){
        if(a<b) return gcd(b,a);
        if(b==0) return a;
        if(a%b==0) return b;
        return gcd(b,a%b);
    }
    public static long lcm(long a, long b){
        return (a/gcd(a,b))*b;
    }

    public static LinkedList<Long> divisors(long N){
        LinkedList<Long> ans = new LinkedList<>();
        for(long n = (long)(Math.sqrt(N)+2); n>0; n--){
            if(n*n>N) continue;
            else if(n*n==N) ans.add(n);
            else if(N%n==0){
                ans.addFirst(n);
                ans.addLast(N/n);
            }
        }
        return ans;
    }
    public static boolean isPrime(long N){
        return divisors(N).size()==2;
    }
    public static ArrayList<Integer> primeList(int max){
        boolean[] isp = new boolean[max+1];
        Arrays.fill(isp, true); isp[0]=false; isp[1]=false;
        for(int p=2; p*p<=max; p++) if(isp[p]){
            for(int i=2; i*p<=max; i++) isp[i*p]=false;
        }

        ArrayList<Integer> list = new ArrayList<>();
        for(int p=2; p<=max; p++) if(isp[p]) list.add(p);
        return list;
    }

    public static Counter<Long> factorize(long N){
        Counter<Long> c = new Counter<>();
        for(long n=2; n*n<=N; n++){
            while(N%n==0){
                c.addOne(n);
                N /= n;
            }
        }
        if(N>1) c.addOne(N);
        return c;
    }
}
class ArrayLib{
    static<E> E getOrDefault(E[] array, int index, E defaultValue){
        if(index<0 || index>=array.length) return defaultValue;
        return array[index];
    }

    static<E extends Comparable<E>> int maxIndexOfArray(E[] a){
        int ans = 0;
        for(int i=1;i<a.length;i++){
            if(a[i].compareTo(a[ans])>0) ans=i;
        }
        return ans;
    }
    static<E extends Comparable<E>> E maxValueOfArray(E[] a){
        return a[maxIndexOfArray(a)];
    }
    static<E extends Comparable<E>> int minIndexOfArray(E[] a){
        int ans = 0;
        for(int i=1;i<a.length;i++){
            if(a[i].compareTo(a[ans])<0) ans=i;
        }
        return ans;
    }
    static<E extends Comparable<E>> E minValueOfArray(E[] a){
        return a[minIndexOfArray(a)];
    }

    // minimum x st. array[x] >= elm
    // assert: the array must be sorted before the function call
    static<E extends Comparable<E>> int lowerBound(E[] array, E elm){
        if(array[0].compareTo(elm)>=0) return 0;
        int min = 0, max = array.length;
        // array[min] < elm
        // array[max] >= elm
        while(min+1<max){
            int mid = (min+max)/2;
            if(array[mid].compareTo(elm) < 0) min = mid;
            else max = mid;
        }
        return max;
    }
    // minimum x st. array[x] > elm
    // assert: the array must be sorted before the function call
    static<E extends Comparable<E>> int upperBound(E[] array, E elm){
        if(array[0].compareTo(elm)>0) return 0;
        int min = 0, max = array.length;
        // array[min] <= elm
        // array[max] > elm
        while(min+1<max){
            int mid = (min+max)/2;
            if(array[mid].compareTo(elm) <= 0) min = mid;
            else max = mid;
        }
        return max;
    }

    static int LIS(Long[] array){
        Long[] dp = new Long[array.length];
        Arrays.fill(dp, Long.MAX_VALUE);
        for(int i=0; i<dp.length; i++){
            dp[ArrayLib.lowerBound(dp, array[i])] = array[i];
        }
        return ArrayLib.lowerBound(dp, Long.MAX_VALUE);
    }

    static boolean nextPermutation(List<Integer> list){
        int pivotPos = -1;
        int 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 false;

        int L = pivotPos+1, R = list.size()-1;
        int minPos = -1;
        int min = Integer.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));

        return true;
    }

    static void print(int[] array, String begin, String space, String end){
        StringBuilder sb = new StringBuilder();
        sb.append(begin);
        for(int i=0; i<array.length-1; i++){
            sb.append(array[i]);
            sb.append(space);
        }
        sb.append(array[array.length-1]);
        sb.append(end);
        System.out.print(sb);
    }
    static void print(int[] array){
        print(array, "", " ", "\n");
    }
    static void print(long[] array, String begin, String space, String end){
        StringBuilder sb = new StringBuilder();
        sb.append(begin);
        for(int i=0; i<array.length-1; i++){
            sb.append(array[i]);
            sb.append(space);
        }
        sb.append(array[array.length-1]);
        sb.append(end);
        System.out.print(sb);
    }
    static void print(long[] array){
        print(array, "", " ", "\n");
    }
    static void print(double[] array, String begin, String space, String end){
        StringBuilder sb = new StringBuilder();
        sb.append(begin);
        for(int i=0; i<array.length-1; i++){
            sb.append(array[i]);
            sb.append(space);
        }
        sb.append(array[array.length-1]);
        sb.append(end);
        System.out.print(sb);
    }
    static void print(double[] array){
        print(array, "", " ", "\n");
    }
    static void print(Object[] array, String begin, String space, String end){
        StringBuilder sb = new StringBuilder();
        sb.append(begin);
        for(int i=0; i<array.length-1; i++){
            sb.append(array[i]);
            sb.append(space);
        }
        sb.append(array[array.length-1]);
        sb.append(end);
        System.out.print(sb);
    }
    static void print(Object[] array){
        print(array, "", " ", "\n");
    }

}

public class Main {
    static long gcd(int... x){
        if(x.length==0) return 0;
        if(x.length==1) return 1;
        long ans = x[0];
        for(int i=1; i<x.length; i++) ans = MathLib.gcd(ans, x[i]);
        return ans;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K = sc.nextInt();
        long ans = 0;
        for(int a=1; a<=K; a++) for(int b=1; b<=K; b++) for(int c=1; c<=K; c++){
            ans += gcd(a,b,c);
        }
        System.out.println(ans);

    }
}


import java.io.*;import java.util.*;import java.math.*;
public class Main
{
	static long mod=1000000007l;
    static int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;
    static long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE;
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;

    static public void main(String[] args)throws Exception
    {
    	st=new StringTokenizer(br.readLine());
    	int k=i();
    	long s=0l;
    	for(int x=1;x<=k;x++)
    	{
    		for(int xa=1;xa<=k;xa++)
    		{
    			int kk=gcd(x,xa);
    			for(int xaa=1;xaa<=k;xaa++)s+=gcd(xaa,kk);
    		}
    	}
    	pl(s);
    }

    static int max(int a,int b){return Math.max(a,b);}
    static int min(int a,int b){return Math.min(a,b);}
    static int abs(int a){return Math.abs(a);}
    static long max(long a,long b){return Math.max(a,b);}
    static long min(long a,long b){return Math.min(a,b);}
    static long abs(long a){return Math.abs(a);}
    static int sq(int a){return (int)Math.sqrt(a);}
    static long sq(long a){return (long)Math.sqrt(a);}
    static int gcd(int a,int b) {return b==0?a:gcd(b,a%b);}
    static int ncr(int n,int c,long m)
    {
        long a=1l;
        for(int x=n-c+1;x<=n;x++)a=((a*x)%m);
        long b=1l;
        for(int x=2;x<=c;x++)b=((b*x)%m);
        long v=(div((int)b,m-2,m)%m);
        return (int)((a*v)%m);
    }
    static int fib(int n)
    {
        double p=(1+Math.sqrt(5))/2;
        return (int)Math.round(Math.pow(p,n)/Math.sqrt(5));
    }
    static boolean[] sieve(int n)
    {
        boolean bo[]=new boolean[n+1];
        bo[0]=true;bo[1]=true;
        for(int x=4;x<=n;x+=2)bo[x]=true;
        for(int x=3;x*x<=n;x+=2)
        {
            if(!bo[x])
            {
                for(int y=x*x;y<=n;y+=x)bo[y]=true;
            }
        }
        return bo;
    }
    static int[] fac(int n)
    {
        int bo[]=new int[n+1];
        bo[0]=0;bo[1]=1;
        for(int x=1;x<=n;x++)
        {
            for(int y=x;y<=n;y+=x)bo[y]++;
        }
        return bo;
    }
    static long div(long a,long b,long m)
    {
        long r=1l;
        a%=m;
        while(b>0)
        {
            if((b&1)==1)r=(r*a)%m;
            b>>=1;
            a=(a*a)%m;
        }
        return r;
    }
    static int i()throws IOException
    {
        if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }
    static long l()throws IOException
    {
        if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
        return Long.parseLong(st.nextToken());
    }
    static String s()throws IOException
    {
        if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
        return st.nextToken();
    }
    static double d()throws IOException
    {
        if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
        return Double.parseDouble(st.nextToken());
    }
    static void p(Object p){System.out.print(p);}
    static void p(String p){System.out.print(p);}
    static void p(int p){System.out.print(p);}
    static void p(double p){System.out.print(p);}
    static void p(long p){System.out.print(p);}
    static void p(char p){System.out.print(p);}
    static void p(boolean p){System.out.print(p);}
    static void pl(Object p){System.out.println(p);}
    static void pl(String p){System.out.println(p);}
    static void pl(int p){System.out.println(p);}
    static void pl(char p){System.out.println(p);}
    static void pl(double p){System.out.println(p);}
    static void pl(long p){System.out.println(p);}
    static void pl(boolean p){System.out.println(p);}
    static void pl(){System.out.println();}
    static int[] ari(int n)throws IOException
    {
        int ar[]=new int[n];
        st=new StringTokenizer(br.readLine());
        for(int x=0;x<n;x++) ar[x]=Integer.parseInt(st.nextToken());
        return ar;
    }
    static int[][] ari(int n,int m)throws IOException
    {
        int ar[][]=new int[n][m];
        for(int x=0;x<n;x++)
        {
            st=new StringTokenizer(br.readLine());
            for(int y=0;y<m;y++)ar[x][y]=Integer.parseInt(st.nextToken());
        }
        return ar;
    }
    static long[] arl(int n)throws IOException
    {
        long ar[]=new long[n];
        st=new StringTokenizer(br.readLine());
        for(int x=0;x<n;x++) ar[x]=Long.parseLong(st.nextToken());
        return ar;
    }
    static long[][] arl(int n,int m)throws IOException
    {
        long ar[][]=new long[n][m];
        for(int x=0;x<n;x++)
        {
            st=new StringTokenizer(br.readLine());
            for(int y=0;y<m;y++)ar[x][y]=Long.parseLong(st.nextToken());
        }
        return ar;
    }
    static String[] ars(int n)throws IOException
    {
        String ar[]=new String[n];
        st=new StringTokenizer(br.readLine());
        for(int x=0;x<n;x++) ar[x]=st.nextToken();
        return ar;
    }
    static double[] ard(int n)throws IOException
    {
        double ar[]=new double[n];
        st=new StringTokenizer(br.readLine());
        for(int x=0;x<n;x++)ar[x]=Double.parseDouble(st.nextToken());
        return ar;
    }
    static double[][] ard(int n,int m)throws IOException
    {
        double ar[][]=new double[n][m];
        for(int x=0;x<n;x++)
        {
            st=new StringTokenizer(br.readLine());
            for(int y=0;y<m;y++)ar[x][y]=Double.parseDouble(st.nextToken());
        }
        return ar;
    }
    static char[] arc(int n)throws IOException
    {
        char ar[]=new char[n];
        st=new StringTokenizer(br.readLine());
        for(int x=0;x<n;x++)ar[x]=st.nextToken().charAt(0);
        return ar;
    }
    static char[][] arc(int n,int m)throws IOException
    {
        char ar[][]=new char[n][m];
        for(int x=0;x<n;x++)
        {
            String s=br.readLine();
            for(int y=0;y<m;y++)ar[x][y]=s.charAt(y);
        }
        return ar;
    }
    static void p(int ar[])
    {
        StringBuilder sb=new StringBuilder("");;
        for(int a:ar) sb.append(a+" ");
        System.out.println(sb);
    }
    static void p(int ar[][])
    {
        StringBuilder sb;
        for(int a[]:ar)
        {
            sb=new StringBuilder("");
            for(int aa:a) sb.append(aa+" ");
            System.out.println(sb);
        }
    }
    static void p(long ar[])
    {
        StringBuilder sb=new StringBuilder("");
        for(long a:ar)sb.append(a+" ");
        System.out.println(sb);
    }
    static void p(long ar[][])
    {
        StringBuilder sb;
        for(long a[]:ar)
        {
            sb=new StringBuilder("");
            for(long aa:a)sb.append(aa+" ");
            System.out.println(sb);
        }
    }
    static void p(String ar[])
    {
        StringBuilder sb=new StringBuilder("");
        for(String a:ar)sb.append(a+" ");
        System.out.println(sb);
    }
    static void p(double ar[])
    {
        StringBuilder sb=new StringBuilder("");
        for(double a:ar)sb.append(a+" ");
        System.out.println(sb);
    }
    static void p(double ar[][])
    {
        StringBuilder sb;
        for(double a[]:ar)
        {
            sb=new StringBuilder("");
            for(double aa:a)sb.append(aa+" ");
            System.out.println(sb);
        }
    }
    static void p(char ar[])
    {
        StringBuilder sb=new StringBuilder("");
        for(char aa:ar)sb.append(aa+" ");
        System.out.println(sb);
    }
    static void p(char ar[][])
    {
        StringBuilder sb;
        for(char a[]:ar)
        {
            sb=new StringBuilder("");
            for(char aa:a)sb.append(aa+" ");
            System.out.println(sb);
        }
    }
}

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

Score : 300 points

Problem Statement

Find \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Here \gcd(a,b,c) denotes the greatest common divisor of a, b, and c.

Constraints

1 \leq K \leq 200

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the value of \displaystyle{\sum_{a=1}^{K}\sum_{b=1}^{K}\sum_{c=1}^{K} \gcd(a,b,c)}.

Sample Input 1

2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

200

Sample Output 2

10813692

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.*;
import java.lang.Math;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
      	int n = sc.nextInt();
      	String s = sc.next();
      	long count = 0;
      	long rc = 0;
      	long gc = 0;
      	long bc = 0;
      	int istack;

      	Map cMap = new HashMap<Character,Integer>() {
          {
        	put('R',1);
        	put('G',10);
        	put('B',100);
          }
        };

      	for(int i = 0;i < n;i++){
        	if(s.charAt(i) == 'R') rc++;
          	else if(s.charAt(i) == 'G') gc++;
          	else if(s.charAt(i) == 'B') bc++;
        }

      	for(int i = 0;i < n;i++){
          for(int j = i+1;j < n;j++){
            if(j+(j-i) < n){
              istack = (int)cMap.get(s.charAt(i))
                      +(int)cMap.get(s.charAt(j))
                      +(int)cMap.get(s.charAt(j+(j-i)));
              if(istack == 111) count++;
            }
          }
        }

      	System.out.println(rc*gc*bc-count);
	}
}

Score : 400 points

Problem Statement

We have a string S of length N consisting of R, G, and B.

Find the number of triples (i,~j,~k)~(1 \leq i < j < k \leq N) that satisfy both of the following conditions:

S_i \neq S_j, S_i \neq S_k, and S_j \neq S_k.

j - i \neq k - j.

Constraints

1 \leq N \leq 4000

S is a string of length N consisting of R, G, and B.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of triplets in question.

Sample Input 1

4
RRGB

Sample Output 1

1

Only the triplet (1,~3,~4) satisfies both conditions. The triplet (2,~3,~4) satisfies the first condition but not the second, so it does not count.

Sample Input 2

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

Sample Output 2

1800


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();
      	String s = sc.next();
      	int count = 0;
      	int istack;

      	Map cMap = new HashMap<Character,Integer>() {
          {
        	put('R',1);
        	put('G',10);
        	put('B',100);
          }
        };

      	for(int i = 0;i < n;i++){
          for(int j = i+1;j < n;j++){
            for(int k = j+1; k < n;k++){
              if(j - i != k - j){
                istack = (int)cMap.get(s.charAt(i))
                  +(int)cMap.get(s.charAt(j))
                  +(int)cMap.get(s.charAt(k));
                if(istack == 111) count++;
              }
            }
          }
        }

      	System.out.println(count);
	}
}


import java.util.Scanner;

public class Main {

    Main(){
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        String S = scanner.next();

        int r=0,g=0,b=0;
        for(int i=0;i<N;i++){
            switch (S.charAt(i)){
                case 'R':
                    r++;
                    break;
                case 'G':
                    g++;
                    break;
                case 'B':
                    b++;
                    break;
            }
        }

        int total = r*g*b;

        for (int i = 0; i < N; i++) {
            for (int j = i+1; j < N-1; j++) {
                int k = j + j - i;

                if(N <= k) continue;

                if(S.charAt(i) != S.charAt(j)
                    && (S.charAt(j) != S.charAt(k)
                    && S.charAt(i) != S.charAt(k))){
                    total--;
                }
            }
        }

        System.out.println(total);
    }

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


import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
  	int n=sc.nextInt(),r=0,b=0,g=0;
    String[] s=sc.next().split("");
    for(int i=0;i<n;i++){
      if(s[i].equals("R")){
        r++;
      }else if(s[i].equals("B")){
        b++;
      }else{
        g++;
      }
    }
    int f=r*g*b;
    for(int i=0;i<n;i++){
      for(int j=i+1;j<n;j++){
        if(n-1>=(2*j-i)){
          if(s[i].equals(s[j])==false&&s[j].equals(s[2*j-i])==false&&s[2*j-i].equals(s[i])==false){
            f=f-1;
          }
        }
      }
    }
    System.out.print(f);
  }
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String[] sArray = sc.next().split("");
		int[] rList = new int[n+1];
		int[] gList = new int[n+1];
		int[] bList = new int[n+1];
		int redCount = 0;
		int greenCount = 0;
		int blueCount = 0;
		for(int i =0;i < sArray.length;i++) {
			if(sArray[i].equals("R")) {
				redCount++;
				rList[i+1]++;
			}
			if(sArray[i].equals("G")) {
				greenCount++;
				gList[i+1]++;
			}
			if(sArray[i].equals("B")) {
				blueCount++;
				bList[i+1]++;
			}
		}
		long answer = redCount * greenCount * blueCount;
		if(answer == 0) {
			System.out.println(0);
			return;
		}
		for(int red =1;red <= n;red++) {
			if(rList[red] ==0) {
				continue;
			}
			for(int green =1;green <= n;green++) {
				if(gList[green] ==0) {
					continue;
				}
				if(red > green) {
					if(red + (red -green)<=n) {
						if(bList[(red + (red -green))] == 1) {
							answer--;
						}
					}
					if(green - (red -green)>0) {
						if(bList[(green - (red -green))] == 1) {
							answer--;
						}
					}
				}else {
					if(green + (green -red)<=n) {
						if(bList[(green + (green -red))] == 1) {
							answer--;
						}
					}
					if(red - (green -red)>0) {
						if(bList[(red - (green -red))] == 1) {
							answer--;
						}
					}

				}
				if((red + green)%2 ==0 ) {
					if(bList[((red +green)/2)] == 1) {
						answer--;
					}
				}
			}
		}

		System.out.println(answer);
	}

}


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

		int n = sc.nextInt();
		String s = sc.next();

		long r = 0;
		long g = 0;
		long b = 0;

		for ( int i=0; i<n; i++ ) {
			if ( s.charAt(i)=='R' ) {
				r++;
			} else if ( s.charAt(i)=='G' ) {
				g++;
			} else {
				b++;
			}
		}

		if ( r*g*b==0 ) {
			System.out.println(0);
			System.exit(0);
		}

		long f = 0;
		for ( int i=0; i<n-2; i++ ) {
			for ( int j=i+1; j<n; j++ ) {
				int k = 2*j-i;
				if ( k < n ) {
					if ( s.charAt(i)!=s.charAt(j) && s.charAt(j)!=s.charAt(k) && s.charAt(k)!=s.charAt(i) ) {
						f++;
					}
				}
			}
		}

		System.out.println(r*g*b-f);
	}

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class Main {
    public static void main(String[] args){
        try{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int n=Integer.parseInt(br.readLine());
            String ss=br.readLine();
            char[] c=ss.toCharArray();
            int ans=0;
            int[][] arr=new int[3][n];
            int r=0,g=0,b=0;
            HashSet<Integer> R=new HashSet<>(),G=new HashSet<>(),B=new HashSet<>();
            for(int i=n-1;i>=0;i--){
                if(c[i]=='R'){
                    r++;
                    R.add(i);
                }else if(c[i]=='G'){
                    g++;
                    G.add(i);
                }else{
                    b++;
                    B.add(i);
                }
                arr[0][i]=r;
                arr[1][i]=g;
                arr[2][i]=b;
            }
            for(int i=0;i<n;i++){
                for(int j=i+1;j<n-1;j++){
                    if(c[i]==c[j]){
                        continue;
                    }
                    int u=j+(j-i);
                    if((c[i]=='R' && c[j]=='G') || (c[i]=='G' && c[j]=='R')){
                        ans+=arr[2][j+1];
                        if(B.contains(u)){
                            ans--;
                        }
                    }else if((c[i]=='R' && c[j]=='B') || (c[i]=='B' && c[j]=='R')){
                        ans+=arr[1][j+1];
                        if(G.contains(u)){
                            ans--;
                        }
                    }else if((c[i]=='G' && c[j]=='B') || (c[i]=='B' && c[j]=='G')){
                        ans+=arr[0][j+1];
                        if(R.contains(u)){
                            ans--;
                        }
                    }
                }
            }

            System.out.println(ans);

        }catch(Exception e){
            System.out.println("KKKK "+e.getMessage());
        }
    }
    static int gcd(int a,int b){
        if(a==0){
            return b;
        }
        if(b==0){
            return a;
        }
        if(a>b){
            return gcd(a%b,b);
        }
        return gcd(a,b%a);

    }
}


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

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

	public Main() {
		FasterScanner sc = new FasterScanner(System.in);

		int n = sc.nextInt();
		String s = sc.nextString();
		int r = 0;
		int g = 0;
		int b = 0;
		long sol;
		for (int i = 0; i < n; i++) {
			char c = s.charAt(i);
			if (c == 'R') r++;
			if (c == 'G') g++;
			if (c == 'B') b++;
		}
		sol = r*g*b;
		for (int i = 0; i < n; i++) {
			char a = s.charAt(i);
			for (int j = 1; ((i+j) < n) && ((i-j) >= 0); j++) {
				char d = s.charAt(i+j);
				char c = s.charAt(i-j);
				if (a != d && a != c && d != c) sol--;
			}
		}

		System.out.println(sol);

		sc.close();
	}

	class FasterScanner {
		private InputStream mIs;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FasterScanner() {
			this(System.in);
		}

		public FasterScanner(InputStream is) {
			mIs = is;
		}

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

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

		public String nextString() {
			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 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 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 boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		public void close() {
			try {
				mIs.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    int N=sc.nextInt();
    String S = sc.next();
    String s[] =S.split("",0);
    int t[] =new int[N];
    int x=0;int w=0;int a=0;int b=0; int c=0;
    for(int i=0;i<N;i++){
      if(s[i].equals("R")){t[i]=1;b=b+1;}
      else if(s[i].equals("G")){t[i]=2;a=a+1;}
      else{t[i]=3;c=c+1;}}
    for(int k=1;k<N-2;k++){
      int l=Math.min(k,N-k);
      for(int j=1;j<l;j++){
          if(t[k-j-1]+t[k-1]+t[k+j-1]==6){
            w=w+1;}}}
    int z=a*b*c;
    System.out.println(z-w);
  }
}


import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    String s = sc.next();
    long[] rk = new long[n];
    long[] gk = new long[n];
    long[] bk = new long[n];
    if(s.charAt(n - 1) == 'R') {
      rk[n - 1] = 1;
    } else if(s.charAt(n - 1) == 'G') {
      gk[n - 1] = 1;
    } else {
      bk[n - 1] = 1;
    }
    for(int i = n - 2; i >= 0; i--) {
      if(s.charAt(i) == 'R') {
        rk[i] = rk[i + 1] + 1;
        gk[i] = gk[i + 1];
        bk[i] = bk[i + 1];
      } else if(s.charAt(i) == 'G') {
        gk[i] = gk[i + 1] + 1;
        rk[i] = rk[i + 1];
        bk[i] = bk[i + 1];
      } else {
        bk[i] = bk[i + 1] + 1;
        rk[i] = rk[i + 1];
        gk[i] = gk[i + 1];
      }
    }
    long ans = 0;
    for(int i = 0; i < n - 2; i++) {
      for(int j = i + 1; j < n - 1; j++) {
        int k = j + (j - i);
        if((s.charAt(i) == 'R') && (s.charAt(j) == 'G')) {
          if(k < n) {
            if(s.charAt(k) == 'B') {
              ans += (bk[j + 1] - 1);
            } else {
              ans += bk[j + 1];
            }
          } else {
            ans += bk[j + 1];
          }
        }
        if((s.charAt(i) == 'R') && (s.charAt(j) == 'B')) {
          if(k < n) {
            if(s.charAt(k) == 'G') {
              ans += (gk[j + 1] - 1);
            } else {
              ans += gk[j + 1];
            }
          } else {
            ans += gk[j + 1];
          }
        }
        if((s.charAt(i) == 'G') && (s.charAt(j) == 'R')) {
          if(k < n) {
            if(s.charAt(k) == 'B') {
              ans += (bk[j + 1] - 1);
            } else {
              ans += bk[j + 1];
            }
          } else {
            ans += bk[j + 1];
          }
        }
        if((s.charAt(i) == 'G') && (s.charAt(j) == 'B')) {
          if(k < n) {
            if(s.charAt(k) == 'R') {
              ans += (rk[j + 1] - 1);
            } else {
              ans += rk[j + 1];
            }
          } else {
            ans += rk[j + 1];
          }
        }
        if((s.charAt(i) == 'B') && (s.charAt(j) == 'R')) {
          if(k < n) {
            if(s.charAt(k) == 'G') {
              ans += (gk[j + 1] - 1);
            } else {
              ans += gk[j + 1];
            }
          } else {
            ans += gk[j + 1];
          }
        }
        if((s.charAt(i) == 'B') && (s.charAt(j) == 'G')) {
          if(k < n) {
            if(s.charAt(k) == 'R') {
              ans += (rk[j + 1] - 1);
            } else {
              ans += rk[j + 1];
            }
          } else {
            ans += rk[j + 1];
          }
        }
      }
    }
    System.out.println(ans);
  }
}


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.LongStream;

public class Main {

    public Main() throws IOException {
    }

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

    //    private final InputStream in = Files.newInputStream(Paths.get("C:\\Users\\ryo.suzuki\\Downloads\\02.txt"));
    private final InputStream in = System.in;
    private final PrintWriter out = new PrintWriter(System.out);
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private void solve() throws IOException {
        try {
//            solveA();
//            solveB();
//            solveC();
            solveD();
//            solveE();
//            solveF();
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.flush();
                out.close();
            }
        }

    }

    // mods
    long MOD = (long) Math.pow(10, 9) + 7;

    private void solveA() {
        int n = nextInt();
        while (n > 0) {
            int wk = n % 10;
            n /= 10;
            if (wk == 7) {
                out.println("Yes");
                return;
            }
        }
        out.println("No");
    }

    private void solveB() {
        long n = nextLong();
        long res = 0;
        for (long i = 1; i <= n; i++) {
            if (i % 3 != 0 && i % 5 != 0)
                res += i;
        }
        out.println(res);
    }

    private void solveC() {
        int k = nextInt();
        long res = 0;
        for (long i = 1; i <= k; i++) {
            for (long j = 1; j <= k; j++) {
                for (long l = 1; l <= k; l++) {
                    long wk = gcd(i, j);
                    wk = gcd(wk, l);
                    res += wk;
                }
            }
        }
        out.println(res);
    }

    private void solveD() {
        int n = nextInt();
        char[] s = (next()).toCharArray();
        long r = 0;
        long g = 0;
        long b = 0;
        for (char c : s) {
            switch (c) {
                case 'R':
                    r++;
                    break;
                case 'G':
                    g++;
                    break;
                case 'B':
                    b++;
                    break;
            }
        }

        long res = r * g * b;
        if (res == 0) {
            out.println(0);
            return;
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i - j < 0 || i + j >= n)
                    continue;
                if (s[i - j] != s[i] && s[i + j] != s[i] && s[i + j] != s[i - j])
                    res--;
            }
        }
        out.println(res);
    }

    private void solveE() {

    }

    private void solveF() {

    }

    static final long CONST_INF = Long.MAX_VALUE / 4;

    private static class VectorCalculation {
        /**
         * 点Pと線(AB)の距離
         *
         * @param p
         * @param a
         * @param b
         * @return
         */
        private double distanceDotAndLine(DoublePair p, DoublePair a, DoublePair b) {
            DoublePair ab = new DoublePair(b.x - a.x, b.y - a.y);
            DoublePair ap = new DoublePair(p.x - a.x, p.y - a.y);

            // ベクトルAB、APの外積の絶対値が平行四辺形Dの面積になる
            double d = Math.abs(crossVector(ab, ap));

            double l = distanceVertex(a, b); // AB間の距離

            double h = d / l;
            return h;
        }

        /**
         * 2点間距離
         *
         * @param p1
         * @param p2
         * @return
         */
        private double distanceVertex(DoublePair p1, DoublePair p2) {
            double dx = p1.x - p2.x;
            double dy = p1.y - p2.y;
            return Math.sqrt(dx * dx + dy * dy);
        }

        /**
         * ベクトル外積
         *
         * @param vl
         * @param vr
         * @return
         */
        private double crossVector(DoublePair vl, DoublePair vr) {
            return vl.x * vr.y - vl.y * vr.x;
        }
    }

    private static class DoublePair implements Comparable<DoublePair> {
        double x;
        double y;

        public DoublePair(double k, double v) {
            this.x = k;
            this.y = v;
        }

        public int compareTo(DoublePair pair) {
            return Double.compare(this.y, pair.y);
        }
    }

    private static class SimplePair implements Comparable<SimplePair> {
        long k;
        long v;

        public SimplePair(int k, int v) {
            this.k = k;
            this.v = v;
        }

        public SimplePair(long k, long v) {
            this.k = k;
            this.v = v;
        }

        public int compareTo(SimplePair pair) {
            return Long.compare(this.v, pair.v);
        }
    }

    private static class SimpleGraphNode {
        private int n;
        private Map<Integer, Integer> childs;

        public SimpleGraphNode(int n) {
            this.n = n;
            childs = new HashMap<Integer, Integer>();
        }

        public void addChild(int child, int d) {
            this.childs.put(child, d);
        }
    }

    private static class SimpleGraph {

        private Map<Integer, SimpleGraphNode> graph;

        public SimpleGraph(int[][] list) {
            graph = new HashMap<Integer, SimpleGraphNode>();
            Arrays.stream(list).forEach(l -> {
                int a = l[0];
                int b = l[1];
                int d = l[2];
                graph.put(a, graph.getOrDefault(a, new SimpleGraphNode(a)));
                graph.get(a).addChild(b, d);
                graph.put(b, graph.getOrDefault(b, new SimpleGraphNode(b)));
                graph.get(b).addChild(a, d);
            });
        }

        public SimpleGraphNode getNode(int i) {
            return graph.getOrDefault(i, new SimpleGraphNode(-1));
        }

    }

    private static class GraphNode {
        private long n;
        private Map<Long, SimplePair> childs;

        public GraphNode(long n) {
            this.n = n;
            childs = new TreeMap<Long, SimplePair>();
        }

        /**
         * not update
         *
         * @param child
         * @param d
         */
        public void addChild(long child, long d) {
            this.childs.put(child, this.childs.getOrDefault(child, new SimplePair(child, d)));
        }

        public SimplePair getChild(long child) {
            return this.childs.getOrDefault(child, new SimplePair(child, CONST_INF));
        }
    }

    private static class Graph {

        private Map<Long, GraphNode> graph;
        int n;

        /**
         * @param n    頂点数
         * @param list
         */
        public Graph(int n, int[][] list) {
            this.n = n;
            graph = new HashMap<Long, GraphNode>();
            /*
            隣接行列作成
            0-indexed
             */
            Arrays.stream(list).forEach(l -> {
                long a = l[0] - 1;
                long b = l[1] - 1;
                long d = l[2];
                graph.put(a, graph.getOrDefault(a, new GraphNode(a)));
                graph.get(a).addChild(b, d);
                graph.put(b, graph.getOrDefault(b, new GraphNode(b)));
                graph.get(b).addChild(a, d);
            });
            /*
            たどり着けない場所はCONST_INFで埋める
            自分へはCOST=0でたどり着ける
             */
            for (long i = 0; i < n; i++) {
                graph.put(i, graph.getOrDefault(i, new GraphNode(i)));
                GraphNode node = graph.get(i);
                for (int j = 0; j < n; j++) {
                    if (i == j)
                        node.addChild(j, 0L);
                    else
                        node.addChild(j, CONST_INF);
                }
            }
        }

        /**
         * ワーシャルフロイド
         */
        public void warshallFloyd() {
            for (long k = 0; k < n; k++) {
                for (long i = 0; i < n; i++) {
                    for (long j = 0; j < n; j++) {
                        long min = Math.min(graph.get(i).getChild(j).v, graph.get(i).getChild(k).v + graph.get(k).getChild(j).v);
                        graph.get(i).getChild(j).v = min;
                    }
                }
            }
        }

        public void partialUpdateAndWarshallFloyd(long x, long y, long w) {
            if (graph.get(x).getChild(y).v > w)
                graph.get(x).getChild(y).v = graph.get(y).getChild(x).v = w;
            long[] l = {x, y};
            for (long k : l) {
                for (long i = 0; i < n; i++) {
                    for (long j = 0; j < n; j++) {
                        if (graph.get(i).getChild(j).v > graph.get(i).getChild(k).v + graph.get(k).getChild(j).v) {
                            graph.get(i).getChild(j).v = graph.get(i).getChild(k).v + graph.get(k).getChild(j).v;
                        }
                    }
                }
            }
        }

        /**
         * 2点間の距離
         *
         * @param from
         * @param to
         * @return
         */
        public long getDistance(long from, long to) {
            return graph.get(from).getChild(to).v;
        }

    }

    public long calcSimpleCombination(long n, long m, long CONST_MOD) {
        long mole = 1;
        for (long i = 1; i <= n + m; i++) {
            mole *= i;
            mole %= CONST_MOD;
        }
        long deno = 1;
        for (long i = 1; i <= n; i++) {
            deno *= i;
            deno %= CONST_MOD;
        }
        for (int i = 1; i <= m; i++) {
            deno *= i;
            deno %= CONST_MOD;
        }
        deno = modInverse(deno, CONST_MOD);
        return (mole * deno) % CONST_MOD;
    }

    long abs(double x) {
        return (long) Math.abs(x);
    }

    long round(double x) {
        return Math.round(x);
    }

    long floor(double x) {
        return (long) Math.floor(x);
    }

    long ceil(double x) {
        return (long) Math.ceil(x);
    }

    double sqrt(double x) {
        return Math.sqrt(x);
    }

    double pow(double x, double y) {
        return Math.pow(x, y);
    }

    long pow(long x, long y) {
        return (long) Math.pow(x, y);
    }

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

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

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

    public long lcmMod(long... ab) {

        Map<Long, Long> ps = new HashMap<Long, Long>();

        for (int abl = 0; abl < ab.length; abl++) {
            long target = ab[abl];
            long l = target;
            for (long p = 2; p * p <= l; p++) {
                long cnt = 0;
                while (target > 1 && target % p == 0) {
                    cnt++;
                    target /= p;
                }
                if (cnt == 0)
                    continue;
                ps.put(p, Long.max(cnt, ps.getOrDefault(p, 0L)));
            }
            if (target > 1)
                ps.put(target, Long.max(1, ps.getOrDefault(target, 0L)));
        }
        long res = 1L;
        for (Map.Entry<Long, Long> e : ps.entrySet()) {
            res *= powMod(e.getKey(), e.getValue());
            res %= MOD;
        }
        return res;
    }

    int upperToInt(char a) {
        return a - 'A';
    }

    int lowerToInt(char a) {
        return a - 'a';
    }

    int numToInt(char a) {
        return a - '0';
    }

    int charToInt(char a) {
        return a >= 'a' ? lowerToInt(a) : a >= 'A' ? upperToInt(a) : numToInt(a);
    }

    char intToUpper(int a) {
        return (char) (a + 'A');
    }

    char intToLower(int a) {
        return (char) (a + 'a');
    }

    char intToNum(int a) {
        return (char) (a + '0');
    }

    void reverse(String array[]) {
        String reversed[] = new String[array.length];
        for (int i = 0; i < array.length; i++) {
            reversed[array.length - i - 1] = array[i];
        }
        for (int i = 0; i < array.length; i++) {
            array[i] = reversed[i];
        }
    }

    void reverse(int array[]) {
        int reversed[] = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            reversed[array.length - i - 1] = array[i];
        }
        for (int i = 0; i < array.length; i++) {
            array[i] = reversed[i];
        }
    }

    void reverse(long array[]) {
        long reversed[] = new long[array.length];
        for (int i = 0; i < array.length; i++) {
            reversed[array.length - i - 1] = array[i];
        }
        for (int i = 0; i < array.length; i++) {
            array[i] = reversed[i];
        }
    }

    void reverse(double array[]) {
        double reversed[] = new double[array.length];
        for (int i = 0; i < array.length; i++) {
            reversed[array.length - i - 1] = array[i];
        }
        for (int i = 0; i < array.length; i++) {
            array[i] = reversed[i];
        }
    }

    void reverse(boolean array[]) {
        boolean reversed[] = new boolean[array.length];
        for (int i = 0; i < array.length; i++) {
            reversed[array.length - i - 1] = array[i];
        }
        for (int i = 0; i < array.length; i++) {
            array[i] = reversed[i];
        }
    }

    void fill(int array[][], int x) {
        for (int a[] : array) {
            Arrays.fill(a, x);
        }
    }

    void fill(long array[][], long x) {
        for (long a[] : array) {
            Arrays.fill(a, x);
        }
    }

    void fill(double array[][], double x) {
        for (double a[] : array) {
            Arrays.fill(a, x);
        }
    }

    void fill(boolean array[][], boolean x) {
        for (boolean a[] : array) {
            Arrays.fill(a, x);
        }
    }

    void fill(int array[][][], int x) {
        for (int a[][] : array) {
            fill(a, x);
        }
    }

    void fill(long array[][][], long x) {
        for (long a[][] : array) {
            fill(a, x);
        }
    }

    void fill(double array[][][], double x) {
        for (double a[][] : array) {
            fill(a, x);
        }
    }

    void fill(boolean array[][][], boolean x) {
        for (boolean a[][] : array) {
            fill(a, x);
        }
    }

    long L_INF = (long) 1e18 + 7;

    boolean isINF(long a) {
        return abs(a) > L_INF / 1000;
    }

    boolean isPlusINF(long a) {
        return a > 0 && isINF(a);
    }

    boolean isMinusINF(long a) {
        return isPlusINF(-a);
    }

    int I_INF = (int) 1e9 + 7;

    boolean isINF(int a) {
        return abs(a) > I_INF / 1000;
    }

    boolean isPlusINF(int a) {
        return a > 0 && isINF(a);
    }

    boolean isMinusINF(int a) {
        return isPlusINF(-a);
    }

    /**
     * 指定したx以下の素数をリストアップする
     */
    public long[] getPrimesUnderX(long x) {
        return LongStream.rangeClosed(2, x)
                .filter(i -> LongStream.rangeClosed(2, (long) Math.sqrt(i)).allMatch(j -> i % j != 0)).toArray();
    }

    public long mod(long i) {
        return i % MOD + ((i % MOD) < 0 ? MOD : 0);
    }

    long powMod(long x, long y) {
        if (y == 0) {
            return 1;
        } else {
            long tmp = powMod(x, y / 2);
            return mod(mod(tmp * tmp) * (y % 2 == 0 ? 1 : x));
        }
    }

    long inv(long x) {
        return powMod(x, MOD - 2);
    }

    int MAX_FACT = 5_000_100;
    long fact[];
    long invFact[];

    /**
     * Combination簡易版
     * 5 C 2
     * 異なる n個のものから r個を選ぶ組み合わせの総数 nCr を求めます。
     * 5!(5*4*3*2*1)
     * /
     * 2!(2*1) *  (5-2)!(3*2*1)
     *
     * @param n
     * @param r
     * @return
     */
    private long getComb(int n, int r) {
        long tmp = 1;
        for (int i = 1; i <= r; i++) {
            tmp *= n - i + 1;
            tmp = mod(tmp);
            tmp *= inv(i);
            tmp = mod(tmp);
        }
        return tmp;
    }

    /**
     * 階乗計算の事前累積和
     * [1, 1, 2, 3, 4, 5, … FACTORIAL_NUM]
     * mod済
     */
    void prepareFact() {
        fact = new long[MAX_FACT];
        Arrays.fill(fact, 0);
        invFact = new long[MAX_FACT];
        Arrays.fill(invFact, 0);
        fact[0] = 1;
        int maxIndex = (int) min(MAX_FACT, (int) MOD);
        for (int i = 1; i < maxIndex; i++) {
            fact[i] = mod(fact[i - 1] * i);
        }
        invFact[maxIndex - 1] = inv(fact[maxIndex - 1]);
        for (int i = maxIndex - 1; i > 0; i--) {
            invFact[i - 1] = mod(invFact[i] * i);
        }
    }

    /**
     * 順列
     * nPk -> n! / (n-k)!
     *
     * @param n
     * @param r
     * @return
     */
    long permutation(int n, int r) {
        if (n < 0 || r < 0 || n < r) {
            return 0;
        }
        return mod(fact[n] * invFact[n - r]);
    }

    /**
     * 組み合わせ
     * nCk -> n! / k!・(n-k)!
     *
     * @param n
     * @param r
     * @return
     */
    long combination(int n, int r) {
        if (n < 0 || r < 0 || n < r) {
            return 0;
        }
        return mod(permutation(n, r) * invFact[r]);
    }

    /**
     * 重複組合せ nHr （同次積）
     * nHr = (n+r-1)Cr
     * 異なるn個のものから重複を許してr個取る組合せの総数
     * 例：
     * リンゴ，ミカン，牛肉の3種類の果物があります．これらの中から6個の食材を買って帰ります．
     * このとき，何通りの買い方がありますか？ただし，含まれない食材があってもよいものとします
     *
     * @param n
     * @param r
     * @return
     */
    long homogeneousProduct(int n, int r) {
        return combination((n - 1) + r, r);
    }

    /**
     * 多項係数
     * 文字aをp個,bをq個,cをr個, dをs個 あわせてn個を1列に並べるときの並べ方
     * n! / p!・q!・r!・s!
     *
     * @param n
     * @param strNum
     * @param mod
     * @return
     */

    /**
     * フェルマーの小定理を用いて逆元を求める。
     * ある数xのmod p（pは素数）の逆数x'はx' = x^(p-2)
     * 繰り返し二乗法を用いて計算する。
     * http://satanic0258.hatenablog.com/entry/2016/04/29/004730
     * {@link BigInteger#modInverse(BigInteger)}とどちらが速いか？
     *
     * @param x
     * @return
     */
    private long modInverse(long x, long mod) {
        long res = 1L;
        long k = mod - 2L;
        long y = x;
        while (k != 0) {
            if (k % 2 != 0) {
                res = (res * y) % mod;
            }
            y = (y * y) % mod;
            k /= 2;
        }
        return res;
    }

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

    private void skipUnprintable() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr]))
            ptr++;
    }

    public boolean hasNext() {
        skipUnprintable();
        return hasNextByte();
    }

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

    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 long min(long... v) {
        long min = Long.MAX_VALUE;
        for (long i : v) min = Math.min(min, i);
        return min;
    }

    public long max(long... v) {
        long max = Long.MIN_VALUE;
        for (long i : v) max = Math.max(max, i);
        return max;
    }
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        int a = stdIn.nextInt() ;
        String S = stdIn.next();
        char C1,C2,C3 ;
        int count = 0;

        for (int i = 0; i < a; i++) {
            C1 = S.charAt(i) ;
            for (int j = i+1; j <a; j++) {
                C2 = S.charAt(j);
                if(C1 == C2) ;
                else{
                    for (int k = j+1; k <a; k++) {
                        //  System.out.println(i+" "+j+" "+k);
                        C3 = S.charAt(k);
                        if( C2 == C3 || C1 == C3);
                        else if(j-i == k-j ) ;
                        else {
                            count++ ;
                        }
                        // System.out.println("GET " +i+" "+j+" "+k);
                    }
                }
            }
        }

        System.out.println(count);
    }
}


import java.util.*;

public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      	int n = sc.nextInt();
      	String ss = sc.next();
      	char[] s = ss.toCharArray();
      	long ans = 0;
      	for(int i = 0; i < n-2 ; i++){
        	for(int j = i+1 ; j < n-1 ; j++){
              	if(s[i]==s[j]) continue;
              	for(int k = j+1 ; k < n ; k++){
                 	if(s[j]!=s[k] && s[k]!=s[i]  && j-i != k-j) ans++;
                }
            }
        }
      	System.out.println(ans);
    }
}


import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        String S = sc.next();

        List<Integer> R = new ArrayList<>();
        List<Integer> G = new ArrayList<>();
        List<Integer> B = new ArrayList<>();

        for (int i = 0; i < S.length(); i++) {
            char  c = S.charAt(i);
            if(c=='R'){
                R.add(i);
            }else if (c=='G'){
                G.add(i);
            }else{
                B.add(i);
            }
        }

        int ans = 0;
        for (int i :R) {
            for (int j :G) {
                for (int k :B) {
                    if ( (i<j && j <k) ||(i>j && j >k)) {
                        if(i+k != 2 *j){
                             ans++;
                        }
                    }else if( (i <k &&  k <j) ||  (i >k &&  k >j)){
                        if(i+j != 2 *k){
                            ans++;
                        }
                    }else if( (j < i &&  i <k) ||  (j >i &&  i >k)){
                        if(j+k != 2 *i){
                            ans++;
                        }

                    }
                }
            }
        }
        System.out.println(ans);
    }

}


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

		String s = sc.next();
        int ans = 0;
        int r = 0;
        int g = 0;
        int b = 0;
        for(int i=0;i<a;i++){if(s.charAt(i)=='R'){r++;}
                             if(s.charAt(i)=='G'){g++;}
                             if(s.charAt(i)=='B'){b++;}
                            }
        int dr = r;
        int dg = g;
        int db = b;
        if(a>3){for(int i=0;i<a-3;i++){if(s.charAt(i)=='R'){r--;}if(s.charAt(i)=='G'){g--;}if(s.charAt(i)=='B'){b--;}
                                       dr = r;dg = g; db = b;
               for(int j=i+1;j<a-1;j++){if(s.charAt(i)=='R'){
                 if(s.charAt(j)=='G'){ans+=db;if(2*j-i<a && s.charAt(2*j-i)=='B'){ans--;}dg--;}
               if(s.charAt(j)=='B'){ans+=dg;if(2*j-i<a && s.charAt(2*j-i)=='G'){ans--;}db--;}
               if(s.charAt(j)=='R'){dr--;}
                                                            }
                                        if(s.charAt(i)=='G'){
                 if(s.charAt(j)=='R'){ans+=db;if(2*j-i<a && s.charAt(2*j-i)=='B'){ans--;}dr--;}
               if(s.charAt(j)=='B'){ans+=dr;if(2*j-i<a && s.charAt(2*j-i)=='R'){ans--;}db--;}
               if(s.charAt(j)=='G'){dg--;}
                                                            }

                                       if(s.charAt(i)=='B'){
                 if(s.charAt(j)=='G'){ans+=dr;if(2*j-i<a && s.charAt(2*j-i)=='R'){ans--;}dg--;}
               if(s.charAt(j)=='R'){ans+=dg;if(2*j-i<a && s.charAt(2*j-i)=='G'){ans--;}dr--;}
               if(s.charAt(j)=='B'){db--;}
                                                           }
                                       }
                                      }
              }

		System.out.println(ans);
	}
}


import java.util.Scanner;

public class Main {
	public static boolean Triple(String S, int i, int j, int k) {
		if ((S.charAt(i) != S.charAt(j)) && (S.charAt(j) != S.charAt(k)) && (S.charAt(k) != S.charAt(i))) {
			if (j - i != k - j) {
				return true;
			}
		}
		return false;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		String S = sc.next();
		int i, j, k;
		long sum = 0;
		for (i = 0; i < N; i++) {
			for (j = 0; j < i; j++) {
				for (k = 0; k < j; k++) {
					if (Triple(S, i, j, k) == true) {
						sum++;
					}
				}
			}
		}

		System.out.println(sum);
	}
}


import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        String S = sc.next();

        List<Integer> R = new ArrayList<>();
        List<Integer> G = new ArrayList<>();
        List<Integer> B = new ArrayList<>();

        for (int i = 0; i < S.length(); i++) {
            char  c = S.charAt(i);
            if(c=='R'){
                R.add(i);
            }else if (c=='G'){
                G.add(i);
            }else{
                B.add(i);
            }
        }

        int ans = 0;
        for (int i :R) {
            for (int j :G) {
                for (int k :B) {
                    int[] aa= new int[3];
                    aa[0]=i;
                    aa[1]=j;
                    aa[2]=k;
                    Arrays.sort(aa);
                   if(aa[1]-aa[0] != aa[2]-aa[1])
                    ans ++;
                }
            }
        }
        System.out.println(ans);
    }

    static int gcd(int a, int b ){
        if (a == 0)
            return b;

        return gcd(b%a, a);
    }

}


import java.util.*;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String s = sc.next();

        long r = 0;
        long g = 0;
        long b = 0;

        for(int i = 0 ; i < s.length() ; i++){
            switch (s.charAt(i)){
                case 'R':
                    r++;
                    break;
                case 'G':
                    g++;
                    break;
                case 'B':
                    b++;
                    break;
            }
        }

        long sum = r*g*b;

        for(int i = 0 ; i < n ; i++){
            for(int d = 1 ; i + 2*d < n ; d++){
                if(s.charAt(i) != s.charAt(i+d) && s.charAt(i+d) != s.charAt(i+2*d) && s.charAt(i+2*d) != s.charAt(i)){
                    sum--;
                }
            }
        }

        System.out.println(sum);
    }
}


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

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

		// 入力
		int n = sc.nextInt();
		String s = sc.next();

		// 計算
		long result = 0;
		int[] sumR = new int[n+1];
		int[] sumG = new int[n+1];
		int[] sumB = new int[n+1];
		for(int i = 0; i < n; i++){
		    char a = s.charAt(i);
		    if(a == 'R'){
		        sumR[i+1] += sumR[i] + 1;
		        sumG[i+1] += sumG[i];
		        sumB[i+1] += sumB[i];
		    }
		    if(a == 'G'){
		        sumR[i+1] += sumR[i];
		        sumG[i+1] += sumG[i] + 1;
		        sumB[i+1] += sumB[i];
		    }
		    if(a == 'B'){
		        sumR[i+1] += sumR[i];
		        sumG[i+1] += sumG[i];
		        sumB[i+1] += sumB[i] + 1;
		    }
		}
		for(int i = 0; i < n; i++){
		    for(int j = i+1; j < n; j++){
		        char a = s.charAt(i);
		        char b = s.charAt(j);
		        if(a == b) continue;
		        if((a == 'R' && b == 'G') || (a == 'G' && b == 'R')){
		            result += sumB[n]-sumB[j+1];
		        }
		        if((a == 'G' && b == 'B') || (a == 'B' && b == 'G')){
		            result += sumR[n]-sumR[j+1];
		        }
		        if((a == 'B' && b == 'R') || (a == 'R' && b == 'B')){
		            result += sumG[n]-sumG[j+1];
		        }
		        if(j-i+j < n && s.charAt(j-i+j) != a && s.charAt(j-i+j) != b) result--;
		    }
		}

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

}


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

	private void solve()throws IOException
	{
		int n=nextInt();
		char s[]=(" "+nextLine()).toCharArray();
		int freq[]=new int[128];
		for(int i=1;i<=n;i++)
			freq[s[i]]++;
		long ans=1l*freq['R']*freq['G']*freq['B'];
		for(int i=1;i<=n;i++)
		{
			for(int j=1;i-j>=1 && i+j<=n;j++)
				if(s[i]!=s[i-j] && s[i]!=s[i+j] && s[i-j]!=s[i+j])
					ans--;
		}
		out.println(ans);
	}

	///////////////////////////////////////////////////////////

	public void run()throws IOException
	{
		br=new BufferedReader(new InputStreamReader(System.in));
		st=null;
		out=new PrintWriter(System.out);

		solve();

		br.close();
		out.close();
	}
	public static void main(String args[])throws IOException{
		new Main().run();
	}
	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	String nextToken()throws IOException{
		while(st==null || !st.hasMoreTokens())
		st=new StringTokenizer(br.readLine());
		return st.nextToken();
	}
	String nextLine()throws IOException{
		return br.readLine();
	}
	int nextInt()throws IOException{
		return Integer.parseInt(nextToken());
	}
	long nextLong()throws IOException{
		return Long.parseLong(nextToken());
	}
	double nextDouble()throws IOException{
		return Double.parseDouble(nextToken());
	}
}


import java.util.*;

public class Main{
	public static void main(String args[]){

		Scanner sc = new Scanner(System.in);

		String a = sc.next();
		String b = sc.next();
		String c = sc.next();

		System.out.println(b+a+c);
		System.out.println(c+b+a);
	}
}

Score : 100 points

Problem Statement

We have three boxes A, B, and C, each of which contains an integer.

Currently, the boxes A, B, and C contain the integers X, Y, and Z, respectively.

We will now do the operations below in order. Find the content of each box afterward.

Swap the contents of the boxes A and B

Swap the contents of the boxes A and C

Constraints

1 \leq X,Y,Z \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the integers contained in the boxes A, B, and C, in this order, with space in between.

Sample Input 1

1 2 3

Sample Output 1

3 1 2

After the contents of the boxes A and B are swapped, A, B, and C contain 2, 1, and 3, respectively.

Then, after the contents of A and C are swapped, A, B, and C contain 3, 1, and 2, respectively.

Sample Input 2

100 100 100

Sample Output 2

100 100 100

Sample Input 3

41 59 31

Sample Output 3

31 41 59


import java.util.Scanner;

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

		// 入力値の取得
		int X = sc.nextInt();
		int Y = sc.nextInt();
		int Z = sc.nextInt();

		// 結果の出力
		System.out.println(Z + " " + X + " " + Y);

		sc.close();
	}
}


import java.util.*;
import static java.lang.System.out;

public class Main {
    public static void main(String... args) {
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());
        int c = Integer.parseInt(sc.next());
        int tmp = 0;
        tmp = a;
        a = b;
        b = tmp;
        tmp = a;
        a = c;
        c = tmp;
        System.out.println(a + " " + b + " " + c);

    }
}


import java.util.Scanner;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    sc.close();

    int tmp = a;
    a = b;
    b = tmp;

    tmp = a;
    a = c;
    c = tmp;

    System.out.println(a + " " + b + " " + c);

  }
}


import java.util.Scanner;

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

        System.out.println("input 1st integer");
        int a = sc.nextInt();
        System.out.println("input 2nd integer");
        int b = sc.nextInt();
        System.out.println("input 3rd integer");
        int c = sc.nextInt();

        temp = a;
        a = b;
        b = temp;

        temp = a;
        a = c;
        c = temp;

        System.out.println(a + " " + b + " " + c);
    }
}


import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int x = sc.nextInt();
    int y = sc.nextInt();
    int z = sc.nextInt();
    System.out.println(z+" "+x+" "+y);
  }
}


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        System.out.println(c + " " + a + " " + b);
    }

}

// x * 3 <= 16


import java.util.*;
    public class Main {
    	public static void main(String[] args){
    		Scanner sc = new Scanner(System.in);
    		int a = sc.nextInt();
    		int b = sc.nextInt();
    		int c = sc.nextInt();
    		System.out.print(c);
          System.out.print(a);
          System.out.print(b);
    	}
    }


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 * 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;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        abc161_a solver = new abc161_a();
        solver.solve(1, in, out);
        out.close();
    }

    static class abc161_a {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int A = in.nextInt();
            int B = in.nextInt();
            int C = in.nextInt();
            int Wk;

            Wk = A;
            A = B;
            B = Wk;
            Wk = C;
            C = A;
            A = Wk;
            out.print(A);
            out.print(' ');
            out.print(B);
            out.print(' ');
            out.print(C);
            out.println();
        }

    }
}


import java.util.*;
public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int temp = A;
		A = B;
		B = temp;
		temp = A;
		A = C;
		C = temp;
		System.out.println(A+" "+B+" "+C);

	}

}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        int z = sc.nextInt();
        System.out.println(z + " " + x + " " + y);
    }
}


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

public class Main {
	public static void main(String[] args) {
		FastScanner sc = new FastScanner(System.in);
		int x = sc.nextInt(), y = sc.nextInt(), z = sc.nextInt();
		System.out.println(z + " " + x + " " + y);
	}

	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 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 long[] nextLongArray(int n) {
	        long[] a = new long[n];
	        for (int i = 0; i < n; i++)
	            a[i] = nextLong();
	        return a;
	    }
	}
}


import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int m = sc.nextInt();
      int a;
      int sum = 0;
      int count = 0;
      for(int i = 0;i<n;i++){
         a = sc.nextInt();
         sum += a;
         if(a>=sum/4/m){
            count++;
         }
      }
      if(count<m){
         System.out.println("No");
      }else{
         System.out.println("Yes");
      }
	}
}

Score : 200 points

Problem Statement

We have held a popularity poll for N items on sale. Item i received A_i votes.

From these N items, we will select M as popular items. However, we cannot select an item with less than \dfrac{1}{4M} of the total number of votes.

If M popular items can be selected, print Yes; otherwise, print No.

Constraints

1 \leq M \leq N \leq 100

1 \leq A_i \leq 1000

A_i are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_N

Output

If M popular items can be selected, print Yes; otherwise, print No.

Sample Input 1

4 1
5 4 2 1

Sample Output 1

Yes

There were 12 votes in total. The most popular item received 5 votes, and we can select it.

Sample Input 2

3 2
380 19 1

Sample Output 2

No

There were 400 votes in total. The second and third most popular items received less than \dfrac{1}{4\times 2} of the total number of votes, so we cannot select them. Thus, we cannot select two popular items.

Sample Input 3

12 3
4 56 78 901 2 345 67 890 123 45 6 789

Sample Output 3

Yes


import java.util.*;

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

      	int N  =sc.nextInt();
       	int M  =sc.nextInt();

          List<Integer> n = new ArrayList<>();

          for(int i=0; i<N; i++){
          n.add(sc.nextInt());
          }

          int sum = 0;
          for(int i=0; i<N; i++){
          sum += n.get(i);
          }

          double a = sum/(4*M);

          int count = 0;
          for(int i=0; i<N; i++){
            if(n.get(i)>=a){
            count++;
            }

          }
          if(M<=count){
          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 shurui = sc.nextInt();  //商品の種類数
		double select = sc.nextInt();  //選びたい商品数
		double sum = 0;  //総投票数
		int shouhin[] = new int[shurui];  //商品配列

//	お菓子1つずつに得票数を記入
		for(int i=0 ; i<shurui ; i++) {
			int tokuhyou = sc.nextInt();
			shouhin[i] += tokuhyou;
			sum += tokuhyou;
		}

//	総投票数がわかったので基準値を求める
		double kijun = Math.ceil(sum/(4*select));

//	並列の中身を開けてみて、選べる商品をカウント
		int count = 0;
		for(int i : shouhin) {
			if(i >= kijun) {
				count ++;
			}
		}

//	選べる商品がM個以上あるかどうか
		System.out.println(count >= select ? "Yes" : "No");
	}
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int N, M;
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
        int[] a = new int[N];
        double check = 0;
        for(int i = 0 ; i < N ; i++)
        {
            a[i] = sc.nextInt();
            check += a[i];
        }
        check *= 1/(4F*M);
        int count = 0;
        for(int i = 0 ; i < N ; i++)
        {
            count += (a[i] >= (int)check) ? 1 : 0;
        }
        System.out.println(count >= M ? "Yes" : "No");
    }
}


import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int M = sc.nextInt();
    int[] A = new int[N];
    int check =0, total =0;
    for (int i=0; i<N; i++) {
      A[i] = sc.nextInt();
      total += A[i];
    }
    for (int i=0; i<N; i++) {
      if (A[i] >= total / (4 * M)) {
        check++;
      }
    }
    if (check >= M) {
      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();
    double M = sc.nextDouble();

    int[]list = new int[N];
    int all=0;
    for(int i=0; i<N; i++){
	  list[i] = sc.nextInt();
      all += list[i];
}
    /*System.out.println(all);
    System.out.println(all/4/M);
	System.out.println(list[0]);*/

      int count =0;
      for(int j=0; j<N; j++){
        if(list[j] >= all/4/M){
         count++;
        }
      }
		if(count >= M){
      System.out.println("Yes");
        }else{
      System.out.println("No");
        }
}
}


import java.util.*;
class Main{
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args){
        int N = sc.nextInt();
        int M = sc.nextInt();
        int a[] = new int[N];
      	int sum = 0;
        for(int i=0;i<N;i++){
            a[i] = sc.nextInt();
          	sum = sum+a[i];
        }
      	Arrays.sort(a);
        boolean anst = true;
        for(int i=(N-1);i>=(N-M-1);i--){
            if(a[i]<(sum/(4*M))){
                anst = false;
                break;
            }
        }
       if(anst)
           System.out.println("Yes");
        else
            System.out.println("No");
    }
}


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

public class Main {

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int[] arr = new int[n];
        int sum = 0;

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
            sum += arr[i];
        }
        int count = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] >= 1 / (4 * m)) {
                count++;
            }
        }
        System.out.println(count > m ? "Yes" : "No");

    }
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt();
		int m = kb.nextInt();
		int p = m;
		int[] a = new int[n];
		int count = 0;
		for (int i = 0; i < n; i++) {
			int j = kb.nextInt();
			count += j;
			a[i] = j;
		}
		for (int b : a) {
			if (count / (4 * m) <= b) {
				p -= 1;
			}
		}
		if (p <= 0) {
			System.out.println("Yes");
		} else {
			System.out.println("No");
		}

		kb.close();
	}

}


import java.util.Scanner;
class Main {
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    int n , m;
    n = sc.nextInt();
    m = sc.nextInt();
    int[] A = new int[n];
    int i , counter;
    counter = 0;
    for(i=0; i < n ;i++){
      A[i] = sc.nextInt();
      if(A[i] / 4 / m < 0){
        counter++;
      }
    }
    if(counter <= m){
      System.out.println("Yes");
    }else{
      System.out.println("No");
    }
  }
}


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Arrays;
    import java.util.stream.IntStream;

    public class Main {

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

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String [] line = br.readLine().split(" ");
            int n = Integer.parseInt(line[0]);
            int m = Integer.parseInt(line[1]);

            String [] votes = br.readLine().split(" ");
            int [] voteInt = new int[n];
            int sum = IntStream.of(voteInt).sum();

            int min = sum/(4*m);
            if(voteInt[n-m-1] >= min){
                System.out.println("Yes");
            }
            else{
                System.out.println("No");
            }
        }
    }


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.lang.Integer;
import java.util.HashMap;

public class Main {
    // driver function to test the above functions
    public static void main(String args[]) throws IOException {
        Reader.init(System.in);
        int n = Reader.nextInt();
        int m = Reader.nextInt();
        int[] arr = new int[n];

        double sum  = 0;
        for (int i=0; i<n; i++){
            arr[i] = Reader.nextInt();
            sum = sum+arr[i];
        }
        sum = (double)(sum/(4*m));
        int ans=0;
        for (int i=0; i<n; i++){
            int val = arr[i];
            if (val>=sum){
                ans++;
            }
        }
        if (ans>=m) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }

    }
}

class Node{
    char val;
    Node nxt = null;
    Node prev=null;

}

class solu{

}

/** Class for buffered reading int and double values */
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call this method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }

    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }

    static String line() throws IOException {
        return reader.readLine();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // write your code here

        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt(), m = in.nextInt();
        int total = 0;
        int[] votes = new int[n];
        for (int i = 0; i < n; i++) {
            votes[i] = in.nextInt();
            total += votes[i];
        }

        int criteria = total / (4 * m);
        int count = 0;
        for (int vote: votes) {
            if (vote >= criteria)count++;
        }

        out.println(count >= m ? "YES": "NO");
        out.close();

    }
}


import java.util.Arrays;
import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author wnqmw
 */
public class Main {
    public static void main(String args[]){
        Scanner kb = new Scanner(System.in);
        int nums = kb.nextInt(), sel = kb.nextInt();

        int[] num = new int[nums];

        int total = 0, totalvotes = 0;
        for(int i = 0; i < nums; i++){
            num[i] = kb.nextInt();
            totalvotes += num[i];

        }
        Arrays.sort(num);
        for(int i = nums-1; i >= 0; i--){
            if(num[i] >= totalvotes/(4*sel)){
                total++;
                if(total == sel){
                    break;
                }
            }
        }
        if(total < sel){
            System.out.println("No");
        }else{
            System.out.println("Yes");
        }
    }
}


import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;

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

		// 入力値の取得
		int N = sc.nextInt();
		int M = sc.nextInt();

		double sum = 0;
		PriorityQueue<Integer> pq = new PriorityQueue<> (Collections.reverseOrder());

		for (int i = 0; i < N; i++) {
			int Ai = sc.nextInt();
			sum += Ai;
			pq.add(Ai);
		}

		double threshold = sum / (4 * M);

		boolean result = true;
		for (int i = 0; i < M; i++) {
			if (pq.poll() < threshold) {
				result = false;
				break;
			}
		}
		// 結果の出力
		System.out.println(result?"Yes":"No");

		sc.close();
	}

}


import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] a = new int[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }
        int sum = 0;
        for(int i = 0; i < n; i++){
            sum = sum + a[i];
        }
        double border = sum/(4*m);
        Arrays.sort(a);
        int count = 0;
        for(int i = 1; i < n-1; i++){
            if(a[n-i] >= border){
                count++;
            } else {
                break;
            }
        }
        if(count >= m){
            System.out.print("Yes");
        } else {
            System.out.print("No");
        }
    }
}


import java.util.*;
import java.math.*;

public class Main {

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

    int[] a = new int[n];

    long sum = 0;

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

    int count = 0;
    double k = (double)1/(4*m);

    for (int i = 0; i < n; i++) {
      if (a[i] >= k*sum) {
        count++;
      }
      if (count == m) {
        System.out.println("Yes");
        return;
      }
    }

    System.out.println("No");

  }

}


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

     	String[] sep = sc.nextLine().split(" ");
      	int n = Integer.parseInt(sep[0]);
      	int m = Integer.parseInt(sep[1]);
      	int[] bigger = new int[n];
      	String[] sepa = sc.nextLine().split(" ");
      	int sumVote = 0;
      	for(int i = 0; i < n; i++)
        {
         	bigger[i] =  Integer.parseInt(sepa[i]);
          	sumVote += bigger[i];
        }
      	int judge = 0;
      	for(int i = 0; i < n; i++)
        {
          	int amari = sumVote % (4 * m);
            if(bigger[i] > sumVote / (4 * m))
            {
                  judge++;
            }else if(bigger[i] == sumVote / (4 * m) && sumVote % (4 * m) == 0){
             	 judge++;
            }
        }

      if(judge >= m)
      {
        	System.out.print("Yes");
      }else{
        	System.out.print("No");
      }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    private static int MOD = 1000000007;

    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String str = br.readLine();
        String[] split = str.split(" ");
        int n = Integer.parseInt(split[0]);
        int m = Integer.parseInt(split[1]);

        split = br.readLine().split(" ");

        int total = 0;

        List<Integer> hyous = new ArrayList<>();
        for (String s : split) {
            int hyou = Integer.parseInt(s);
            total += hyou;
            hyous.add(hyou);
        }

        hyous.sort(Collections.reverseOrder());

        int ikichi = total / (4 * m);
        int amari = total % (4 * m);
        if(amari != 0) {
            ikichi++;
        }
        for (int i = 0; i < m; i++) {
            int hyou = hyous.get(i);
            if (hyou < ikichi) {
                System.out.println("No");
                return;
            }
        }

        System.out.println("Yes");

    }
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

//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);
//ArrayList<String> cc = new ArrayList<>(n);
//Collections.sort(list);
//Array.sort(list);
//Arrays.asList(c).contains("a")

public class Main {
	private static Scanner sc = new Scanner(System.in);
	static void p(String ans) {System.out.println(ans);};
	static void p(int ans) {System.out.println(ans);};
	static void p(long ans) {System.out.println(ans);};
	static void p(double ans) {System.out.println(ans);};
	//static String eikomoji[]={"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"};
	public static void main(String[] args) {
	int N=sc.nextInt();
	int M=sc.nextInt();
	ArrayList<Integer> cc = new ArrayList<>(N+1);
	long wa=0;
	for(int i=0;i<N;i++) {
		cc.add(sc.nextInt());
		wa+=cc.get(i);
	}
	Collections.sort(cc,Collections.reverseOrder());
	int ctn=0;
	boolean a=true;
	for(int i=0;i<cc.size();i++) {
		if(cc.get(i)>=(wa/(4*M))) {
		ctn++;
		}
		if(ctn==M) {
			p("Yes");
			a=false;
			break;
		}
	}
	if(a) {
		p("No");
	}
	//if() {}

	}
}


import java.util.*;

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

      	Integer a[] = new Integer[N];
      	for (int i=0; i<N; i++) {
            a[i] = sc.nextInt();
        }
        Arrays.sort(a, Comparator.reverseOrder());

      	double sum = 0;
        for (int i=0; i<N; i++) {
          sum += a[i];
        }

      	double sum2 = sum * 1 / (4 * M);

      	System.out.println(sum2);
      	int i2 = 0;
      	while(a[i2] >= sum2) {
          i2++;
        }

      	if (i2 >= M)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


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

/* Name of the class has to be "Main" only if the class is public. */
class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		try {
		    Scanner sc=new Scanner(System.in);
		    int n=sc.nextInt();
		    int m=sc.nextInt();
		    int sum=0;
		    int ar[]=new int[n];
		    for(int i=0;i<n;i++)
		    {
		        ar[i]=sc.nextInt();
		        sum+=ar[i];
		    }
		    sum=sum/(4*m);
		    int c=0;
		    for(int i=0;i<n;i++)
		    {
		        if(ar[i]>=sum)
		        c++;
		        if(c==m)
		        break;
		    }
		    if(c<m)
		    System.out.println("No");
		    else
		    System.out.println("Yes");

		} catch(Exception e) {
		}
	}
}


import java.util.*;

public class Main {

	static void log(int i) {
		System.out.println(i);
	}
	static void log(String s) {
		System.out.println(s);
	}

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

		int n = sc.nextInt();
		int m = sc.nextInt();
		int ary[] = new int[n];
		long total=0;

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

		int aryPick[] = new int[m];

		// loop for input data
		int ctr=0;
		for (int j =0; j<n && ctr < m ; j++) {
			if ( (4 * m * ary[j]) >= total) {
				aryPick[ctr]=ary[j];
				ctr++;
			}
		}

		sc.close();
		log( (ctr<m) ? "No" : "Yes");
	}
}


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int sum = 0;
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            sum += a[i];
        }
        int v = sum / (4 * m);
        int count = 0;
        for (int i = 0; i < n; i++) {
            if(count >= m){
                break;
            }
            if(a[i] >= v){
                count++;
            }
        }
        if(count < m){
            System.out.println("No");
        }else{
            System.out.println("Yes");
        }
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int[] a = new int[n];
        int sum = 0;
        int b = 0;
        for(int i = 0; i < n; i++){
            a[i] = scan.nextInt();
            sum += a[i];
        }
        for(int i = 0; i < n; i++){
            if(a[i] >= sum / (4 * m) && sum % (4 * m) == 0){
                b++;
            }else if(a[i] > sum / (4 * m)){
                b++;
            }
        }
        if(b >= m){
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

class Solver {
    public void Solve() {
        int n = sc.nextInt();
        int m = sc.nextInt();
        int a[] = new int[n];
        long wa = 0;
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            wa += a[i];
        }
        Arrays.sort(a);
        double dd = wa * 1.0 / (4.0 * m);
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] >= dd) {
                cnt++;
            } else {

            }
        }
        if (cnt >= m) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }

    }

    Scanner sc;

    Solver(Scanner in) {
        this.sc = in;
    }
}

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


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

public class Main implements Runnable{

	public static void main(String[] args) {
		new Thread(null,new Main(), "" ,Runtime.getRuntime().maxMemory()).start();
	}

	public void run() {
		FastScanner sc=new FastScanner();
		int N=sc.nextInt();
		int M=sc.nextInt();
		int[] A=new int[N];
		for(int i=0;i<N;++i) {
			A[i]=-sc.nextInt();
		}
		Arrays.sort(A);
		for(int i=0;i<N;++i)A[i]*=-1;
		int sum=Arrays.stream(A).sum();
		int p=0;
		while(p<N&&(long)(4*M)*A[p]>=sum)++p;
		System.out.println(p>=M?"Yes":"No");
	}

	void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));}
}

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 static java.lang.System.*;

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

public class Main {

	public void solve() {

		int N = nextInt();
		int M = nextInt();
		int[] A = new int[N];
		int total = 0;
		for(int i=0;i<N;i++) {
			A[i] = nextInt();
			total += A[i];
		}
		int x = total/(4*M);
		int count = 0;
		for(int i=0;i<N;i++) {
			if(A[i]>x) {
				count++;
			}
		}
		String result = count >= M ? "Yes" :"No";
		out.println(result);
	}

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

	private void skipUnprintable() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
	}

	public boolean hasNext() {
		skipUnprintable();
		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() {
		return (int) nextLong();
	}

	public int gcd(int a, int b) {
		return b == 0 ? a : gcd(b, a % b);
	}

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

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

	double sqrt(double x) {
		return Math.sqrt(x);
	}

	double pow(double x, double y) {
		return Math.pow(x, y);
	}
	int max(int a,int b) {
		return Math.max(a, b);
	}

	public static void main(String[] args) {
		Main m = new Main();

		m.solve();
	}

	public void println(Object o) {
		out.println(o);
	}

}