akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<vector>
#include<algorithm>

struct unit{
	int x;
	int type;
};

int main(){
	int n;
	long long ans=1;
	long long mod= 1000000007;
	std::vector<unit> list;

	std::cin>>n;
	list.resize(2*n);

	for(int i=0; i<n; i++){
		std::cin>>list[i].x;
		list[i].type= 1;
	}

	for(int i=0; i<n; i++){
		std::cin>>list[n+i].x;
		list[n+i].type= 2;
	}

	std::sort(list.begin(), list.end(), [](const unit &a, const unit &b){return a.x< b.x;});

	int stackSize= 0;
	int elmType= 0;

	for(int i=0; i< 2*n; i++){
		if(elmType==0){
			stackSize++;
			elmType= list[i].type;
			continue;
		}

		if(elmType==list[i].type)
			stackSize++;

		if(elmType!=list[i].type){
			ans= (ans*stackSize) %mod;
			stackSize--;

			if(stackSize<=0)
				elmType=0;
		}
	}


	std::cout<< ans <<std::endl;

	return 0;
}

Measured memory_bytes: 1792
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

long int modpow(long int a,long int n,long int mod){
  long int ans=1;
  while(n>0){
    if(n & 1){
      ans=ans*a%mod;
    }
    a=a*a%mod;
    n/=2;
  }
  return ans;
}

long int modinv(long int a,long int mod){
  return modpow(a,mod-2,mod);
}


/****************************************\
| Thank you for viewing my code:)        |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka                  | 
\****************************************/

int main(void){
  /*
  解法2
  (答え)=((総和の2乗)-(2乗の総和))/2
  ということがわかるので，それを実装

  2で割る部分は解法1と同じ

  */
  long int n;
  scanf("%ld",&n);

  long int A[n];
  
  //総和
  long int all=0;
  
  //2乗の総和
  long int sq_all=0;

  long int MOD=1e9+7;

  for(int i=0;i<n;i++){
    scanf("%ld",&A[i]);
    all+=A[i];

    //常にMODを取る！
    all%=MOD;

    sq_all+=modpow(A[i],2,MOD);
    
    //常にMODを取る！
    sq_all%=MOD;
  }


  //MOD上の2の逆元
  long int v=modinv(2,MOD);

  long int ans=modpow(all,2,MOD);
  ans-=sq_all;
  ans+=MOD;
  ans%=MOD;

  //常にMODを取る！
  printf("%ld\n",ans*v%MOD);

  return 0;
}

Measured memory_bytes: 3232
#include <stdio.h>
#include <math.h>

#define PI 3.1415926535897932

int main(){
    int a, b, x;
    
    scanf("%d%d%d", &a, &b, &x);

    if(2.0 * x / (a * b) <= a){
        printf("%lf\n", atan((double) a * b * b / (2 * x)) * 180 / PI);
    }else{
        printf("%lf\n", atan(2.0 * (a * a * b - x) / (a * a * a)) * 180 / PI);
    }
}

Measured memory_bytes: 128
#include<stdio.h>

int main(void)
{
	int a, b, c;
	int i = 0;
	int num = 0;
	int yak = 7;
	
	scanf("%d %d %d", &a, &b, &c);
	
	while(num<c){
		i++;
		num += a;
		if(i==yak){
			yak += 7; 
			num += b;}}
	
	printf("%d\n", i);
}


Measured memory_bytes: 2112
n = int(input())
L = list(map(int, input().split()))
d = {}
for a in L:
    if a not in d:
        d[a] = 1
    else:
        d[a] += 1
cnt = 0
for k, v in d.items():
    if v > 1:
        cnt += v * (v - 1) // 2
for a in L:
    if d[a] >= 2:
        print(cnt - d[a] * (d[a] - 1) // 2 + ((d[a] - 1) * (d[a] - 2)) // 2)
    else:
        print(cnt)


Measured memory_bytes: 24748
N = gets.to_i
A = gets.split(' ').map(&:to_i)
B = gets.split(' ').map(&:to_i)
C = gets.split(' ').map(&:to_i)
s = 0
A.each_with_index do |a, i|
  s += B[a - 1]
  s += C[a - 2] if i > 0 && a == A[i - 1] + 1
end

puts s

Measured memory_bytes: 1788
#include<iostream>
#include<cstring>

using namespace std;

int main()
{
	int K, T;
	cin >> K >> T;
	int *a = new int[T];
	int max = 0;
	for (int i = 0; i < T; i++)
	{
		cin >> a[i];
		if (a[i] > max)
			max = a[i];
	}
	if (max > (K - max + 1))
	{
		cout << max - (K - max + 1) << endl;
	}
	else
		cout << 0 << endl;
	return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int MAX_N=100009;
int tree[3*MAX_N];
int inv=INT_MAX;

int N,Q;

void init(int _N,int iniv)
{
	for(N=1;N<=_N;)
		N*=2;
	for(int i=0;i<2*N-1;i++)
		tree[i]=iniv;
}

void update(int p,int a)
{
	p+=N-1;
	tree[p]=a;
	for(;p>0;)
	{
		p=(p-1)/2;
		tree[p]=min(tree[p*2+1],tree[p*2+2]);
	}
}

int query(int l,int r,int k,int nl,int nr)
{
	if(nr<=l||r<=nl)return inv;
	if(l<=nl&&nr<=r)return tree[k];
	 
	int vl=query(l,r,k*2+1,nl,(nl+nr)/2);
	int vr=query(l,r,k*2+2,(nl+nr)/2,nr);
	return min(vl,vr);
}


int main()
{
	scanf("%d%d",&N,&Q);
	init(N,inv);
	for(int i=0;i<Q;i++)
	{
		int con,x,y;
		scanf("%d%d%d",&con,&x,&y);
		if(!con)
			update(x,y);
		else
			printf("%d\n",query(x,y+1,0,0,N));
	}
}

Measured memory_bytes: 2212
#include <iostream>

using namespace std;

int func(int x) {
    if (x % 2) return 0;
    return 1 + func(x / 2);
}

int main() {
    int x, y, z;
    cin >> x >> y >> z;
    if (x % 2 || y % 2 || z % 2) cout << 0 << endl;
    else if (x == y && y == z && z == x)cout << -1 << endl;
    else {
        int ans = func(y + z);
        ans = min(ans, func(x + z));
        cout << min(ans, func(x + y));
    }
    return 0;
}

Measured memory_bytes: 256
n = int(input())
l = [int(input()) for i in range(5)]
l2 = []
for i in range(5):
    if n % l[i] == 0:
        l2.append(n // l[i])
    else:
        l2.append(n // l[i] + 1)
l2.sort(reverse = True)
print(l2[0] + 4)

Measured memory_bytes: 3060
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <stdlib.h>
using namespace std;
queue<char> S[3];
void D(int index){
	if (S[index].size() == 0)
	{
		printf("%c", 'A' + index);
		exit(0);
	}
	char cur = S[index].front();
	S[index].pop();
	D(cur - 'a');
}
int main(){
	for (int j = 0; j < 3; j++)
	{
		char ss[10000] = "";
		scanf("%s", ss);

		for (int i = 0; i < strlen(ss); i++)
		{
			S[j].push(ss[i]);
		}
	}
	D(0);

	return 0;
}

Measured memory_bytes: 256
#!/usr/bin/env python3
import sys
import math

def solve(A: int, B: int, N: int):
    max_b = 0

    k = min(N,B-1)
    # print(k)
    # for x in range(min(N+1,1000000)):
    max_b = math.floor(A*k/B)
    print(max_b)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    N = int(next(tokens))  # type: int
    solve(A, B, N)

if __name__ == '__main__':
    main()


Measured memory_bytes: 62096
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
#include<fstream>
using namespace std;
typedef long long ll;
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

int main()
{
	fast_io;
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int a;
	cin >> a;

	int res = a + (a * a) + (a * a * a);

	cout << res << endl;

	return 0;
}

Measured memory_bytes: 3160
#include <bits/stdc++.h>
#define rep(i ,n) for(int i=0 ; i<n ; i ++ )
using namespace std;

int main() {
 int h, n;
  cin>>h>>n;
  int s=0;
  vector<int> a(n);
  rep(i,n){
    cin>>a.at(i);
    s += a.at(i);
  }
  if(h-s>0){
    cout<<"No"<<endl;
  }
  else{
    cout<<"Yes"<<endl;
  }
  
}




 






Measured memory_bytes: 640
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <array>
#include <vector>
#include <utility>
#include <bitset>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <stack>

using namespace std;

using ll = long long;
using p = pair<ll, ll>;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr ll MAX_N = 100000 + 1;

vector<p> G[MAX_N + 1];
ll Nodes[MAX_N + 1];

void swaping(vector<ll>& v, ll x, ll pos) {
    for (ll i = 1; i < v.size(); i++) {
        if (v[i] == x) {
            swap(v[pos], v[i]);
            return;
        }
    }
}

int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N, x;
    cin >> N >> x;

    if (x == 1 || x == 2 * N - 1) {
        cout << "No" << endl;
        return 0;
    }

    if (N == 2) {
        cout << "Yes" << endl;
        cout << 1 << endl;
        cout << 2 << endl;
        cout << 3 << endl;
        return 0;
    }

    vector<ll> v(2 * N, 0);
    for (ll i = 1; i < 2 * N; i++) {
        v[i] = i;
    }

    if (x == 2) {
        swaping(v, x - 1, N - 1);
        swaping(v, x, N);
        swaping(v, x + 1, N + 1);
        swaping(v, x - 2, N + 2);
    } else {
        swaping(v, x + 1, N - 1);
        swaping(v, x, N);
        swaping(v, x - 1, N + 1);
        swaping(v, x + 2, N + 2);
    }

    cout << "Yes" << endl;
    for (ll i = 1; i < 2 * N; i++) {
        cout << v[i] << endl;
    }

    return 0;
}



Measured memory_bytes: 5376
using System;
using System.Collections;
using System.Collections.Generic;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            var read = Console.ReadLine();
            var split = read.Split(' ');
            int a = (int)split[0][0];
            int b = (int)split[1][0];

            if (a < b) Console.WriteLine("<");
            else if (a == b) Console.WriteLine("=");
            else Console.WriteLine(">");

            

        }
    }
}


Measured memory_bytes: 13140
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin >> n;

    vector<int> a(n);
    for(int i=0; i<n; i++){
        cin >> a[i];
    }

    int ans = 0, current = 1;
    for(int i=0; i<n; i++){
        if(a[i] != current) ans += 1;
        if(a[i] == current) current += 1;
    }
    if(ans == n) cout << -1 << endl; 
    else cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1024
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.stream.IntStream;

/**
 * 解説どおりに作成したソースコード
 */
public class Main {

	public static void main(String[] args) {
		try (Scanner scanner = new Scanner(System.in)) {
			int n = scanner.nextInt();
			@SuppressWarnings("unchecked")
			List<Path>[] pathLists = new List[n];
			IntStream.range(0, n).forEach(i -> pathLists[i] = new ArrayList<>());
			IntStream.range(0, n - 1).forEach(i -> {
				int from = scanner.nextInt() - 1;
				int to = scanner.nextInt() - 1;
				pathLists[from].add(new Path(from, to, 1));
				pathLists[to].add(new Path(to, from, 1));
			});
			// ノード1からの距離の配列
			int[] distances1 = dijkstra(pathLists, 0, n);
			// ノードnからの距離の配列
			int[] distancesn = dijkstra(pathLists, n - 1, n);
			// 黒と白に塗られたマスの数
			int black = 0, white = 0;
			for (int i = 0; i < n; i++) {
				if (distances1[i] <= distancesn[i]) {
					black++;
				} else {
					white++;
				}
			}
			System.out.println((black > white) ? "Fennec" : "Snuke");
		}
	}

	/**
	 * @param pathLists ノード間の連結パス一覧の配列
	 * @param src       開始ノード
	 * @param n         ノードの数
	 * @return 開始ノードからの距離の配列
	 */
	private static int[] dijkstra(List<Path>[] pathLists, int src, int n) {
		int[] distances = new int[n];
		Arrays.fill(distances, Integer.MAX_VALUE);
		// 最短距離が確定したかどうか
		boolean[] fixed = new boolean[n];
		Arrays.fill(fixed, false);
		// 出発地点までの距離を0とする
		distances[src] = 0;
		Queue<Path> queue = new PriorityQueue<>();
		queue.add(new Path(src, src, 0));
		while (!queue.isEmpty()) {
			int now = queue.poll().to;
			if (!fixed[now]) {
				fixed[now] = true;
				for (Path path : pathLists[now]) {
					if (!fixed[path.to] && distances[path.to] > distances[now] + path.cost) {
						distances[path.to] = distances[now] + path.cost;
						queue.add(new Path(path.to, path.to, distances[path.to]));
					}
				}
			}
		}
		return distances;
	}

	/**
	 * ノード間の一つのパスを表すクラス
	 */
	private static class Path implements Comparable<Path> {
		/** 開始ノード */
		@SuppressWarnings("unused")
		int from;
		/** 終了ノード */
		int to;
		/** 移動コスト */
		int cost;

		Path(int from, int to, int cost) {
			super();
			this.from = from;
			this.to = to;
			this.cost = cost;
		}

		@Override
		public int compareTo(Path path) {
			return this.cost - path.cost;
		}
	}
}


Measured memory_bytes: 107336
import java.util.Scanner;

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


Measured memory_bytes: 26128
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Main {

    public static void main(String[] args) {
        FastScanner fs = new FastScanner();
        int N = fs.nextInt();
        boolean flag = false;

        for (int i = 1; i <= 9; ++i) {
            for (int j = 1; j <= 9; ++j) {
                if (N == i*j) flag = true;
            }
        }

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

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




Measured memory_bytes: 32552
def area(p,q):return p[0]*q[1]-p[1]*q[0]
 
n=int(input())
point=[]
x_1,y_1=[int(i) for i in input().split(" ")]
 
for _ in range(n-1):
    x,y=[int(k) for k in input().split(" ")]
    point.append([x-x_1,y-y_1])
a=0
for j in range(n-2):a+=area(point[j],point[j+1])
print(a*0.5)

Measured memory_bytes: 7728
#include "bits/stdc++.h"
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
using ll = long long;
using namespace std;

ll GCD(ll a, ll b) {
	return b ? GCD(b, a%b) : a;
}

//sort(a, a + N, greater<int>())   // descend
//sort(a, a+N)                    //  ascend

int main() {
	ll n;
	cin >> n;
	ll a[110000];
	rep(i, 0, n) cin >> a[i];

	ll cnt = 0;
	ll sum = 0;

	rep(i, 0, n) {
		if (a[i] < 0) cnt++;
	}
	ll b[110000];
	rep(i, 0, n) b[i] = abs(a[i]);
	sort(b, b + n);


	rep(i, 0, n) sum += b[i];
	
	if (cnt % 2 == 1) sum -= 2 * b[0];
	
	cout << sum;
}


Measured memory_bytes: 1792
<?php

fscanf(STDIN, "%d %d %d %d %d", $W, $H, $x, $y, $r);
$boolX = $r <= $x && $x <= $W - $r;
$boolY = $r <= $y && $y <= $H - $r;
if($boolX && $boolY){
  echo "Yes"."\n";
}else{
  echo "No"."\n";
}


Measured memory_bytes: 9192
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <map>
#include <vector>
#define REP(i,n) for(int i=0;i<(int)n;i++)
#define REP1(j,n) for(int j=0;j<(int)n;j++)
#define REPD(i,n) for(int i=n;i>=0;i--)
#define REPD1(j,n) for(int j=n;j=>0;j--)
#define REPS(i,n) for(int i=1;i<=(int)n;i++)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
using namespace std;
typedef long long ll;


int main(void) {
	int a, b;
	cin >> a >> b;
	cout << (a - 1)*(b - 1) << endl;

	return 0;
}




Measured memory_bytes: 256
#include <iostream>
#include <string>

int main() {
  unsigned long N, K;
  std::cin >> N >> K;

  std::string S;
  std::cin >> S;

  unsigned long count = 0;
  for (unsigned long i = 0; i < N - 1; ++i) {
    if (S[i] == S[i + 1]) {
      count += 1;
    }
  }

  std::cout << std::min(count + K * 2, N - 1) << std::endl;

  return 0;
}


Measured memory_bytes: 512
#include<iostream>

using namespace std;

int main(){
    int a, b;
    cin >> a >> b;
    if(a > b){
        cout << "a > b" << endl;
    }else if(a < b){
        cout << "a < b" << endl;
    }else if(a == b){
        cout << "a == b" << endl;
    }
}


Measured memory_bytes: 3104
from bisect import *
import math
def is_prime(n):
    if n == 1: return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True
a=[i for i in range(2,10**5)if is_prime(i)and is_prime((i+1)//2)]
#print(a)
n=int(input())
for i in range(n):
    c,d=map(int,input().split())
    print(bisect_right(a,d)-bisect_left(a,c))

Measured memory_bytes: 3444
#include <iostream>

 

using namespace std;

 

int main()
{
    int a, b;
    cin >> a >> b;
    if (a > b)
        cout << "a > b" << endl;
    else if (a < b)
        cout << "a < b" << endl;
    else 
        cout << "a == b" << endl;

 

}


Measured memory_bytes: 3084
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <bits/stdc++.h>
using namespace std;

int main()
{
    long long X,K,D;
    cin >> X >> K >> D;
    X = abs(X);
    if(X/D >= K){
        cout << X-D*K << endl;
    }
    else{
        long long Y = X - D*(X/D);
        if((K-X/D)%2 == 1){
            cout << abs(Y - D) << endl;
        }
        else{
            cout << Y << endl;
        }
    }
}



Measured memory_bytes: 3596
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>

using namespace std;

long long ans(long long N) {
	if (N < 10) return N;
	long long NN = N;
	while (NN >= 10) {
		if (NN % 10 != 9) break;
		NN /= 10;
	}
	long long ret = 0;
	while (N >= 10) {
		ret += 9;
		N /= 10;
	}
	ret += N;
	if (NN >= 10) {
		ret -= 1;
	}
	return ret;
}

long long naive(long long N) {
	long long ret = 0;
	for (long long NN = 1; NN <= N; NN++) {
		long long NNN = NN;
		long long ret_kari = 0;
		while (NNN > 0) {
			ret_kari += NNN % 10;
			NNN /= 10;
		}
		ret = max(ret, ret_kari);
	}
	return ret;
}

int main() {
	long long N;
	cin >> N;
	cout << ans(N) << endl;
	//for (long long N = 1; N <= 100; N++) {
	//	if(ans(N) != naive(N)) cout << N << " " << ans(N) << " " << naive(N) << endl;
	//}
	system("pause");
	return 0;
}


Measured memory_bytes: 504
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: ngupta
 * Date: 5/5/2020 AD
 * Time: 00:33
 */
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        int[] height = new int[N + 1];
        int[] ma = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            height[i] = sc.nextInt();
        }

        for (int i = 0; i < M; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            ma[x] = Math.max(ma[x], height[y]);
            ma[y] = Math.max(ma[y], height[x]);
        }

        int count = 0;
        for (int i = 1; i < ma.length; i++) {
            if (height[i] > ma[i])
                count++;
        }

        System.out.println(count);
    }
}


Measured memory_bytes: 50100
#include <bits/stdc++.h>
#include<string>
using namespace std;
typedef long long ll;
typedef long double lb;
typedef vector<int,vector<int>> vec;
typedef pair<int,int> P;
#define REP(i,j,n) for (int i=j;i<(n);i++)
#define RREP(i,n) for (int i = n; 0<i;i--)
#define print(out) cout<< out  << "\n";
#define all(vec) vec.begin(),vec.end()
#define mod 1000000007
const int INF = 1e9;

int main(){
  ll N,K;cin>>N>>K;
  ll answer = 0;
 
  if (K % 2 == 0) {
    ll c1 = N / K;
    ll c2 = N / (K / 2) - c1;
    answer = c1*c1*c1+c2*c2*c2;
  } else {
    ll c = N / K;
    answer = c*c*c;
  } 
  print(answer)

  return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <cassert>
#include <string>
#include <algorithm>
#include <cstring>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = 1234567;
int a[MAXN];

int main() {
    int N;
    scanf("%d", &N);
    vector<pii> V;
    vector<int> V3;
    int cnt = 0, cnt0 = 0;
    for (int i = 0; i < N; i++) {
        scanf("%d", &a[i]);
        if (a[i] > 0) cnt++;
        else if (a[i] == 0) cnt0++;
        V.push_back(make_pair(abs(a[i]), i));
        V3.push_back(a[i]);
    }
    sort(V.rbegin(), V.rend());
    LL ans = 0;
    if ((cnt + cnt0) == N || !cnt) {
       ans = -V[N - 1].first;
       for (int i = 0; i < N - 1; i++) ans += V[i].first;
       printf("%lld\n", ans);
       if (!cnt) {// negative
          LL sum = -V[N - 1].first;
          for (int i = N - 2; i >= 0; i--) {
              printf("%lld %d\n", sum, a[V[i].second]);
              sum += V[i].first;
          }
       } else {
          LL sum = V[N - 1].first;
          for (int i = N - 2; i >= 1; i--) {
              printf("%lld %d\n", sum, V[i].first);
              sum -= V[i].first;
          }
          printf("%d %lld\n", V[0].first, sum);
       }
    } else {
       sort(V3.begin(), V3.end());
       for (auto i : V) ans += i.first;
       printf("%lld\n", ans);
       LL sum = V3[0];
       for (int i = 1; i < N - 1; i++) {
           if (V3[i] < 0) continue;
           printf("%lld %d\n", sum, V3[i]);
           sum -= V3[i];
       }
       printf("%d %lld\n", V3[N - 1], sum);
       sum = V3[N - 1] - sum;
       for (int i = N - 1; i >= 1; i--) {
           if (V3[i] >= 0) continue;
           printf("%lld %d\n", sum, V3[i]);
           sum -= V3[i];
       }
    }
    return 0;
}


Measured memory_bytes: 3956
#include<stdio.h>
int main(void)
{
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);

	if (a <= b)
	{
		if (b <= c)
		{
			printf("%d %d %d\n",a,b,c);
		}
		else if (c <= b)
		{
			if (a <= c)
			{
				printf("%d %d %d\n",a,c,b);
			}
			else
			{
				printf("%d %d %d\n",c,a,b);
			}
		}
	}
	else if(b <= a)
	{

		if (a <= c)
		{
			printf("%d %d %d\n", b, a, c);
		}
		else if (c <= a)
		{
			if (b <= c)
			{
				printf("%d %d %d\n", b, c, a);
			}
			else
			{
				printf("%d %d %d\n", c, b, a);
			}
		}
	}
	return 0;
}


Measured memory_bytes: 2056
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    string s;
    cin >> s;
    sort(s.begin(), s.end());
    if(s == "abc"){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
// https://atcoder.jp/contests/abc179/tasks/abc179_b

#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "map"
#include "numeric"
#include "queue"
#include "set"
#include "string"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define rep1(i, to) for (ll i = 1; i <= (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
#define repr(i, from) for (ll i = from - 1; i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define unless(cond) if (!(cond))
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
template <typename T>
using V = vector<T>;
using VL = V<ll>;
template <typename T>
using VV = V<V<T>>;
using VVL = VV<ll>;
template <typename T>
using VVV = VV<V<T>>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T>
using asc_pq = priority_queue<T, V<T>, greater<T>>;

template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
inline ll len(V<T> arr) {
  return arr.size();
}

struct exit_exception : public std::exception {
  const char* what() const throw() { return "Exited"; }
};

template <typename T>
void drop(T res) {
  cout << res << endl;
  throw exit_exception();
}

const ll INF = 1e18;

void solve();

#ifndef TEST
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  try {
    solve();
  } catch (exit_exception& e) {
  }

  return 0;
}
#endif

void solve() {
  ll N;
  cin >> N;

  ll ct = 0;

  rep(i, N) {
    ll a, b;
    cin >> a >> b;

    if (a == b) {
      ct++;
    } else {
      ct = 0;
    }

    if (ct == 3) drop("Yes");
  }

  cout << "No"
       << "\n";
}


Measured memory_bytes: 3604
// Aizu 1012: Operations with Finite Sets
// 2017.9.23 bal4u@uu

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 510

int set[5][110], sLen[5];
int uSet[MAX], uLen;
int ans[MAX], aLen;
char buf[MAX], *p;

int cmp(int *a, int *b) { return *a - *b; }
int expr(int *a);

int norm(int *a, int len)
{
	int i, j;

	if (len <= 0) return 0;
	qsort(a, len, sizeof(int), cmp);
	for (i = 0, j = 1; j < len; j++) {
		if (a[j] != a[i]) {
			if (++i != j) a[i] = a[j];
		}
	}
	return i+1;
}

int bsch(int x, int *a, int len)
{
	int m, l = 0, r = len-1;

	if (len <= 0) return 0;
    while (l < r) {
        m = (l + r) >> 1;
		if (*(a+m) == x) return 1;
        if (*(a+m) < x) l = m + 1; else r = m;
    }
	return *(a+l) == x;
}

int factor(int *a)
{
	int i, la, lx, f = 0;
	int *x;

	while (*p == 'c') p++, f = !f;
	if (*p == '(') { p++; x = a, lx = expr(x); p++; }
	else if (*p >= 'A' && *p <= 'E') {
		int id = *p++ - 'A';
		x = set[id], lx = sLen[id];
	} else while (1);  // panic
	if (f) {
		for (la = i = 0; i < uLen; i++) {
			if (!bsch(uSet[i], x, lx)) a[la++] = uSet[i];
		}
	} else {
		la = lx;
		if (x != a) memcpy(a, x, sizeof(int)*la);
	}
	return la;
}

int expr(int *x)
{
	int i, lx, ly, lz, op;
	int *y, *z;

	lx = factor(x);
	while (1) {
		op = *p;
		if (op == 'u' || op == 'i' || op == 'd' || op == 's') p++;
		else break;
		y = calloc(MAX, sizeof(int));
		ly = factor(y);
		z = calloc(MAX, sizeof(int)); lz = 0;
		if (op == 'u') {
			lz = lx, memcpy(z, x, sizeof(int)*lz);
			for (i = 0; i < ly; i++) z[lz++] = y[i];
			lz = norm(z, lz);
		} else if (op == 'i') {
			for (i = 0; i < lx; i++) {
				if (bsch(x[i], y, ly)) z[lz++] = x[i];
			}
		} else if (op == 'd') {
			for (i = 0; i < lx; i++) {
				if (!bsch(x[i], y, ly)) z[lz++] = x[i];
			}
		} else {  // op == 's'
			for (i = 0; i < lx; i++) {
				if (!bsch(x[i], y, ly)) z[lz++] = x[i];
			}
			for (i = 0; i < ly; i++) {
				if (!bsch(y[i], x, lx))	z[lz++] = y[i];
			}
		}
		lx = lz, memcpy(x, z, sizeof(int)*lz);
		lx = norm(x, lx);
		free(z);
		free(y);
	}
	return lx;
}

int main()
{
	int n, i, id, a;
	char s[5];

	while (1) {
		memset(sLen, 0, sizeof(sLen)), uLen = 0;
		while (1) {
			if (scanf("%s%d", s, &n) != 2) return 0;
			if (*s == 'R') break;
			if (*s < 'A' || *s > 'E') while (1);  // panic
			id = *s - 'A';
			if (sLen[id] > 0) while (1); // panic
			for (i = 0; i < n; i++) scanf("%d", &a), set[id][i] = a, uSet[uLen++] = a;
			sLen[id] = norm(set[id], n);
		}
		uLen = norm(uSet, uLen);

		scanf("%s", buf); p = buf;
		aLen = expr(ans);
		aLen = norm(ans, aLen);
		if (aLen == 0) puts("NULL");
		else {
			printf("%d", ans[0]);
			for (i = 1; i < aLen; i++) printf(" %d", ans[i]);
			putchar('\n');
		}
	}
	return 0;
}

Measured memory_bytes: 1856
#![allow(unused_imports)]
#![allow(dead_code)]

use proconio::input;
use proconio::marker::{Bytes, Chars};
use std::collections::*;

fn main() {
    input! {l:i32,r:i32,d:i32}
    let ans = r / d - (l - 1) / d;
    println!("{}", ans);
}

Measured memory_bytes: 2104
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
	
	Scanner in = new Scanner(System.in);
	int a, b, d=0;
	boolean check = false;

	while(in.hasNext()){
	    
	    a = in.nextInt();
	    b = in.nextInt();

	    if(a == 0 && b == 0) break;
	    if(d!=0) System.out.println();

	    check = false;

	    while(a>0 && a<=b){
		if(a%4 == 0){
		    if(a%100 == 0){
			if(a%400 == 0){
			    System.out.println(a);
			    check = true;
			}
		    }else{ System.out.println(a); check = true; }
		}
		a++;
	    }

	    if(check == false) System.out.println("NA");
	    d++;

	}
    }
}

Measured memory_bytes: 20724
#include<iostream>
#include<vector>

using namespace std;

int main()
{
    string s;
    string ans;
    ans = "";
    cin >> s;
    for(int i=0; i<s.length(); i++){
        if(s[i]=='1'){
            ans += "9";
        }
        else{
            ans += "1";
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
a = int(input())
if 100 <= a <= 111:
    print(111)
elif 112 <= a <= 222:
    print(222)
elif 223 <= a <= 333:
    print(333)
elif 334 <= a <= 444:
    print(444)
elif 443 <= a <= 555:
    print(555)
elif 556 <= a <= 666:
    print(666)
elif 667 <= a <= 777:
    print(777)
elif 778 <= a <= 888:
    print(888)
else:
    print(999)

Measured memory_bytes: 3060
h, w, a, b = map(int, raw_input().split())
p = 10 ** 9 + 7
z = 0
s = 1
for i in range(w - b - 1):
	s = s * (h + w - b - 2 - i) * pow(i + 1, p - 2, p) % p
for i in range(h - a):
	z = (z + s) % p
	s = s * (b + i) * (h - 1 - i) * pow((i + 1) * (h + w - b - 2 - i), p - 2, p) % p
print z

Measured memory_bytes: 5764
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int main()
{
    int n;
    cin >> n;
    vector<long long> a(n, 0);
    vector<pair<int, int> > b(n);
    for(int i = 0; i < n; i++){
        scanf("%lld%d", &a[i], &b[i].first);
        b[i].second = i;
    }
    sort(b.begin(), b.end());
    if(a[b[0].second] > b[0].first){
        cout << "No" << endl;
        return 0;
    }
    for(int i = 1; i < n; i++){
        a[b[i].second] += a[b[i-1].second];
        if(a[b[i].second] > b[i].first){
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 3456
#include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define riterator reverse_iterator
using namespace std;

inline int read()
{
	int x = 0,f = 1; char ch = getchar();
	for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
	for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
	return x*f;
}

const int N = 2e5;
struct Edge
{
	int nxt,v;
} e[(N<<1)+3];
int fe[N+3];
int fa[N+3];
int mx[N+3];
int f[N+3],g[N+3],h[N+3];
int ans[N+3];
int n,en;

void addedge(int u,int v)
{
	en++; e[en].v = v;
	e[en].nxt = fe[u]; fe[u] = en;
}

int dfs2(int u,int x)
{
	if(u>x) return 0;
	int ret = 1;
	for(int i=fe[u]; i; i=e[i].nxt)
	{
		int v = e[i].v; if(v==fa[u]) continue;
		ret += dfs2(v,x);
	}
	return ret;
}

void dfs1(int u)
{
	for(int i=fe[u]; i; i=e[i].nxt)
	{
		int v = e[i].v; if(v==fa[u]) continue;
		fa[v] = u;
		mx[v] = max(mx[u],v);
		dfs1(v);
	}
	if(u>mx[fa[u]])
	{
		h[u] = 1;
		for(int i=fe[u]; i; i=e[i].nxt)
		{
			int v = e[i].v; if(v==fa[u]) continue;
			f[v] = dfs2(v,u);
			g[v] = dfs2(v,mx[fa[u]]); h[u] += g[v];
		}
	}
}

void dfs3(int u)
{
	for(int i=fe[u]; i; i=e[i].nxt)
	{
		int v = e[i].v; if(v==fa[u]) continue;
		ans[v] = ans[u]; if(u>mx[fa[u]]) {ans[v] += f[v]-g[v];} if(mx[v]>mx[u]) {ans[v] += h[v];}
		dfs3(v);
	}
}

int main()
{
	scanf("%d",&n);
	for(int i=1; i<n; i++)
	{
		int u,v; scanf("%d%d",&u,&v);
		addedge(u,v); addedge(v,u);
	}
	mx[1] = 1; f[1] = 1; dfs1(1);
//	printf("f: "); for(int i=1; i<=n; i++) printf("%d ",f[i]); puts("");
//	printf("g: "); for(int i=1; i<=n; i++) printf("%d ",g[i]); puts("");
//	printf("h: "); for(int i=1; i<=n; i++) printf("%d ",h[i]); puts("");
	dfs3(1);
	for(int i=2; i<=n; i++) printf("%d ",ans[i]); puts("");
	return 0;
}

Measured memory_bytes: 24576
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;

typedef long long ll;
#define rep(i, n) for(int i = 0; i < n; i++)

int main(){
    ll N, A, B, C, D, E;
    cin >> N >> A >> B >> C >> D >> E;
    vector<ll> cap(5);
    cap[0] = A, cap[1] = B, cap[2] = C, cap[3] = D, cap[4] = E;
    ll mn = pow(10, 16);
    int mn_itr = 0;
    rep(i, 5){
        if(mn > cap[i]){
            mn = cap[i];
            mn_itr = i;
        }
    }
    ll ans = 4;
    ans += (N + cap[mn_itr] - 1) / cap[mn_itr];
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3620
N, C = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(C)]
G = [list(map(int, input().split())) for _ in range(N)]

cost = [[0]*C for _ in range(3)]
for i in range(C):
    for a in range(N):
        for b in range(N):
            cost[(a+b)%3][i] += D[G[a][b]-1][i]

ans = 10**10
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i == j or j == k or k == i:
                pass
            else:
                ans = min(ans, cost[0][i] + cost[1][j] + cost[2][k])

print(ans)


Measured memory_bytes: 45916
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, x;
    cin >> n >> x;
    int sum = 0, mn = 1e9;
    for(int i = 0; i < n; i++)
    {
        int a;
        cin >> a;
        sum += a;
        mn = min(mn, a);
    }

    cout << n + (x - sum) / mn;

    return 0;
}


Measured memory_bytes: 256
n = int(input())
b = [int(x) for x in input().split()]
a = [b[0]]
for i in range(n-2):
  if b[i] <= b[i+1]:
    a.append(b[i])
  else:
    a.append(b[i+1])
a.append(b[n-2])
print(int(sum(a)))

Measured memory_bytes: 3060
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

q, h, s, d = read_line(int, int, int, int)
n, = read_line(int)

# print(q, h, s, d)
# print(n)

# print()

menu = sorted({0.25: (q, q * 4), 0.5: (h, h * 2), 1: (s, s), 2: (d, d * 0.5)}.items(), key = lambda x: x[1][1])

# print(list(menu))

charge = 0
while True:
    for size, (price, _) in menu:
        if n >= size:
            count = int(n // size)
            charge += count * price
            n -= size * count
        if n == 0:
            print(int(charge))
            exit()


Measured memory_bytes: 3064
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

def func(remain):
    if remain == 1:
        return ['a']
    arr = [chr(ord('a') + i) for i in range(remain)]
    tmp = func(remain - 1)
    res = []
    for i in range(remain):
        res.extend(deepcopy(tmp))
        for j in range(i * len(tmp), (i + 1) * len(tmp)):
            if len(set(res[j])) < i:
                continue
            res[j] += arr[i]
    i = 0
    while i < len(res):
        if len(res[i]) < remain:
            del res[i]
        else:
            i += 1
    return res

n = INT()
ans = func(n)
ans.sort()
for i in range(len(ans)):
    print(ans[i])


Measured memory_bytes: 15860
n = int(input())
lis = list(input().split())
dic = {}
for a in lis:
  if a in dic:
    dic[a] += 1
  else:
    dic[a] = 1
count = 0
for b in dic.keys():
  count += 1
if count == 3:
  print("Three")
else:
  print("Four")

Measured memory_bytes: 3060
//#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a<=x&&x<b)
#define MP make_pair
#define PB push_back
#ifdef int
const ll INF = (1LL<<60);
#else
const int INF = (1LL<<30);
#endif
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

template<typename T>
inline void compress(vector<T> & c){
    sort(ALL(c));
    c.erase(unique(ALL(c)),c.end());
}
template<typename T>
inline size_t idx(T i, vector<T> const& c){
    return lower_bound(ALL(c),i) - c.begin();
}

int xx[5010], yy[5010], sum[5010][5010];

signed main(void)
{
  int n, m;
  cin >> n >> m;
  VI x(n), y(n);
  REP(i, n) {
    cin >> xx[i] >> yy[i];
    x[i] = xx[i], y[i] = yy[i];
  }
  compress(x);
  compress(y);
  REP(i, n) {
    int tx = idx(xx[i], x), ty = idx(yy[i], y);
    sum[ty+1][tx+1]++;
  }
  REP(i, y.size()) REP(j, x.size()) {
    sum[i+1][j+1] += sum[i+1][j] + sum[i][j+1] - sum[i][j];
  }

  REP(i, m) {
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    sx = idx(sx, x), sy = idx(sy, y);
    gx = idx(gx+1, x), gy = idx(gy+1, y);
    cout << sum[gy][gx] - sum[gy][sx] - sum[sy][gx] + sum[sy][sx] << endl;
  }

  return 0;
}

Measured memory_bytes: 199016
#include<iostream>
#include<ctype.h>
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
string s,ss;
queue<int> q;
bool vis[200];
int main(){
	int l;cin>>l;int er=l,ans=0;
	cin>>s>>ss;
	if(s==ss)goto portal0;
	if(s[0]!=ss[0])goto portalfail;
	for(int i=0;i<l;++i)vis[s[i]-'a']=1; 
	for(int i=0;i<l;++i)if(!vis[ss[i]-'a'])goto portalfail;
	
	for(int i=er-1;i>=0;--i){
		if(i && ss[i-1]==ss[i])goto go;
		er=min(er,i);
		while(er>=0 && s[er]!=ss[i])--er;
		if(er<0)goto portalfail;
		while(!q.empty() && q.front()>=i+q.size())q.pop();
		q.push(er);
		if(i!=er)ans=max(ans,int(q.size()));
		go:
			;
	} 
	printf("%d",ans+1);
	return 0;
	portal0:
		printf("0\n");
	return 0;
	portalfail:
		printf("-1\n");
	return 0;
}


Measured memory_bytes: 6280
#include <bits/stdc++.h>

using namespace std;


signed main(){

    int n;
    cin >> n;

    long long ans = 1LL * n * (n + 1) * (n + 2) / 6;
    for(int i = 0; i < n - 1; ++i){
        int u, v;
        cin >> u >> v;
        if(u > v)
            swap(u, v);
        --u;
        ans -= 1LL * (u + 1) * (n - v + 1);
    }
    cout << ans << endl;
}




Measured memory_bytes: 3520
# -*- coding: utf-8 -*-

import sys
import itertools
import math

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def perm(n, r):
    return math.factorial(n) // math.factorial(n - r)

MOD = 1000000007

def powm(n, p):
    if p == 0:
        return 1
    elif p % 2 == 0:
        x = powm(n, p // 2)
        return (x * x) % MOD
    else:
        x = powm(n, p - 1)
        return (x * n) % MOD

def invm(n):
    return powm(n, MOD - 2)

def makeTbFac(N):
    tbFac = list(range(N+1))
    tbFac[0] = 1
    for i in range(1,N+1):
        tbFac[i] *= tbFac[i-1]
        tbFac[i] %= MOD
    return tbFac

def prob(i,j):
    return 1 / (abs(j-i) + 1)

def coef(j,N):
    U = 0
    for k in range(0, N):
        U += prob(k,j)
    return U * math.factorial(N)

def probm(i,j):
    return invm(abs(j-i) + 1)

def coefm(j,N,tbFac):
    U = 0
    for k in range(0, N):
        U += probm(k,j)
        U %= MOD
    return U * tbFac[N] % MOD

def cost(xs):
    L = len(xs)
    if L == 0:
        return 0
    if L == 1:
        return xs[0]
    if L == 2:
        return 3 * (xs[0] + xs[1])
    if L == 3:
        return 11 * (xs[0] + xs[2]) + xs[1] * 12

    T = 0
    for i in range(L):
        s = sum(xs) * math.factorial(L-1)
        leftLen = i
        rightLen = L-i-1
        left = 0
        right = 0
        if leftLen > 0:
            #left = cost(xs[:i]) * math.factorial(rightLen) * comb(L-1, rightLen)
            left = cost(xs[:i]) * perm(L-1, rightLen)
        if rightLen > 0:
            #right = cost(xs[i+1:]) * math.factorial(leftLen) * comb(L-1, leftLen)
            right = cost(xs[i + 1:]) * perm(L - 1, leftLen)
        T += (s + left + right) % MOD
    return T % MOD

def main():
    """
    for N in range(10):
        ls = list(range(N))
        cnt = [0] * N
        for p in itertools.permutations(ls, N):
            blocks = [True] * N
            for i in p:
                cnt[i]+=1
                k = i - 1
                while k >= 0 and blocks[k]:
                    cnt[k]+=1
                    k-=1
                k = i + 1
                while k < N and blocks[k]:
                    cnt[k]+=1
                    k+=1
                blocks[i] = False
        print(cnt)
    """
    N = int(input())
    As = [int(x) for x in input().split()]

    tbFac = makeTbFac(N)

    def makeTbInvSum(N):
        tb = [0] * N
        for k in range(1, N):
            tb[k] = (tb[k-1] + invm(k)) % MOD
        return tb

    tbInvSum = makeTbInvSum(N+1)

    ret = 0
    for i in range(N):
        co1 = tbInvSum[i+1] * tbFac[N] % MOD
        co2 = tbInvSum[N-i] * tbFac[N] % MOD
        coef = co1 + co2 - tbFac[N]
        ret += As[i] * coef
        ret %= MOD

    #ret = cost(As)

    # 出力
    print("{}".format(ret))

if __name__ == '__main__':
    main()



Measured memory_bytes: 15032
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

int main() {

    size_t N;
    cin >> N;
    vector<pair<long long, long long>> a(N + 1);
    vector<vector<long long>> dp(N + 1, vector<long long>(N + 1, 0));
    for (size_t i = 1; i <= N; ++i) {
        cin >> a[i].first;
        a[i].second = (long long)i;
    }
    sort(a.begin() + 1, a.end(), greater<pair<long long, long long>>());
    for (size_t i = 1; i <= N; ++i) {
        for (size_t j = 0; j < i; ++j) {
            auto l = j;
            auto r = i - 1 - j;
            dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + a[i].first * abs((long long)l + 1 - a[i].second));
            dp[r][l + 1] = max(dp[r][l + 1], dp[r][l] + a[i].first * abs((long long)N - (long long)l - a[i].second));
        }
    }
    long long res = 0;
    for (size_t i = 1; i <= N; ++i) {
        res = max(res, dp[i][N - i]);
    }
    cout << res;
    return 0;
}

Measured memory_bytes: 34596
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<vector>
#include<numeric>
#include<random>
#define all(v) v.begin(),v.end()
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
	int x;
	int y;
	int r;
	cin >> x >> y;
	if (x < y)swap(x, y);
	while (y>0) {
		r = x % y;
		x = y;
		y = r;
	}
	cout << x << endl;

	return 0;
}


Measured memory_bytes: 3076
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#ifdef ONLINE_JUDGE
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
#define fu(a,b,c) for(int a=b;a<=(int)c;++a)
#define fd(a,b,c) for(int a=b;a>=(int)c;--a)
#define cu(a,b,c) for(int a=b;a<(int)c;++a)
#define cd(a,b,c) for(int a=b-1;a>=(int)c;--a)
#define gcd(a,b) __gcd(a,b) 
#define pii pair<int,int>
#define pll pair<long long,long long>
#define all(a) a.begin(),a.end()
#define F first
#define S second
#define pb push_back
#define pf push_front
#define MOD 1000000007
#define MAXN 52
#define MAXH 20
#define MAXM 52
#define INF 100000000
#define ll long long
#define ull unsigned long long
#define RNG rng mt19937(time(NULL))
#define udist(a,b) uniform_int_distribution<uint32_t> distribution(a,b)
#define max3(a,b,c) max(a, max(b, c))
#define min3(a,b,c) min(a, min(b, c))
#define gmax(a,b) a = max(a, b)
#define gmin(a,b) a = min(a, b)
#define whatis(x) cerr << #x << " is " << x << endl;
#define fast_io() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.1415926535897932384626433
#define vec vector
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 3000;

int main(){
	fast_io();
	int n;
	cin >> n;
	vector<int> v(n);
	cu(i,0,n)cin >> v[i];
	ll dp[n][n];
	cu(i,0,n){dp[i][i] = ((n&1)?1:-1) * v[i];}
	cu(l,1,n){
		cu(i,0,n-l){
			if((l&1) == (n&1)){
				dp[i][i + l] = min(dp[i][i + l - 1] - v[i + l], dp[i+1][i+l] - v[i]);
			}
			else{
				dp[i][i + l] = max(dp[i][i + l - 1] + v[i + l], dp[i+1][i+l] + v[i]);
			}
		}
	}
	cout << dp[0][n-1] << '\n';
}


Measured memory_bytes: 70144
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    a, b = map(int, input().split())

    ans = 0

    while ans * (a - 1) + 1 < b:
        ans += 1

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()


Measured memory_bytes: 38256
#include <iostream>
#include <queue>
using namespace std;

#define MAX_H 200
#define MAX_W 200
int cmap[10][10];
int A[MAX_H+1][MAX_W+1];

typedef long long ll;
typedef pair<int, int> P;
#define INF 1000000000

int main() {
    int H, W;
    cin >> H >> W;
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            cin >> cmap[i][j];
        }
    }
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> A[i][j];
        }
    }
    // ダイクストラ法。グラフの方向を逆にして、各数字から1までの最短距離を求める
    priority_queue<P, vector<P>, greater<P>> pque;
    pque.push({0, 1});
    int d[10];
    for (int i = 0; i < 10; i++) d[i] = INF;
    d[1] = 0;
    while (pque.size() > 0) {
        P v = pque.top();
        pque.pop();
        if (d[v.second] < v.first) continue;
        for (int i = 0; i < 10; i++) {
            if (i != v.second && d[i] > d[v.second] + cmap[i][v.second]) {
                d[i] = d[v.second]+ cmap[i][v.second];
                pque.push({d[i], i});
            }
        }
    }
    // for (int i = 0; i < 10; i++) cout << d[i] << " ";
    // cout << endl;
    ll res = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (A[i][j] == -1) continue;
            res += d[A[i][j]];
        }
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ull = unsigned long long;
 
const double PI = acos(-1);
const int inf = 2e9;
const long long INF = 2e18;
const long long MOD = 1e9+7;
 
#define sx(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i,n) for (int i = 0; i < n; i++)
#define ALL(a) begin(a),end(a)
 
int main(void) {
    string s;
    int K;
    cin >> s >> K;
    set<string> st;
    for (int i=1; i<7; i++) {
        for (int j=0; j+i <= s.size(); j++) {
            st.insert(s.substr(j, i));
        }
    }
    auto it = st.begin();
    REP(i,K-1) it++;
    cout << *it << endl;
    return 0;
}

Measured memory_bytes: 2176