akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long int n;
  cin >> n;
  string a = "abcdefghijklmnopqrstuvwxyz";
  string ret = "";

  while (n > 0) {
    --n;
    ret += a[(int)(n % 26)];
    n /= 26;
  }
  reverse(ret.begin(), ret.end());
  cout << ret << endl;
  return 0;
}

// 9223372036854775807 -> long long int max value
// 1000000000000001    -> problem size


Measured memory_bytes: 3620
in_a = input().split(" ")
in_b = input().split(" ")
in_c = input().split(" ")

total_a = set(in_a + in_b)
total_b = set(in_b + in_c)
total_c = set(in_c + in_a)


if len(total_a) != 4 and len(total_b) != 4 and len(total_c) != 4:
    print("NO")
else:
    print("YES")

Measured memory_bytes: 3060
import itertools

def kyori(xi, yi, xj, yj):
    return ((xi-xj)**2 + (yi-yj)**2)**0.5

n = int(input())
x = []
y = []
for i in range(n):
    xi, yi = map(int, input().split())
    x.append(xi)
    y.append(yi)

lst = list(range(n))
p = itertools.permutations(lst)
goukei = 0
cnt = 0
for kumi in p:
    cnt += 1
    for i in range(n-1):
        goukei += kyori(x[kumi[i]], y[kumi[i]], x[kumi[i+1]], y[kumi[i+1]])

print(goukei/cnt)

Measured memory_bytes: 3064
n, m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
mod = 10**9+7

def count(li):
    l = 1-len(li)
    cnt = 0
    for ind, i in enumerate(li):
        cnt += i*(l+2*ind)
    return cnt%mod

print((count(x)*count(y))%mod)


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


def main():
    from bisect import bisect_left

    s = input()
    t = input()
    n = len(s)
    m = len(t)
    alpha = [[] for _ in range(26)]

    # See:
    # https://www.youtube.com/watch?v=lWETOlGiuaI
    # KeyInsight
    # 部分列判定：場所i以降で文字Cが最初に現れる位置を高速に求める
    # アルファベットの出現位置をリストで記録

    # sで各文字が出現する位置を2周分記録
    for index, si in enumerate(s):
        alpha[(ord(si) - ord('a'))].append(index)

    for index, si in enumerate(s):
        alpha[(ord(si) - ord('a'))].append(index + n)

    ans = 0
    p = 0

    for ti in t:
        c = ord(ti) - ord('a')

        if len(alpha[c]) == 0:
            print('-1')
            exit()

        # 二分探索でWAを量産
        # See:
        # https://atcoder.jp/contests/abc138/submissions/6999265
        p = alpha[c][bisect_left(alpha[c], p)] + 1

        if p >= n:
            p -= n
            ans += n

    ans += p

    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 11764
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

    public class Program
    {
        public static void Main(string[] args)
        {
            new A().test();
        }
    }

	public class A
	{
		public void test()
		{
			// スペース区切りの整数の入力
			var input = CommonRead.ReadStr();

			// 文字列チェックする
			if (input.IndexOf("7") > -1)
			{
				Console.WriteLine("Yes");
			}
			else
			{
				Console.WriteLine("No");
			}


		}
	}

static class CommonRead
{
    public static string ReadStr()
    {
        return Console.ReadLine();
    }
    public static string[] ReadStrA()
    {
        return ReadStr().Split(' ');
    }
    public static List<string> ReadStrL()
    {
        return ReadStrA().ToList();
    }
    public static int ReadInt()
    {
        return Convert.ToInt32(ReadStr());
    }
    public static int[] ReadIntA()
    {
        return ReadStrA().Select(s => Convert.ToInt32(s)).ToArray();
    }
    public static List<int> ReadIntL()
    {
        return ReadStrA().Select(s => Convert.ToInt32(s)).ToList();
    }
    public static long ReadLong()
    {
        return Convert.ToInt64(ReadStr());
    }
    public static long[] ReadLongA()
    {
        return ReadStrA().Select(s => Convert.ToInt64(s)).ToArray();
    }
    public static List<long> ReadLongL()
    {
        return ReadStrA().Select(s => Convert.ToInt64(s)).ToList();
    }

    public static decimal ReadDecimal()
    {
        return Convert.ToDecimal(ReadStr());
    }
    public static decimal[] ReadDecimalA()
    {
        return ReadStrA().Select(s => Convert.ToDecimal(s)).ToArray();
    }
    public static List<decimal> ReadDecimalL()
    {
        return ReadStrA().Select(s => Convert.ToDecimal(s)).ToList();
    }

    public static double ReadDouble()
    {
        return Convert.ToDouble(ReadStr());
    }
    public static double[] ReadDoubleA()
    {
        return ReadStrA().Select(s => Convert.ToDouble(s)).ToArray();
    }
    public static List<double> ReadDoubleL()
    {
        return ReadStrA().Select(s => Convert.ToDouble(s)).ToList();
    }

}




Measured memory_bytes: 18220
N = int(input())
s = sorted([int(input()) for _ in range(N)])
ans = sum(s)
if ans % 10 == 0:
    for i in s:
        if (ans - i) % 10 != 0:
            print(ans - i)
            break
    else:
        print(0)
else:
    print(ans)

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

#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define endl '\n'

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;

const ll INF = 2e18;
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;


void wrong() {
	cout << -1;
	exit(0);
}

int N, M, A[20][20], C[20], X, B[20];
ll ans = INF;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> N >> M >> X;
	for(int i=0; i<N; i++) {
		cin >> C[i];
		for(int j=0; j<M; j++) cin >> A[i][j];
	}

	bool ok = 0;
	for(int b=0; b<(1<<N); b++) {
		for(int i=0; i<M; i++) B[i] = 0;

		ll sum = 0;
		for(int i=0; i<N; i++) if(b & (1 << i)) {
			for(int j=0; j<M; j++) B[j] += A[i][j];
			sum += C[i];
		}

		bool pos = 1;
		for(int i=0; i<M; i++) if(B[i] < X) pos = 0;

		if(pos) {
			ok = 1;
			ans = min(ans, sum);
		}
	}

	if(ok) cout << ans;
	else wrong();
}


Measured memory_bytes: 3652
#include<stdio.h>

int main(){
  int n,S[10000],i,q,T[500],j,cnt=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&S[i]);
  }
  scanf("%d",&q);
  for(i=0;i<q;i++){
    scanf("%d",&T[i]);
  }
  for(i=0;i<q;i++){
    for(j=0;j<n;j++){
      if(S[j]==T[i]){
	cnt++;
	break;
      }
    }
  }
  printf("%d\n",cnt);
  return 0;
}
	
  



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

int main(){
  string s;
  int apos, zpos;
  
  cin >> s;
  
  apos = s.find_first_of('A');
  zpos = s.find_last_of('Z');
  
  cout << zpos - apos + 1 << endl;
  
  
  return 0;  
}

Measured memory_bytes: 640
import java.util.*;

public class Main {

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

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

        int cnt = 0;
        boolean hasNext = true;
        while (hasNext) {
            for (int i = 0; i < N; i++) {
                if (a[i] % 2 != 0) {
                    hasNext = false;
                    break;
                } else {
                    a[i] = a[i] / 2;
                }
            }
            if (hasNext) {
                cnt++;
            }
        }

        System.out.println(cnt);
    }
}


Measured memory_bytes: 23380
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE GADTs #-}

-- Control
--import Control.Functor
import Control.Applicative
import Control.Monad
import Control.Monad.ST
import Control.Arrow
--import Control.Category


-- Data
import qualified Data.List as L
-- import qualified Data.Set as S
-- import qualified Data.IntSet as IS
-- import qualified Data.Map as M
-- import qualified Data.IntMap as IM
-- import qualified Data.Sequence as Seq
-- import qualified Data.Tree as Tree
-- import qualified Data.Graph as G
-- import qualified Data.Tuple as Tup
-- import qualified Data.Array as Ary
--import qualified Data.Vector as Vec

import Data.Bool
import Data.Char
import Data.Monoid 
import Data.Ord
import Data.Maybe
import Data.Bits
import Data.Either
import Data.Eq
import Data.Function


-- Debug
import qualified Debug.Trace as DT


-- io
import Text.Printf


main ::IO()
main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]
  putStrLn $ show $ solve n k

solve :: Int -> Int -> Int
solve n 1 = 0
solve n k = n - k


Measured memory_bytes: 508
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <array>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <utility>
#include <functional>
#include <bitset>
#include <cstdint>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;
template<class T, class U> void init_n(vector<T>& v, size_t n, U x) 
{ v = vector<T>(n, x); }
template<class T> void init_n(vector<T>& v, size_t n) { init_n(v, n, T()); }
template<class T> void read_n(vector<T>& v, size_t n, size_t o = 0) 
{ v = vector<T>(n+o); for (size_t i=o; i<n+o; ++i) cin >> v[i]; }
template<class T> void read_n(T a[], size_t n, size_t o = 0)
{ for (size_t i=o; i<n+o; ++i) cin >> a[i]; }
template<class T> T gabs(const T& x) { return max(x, -x); }
#define abs gabs

i64 a, b, c;
int main() {
  cin >> a >> b >> c;
  if (a + c == 2 * b) cout << "YES\n";
  else cout << "NO\n";
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<time.h>

using namespace std;

#define ll long long
#define MOD 1000000007

int n;
int a[500001];
int b[500002];
int zerob[500003];
ll dp[2][500001];

vector<int> xat[1<<20];

int get0num(int l, int r){
	return zerob[r]-zerob[l];
}

int main(){
	cin >> n;
	for(int i = 0;i < n;i++){
		cin >> a[i];
	}
	b[0] = 0;
	for(int i = 0;i < n;i++){
		b[i+1] = b[i]^a[i];
	}
	n++;
	zerob[0] = 0;
	for(int i = 0;i < n;i++){
		xat[b[i]].push_back(i);
		zerob[i+1] = zerob[i]+(b[i]==0?1:0);
	}
	
	ll ans = 0;
	map<int,int> fin;
	for(int i = 0;i < n;i++){
		int X = b[i];
		if(X == 0)continue;
		if(fin[X] == 1)continue;
		if(b[n-1] != 0 && b[n-1] != X)continue;
		fin[X] = 1;

		//座標圧縮パート
		vector<pair<int,int> > target;
		int index_last = 0;
		int count = 1;
		if(b[n-1] == 0)xat[X].push_back(n);//番兵
		target.push_back(make_pair(0,1));
		for(int j = 1;j < xat[X].size();j++){//座標圧縮
			int at = xat[X][j];
			int num0 = get0num(xat[X][index_last], at);
			if(num0==0){
				count++;
				continue;
			}
			target.push_back(make_pair(X,count));
			target.push_back(make_pair(0,num0));
			index_last = j;
			count = 1;
		}
		if(b[n-1] != 0)target.push_back(make_pair(X,count));

		//DPパート
		int n2 = target.size();
		dp[0][n2-1] = b[n-1]==0?0:1;
		dp[1][n2-1] = (dp[0][n2-1]+1)%2;
		for(int at = n2-2;at!=-1;at--){
			for(int before = 0;before<2;before++){
				dp[before][at] = 0;
				dp[before][at] += dp[before][at+1];
				int y = target[at].first;
				int len = target[at].second;
				if(y==0 && before==1){
					dp[before][at] += dp[0][at+1]*len%MOD;//選ぶ
				}else if(y!=0 && before==0){
					dp[before][at] += dp[1][at+1]*len%MOD;//選ぶ
				}
				dp[before][at]%=MOD;
			}
		}
		ans+=dp[0][0];
		ans%=MOD;
	}
	if(b[n-1]!=0){
		cout << ans << endl;
		return 0;
	}
	int num0 = get0num(0,n);
	ll ans0 = 1;
	for(int i = 0;i < num0-2;i++){
		ans0*=2;
		ans0%=MOD;
	}
	cout << (ans0+ans)%MOD << endl;
}

Measured memory_bytes: 59508
/*  -*- coding: utf-8 -*-
 *
 * b.cc: B: Polygon
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 10;

/* typedef */

/* global variables */

int ls[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", ls + i);
  sort(ls, ls + n);

  int sum = 0;
  for (int i = 0; i < n - 1; i++) sum += ls[i];

  if (sum > ls[n - 1]) puts("Yes");
  else puts("No");
  return 0;
}


Measured memory_bytes: 256
N, M = map(int, input().split())
a = [0]*N
b = [0]*N
c = [0]*M
d = [0]*M
for i in range(N):
  a[i], b[i] = map(int, input().split()) 
for j in range(M):
  c[j], d[j] = map(int, input().split())

for i in range(N):
  min_calc=10e8+10e8
  min_num=N
  for j in range(M):
    calc = abs(a[i] - c[j]) + abs(b[i] - d[j])
    if calc < min_calc:
      min_calc = min(min_calc, calc)
      min_num = j+1
  print(min_num)

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

char grp(int a){
  if(a == 1 ||a == 3 ||a == 5 ||a == 7 ||a == 8 ||a == 10 ||a == 12) return 'a';
  else if(a == 4 ||a == 6 ||a == 9 ||a == 11) return 'b';
  else if(a == 2) return 'c';
}

int main(){
  int x,y;
  cin >>x>>y;
  if(grp(x) == grp (y)) cout << "Yes" << endl;
  else cout << "No" << endl;
}
  

Measured memory_bytes: 256
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            String strSplit[] = br.readLine().split(" ");
            int H = Integer.parseInt(strSplit[0]);
            int W = Integer.parseInt(strSplit[1]);
            char HW[][] = new char[H][W];
            int xNESW[] = {0, 1, 1 ,1 ,0 ,-1, -1, -1};
            int yNESW[] = {-1, -1, 0, 1, 1, 1, 0, -1};
            
            for(int i=0; i<H; i++){
                String str = br.readLine();
                for(int j=0; j<W; j++){
                    HW[i][j] = str.charAt(j);
                }
            }
            
            for(int i=0; i<H; i++){
                for(int j=0; j<W; j++){
                    char count = '#';
                    
                    if(HW[i][j] == '.'){
                        count = '0';
                        for(int k=0; k<8; k++){
                            if((i+yNESW[k]) >= 0 && (i+yNESW[k])<H && (j+xNESW[k])>=0 && (j+xNESW[k])<W && HW[i+yNESW[k]][j+xNESW[k]] == '#'){
                                count++;
                            }
                        }
                        
                        //HW[i][j] = count;
                    }
                    
                    System.out.print(count);
                }
                System.out.println();
            }
            
            /*for(int i=0; i<H; i++){
                for(int j=0; j<W; j++){
                    System.out.print(HW[i][j]);
                }
                System.out.println();
            }*/
            
        }catch(Exception e){
            
        }
    }
}

Measured memory_bytes: 33316
_n, m = gets.split.map(&:to_i)

data = m.times.map.with_index do |i|
  p, y = gets.split.map(&:to_i)
  [p, i, y]
end

ken = data.group_by(&:first)

ken.each_value do |value|
  value
    .sort_by(&:last)
    .each.with_index(1) {|(_p, data_index, _y), order| data[data_index][2] = order }
end

data.each {|p, _i, y| puts "%06d%06d" % [p, y] }


Measured memory_bytes: 24444
if __name__=="__main__":
    K, A, B = input().split(' ')
    K = int(K)
    A = int(A)
    B = int(B)
    if B <= A or K + 1 <=A or B - A <= 2:
        print(K + 1)
    else:
        ans = 1
        K -= A - 1
        ans = A
        sum = int(K / 2)
        K = (K & 1)
        ans += (B - A) * sum
        print(ans + K)

Measured memory_bytes: 3064
L,x[1<<17];d[21][1<<17];i,k,l,r,m;
main(n){
	for(scanf("%d",&n);i<n;)scanf("%d",++i+x);
	for(scanf("%d%*d",&L);k<20;k++)for(i=0;i++<n;d[k+1][i]=l)
		for(l=d[k][d[k][i]],r=n+1;!k&r-l>1;)x[m=l+r>>1]>x[i]+L?r=m:(l=m);
	for(;~scanf("%d%d",&l,&r);printf("%d\n",m))
		for(m=l<r||(l^=r^=l^=r),k=21;--k;)d[k][l]<r?m+=1<<k-1,l=d[k][l]:0;
}

Measured memory_bytes: 10880
using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
		string[] str = Console.ReadLine().Split();
		int R = int.Parse(str[0]);
		int C = int.Parse(str[1]);
		int K = int.Parse(str[2]);
		int[]A = new int[K];
		int[]B = new int[K];
		long[]V = new long[K];
		for(var i=0;i<K;i++){
			string[] str2 = Console.ReadLine().Split();
			A[i] = int.Parse(str2[0]);
			B[i] = int.Parse(str2[1]);
			V[i] = long.Parse(str2[2]);
		}
		var list = new List<int>[R];
		for(var i=0;i<R;i++){
			list[i] = new List<int>();
		}
		for(var i=0;i<K;i++){
			list[A[i]-1].Add(i);
		}
		for(var i=0;i<R;i++){
			list[i].Sort((x,y)=>B[x]-B[y]);
		}
		long[,,]D = new long[R+1,C+1,4];
		for(var i=0;i<R;i++){
			int it = 0;
			int a = 0;
			for(var j=0;j<4;j++){
				D[i+1,0,0] = Math.Max(D[i+1,0,0],D[i,0,j]);
			}
			while(a < C){
				D[i+1,a+1,0] = D[i+1,a,0];
              	long t1 = 0;
				for(var j=0;j<4;j++){
					D[i+1,a+1,0] = Math.Max(D[i+1,a+1,0],D[i,a+1,j]);
                  	t1 = Math.Max(t1,D[i,a+1,j]);
				}
				D[i+1,a+1,1] = D[i+1,a,1];
				D[i+1,a+1,2] = D[i+1,a,2];
				D[i+1,a+1,3] = D[i+1,a,3];
				if(it < list[i].Count && a==B[list[i][it]]-1){
					D[i+1,a+1,1] = Math.Max(D[i+1,a+1,1],Math.Max(D[i+1,a,1],t1+V[list[i][it]]));
					D[i+1,a+1,2] = Math.Max(D[i+1,a+1,2],Math.Max(D[i+1,a,2],D[i+1,a,1]+V[list[i][it]]));
					D[i+1,a+1,3] = Math.Max(D[i+1,a+1,3],Math.Max(D[i+1,a,3],D[i+1,a,2]+V[list[i][it]]));
					it++;
				}
				a++;
			}
		}
		long ans = D[R,C,0];
		for(var i=1;i<=3;i++){
			ans = Math.Max(ans,D[R,C,i]);
		}
		for(var i=0;i<=R;i++){
          	for(var j=0;j<=C;j++){
              	//Console.Error.WriteLine(D[i,j,0]+" "+D[i,j,1]+" "+D[i,j,2]+" "+D[i,j,3]);
            }
        }
		Console.WriteLine(ans);
	}
}

Measured memory_bytes: 307396
/*
				" اللهم صلي و سلم علي سيدنا محمد و علي اله وصحبه اجمعين "
*/


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define f for
#define ll long long
#define ull unsigned long long
#define endl "\n"
using namespace std;
#define BI  acos(-1)
#define all(a) a.begin(), a.end()
void Open()
{
#ifndef ONLINE_JUDGE
	freopen("Input.txt", "r", stdin);
	freopen("Output.txt", "w", stdout);
#endif
}
ll gcd(ll a, ll b)
{
	return b == 0 ? a : gcd(b, a % b);
}
void madesetprobblem()
{
	//$ $
	//less than or equal => $ 1 \leq 100 $
	//  $(1 \le A,B \le 10^5 )$
	// a[i] => $a_i$
	// a[i][j] => $a_{i,j}$
	// double qoutes ''yext''
	/*
		image
		\begine{center}
		\includegraphics{image.jpg}
		\end{center}
	*/
	/*
		list
		\begin{itemize}
			\item first one
			\item second one
		\end{itemize}
	*/
}
int n,k;
vector<int>a(1e5 + 2),dp(1e5+2,-1);
int ans(int c = 0)
{
	if (c == n-1)
		return 0;
	int ans1 = INT_MAX, ans2 = INT_MAX;
	if (dp[c] != -1)
		ans1 = dp[c];
	else
	{
		f(int i = 1; i <= k; i++)
		{
			if (c + i < n)
				ans1 = min(ans1,(abs(a[c] - a[c + i]) + ans(c + i)));
		}
		dp[c] = ans1;
	}
	//cout <<c<<" "<< ans1 << " " << ans2 << endl;
	return ans1	;
}
int main()
{
	// توكلت علي الله 
//Open();
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin >> n>>k;
	f(int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	cout << ans();
}

Measured memory_bytes: 7296
#include <iostream>
#include <bits/stdc++.h>
#include <math.h>

using namespace std;

int main(){
    string s;
    int q;
    cin>>s>>q;
    string mae="";
    string usiro="";
    vector<pair<int,pair<int,string>>> sizi(q);
    string status="omote";
    for(int i=0;i<q;i++){
        cin>>sizi.at(i).first;
        if(sizi.at(i).first==1){
            sizi.at(i).second.first=0;
            sizi.at(i).second.second="ERROR";
        }else{
            cin>>sizi.at(i).second.first>>sizi.at(i).second.second;
        }
    }
    
    for(int i=0;i<q;i++){
        if(sizi.at(i).first!=1){
            if(sizi.at(i).second.first==1){
                if(status=="omote"){
                    mae=sizi.at(i).second.second+mae;
                }else{
                    mae+=sizi.at(i).second.second;
                }
            }else{
                if(status=="omote"){
                    usiro+=sizi.at(i).second.second;
                }else{
                    usiro=sizi.at(i).second.second+usiro;
                }
            }
        }else{
            if(status=="omote") status="ura";
            else status="omote";
            string pre=usiro;
            usiro=mae;
            mae=pre;
        }
    }
    
    if(status=="omote") cout<<mae+s+usiro<<endl;
    else{
        reverse(mae.begin(),mae.end());
        reverse(usiro.begin(),usiro.end());
        reverse(s.begin(),s.end());
        cout<<mae+s+usiro<<endl;
    }
    
    return 0;
}

Measured memory_bytes: 15428
K, Q = map(int, input().split())
D = list(map(int, input().split()))
for _ in range(Q):
    n, x, m = map(int, input().split())
    ds = []
    sumd = 0
    r = 0
    for i, d in enumerate(D):
        tmp = -(-(n-1-i)//K)
        ds.append(d%m)
        sumd += (d%m)*tmp
        if d%m == 0:
            r += tmp
#    print(ds, sumd+x, x, r)
    r += (sumd+x)//m - x//m
#    print(r)
    print(n-1-r)
    


Measured memory_bytes: 65116
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
const int INF = 1e9;

int main() {
  int a, b, c, cnt = 1;
  cin >> a >> b >> c;
  if (a != b) cnt++;
  if (a != c && b != c) cnt++;
  cout << cnt << endl;
}


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

using namespace std;
const int N = 1e5 + 10, inf = 1e9 + 1;
int a[N], ps[N], fen[3*N], n;
long long CNT;

void add(int idx) {
    for (idx += N; idx < 3*N; idx += idx&(-idx)) fen[idx]++;
}
int get(int idx) {
    int res = 0;
    for (idx += N; idx; idx -= idx&(-idx)) res += fen[idx];
    return res;
}
bool isval(int x) {
    long long cnt = 0;
    for (int i = 1; i <= n; i++) ps[i] = (a[i - 1] >= x) + ps[i - 1];
    memset(fen, 0, sizeof fen);
    for (int i = 0; i + 1 <= n; i+=2) {
        add(2 * ps[i] - i + 1);
        cnt += get(2 * ps[i + 1] - i - 1);
        add(2 * ps[i + 1] - i - 1);
    }
    memset(fen, 0, sizeof fen);
    add(0);
    for (int i = 1; i + 1 <= n; i+=2) {
        add(2 * ps[i] - i + 1);
        cnt += get(2 * ps[i + 1] - i - 1);
        add(2 * ps[i + 1] - i - 1);
    }
    return cnt >= CNT;
}

int main()
{
    cin >> n;
    CNT = 1ll * n * (n + 1) / 2, CNT = (CNT + 1)/2;
    for (int i = 0; i < n; i++) cin >> a[i];
    int dw = 0, up = inf;
    while (up - dw > 1) {
        int mid = (up+dw)>>1;
        if (isval(mid)) dw = mid;
        else up = mid;
    }
    cout << dw << "\n";

    return 0;
}


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

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define spa << " " <<
#define test cout<<"test"<<endl;
#define fi first
#define se second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (m) - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T>
void chmin(T &a,T b){if(a>b)a=b;}
template<typename T>
void chmax(T &a,T b){if(a<b)a=b;}
void pmod(ll &a,ll b){a=(a+b)%MOD;}
void pmod(ll &a,ll b,ll c){a=(b+c)%MOD;}
void qmod(ll &a,ll b){a=(a*b)%MOD;}
void qmod(ll &a,ll b,ll c){a=(b*c)%MOD;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>
void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T>
void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++)
{for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>
void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];
for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>
vector<vector<T>>vec(ll x, ll y, T w){
  vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,0,-1,0,1,1,-1,-1};
vector<ll>dy={0,1,0,-1,1,-1,1,-1};
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}
ostream &operator<<(ostream &os, pair<ll, ll>&p){
  return os << p.first << " " << p.second;
}  
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  //cout<<fixed<<setprecision(10);
  ll res=0,buf=0;
  bool judge = true;
  set<ll>st;
  buf=1;
  rep(i,0,30){
    st.insert(buf);
    buf*=2;
  }
  ll n;cin>>n;
  if(st.count(n))judge=false;
  if(n==1)judge=false;
  ans1(judge);
  if(judge){
    for(ll i=2;i<n;i+=2){
      cout<<i spa i+1<<endl;
      cout<<i+1 spa 1<<endl;
      cout<<1 spa n+i<<endl;
      cout<<n+i spa n+i+1<<endl;
    }
    cout<<n+1 spa 2<<endl;
    if(n%2==0){
      ll x=n;
      while(x%2==0){
        x/=2;
      }
      x=n/x;
      cout<<n+x spa n<<endl;
      cout<<n-x+1 spa 2*n<<endl;
    }
    
  }
  return 0;
}

Measured memory_bytes: 2304
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) 
#define X first
#define Y second 
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a) 
#define mst(a,b) memset(a, b, sizeof a)
#define rep(i,n) for(int i = 0; (i)<(n); i++)
#define rep1(i,n) for(int i = 1; (i)<=(n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a,b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 1e6+10, M = 2e6+10, mod = 1e9+7, inf = 0x3f3f3f;
ll cnt[N], pre[N]; 

int find(int x)
{
	return x == pre[x] ? x : pre[x] = find(pre[x]);
}

int main()
{
	IOS;
	ll n, m, ans = MIN;
	cin>>n>>m;
	rep1(i, n)
		pre[i] = i;
	while(m--)
	{
		int a, b;
		cin>>a>>b;
		int x = find(a), y = find(b);
		if(x != y)
			pre[x] = y;
	}
	rep1(i, n)
	{
		int x = find(i);
		cnt[x]++, ans = max(ans, cnt[x]);
	}
	cout<<ans<<nl;
	
	AC;
} 

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

using namespace std;

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &a) { return is >> a.first >> a.second; }

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, pair< T1, T2 > &a) { return os << a.first << " " << a.second; }

template< typename T >
istream &operator>>(istream &is, vector< T > &vc) {
  for(int i = 0; i < vc.size(); i++) is >> vc[i];
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, vector< T > &vc) {
  for(int i = 0; i < vc.size(); i++) os << vc[i] << endl;
  return os;
}

#define reps(i, j, n) for(int i = j; i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define UNQ(s) { sort(ALL(s)); (s).erase(unique( ALL(s)), (s).end());}

typedef long long int64;
const int64 INF = 1LL << 55;
typedef pair< int, int > Pi;
typedef pair< int, Pi > Pii;


int64 S[200], V[200], N, U;

pair< int64, int64 > ps[1 << (30 / 2)];

int64 type1() {
  int n1 = N / 2;
  int n2 = N - n1;
  for(int i = 0; i < 1 << n1; i++) {
    int64 w2 = 0, v2 = 0;
    for(int j = 0; j < n1; j++) {
      if(i >> j & 1) {
        w2 += S[j];
        v2 += V[j];
      }
    }
    ps[i] = make_pair(w2, v2);
  }
  sort(ps, ps + (1 << n1));
  int m = 1;
  for(int i = 0; i < 1 << n1; i++) {
    if(ps[m - 1].second < ps[i].second) {
      ps[m++] = ps[i];
    }
  }
  int64 res = 0;
  for(int i = 0; i < 1 << n2; i++) {
    int64 w2 = 0, v2 = 0;
    for(int j = 0; j < n2; j++) {
      if(i >> j & 1) {
        w2 += S[n1 + j];
        v2 += V[n1 + j];
      }
    }
    if(w2 <= U) {
      int64 v = (upper_bound(ps, ps + m, make_pair(U - w2, INF)) - 1)->second;
      res = max(res, v2 + v);
    }
  }
  return (res);
}

int64 type2() {
  int64 dp[100004];
  fill_n(dp, 100004, -1);
  dp[0] = 0;
  for(int i = 0; i < N; i++) {
    for(int j = U; j >= S[i]; j--) {
      dp[j] = max(dp[j], dp[j - S[i]] + V[i]);
    }
  }
  return (*max_element(dp, dp + U + 1));
}


int64 type3() {
  int64 dp[1004 * 200];
  fill_n(dp, 1004 * 200, INF);
  dp[0] = 0;
  for(int i = 0; i < N; i++) {
    for(int j = 1003 * 200 - V[i]; j >= 0; j--) {
      dp[j + V[i]] = min(dp[j + V[i]], dp[j] + S[i]);
    }
  }
  int ret = 0;
  for(int i = 0; i < 1003 * 200; i++) {
    if(dp[i] <= U) ret = max(ret, i);
  }
  return (ret);
}


int main() {
  cin >> N >> U;
  for(int i = 0; i < N; i++) {
    cin >> S[i] >> V[i];
  }
  cout << type2() << endl;
}


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

int main(){
    int N,M;
    while(cin >> N >> M, N != 0){
        vector<int> p(N);
        for(int i = 0; i < N; i++) cin >> p[i];
        sort(p.rbegin(), p.rend());

        int ans = 0;
        for(int i = 0; i < N; i++){
            if((i+1) % M) ans += p[i]; 
        }
        cout << ans << endl;
    }
}


Measured memory_bytes: 3092
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

string S;
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> S;
  int s = 0, m = 0;
  rep(i, S.length()) {
    if (S[i] == 'g') {
      if (m > 0) {
        s++;
        m--;
      }
      else {
        m++;
      }
    }
    else {
      if (m > 0) {
        m--;
      }
      else {
        s--;
        m++;
      }
    }
  }
  cout << s << "\n";
  return 0;
}


Measured memory_bytes: 512
#include<stdio.h>
int main(void){
	int S,h=0,m=0,s=0;
	
	scanf("%d",&S);
	
	h=S/3600;
	m=(S%3600)/60;
	s=(S%3600)%60;
	
	printf("%d:%d:%d\n",h,m,s);
	return 0;
}

Measured memory_bytes: 592
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <string>
#include <queue>
#define PIE 3.14159265358979
#define rep(i,n) for(int i = 0;i < (n);i++)
using namespace std;

class union_find{
    int n;
    vector<int> par;
    vector<int> rank;
public:
    union_find(int n){
        this->n=n;
        par = vector<int>(n,0);
        rep(i,n){
            par[i]=i;
        }
        rank = vector<int>(n,0);
    }
    int find(int x){
        if(par[x]==x){
            return x;
        }else{
            par[x]=find(par[x]);
            return par[x];
        }
    }
    void unite(int x,int y){
        x=find(x);
        y=find(y);
        if(x==y){return;}
        if(rank[x]<rank[y]){
            par[x]=y;
        }else{
            par[y]=x;
            if(rank[x]==rank[y])rank[x]++;
        }
    }
    bool same(int x,int y){
        return find(x)==find(y);
    }
};

int main(){
    int N,K,L;
    cin>>N>>K>>L;
    union_find road(N);
    union_find rail(N);
    map<pair<int,int>,int> mp;
    rep(i,K){
        int a,b;
        cin>>a>>b;
        a--,b--;
        road.unite(a,b);
    }
    rep(i,L){
        int a,b;
        cin>>a>>b;
        a--,b--;
        rail.unite(a,b);
    }
    rep(i,N){
        mp[make_pair(road.find(i),rail.find(i))]++;
    }
    rep(i,N-1){
        cout<<mp[make_pair(road.find(i),rail.find(i))]<<" ";
    }
    cout<<mp[make_pair(road.find(N-1),rail.find(N-1))]<<endl;
}


Measured memory_bytes: 15104
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long ans = 0;
		int san = sc.nextInt();
		for(int i=0;i<n-1;i++) {
			int a = sc.nextInt();
			if(san>=a) {
				ans += san-a;
			}
			else {
				san = a;
			}
		}
		System.out.println(ans);
	}
}


Measured memory_bytes: 58916
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;


signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
 
    int a,b,c;
    cin>>a>>b>>c;
    int cnt[22]={};
    cnt[a]++;
    cnt[b]++;
    cnt[c]++;
    if(cnt[a]==2 || cnt[b]==2 || cnt[c]==2){
        cout << "Yes" << endl;
    }
    else cout << "No\n";
}

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

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int p[n];
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  int ret = 1;
  int tmp_min = p[0];
  for (int j = 1; j < n; j++) {
    if (tmp_min >= p[j]){
      tmp_min = p[j];
      ret++;
    }
  }
  cout << ret << '\n';

  return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> A(N), B(N), C(N - 1);
  for (int i = 0; i < N; ++i) cin >> A[i];
  for (int i = 0; i < N; ++i) cin >> B[i];
  for (int i = 0; i < N - 1; ++i) cin >> C[i];
  
  int ans = 0;
  int ate = 0;
  for (int i = 0; i < N; ++i) {
    ans += B[A[i] - 1];
    if (ate + 1 == A[i]) ans += C[ate - 1];
    ate = A[i];
  }
  
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <numeric>
#include <set>

using namespace std;
struct aaa{aaa(){cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(20);};}aaa;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v){o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
#define debug(v) {cerr<<"\033[1;36m[debug]\033[m "<<#v<<" : "<<v<<endl;}

using int64 = long long;

int main() {
    int n,m;
    cin >> n >> m;
    vector<vector<int>> g(n+1);
    for (int i=0; i<m; i++) {
        int a,b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    bool ans = false;
    for (auto u : g[1]) {
        for (auto v : g[u]) {
            if (v == n) ans = true;
        }
    }
    cout << (ans? "POSSIBLE" : "IMPOSSIBLE") << endl;
}

Measured memory_bytes: 12016
import sys
input = lambda: sys.stdin.readline().rstrip()
import math

N, M = map(int, input().split())

def find(x):
    if parents[x] < 0:
        return x
    else:
        parents[x] = find(parents[x])
        return parents[x]

def union(x,y):
    x = find(x)
    y = find(y)

    if x == y:
        return

    if parents[x] > parents[y]:
        x,y = y, x

    parents[x] += parents[y]
    parents[y] = x 

def same(x,y):
    return find(x) == find(y)

def parent_count(x):
    return -parents[find(x)]

edges = []
for i in range(M):
    a, b = map(int, input().split())
    edges.append((a-1,b-1))

ans = [0] * (M+1)
parents = [-1] * N
ans[-1] = N*(N-1)//2

for i in range(M-1, -1, -1):
    s, e = edges[i]
    if same(s, e):
        ans[i] = ans[i+1]
    else:
        ans[i] = ans[i+1] - parent_count(s) * parent_count(e)
        union(s, e)
        
for i in range(1,M+1):
  	print(ans[i])

Measured memory_bytes: 90716
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main(void){
    int K;
    vector<int> L= {1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};
    cin >> K;
    
    cout <<  L.at(K-1) << endl;
    return 0;
    
}

Measured memory_bytes: 256
// luogu-judger-enable-o2
// SeptEtavioxy
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<cmath>
#define re register
#define ll long long
#define il inline
#define rep(i,s,t) for(re int i=(s);i<=(t);i++)
#define rev_rep(i,s,t) for(re int i=(s);i>=(t);i--)
#define each(i,u) for(int i=head[u];i;i=bow[i].nxt)
#define pt(ch) putchar(ch)
#define pti(x) printf("%d",x)
#define ptll(x) printf("%lld",x)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
// c_ints
il int ci(){
	re char ch;int f=1;
	while(!isdigit(ch=getchar()))f= ch=='-'?-1:1 ;
	re int x= ch^'0';
	while(isdigit(ch=getchar()))x=(x*10)+(ch^'0');
	return f*x;
}
enum{N=200024,D=4024};
const int mod = 1e9+7;
void exgcd(ll a,ll b,ll&x,ll&y){
	if( !b ){ x=1,y=0; return ; }
	exgcd(b,a%b,y,x); y-=x*(a/b);
}
il ll inv(ll a){
	ll x,y;
	exgcd(a,mod,x,y);
	return (x+mod)%mod;
}
ll fac[D*2],F[D][D];
const int base = 2001;
#define f(i,j) F[(i)+base][(j)+base]
int a[N],b[N];
int main(){
//	printf("%lld",sizeof(G)+sizeof(a)+sizeof(b)+sizeof(fac);
//	return 0;
	int n=ci();
	fac[0]=1;
	rep(i,1,8000) fac[i]=fac[i-1]*i%mod;
	ll ans= 0;
	rep(i,1,n){
		a[i]=ci(), b[i]=ci();
		f(-a[i],-b[i])++;
		ans= (ans
			-fac[(a[i]+b[i])*2]
			*inv(fac[a[i]*2]*fac[b[i]*2]%mod)
		)%mod;
	}
//	printf("%lld\n",ans);
	rep(i,-2000,2000){
		rep(j,-2000,2000){
			f(i,j)= (f(i,j)+f(i-1,j)+f(i,j-1))%mod;
		}
	}
	rep(i,1,n) ans= (ans+f(a[i],b[i]))%mod;
	ans= ans*inv(2)%mod;
	ptll((ans+mod)%mod);
	return 0;
}

Measured memory_bytes: 127616
#include<stdio.h>
int main()
{
    int  a,b,c,sum=0,sum1,i,j,temp,r;
    scanf("%d %d %d",&a,&b,&c);
    for(i=1;i<=a;i++)
    {
        sum1=0;
        temp=i;
        while(temp!=0)
        {
            r=temp%10;
            sum1=sum1+r;
            temp=temp/10;
        }
        if(sum1>=b&&sum1<=c)
        {
            sum=sum+i;
        }
    }
    printf("%d\n",sum);
    return 0;
}


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

#define pi 3.141592654
#define fix(n) cout << fixed << setprecision(n)

#define rep(i,a,b) for(int i=a;i<b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)

#define ll  long long
#define ull unsigned long long

#define mp make_pair
#define pb push_back
#define fi first
#define se second

#define vi      vector<int>
#define pii     pair<int,int>
#define piii    pair<pair<int,int>,int>
#define all(v)  v.begin(), v.end()

int mod = 1000000007;
const int N = 1005;

// gcd
ll gcd(ll a , ll b)
{
    if ( b == 0)
    return a;
    return gcd( b, a % b);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, m;
	cin >> n >> m;
	ll x = pow(2,m);
	ll p = m * 1900;
	ll z = (n-m) * 100;
	ll ans = p + z;
	ans *= x;
	cout << ans << endl;
	return 0;
}


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

int main() {
  int a,b,c,d;
  cin >> a >> b >> c >> d ;
  cout <<(a+b==c+d ? "Balanced" : a+b>c+d ? "Left" : "Right")<< endl;
}

Measured memory_bytes: 384
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <random>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

ll N;
vector<ll> A;
vector<ll> cA;
vector<ll> factorial;
const ll MOD = 1e9 + 7;

void input() {
    cin >> N;
    A.resize(N);
    for (auto& v : A) {
        cin >> v;
    }
}

ll modinv(ll x) {
    ll ans = 1;
    const ll p = MOD - 2;
    ll cpow = x;
    for (ll i = 0; p >> i; i++) {
        if ((p >> i) & 1)
            ans = (ans * cpow) % MOD;
        cpow = (cpow * cpow) % MOD;
    }
    ans = (ans + MOD) % MOD;
    return ans;
}

ll solve() {
    ll cmi[N + 1];
    cmi[0] = 0;
    for (ll i = 1; i < N + 1; i++) {
        cmi[i] = (cmi[i - 1] + modinv(i)) % MOD;
    }
    ll ans = 0;
    for (ll i = 0; i < N; i++) {
        ll cpi = ((cmi[i + 1] + cmi[N - i] - cmi[1]) % MOD * A[i]) % MOD;
        ans = (ans + cpi) % MOD;
    }
    ll pat = 1;
    for (ll i = 1; i <= N; i++)
        pat = (pat * i) % MOD;
    ans = (ans * pat) % MOD;
    return (ans + MOD) % MOD;
}

int main() {
    input();
    
    cout << solve() << endl;
    
    return 0;
}



Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
using mii=map<int,int>;
using pii=pair<int,int>;
using vlai=valarray<int>;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9
 
using vd=vector<double>;
using vvd=vector<vd>;
 
 
int main(){
    int h,w;
    cin>>h>>w;
    vvi f(h+2,vi(w+2,1));
    vvi walk(h+2,vi(w+2,0));
    int gy,gx,y=0,x=0,v=0;
    range(i,1,h+1)range(j,1,w+1){
        char c;
        cin>>c;
        switch(c){
            case 'G': gy=i,gx=j,f[i][j]=0; break;
            case '.': f[i][j]=0;break;
            case '#': break;
            case '^': y=i,x=j,v=0,f[i][j]=0; break;
            case '>': y=i,x=j,v=1,f[i][j]=0; break;
            case 'v': y=i,x=j,v=2,f[i][j]=0; break;
            case '<': y=i,x=j,v=3,f[i][j]=0; break;
        }
    }
    walk[y][x]=1;
    rep(loop,(2650+2750)/2){
        vvi vec={{-1,0},{0,1},{1,0},{0,-1}};
        if(f[y+vec[(v+1)%4][0]][x+vec[(v+1)%4][1]]==0){ //右手が道なら右に曲がって前に進む
            //cout<<"hoge";
            v=(v+1)%4;
            y+=vec[v][0], x+=vec[v][1];
            walk[y][x]=1;
        }else if(f[y+vec[v][0]][x+vec[v][1]]==1){ //（右が壁で、）正面が壁なら左に回る
            //cout<<"fuga";
            v=(v+3)%4;
        }else{ //そうでないならまえに進む
            //cout<<"zako";
            y+=vec[v][0],x+=vec[v][1];
            walk[y][x]=1;
        }
        if(y==gy&&x==gx){
            break;
        }
        // cout<<y<<","<<x<<","<<v<<endl;
        // rep(i,h+2){
        //     rep(j,w+2){
        //         if(i==y&&j==x){
        //             cout<<"^>v<"[v];
        //         }else{
        //             cout<<".#"[f[i][j]];
        //         }
        //     }
        //     cout<<endl;
        // }
    }
    if(y==gy&&x==gx){
        int ans=0;
        rep(i,h+2)rep(j,w+2)ans+=walk[i][j];
        cout<<ans<<endl;
    }else cout<<-1<<endl;
    return 0;
}


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

using namespace std;

int main()
{
  int n, k;

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

  vector<int> a(n);

  for (int i = 0; i < n; i++)
  {
    scanf("%d", &a[i]);
  }

  vector<int> b(200100);

  for (int i = 0; i < 200100; i++)
  {
    b[i] = 0;
  }

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

  sort(b.begin(), b.end());

  int ans = 0;

  for (int i = 0; i < 200100 - k; i++)
  {
    ans += b[i];
  }

  printf("%d", ans);
}

Measured memory_bytes: 1792
#include<iostream> 
#include<string> 
#include<vector> 
#include<algorithm> 
using namespace std; 
int main()
{ 
	long long int n, m; 
	cin >> n >> m;
	vector<long long int> a(n); 
	vector<long long int> b(m); 
	for (int i = 0; i < n; i++)
	{
		cin >> a.at(i);
	} 
	long long int sum = 0;
	for (int i = 0; i < m; i++)
	{
		cin >> b.at(i);
		sum += b.at(i);
	}
	long long int ans = 0; 
	for (int i = 0; i < n; i++)
	{
		ans += a.at(i)*sum;
	}
	cout << ans << endl;

}



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


#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)

int n,q;
int a[125252];

int bef[125252], aft[125252];

int main(){
  scanf("%d%d",&n,&q);
  REP(i,n)scanf("%d",a+i),a[i]--;
  if(n==1){
    printf("%d\n",a[0]+1);
    return 0;
  }
  REP(i,n)bef[i] = aft[i] = -1;
  REP(i,n-1){
    int x = a[i];
    int y = a[i+1];
    aft[x] = y;
    bef[y] = x;
  }
  int headval = a[0];
  int tailval = a[n-1];
  while(q--){
    int x;
    scanf("%d",&x);
    x--;
    if(headval == x){
      // [head=x] / [nxt, ..., tail]
      // -> [nxt, ..., tail] - [x]
      int nxt = aft[x];
      aft[x] = bef[nxt] = -1;
      aft[tailval] = x;
      bef[x] = tailval;
      headval = nxt;
      tailval = x;
    }else if(tailval == x){
      // [head, ..., bef] / [tail=x]
      // -> [x] - [head, ..., bef]
      int prv = bef[x];
      bef[x] = aft[prv] = -1;
      bef[headval] = x;
      aft[x] = headval;
      headval = x;
      tailval = prv;
    }else{
      // [head, ..., s] / [x] / [t, ..., tail]
      // -> [t, ..., tail] - [x] - [head, ..., s]
      int s = bef[x];
      int t = aft[x];
      bef[x] = aft[s] = -1;
      aft[x] = bef[t] = -1;
      aft[tailval] = x;
      bef[x] = tailval;
      bef[headval] = x;
      aft[x] = headval;
      headval = t;
      tailval = s;
    }
  }
  int cur = headval;
  REP(i,n){
    printf("%d%c",cur+1,i==n-1?'\n':' ');
    cur = aft[cur];
  }
  return 0;
}


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

using namespace std;

#ifdef DEBUG_MODE
	#define DBG(n) n;
#else
	#define DBG(n) ;
#endif
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll,ll> P;

int main(){
	
	int n;cin >> n;
	
	if(n == 1){
		cout << "Hello World" << endl;
	}
	else {
		int a,b;cin >> a >> b;
		cout << a + b << endl;
	}
	
	return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<cmath>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

int N;
pll goal[1000];
vector<ll> d;

int main() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> goal[i].first >> goal[i].second;
	}

	int amari = (abs(goal[0].first) + abs(goal[0].second)) % 2;
	for (int i = 0; i < N; i++) {
		if (amari != ((abs(goal[i].first) + abs(goal[i].second)) % 2)) {
			cout << "-1" << endl;
			return 0;
		}
		goal[i] = make_pair(goal[i].first + goal[i].second, goal[i].first - goal[i].second);
	}

	for (int i = 30; i >= 0; i--) {
		d.push_back((1 << i));
	}

	if (amari == 0)d.push_back(1);

	cout << d.size() << endl;
	for (int i = 0; i < d.size(); i++) {
		cout << d[i];
		if (i != d.size() - 1)cout << " ";
	}
	cout << endl;

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < d.size(); j++) {

			int dx, dy;

			if (goal[i].first >= 0) {
				goal[i].first -= d[j];
				dx = 1;
			}
			else {
				goal[i].first += d[j];
				dx = -1;
			}

			if (goal[i].second >= 0) {
				goal[i].second -= d[j];
				dy = 1;
			}
			else {
				goal[i].second += d[j];
				dy = -1;
			}

			if (dx == 1 && dy == 1)cout << "R" ;
			else if (dx == 1 && dy == -1)cout << "U";
			else if (dx == -1 && dy == 1)cout << "D";
			else cout << "L";

		}
		cout << endl;
	}

	return 0;
}

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

int main() {
  ll n, m;
  cin >> n >> m;
  using P = pair<ll,ll>;
  vector<P> vp;
  rep(i, n){
    ll a,b;
    cin >> a >> b;
    vp.emplace_back(a,b);
  }
  sort(vp.begin(),vp.end());
  ll ans = 0;
  for(auto p: vp){
    if(m < 1) break;
    ans += min(p.second, m) * p.first;
    m -= p.second;
  }
  cout << ans << endl;
}


Measured memory_bytes: 2420
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
ll MOD = 1e9 + 7;

int main() {
    int x;
    cin >> x;
    if (x == 1)
        cout << 0 << endl;
    else
        cout << 1 << endl;
}

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

int main(){
 int n;
 cin >> n;
 char s;
 bool flag = false;
 for(int i=0;i<n;i++){
 	cin >> s;
   	if(s=='Y') flag = true;
 }
 if(flag) cout << "Four" << endl;
 else cout << "Three" << endl;
}

Measured memory_bytes: 256