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

typedef long long ll;
typedef unsigned long long ull;

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 10000
#define mod 1000000007

struct num{
	int val,idx,sgn;
};

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

  int N;
  cin>>N;
  vector<vector<int>> A(2,vector<int>(N));
  rep(k,2)rep(i,N)cin>>A[k][i];
  int res=INF;
  rep(S,(1<<N)){
  	vector<num> ns;
  	rep(i,N){
  		ns.push_back((num){A[(S>>i)&1][i],(int)i,(int)((((S>>i)&1)==0)?(i&1):(1-(i&1)))});
  	}
  	sort(all(ns),[](const num& a,const num& b){
  		if(a.val == b.val)return a.idx < b.idx;
  		else return a.val < b.val;
  	});
  	bool ok=true;
  	vector<int> B(N);
  	for(int i=0;i<N;){
  		int j=i;
  		while(j<N&&ns[j].val==ns[i].val)j++;
  		vector<ll> odk,evk;
  		vector<ll> odidx,evidx;
  		repl(k,i,j){
  			if(k%2==0)evk.push_back(k);
  			else odk.push_back(k);
  			if(ns[k].sgn==0) evidx.push_back(ns[k].idx);
  			else odidx.push_back(ns[k].idx);
  		}
  		if(odk.size()!=odidx.size()||evk.size()!=evidx.size()){
  			ok=false;
  			break;
  		}
  		rep(k,odk.size()){
  			B[odk[k]]=odidx[k];
  		}
  		rep(k,evk.size()){
  			B[evk[k]]=evidx[k];
  		}
  		i=j;
  	}
  	if(!ok)continue;
  	int sum=0;
  	rep(i,N)rep(j,i)if(B[i]<B[j])sum++;
  	minch(res,sum);
  }
  if(res==INF)cout<<-1<<endl;
  else cout<<res<<endl;

  return 0;
}


Measured memory_bytes: 384
#include<stdio.h>

int main(void)
{
	int b[3];
	scanf("%d %d %d",&b[0],&b[1],&b[2]);
	if(b[2]==1 || b[0]==1&&b[1]==1){
		printf("Open\n");
	}
	else{
		printf("Close\n");
	}
	return 0;
}


Measured memory_bytes: 2060
#include<iostream>
#include<string>
#include<stdio.h>
#include <algorithm>
#include <set>
#include<math.h>
#include<vector>

using namespace std;

int main(){
    int a,p;cin >> a >> p;
    int t = 3*a+p;
    cout << t/2 << endl;
}

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

namespace AOJ0061
{
    class Program
    {
        static void Main(string[] args)
        {
            string input;
            IDictionary<int, int> table = new Dictionary<int, int>();

            while (true)
            {
                input = Console.ReadLine();
                if (string.IsNullOrEmpty(input)) continue;
                string[] num = input.Split(',');
                if (int.Parse(num[0]) == 0 && int.Parse(num[1]) == 0) break;

                table[int.Parse(num[0])] = int.Parse(num[1]);
            }

            List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>(table);
            list.Sort((a, b)=> b.Value - a.Value);

            while (true)
            {
                input = Console.ReadLine();
                if (string.IsNullOrEmpty(input)) break;

                int team = int.Parse(input);
                int current = 0;
                int count = 0;

                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].Value != current)
                    {
                        count++;
                        current = list[i].Value;
                    }
                    if (list[i].Key == team) break; 
                }

                Console.WriteLine(count);
            }
        }
    }
}

Measured memory_bytes: 9684
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<queue>
#include<deque>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<sstream>
#include<list>
#include<climits>

using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define nrep(i,i0,n) for(int i=(i0);i<(int)(n);++i)
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vii;
typedef long long ll;

ll dp[51][51];

int main(){
	int n,p;cin>>n>>p;
	int odd,even;
	odd=even=0;
	rep(i,n){
		int t;cin>>t;
		if(t%2)odd++;
		else even++;
	}
	memset(dp,0,sizeof(dp));
	rep(i,n+1)rep(j,i+1){
		if(i==0||j==0||i==j)dp[i][j]=1;
		else dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
	}
	ll ret=0;
	rep(i,odd+1){
		if(i%2==0)continue;
		ret+=dp[odd][i];
	}
	rep(i,even)ret*=2;
	if(p==0){
		ll temp=1;
		rep(i,n)temp*=2;
		ret=temp-ret;
	}
	cout<<ret<<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;

typedef long long ll;

int main() {
    ll x, y, ans = 1;
    cin >> x >> y;
    while (x * 2 <= y) {
        x *= 2;
        ans++;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>   
#include<limits.h>
using namespace std;
#define i64  long long
//  #define int  long long
 
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807
#define MOD 1000000007


int fn(int X,vector<int> & D,vector<int> &kD)
{
  if(X == 0) return I64_MAX;
  return (kD[X] + X*(D[D.size()-1] - D[X]))/X;
}

void solve()
{
  int N;
  cin>>N;
  vector<int> vec(N+1,0);
  vector<int> D(N+1,0);
  vector<int> kD(N+1,0);
  vector<int> X_1(N+1,0);

  for (int i = 0; i < N; i++)
  {
    int A;
    cin>>A;
    // scanf("%d",&A);
    vec[A]++;
  }


  for (int i = 0; i < N+1; i++)
  {
    D[vec[i]]++;
  }


  for (int i = 0; i <= N; i++)
  {
    kD[i] = i *D[i];
  }

  for (int i = 0; i <= N-1; i++)
  {
    D[i+1] += D[i];
    kD[i+1] += kD[i];
  }

  for (int i = 1; i < N+1; i++)
  {
    X_1[i] = (kD[i] + i*(D[N] - D[i]))/i;
  }
  int X = N;
  for (int k = 1; k <= N; k++)
  {

    int ANS;
    while(X&&(k > X_1[X]))X--;
    ANS = X;
    cout<<ANS<<endl;
  }
  
  return;
}

int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  solve();
 
  return 0;
}

Measured memory_bytes: 5632
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>


using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

class AAlready2018 {
public:
void solve(std::istream& cin, std::ostream& cout) {
    string S;
    cin >> S;
    S[3] = '8';
    cout << S << endl;
}
};


int main() {
	AAlready2018 solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


Measured memory_bytes: 3592
N, X, Y = map(int, input().split())

X -= 1
Y -= 1

ans = [0]*(N-1)
for i in range(N):
    for j in range(i+1, N):
        if i == j:
            continue
        cnt = min(abs(i-j), abs(X-i)+abs(j-Y)+1, abs(j-X)+abs(Y-i)+1)
        ans[cnt-1] += 1

for i in range(N-1):
    print(ans[i])


Measured memory_bytes: 9336
n=int(input())
S=[0]*n
for i in range(n):
  S[i]=list(str(input()))

alfa='abcdefghijklmnopqrstuvwxyz'

ans=[]
for i in alfa:
  min_count=50
  for j in S:
    count = j.count(i)

    if count<min_count:
      min_count=count
  ans.append(i*min_count)
  print(i*min_count,end='')
#文字列が作れない場合空文字列を出力する
print()

Measured memory_bytes: 3060
#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

vector<bitset<40> > neighbor;
vector<int> strong;

vector<vector<int> > memo;

int solve(int i, bitset<40> ng)
{
    if(i == -1)
        return 0;

    bitset<40> ng2((1<<(i+1)) - 1);
    ng2 &= ng;
    if(i < 22 && memo[i][ng2.to_ulong()] != -1)
        return memo[i][ng2.to_ulong()];

    int ret = solve(i-1, ng);
    if(!ng[i])
        ret = max(ret, solve(i-1, ng |= neighbor[i]) + strong[i]);

    if(i < 22)
        memo[i][ng2.to_ulong()] = ret;

    return ret;
}

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        map<string, int> index;
        vector<vector<string> > d(n);
        strong.resize(n);
        for(int i=n-1; i>=0; --i){
            string s;
            int m;
            cin >> s >> strong[i] >> m;
            index[s] = i;
            d[i].resize(m);
            for(int j=0; j<m; ++j)
                cin >> d[i][j];
        }

        neighbor.assign(n, 0);
        for(int i=0; i<n; ++i){
            for(unsigned j=0; j<d[i].size(); ++j){
                int k = index[d[i][j]];
                neighbor[i][k] = true;
            }
        }

        memo.resize(22);
        for(int i=0; i<22; ++i)
            memo[i].assign(1<<(i+1), -1);

        cout << (solve(n-2, neighbor[n-1]) + strong[n-1]) << endl;
    }
}

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

int main() {
  int N, x; cin >> N>> x;
  int a[N];
  for(int i = 0; i<N; i++) cin >>a[i];
  sort(a, a+N);
  int ans = 0;
  for(int i = 0; i<N;i++){
    if(a[i]<=x){
      if(i!=N-1){
        x-=a[i];
        ans++;
      } else if(a[i]==x) ans++;
        }
    else {
      break;
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 3640
#include<iostream>
typedef long long ll;
using namespace std;
 
ll nextS(ll n){
	if(n%2==0)return n/2;
	else return (n+1)/2;
}
ll nextD(ll n){
	if(n%2==0)return n/2;
	else return (n-1)/2;
}

ll solve(ll s, ll d, ll acc){
	if(s==d)return acc;
	ll ans = (s%2==0?0:1) + (d%2==0?0:1);
	return solve(nextS(s), nextD(d), acc + ans);
}

int main(){
	int N;
	ll s,d;
	cin >> N;
	for(int i=0; i<N; i++){
		cin >> s >> d;
		cout << solve(s,d, 0) << endl;
	}
}

Measured memory_bytes: 1164
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i = (a);i <= (b);i++)
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
const int maxm = 1e6 + 5;
const double eps = 1e-8;
const double pi = acos(-1.0);
ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
string s;
int q,t,g;
int main(){
//	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> s;
	scanf("%d",&q);
	bool f = 0;
	string hd = "",bk = "";
	while(q--){
		scanf("%d",&t);
		if(t==1) f ^= 1;
		else{
			char ch;
			scanf("%d",&g); getchar();
			scanf("%c",&ch);
			if(g == 1){
				if(!f) hd += ch;
				else bk += ch;
			}
			else{
				if(!f) bk += ch;
				else hd += ch;
			}
		}
	}
	reverse(hd.begin(),hd.end());
	string ans = hd + s + bk;
	if(f) reverse(ans.begin(),ans.end());
	cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1348
macro_rules! read_line {
    () => {{
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).unwrap();
        input
    }};
}
pub fn read_and_parse<T: std::str::FromStr>() -> T {
    read_line!().trim().parse().ok().unwrap()
}
fn gcd(mut a: usize, mut b: usize) -> usize {
    while a % b != 0 {
        // instead of (a, b) = (b, a % b)
        std::mem::swap(&mut a, &mut b);
        b = (b % a + a) % a;
    }

    b
}
fn main() {
    let n: usize = read_and_parse();
    let mut ans: usize = 0;
    let mut mem: Vec<Vec<_>> = Vec::with_capacity(n);

    for i in 0..n {
        mem.push(Vec::with_capacity(n));
        for j in 0..n {
            mem[i].push(gcd(i+1, j+1));
        }
    }

    for i in 0..n {
        for j in 0..n {
            for k in 0..n {
                let x = mem[i][j];
                ans += mem[x-1][k];
            }
        }
    }

    println!("{}", ans);
}


Measured memory_bytes: 2424
h, w = map(int, input().split())
S = [list(input()) for i in range(h)]

for i in range(h):
    for j in range(w):
        flag = True
        if S[i][j] == '#':
            for di, dj in zip([-1, 0, 1, 0], [0, -1, 0, 1]):
                if 0 <= i+di < h and 0 <= j+dj < w and S[i+di][j+dj] == '#':
                    flag = False
                    break
        else:
            flag = False
        if flag:
            break
    if flag:
        break

if flag:
    print('No')
else:
    print('Yes')


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



int main(){
  int N;
  string jpy;
  cin>>N;
  
  double sum=0.0;
  vector<double> x(N);
  for(int i=0;i<N;i++){
    cin>>x[i];
    cin>>jpy;
    if (jpy=="JPY"){
      sum+=1.0*x[i];
    }else{
      sum+=380000.0*x[i];
    }
  }
  
    cout<<sum<<endl;
  
  
  return 0;
}

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

using namespace std;

struct edge {int from, to; string cost;};

int main()
{
	const string INF = "{}";
	int n, a, s, g;

	while(true){
		scanf("%d %d %d %d\n",&n, &a, &s, &g);
		if(n==0 && a==0 && s==0 && g==0){return 0;}
		edge* es = new edge[a];
		string* d = new string[n];
		string goal = "";

		for(int k=0; k<a; k++){
			cin >> (&es[k])->from >> (&es[k])->to >> (&es[k])->cost;
		}

		/* 逆からベルマンフォードによる最短路計算と負閉路判定 */
		for(int k=0; k<n; k++){d[k]=INF;}
		d[g]="";
		for(int p=0; p<=6*n; p++){
			bool update = false;
			for(int k=0; k<a; k++){
				edge e =es[k];
				if(d[e.to] != INF && d[e.from] > e.cost+d[e.to]){
					d[e.from] = e.cost+d[e.to];
					update = true;
				}
			}
			if((!update)&&(p<n)){goal = d[s];break;}
			else if (!update){break;}
			else if(p==(n-1)){goal = d[s];} //最短路の記憶
		}
		////////////////////////////////////////////////////////////

		if( (goal == INF)||(goal != d[s]) ){cout << "NO" << endl;}
		else{cout << goal << endl;}

		delete[] es;
		delete[] d;
	}
}

Measured memory_bytes: 1772
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
long long mo = 1e9 + 7;
typedef long long ll;
typedef pair<int,int> P;

int main(){
    ll N,x;
    cin >> N >> x;
    vector<ll> a(N),b(N,0);
    rep(i,N){
        cin >> a[i];
    }
    rep(i,N-1){
        b[i] = a[i] + a[i+1];
        //cout << b[i] <<" "<< a[i] << " " << a[i+1] <<endl;
    }
    //cout << endl;
    ll cnt = 0;
    rep(i,N-1){
        if(b[i] > x){
            cnt += (b[i] - x);
            b[i+1] -= min((b[i] - x), a[i+1]);
            b[i] = x;
            /*
            if(i > 0 && b[i-1] > x){
                b[i] -= min(b[i] - x, b[i-1] -x);
                b[i-1] -= min(b[i] - x, b[i-1] -x);
            }
            if(i <= N-2 && b[i+1] > x){
                b[i] -= min(b[i] - x, b[i+1] -x);
                b[i+1] -= min(b[i] - x, b[i+1] -x);
            }
            */
        }
    }
    cout << cnt << endl;
}

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

long long combination(int n, int r)
{
    if(n < r)
        return 0;
    if(n-r < r)
        r = n-r;
    long long ret = 1;
    for(int i=0; i<r; i++){
        ret *= (n--);
        ret /= i+1;
    }
    return ret;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);
    
    
    int n;
    cin>>n;
    string s;
    cin>>s;
    int ans = 0;
    for(int j=0;j<1000;j++){
        int cnt=0;
        int r=100;
        int now=0;
        int i=j;
        bool pos = false;
        while(now < n && s[now]!=('0'+i/r)) now++;
        r/=10;
        i %= 100;
        now++;
        while(now < n && s[now]!=('0'+i/r)) now++;
        r/=10;
        i %= 10;
        now++;
        while(now < n && s[now]!=('0'+i/r)) now++;
        if(now < n){
            //cerr << j << endl;
            ans++;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 384
import java.util.Scanner;
 
public class Main{
    public void solve(){
        Scanner sc=new Scanner(System.in);
         
        int n=sc.nextInt();
         
        for(int i=0;i<n;i++){
            int x=sc.nextInt();
            int y=sc.nextInt();
            int b=sc.nextInt();
            int p=sc.nextInt();
             
            int kin=x*b+y*p;
             
            if(b>=5 && p>=2){
                kin = (x*b + y*p)*4/5;
            }else{
                if(b<5){
                b=5;
                }
                if(p<2){
                p=2;
                }
                int wari = (x*b + y*p)*4/5;
                 
                if(kin>wari){
                    kin = wari;
                }
            }
            System.out.println(kin);
        }
    }
     
    public static void main(String[]args){
        Main obj = new Main();
        obj.solve();
    }
}

Measured memory_bytes: 23268
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#include <cmath>
#include <climits>
#define REP(i, n) for(int i=0; i<(n); i++)
#define FOR(i, a, n) for(int i=(a); i<(n); i++)
using namespace std;

int main(){
    int r,g,b;cin>>r>>g>>b;
    string ans = (10*g+b)%4 == 0 ? "YES":"NO";
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()


int main(){
    int n;
    ll x;
    cin>>n>>x;
    vector<ll> a(n);
    rep(i,n) cin>>a[i];
    
    ll ans=0;
    rep(i,n-1){
        if(a[i]+a[i+1]>x){
            if(a[i]>x){
                ans+=a[i+1];
                ans+=a[i]-x;
                a[i+1]=0;
            }
            else{
                ll tmp=a[i]+a[i+1]-x;
                ans+=tmp;
                a[i+1]-=tmp;
            }
        }
    }

    cout<<ans<<endl;
}

Measured memory_bytes: 4020
# python3
# -*- coding: utf-8 -*-
import sys
import math
#from math import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint

INF = float('inf')

ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
sys.setrecursionlimit(1000000000)


def lcm(a, b):
    return (a * b) // gcd(a, b)


# main
n,m = mis()
alist = lmis()

alist.sort()
alist.reverse()

min = sum(alist) * float(1/(4*m))

for i in range(m):
    if alist[i] < min:
        print('No')
        exit(0)

print('Yes')


Measured memory_bytes: 4064
# include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
typedef long long ll;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin>>s;
    stack<char>st;
    int ans=s.size();
    for(auto isi:s){
        if(isi=='S') {
            st.push('S');

        }
        else {
            if(st.empty()==false){
                st.pop();
                ans-=2;
            }
        }
    }   
    cout<<ans<<endl;
}

Measured memory_bytes: 720
#include<iostream>
#include<string>
#include<queue>
#include<utility>
#include<algorithm>
#include<cctype>
using namespace std;


int a[110];

int main(){

int n,num;
cin>>n;
	
	for(int i=0;i<n;i++){
	cin>>a[i];
	}
	
	sort(a,a+n);
	
	
	/*for(int i=0;i<n;i++){
	cout<<a[i];
	}
	cout<<endl;
	cout<<a[0]<<endl;
	cout<<a[n-1]<<endl;
	*/
	num=a[n-1]-a[0];
	
	cout<<num<<endl;

}

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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}


int nextInt() { int x; scanf("%d", &x); return x;}
ll nextLong() { ll x; scanf("%lld", &x); return x;}

const ll MOD = 1e9 + 7;

int main2() {
  int N = nextInt();
  vector<int> x(N);
  REP(i, N) x[i] = nextInt();

  ll ans = 1;
  ll fr = 0;
  for (int i = 0; i < N; i++) {
    bool possible = i==0 || fr <= (x[i-1]+1)/2;
    if (!possible) {
      ans *= fr;
      ans %= MOD;
      fr--;
    }
    fr++;
  }
  while (fr > 0) {
    ans *= fr;
    ans %= MOD;
    fr--;
  }

  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}

Measured memory_bytes: 640
#include <stdio.h>
#include <string.h>
#define  N 26

int main(){
	char input[N+1];
	int i,j;
	scanf("%s",input);

	for(i = 0; i < strlen(input); i++) {
		for (j = i+1; j < strlen(input); j++) {
			if(input[i] == input[j]) {
				printf("no\n");
				return 0;
			}
		}
	}
	printf("yes\n");
	return 0;
}


Measured memory_bytes: 128
c=[[0]*50for _ in range(50)]
r=0
for a in[*open(0)][1:]:
  a=round(float(a)*10**9)
  x=y=0
  while a%2<1:
    a//=2
    x+=1
  while a%5<1:
    a//=5
    y+=1
  r+=sum(sum(d[max(0,18-y):])for d in c[max(0,18-x):])
  c[x][y]+=1
print(r)

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

int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  vector<long long> S(N+1,0);
  int mod = 1000000007;
  for(int i=0; i<N; i++){
    cin >> A[i];
    S[i+1] = S[i] + A[i];
  }
  long long ans = 0;
  
  for(int i=0; i<N; i++){
    long long sum = (S[N] - S[i+1]) % mod;
    ans += A[i] * sum;
    ans %= mod;
  }
  cout << ans << endl;
}
    

Measured memory_bytes: 5636
from collections import deque

N, X, Y = [int(x) for x in input().split()]
result = {x: 0 for x in range(N)}

# 面倒なので0オリジンに修正
X -= 1
Y -= 1

# 全位置から移動できるパターンを幅優先探索で網羅する
for i in range(N):

    queue = deque()
    visited = set()

    # 開始位置と距離を初期値として入れる
    queue.appendleft((i, 0))

    while queue:
        now_pos, distance = queue.pop()

        # 訪れていない場合、その位置に移動するまでの距離に結果を加算
        # 訪問済フラグを立てる
        if not now_pos in visited:
            result[distance] += 1
            visited.add(now_pos)

        # 前、後ろ、X <-> Y の移動を実行
        # すでに訪問済の場合は追加しない
        if now_pos - 1 >= 0:
            if not now_pos - 1 in visited:
                queue.appendleft((now_pos - 1, distance + 1))

        if now_pos + 1 < N:
            if not now_pos + 1 in visited:
                queue.appendleft((now_pos + 1, distance + 1))

        if now_pos == X:
            if not Y in visited:
                queue.appendleft((Y, distance+1))

        if now_pos == Y:
            if not X in visited:
                queue.appendleft((X, distance+1))

for i in range(1, N):
    print(result[i] // 2)


Measured memory_bytes: 79804
#include"bits/stdc++.h"
using namespace std;
#define LL long long
#define REP(i, n) for(int (i)=0; (i)<(n); (i)++)
#define PB push_back
#define MP make_pair
#define MOD 1000000007

//7!=5040
//N=100

int N;
LL D[100],A[100];

map<pair<int,vector<int>>, vector<int>> dp2;
vector<int> ins(vector<int> v, int p){
	if(dp2.count(MP(p,v)) == 1) return dp2[MP(p,v)];

	//if(v.size() == 7) cout << "look" << endl;
	//for(int k=0; k<v.size(); k++) cout << v[k] << " ";
	//cout << endl;

	vector<int> memo = v;

	v.insert(v.begin()+p, 0);
	for(int i=0; i<v.size(); i++){
		if(v[i] < 7) v[i]++;
		else{
			v.erase(v.begin()+i);
			i--;
		}
	}

	//for(int k=0; k<v.size(); k++) cout << v[k] << " ";
	//cout << endl;

	return dp2[MP(p,memo)] = v;
}

map<pair<int,vector<int>>, LL> dp;
LL solve(int now, vector<int> v){ //v[i]はi番目に食べるもの、値域1～7
	if(now == N) return 0;
	if(dp.count(MP(now,v)) == 1) return dp[MP(now,v)];

	//for(int k=0; k<v.size(); k++) cout << v[k] << " ";
	//cout << endl;

	LL ans = 0;
	for(int i=0; i<v.size()+1; i++){
		LL s = 0;
		for(int j=0; j<i; j++){
			if(D[now-v[j]] >= v[j]){
				s += A[now-v[j]];
			}
		}
		for(int j=i; j<v.size(); j++){
			if(D[now] >= v[j]){
				s += A[now];
			}
		}
		s += solve(now+1, ins(v,i));
		ans = max(ans, s);
	}
	return dp[MP(now,v)] = ans;
}

int main(){
	scanf("%d", &N);
	for(int i=0; i<N; i++) scanf("%lld",D+i);
	for(int i=0; i<N; i++) scanf("%lld",A+i);

	vector<int> v;
	LL ans = solve(0,v);
	printf("%lld\n",ans);

	return 0;
}



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

using namespace std;

string s;

void CheckString(){

    string one = "dream", two = "erase", thr = "eraser", fur = "dreamer";

    // check last 5 charcters of string
        // does it match dream
        // does it match erase
    
    // if not check the last 6 characters of string
        // eraser
    
    // if not check the last 7 characters of string
        // dreamer

    bool done = true;
    
    while (done) {
        
        if (s.size() >= 5 && s.compare(s.size()-5, s.size(), one) == 0){
            s.erase(s.size()-5, s.size());
        }
        else if (s.size() >= 5 && s.compare(s.size()-5, s.size(), two) == 0){
            s.erase(s.size()-5, s.size());
        }     
        else if (s.size() >= 6 && s.compare(s.size()-6, s.size(), thr) == 0){
            s.erase(s.size()-6, s.size());
        } 
        else if (s.size() >= 7 && s.compare(s.size()-7, s.size(), fur) == 0){
            s.erase(s.size()-7, s.size());
        } 
        else {
            done = false;
        }
    }
}

int main () {

    cin >> s;
    CheckString();
    if (s.empty()) cout << "YES" << endl;
    else cout << "NO" << endl;

    return 0;
}

Measured memory_bytes: 512
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#define _USE_MATH_DEFINES
#include<math.h>
#include<queue>
#include<deque>
#include<stack>
#include<cstdio>
#include<utility>
#include<set>
#include<list>
#include<cmath>
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const ll dx[4] = { 0, -1, 1, 0 };
const ll dy[4] = { -1, 0, 0, 1 };
const int INF = 1e9 + 7;
int gcd(int x, int y) {
	if (x < y)swap(x, y);
	if (y == 0)return x;
	return gcd(y, x%y);
}
void mul(ll a, ll b) {
	a = a * b % INF;
}
double mysqrt(double x) {
	double l = 0, r = x;
	for (int i = 0; i < 64; ++i) {
		double m = (l + r) / 2.0;
		if (m*m < x)l = m;
		else r = m;
	}
	return l;
}
///////////////////////////////////////

int main() {
	string S; cin >> S;
	ll sum = 0;
	int cnt = 0;
	for (int i = 0; i < S.size(); ++i) {
		if (S[i] == 'W') {
			cnt++;
			sum += i;
		}
	}
	ll sum2 = 0;
	for (int i = 0; i < cnt; ++i) {
		sum2 += i;
	}
	cout << sum - sum2 << endl;
	return 0;
}


Measured memory_bytes: 640
using System;
using System.Linq;
using System.Collections.Generic;

class Program {

    static int MAX = 6;
    const int wht = 6, gry = 1, blk = 2;
    const int inf = int.MaxValue;

    static List<int>[] g = null;
    static List<int> srtLst = new List<int>();
    static bool[] added = null;

    static int[] stt = null;
    static int[] indeg = null;


    static void bfs(int s) {
        Queue<int> q = new Queue<int>();
        q.Enqueue(s);
        added[s] = true;

        while (q.Count > 0) {
            int u = q.Dequeue();
            srtLst.Add(u);

            for (int i = 0; i < g[u].Count; i++) {
                int v = g[u][i];
                indeg[v]--;

                if (indeg[v] == 0 && !added[v]) {
                    added[v] = true;
                    q.Enqueue(v);
                }
            }
        }
    }

    static void tsort() {
        indeg = indeg.Select(e => e = 0).ToArray();

        for (int i = 0; i < MAX; i++) {
            for (int j = 0; j < g[i].Count; j++) {
                int v = g[i][j];
                indeg[v]++;
            }
        }

        for (int i = 0; i < MAX; i++) {
            if (indeg[i] == 0 && !added[i]) bfs(i);
        }


    }

    static void Main(string[] args) {


        var inp = Console.ReadLine().Split(' ').Select(z => int.Parse(z)).ToArray();
        int v, e; v = inp[0]; e = inp[1];

        MAX = v;
        indeg = new int[MAX];
        stt = new int[MAX];
        added = new bool[MAX];
        g = new List<int>[MAX];


        added = added.Select(i => false).ToArray();

        g = g.Select(i => new List<int>()).ToArray();


        for (int i = 0; i < e; i++) {
            var c = Console.ReadLine().Split(' ').Select(z => int.Parse(z)).ToArray();

            g[c[0]].Add(c[1]);
        }


        tsort();

        foreach (var item in srtLst) {
            Console.WriteLine(item);
        }

    }
}



Measured memory_bytes: 23668
#include <bits/stdc++.h>
using namespace std;
using  ll = long long int;
using inti=int;
#define int long long int
// Function to sort the vector elements
// by second element of pairs
bool sortbysec(const pair<int, int>& a,
               const pair<int, int>& b)
{
    return (a.second < b.second);
}

// Function to find maximal disjoint set
void maxDisjointIntervals(vector<pair<int, int> > list)
{

    // Sort the list of intervals
    sort(list.begin(), list.end(), sortbysec);

    // First Interval will always be
    // included in set
    ll cnt=1;
//    cout << "[" << list[0].first << ", "
//         << list[0].second << "]" << endl;

    // End point of first interval
    int r1 = list[0].second;

    for (int i = 1; i < list.size(); i++) {
        int l1 = list[i].first;
        int r2 = list[i].second;

        // Check if given interval overlap with
        // previously included interval, if not
        // then include this interval and update
        // the end point of last added interval
        if (l1 >= r1) {
            cnt++;
            r1 = r2;
        }
    }
    cout<<cnt;
}
inti main()
{

    ll n;
    cin>>n;
    vector<pair<ll,ll>> v;
    for(ll i=0;i<n;i++)
    {

        ll x,l;
        cin>>x>>l;
        ll a=x-l,b=x+l;
        v.push_back({a,b});



    }
    maxDisjointIntervals(v);

}


Measured memory_bytes: 3440
#include <bits/stdc++.h>
#define l first
#define r second
#define INF (1e9)
using namespace std;
typedef pair <int,int> P;
int m,n,used[101],memo[101][101];
P cmd[101][101];

int check(int a,int b){

  if(memo[a][b]!=-1) return memo[a][b];
  for(int i=0;i<n;i++)
    if(cmd[a][i].l>cmd[b][i].r)return memo[a][b]=0;
  return memo[a][b]=1;
}

int solve(){
  memset(memo,-1,sizeof(memo));
  int flg=1;
  while(flg){
    flg=0;
    for(int i=0,j;i<m&&!flg;i++){
      if(used[i]) continue;
      j=0;
      for(j=0;j<m;j++)if(!used[j]&&!check(i,j))break;
      if(j==m) flg=used[i]=1;
    }
  }
  for(int i=0;i<m;i++) if(!used[i]) return 0;
  return 1;
}

int main(){
  int ans=1;
  for(int i=0;i<101;i++)
    for(int j=0;j<101;j++)cmd[i][j]=P(-INF,INF);

  cin>>m>>n;
  for(int i=0;i<m;i++){
    int k;
    cin>>k;
    while(k--){
      string cond;
      int s,t;
      cin>>s>>cond>>t;
      s--;
      if(cond=="<=")cmd[i][s].r=min(cmd[i][s].r,t);
      if(cond==">=")cmd[i][s].l=max(cmd[i][s].l,t);
      if(cmd[i][s].l>cmd[i][s].r)ans=0;
    }
  }

  cout << (ans*solve()? "Yes":"No")<<endl;
  return 0;
}

Measured memory_bytes: 1336
#include <bits/stdc++.h>
#define rep(i,s,t) for (int i=s;i<=t;++i)
typedef long long ll;
const int maxn=1e5+10;
const int mod=1e9+7;
using namespace std;
ll fac[maxn],inv[maxn];
ll quick_mod(ll x,ll y){
    ll ans=1;
    for(;y;y>>=1){
        if (y&1) ans=ans*x%mod;
        x=x*x%mod;
    }
    return ans;
}
void init(){
    fac[0]=1;
    rep(i,1,maxn-10) fac[i]=fac[i-1]*i%mod;
    inv[maxn-10]=quick_mod(fac[maxn-10],mod-2);
    for(int i=maxn-11;i>=0;--i) inv[i]=inv[i+1]*(i+1)%mod;
}
ll C(int n,int m){
    return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
ll invv[maxn];
ll dp[1010][1010];
int main()
{
    init();
    int n,a,b,c,d;
    cin>> n >> a >> b >> c >> d;
    rep(i,0,b+1) dp[i][0]=1;
    rep(i,1,n) invv[i]=quick_mod(i,mod-2);
    rep(i,a,b){
        rep(j,0,n){
            if (!dp[i][j]) continue;
            if (j) (dp[i+1][j]+=dp[i][j])%=mod;
            ll tmp=1;
            rep(k,1,d){
                if (j+k*i>n) break;
                (tmp*=C(n-j-i*(k-1),i)*invv[k]%mod)%=mod;
                if (c<=k&&k<=d){
                    (dp[i+1][j+k*i]+=(dp[i][j]*tmp)%mod)%=mod;
                }
            }
        }
    }
    cout<< dp[b+1][n] <<endl;
    return 0;
}


Measured memory_bytes: 10496
def can_set(num, string):
    d0, d1, d2 = num

    ind0 = string.find(d0)
    if ind0 == -1 or ind0 == len(string) - 1:
        return False

    string = string[ind0 + 1:]

    ind1 = string.find(d1)
    if ind1 == -1 or ind1 == len(string) - 1:
        return False

    string = string[ind1 + 1:]

    ind2 = string.find(d2)

    if ind2 == -1:
        return False

    return True


n = int(input())
s = input().strip()

count = 0
for i in range(1000):
    num = str(i).zfill(3)
    if can_set(num, s):
        count += 1

print(count)


Measured memory_bytes: 3188
#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long a,b;
	char c;
	while(~scanf("%lld %c %lld",&a,&c,&b)){
		if(c=='+'){
			printf("%lld\n",a+b);
		}else{
			printf("%lld\n",a-b);
		}
	}
	return 0;
}

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

using namespace std;

#define ll long long
#define uint unsigned int

void yes() {
    cout << "Yes\n";
    exit(0);
}

int main() {
    string s;
    cin >> s;

    bool good = 1;
    for (int i = 0; i < 3; ++i) {
        if (s[i] != s[0]) {
            good = 0;
        }
    }
    if (good) {
        yes();
    }

    good = 1;
    for (int i = 1; i < 4; ++i) {
        if (s[i] != s[1]) {
            good = 0;
        }
    }

    if (good) {
        yes();
    }

    cout << "No\n";

    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main()
{
  string s;
  cin >> s;
  vector<string> vec={"SUN","MON","TUE","WED","THU","FRI","SAT" };
  map<string, int> m;
  rep(i, vec.size()) {
    m[vec[i]] = 7 - i;
  }
  cout << m[s] << endl;
  
  return 0;
}


Measured memory_bytes: 3616
#include<bits/stdc++.h>
using namespace std;
int main(){
	double n,m;
	cin>>n>>m;
	int ok=-1;
	for(int i=0;i<=9;i++){
		double j=double(m*10+i)*0.08;
		if(int(j)==n){
			ok=m*10+i;
			break;
		}
	}
	cout<<ok<<endl;
	return 0;
}


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

struct unionfind{
  vector<int> par;
  vector<int> size;
  unionfind(int n):par(n),size(n,1){
      rep(i,n)par[i]=i;
  }
  int root(int x){
      if(par[x]==x)return x;
      return root(par[x]);
  }
  void unite(int x,int y){
      x=root(x);
      y=root(y);
      if(x==y)return;
      if(size.at(par[x])<size.at(y))swap(x,y);
      size.at(y)+=x;
      par[y]=x;
  }
  bool same(int x,int y){
      return root(x)==root(y);
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n,q;cin>>n>>q;
  unionfind tree(n);
  rep(i,q){
      int com,x,y;
      cin>>com>>x>>y;
      if(com==0)tree.unite(x,y);
      else{
        if(tree.same(x,y))cout<<1<<endl;
        else cout<<0<<endl;
      }
  }
  return 0;
}



Measured memory_bytes: 3304
// Code for B - Algae
#include <bits/stdc++.h>
using namespace std;

int main() {
  int r, d, x2000;
  cin >> r >> d >> x2000;

  int tmp = x2000;
  for (int i = 1; i <= 10; i++) {
    int next = tmp * r - d;
    cout << next << endl;
    tmp = next;
  }
	return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#pragma warning(disable:4996)
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define MOD 1000000007
using namespace std;

int main(int argc, char* argv[])
{
    long h, w;
    scanf("%ld%ld", &h, &w);

    char str[100][100]={0};
    char str2[100][100]={0};

    long i, j;
    for(i=0; i<h; i++) {
        scanf("%s", str[i]);
    }

    for(i=0; i<h; i++) {
        for(j=0; j<w; j++) {
            if(str[i][j]=='#') {
                str2[i][j]='#';
            }
            else {
                str2[i][j]='0';
                if(i>0) {
                    if(j>0 && str[i-1][j-1]=='#') str2[i][j]++;
                    if(str[i-1][j]=='#') str2[i][j]++;
                    if(j<w-1 && str[i-1][j+1]=='#') str2[i][j]++;
                }
                {
                    if(j>0 && str[i][j-1]=='#') str2[i][j]++;
                    if(j<w-1 && str[i][j+1]=='#') str2[i][j]++;
                }
                if(i<h-1) {
                    if(j>0 && str[i+1][j-1]=='#') str2[i][j]++;
                    if(str[i+1][j]=='#') str2[i][j]++;
                    if(j<w-1 && str[i+1][j+1]=='#') str2[i][j]++;
                }
            }
        }
    }

    for(i=0; i<h; i++) {
        printf("%s\n", str2[i]);
    }
   

    return 0;
}


Measured memory_bytes: 128
N = int(input())
def calc(N):
    if len(str(N)) == 1:
        return N
    if len(list(set(str(N)[1:]))) == 1 and '9' in  list(set(str(N)[1:])):
        return int(str(N)[0])+9*len(str(N)[1:])
    ret = 0
    f = 0
    for idx,c in enumerate(str(N)):
        tmp = int(c)
        if f == 1:
            ret += 9
            continue
        if tmp != 9 and idx == 0:
            ret += tmp-1
            f = 1
            continue
        elif tmp != 9:
            ret -= 1
            f = 1
            ret += 9
            continue
        else:
            ret += 9
            continue
    return ret
print(calc(N))


Measured memory_bytes: 3064
//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF= 1e+18+1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll> >vvl;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> T;
const ll MOD=1000000007LL;
string abc="abcdefghijklmnopqrstuvwxyz";
string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main(){
  ll a,b,c;cin>>a>>b>>c;
  vl vec={5,5,7};
  vl v={a,b,c};
  sort(v.begin(),v.end());
  if(v==vec)cout<<"YES"<<endl;
  else cout<<"NO"<<endl;
}

Measured memory_bytes: 256
#include <iostream>
#include<bits/stdc++.h>
#include<vector>
#include<string>
#define ll long long
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vi vector<ll>
#define el printf("\n")
#define N 100001
using namespace std;
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.first < b.first);
}


int main()
{   IOS;
cout.precision(30);
   ll n,m;
   cin >> n >>m ;
   vector<pair<ll,ll>> L;
   vi L1;
   for(int i=0;i<n;i++)
   {
       ll x,y;
       cin >> x >> y;
       L.push_back(make_pair(x,i));
       L1.push_back(y);
   }
   sort(L.begin(),L.end(),sortbysec);
   ll res=0;
   ll j=m;
   ll i=0;
   while (j!=0)
   {

       if(L1[L[i].second]>=j)
         {res+=(L[i].first)*j;
         j=0;
         }
       else if (L1[L[i].second]<=j)
       {
           j=j-L1[L[i].second];
           res+=L1[L[i].second]*L[i].first;
          // cout << res << endl;
       }
        i++;
   }
    cout << res;
    return 0;
}


Measured memory_bytes: 3696
#include<bits/stdc++.h>
using namespace std;
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<long long> vlong;
#define _GLIBCXX_DEBUG
#define vpush(a,x) a.push_back(x);
#define rep(i, n) REP(i, 0, n)
#define all(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
const int INF = 1 << 30;
const int dx[] = {1,0,-1,0,1,1,-1,-1};
const int dy[] = {0,-1,0,1,1,-1,-1,1};
#define stp(x)  setprecision(x)
int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n,a,b;
  cin>>n>>a>>b;
  int count=0;
  REP(i,1,n+1){
    int tmp=i;
    int sum=0;
    while(true){
      if(tmp==0){
        break;
      }
      sum+=tmp%10;
      tmp/=10;
    }
    if(a<=sum && sum<=b){
      count+=i;
    }
  }
  cout<<count<<'\n';
  return(0);
}


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

using namespace std;

int n, k;
int sc[3], tr[150];
int f[N][2];
char s[N];

int main() {
	scanf("%d%d", &n, &k);
	tr['r'] = 2, tr['p'] = 1, tr['s'] = 0;
	scanf("%d%d%d", &sc[0], &sc[1], &sc[2]);
	scanf("%s", s + 1);
	int ans = 0;
	for(int i = 1; i <= k; ++i) {
		for(int j = i; j <= n; j += k) {
			if(j - k < 0) {
				f[j][1] = sc[tr[s[j]]];
				f[j][0] = 0;
			} else {
				if(s[j] == s[j - k]) {
					f[j][1] = sc[tr[s[j]]] + f[j - k][0];
					f[j][0] = max(f[j - k][1], f[j - k][0]);
				} else {
					f[j][1] = sc[tr[s[j]]] + max(f[j - k][1], f[j - k][0]);
					f[j][0] = max(f[j - k][1], f[j - k][0]);
				}
			}
			if(j + k > n) {
				ans += max(f[j][0], f[j][1]);
			}
		}
	}
	cout << ans << endl;
	return 0;
}

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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

const int mod=1000000007;
int inv2=(mod+1)/2;

inline void add(int &a,int b){
    a+=b;
    if(a>=mod)a-=mod;
}

int N,Q;
int A[3333];
int X[3333],Y[3333];

int w[3333][3333];

signed main(){
    cin>>N>>Q;
    rep(i,N)cin>>A[i];

    rep(i,Q){
        cin>>X[i]>>Y[i];
        X[i]--;Y[i]--;
    }

    rep(i,N)rep(j,i)w[i][j]=1;

    for(int i=Q-1;i>=0;i--){
        int x=X[i];
        int y=Y[i];

        rep(j,N)if(j!=x&&j!=y){
            w[j][x]=w[j][y]=(w[j][x]+w[j][y])*inv2%mod;
            w[x][j]=w[y][j]=(w[x][j]+w[y][j])*inv2%mod;
        }
        w[x][y]=w[y][x]=(w[x][y]+w[y][x])*inv2%mod;
    }

    int K=1;rep(i,Q)K=K*2%mod;
    rep(i,N)rep(j,N)w[i][j]=w[i][j]*K%mod;

    int ans=0;
    rep(i,N)for(int j=i+1;j<N;j++){
        if(A[i]<A[j]){
            add(ans,w[i][j]);
        }
        else if(A[i]>A[j]){
            add(ans,w[j][i]);
        }
    }
    cout<<ans<<endl;
    return 0;
}



Measured memory_bytes: 80128
#include "bits/stdc++.h"
using namespace std;

int main() {
	int N;
	cin >> N;
	vector<pair<long long, long long>>pos(N);
	for (int i = 0; i < N; ++i) {
		cin >> pos[i].first >> pos[i].second;
	}

	int par = (abs(pos[0].first) + abs(pos[0].second)) % 2;
	for (int i = 0; i < N; ++i) {
		int par2 = (abs(pos[i].first) + abs(pos[i].second)) % 2;
		pos[i] = pair<long long, long long>(pos[i].first + pos[i].second, pos[i].first - pos[i].second);
		if (par2 != par) {
			cout << -1 << endl;
			return 0;
		}
	}

	vector<long long>d;
	for (int i = 30; i >= 0; --i) {
		d.push_back(1LL << i);
	}
	if (0 == par % 2) {
		d.push_back(1);
	}
	cout << d.size() << endl;
	for (int i = 0; i < d.size(); ++i) {
		if (0 != i) {
			cout << " ";
		}
		cout << d[i];
	}
	cout << endl;

	for (int i = 0; i < N; ++i) {
		int xdir, ydir;
		long long xsum = 0;
		long long ysum = 0;
		for (int j = 0; j < (int)d.size(); ++j) {
			if (xsum <= pos[i].first) {
				xdir = 1;
				xsum += d[j];
			}
			else {
				xdir = -1;
				xsum -= d[j];
			}
			if (ysum <= pos[i].second) {
				ydir = 1;
				ysum += d[j];
			}
			else {
				ydir = -1;
				ysum -= d[j];
			}

			if (xdir == 1 && ydir == 1) {
				cout << "R";
			}
			else if (xdir == 1 && ydir == -1) {
				cout << "U";
			}
			else if (xdir == -1 && ydir == -1) {
				cout << "L";
			}
			else {
				cout << "D";
			}
		}
		cout << endl;
	}
	return 0;
}


Measured memory_bytes: 3648
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main()
{
    int N;
    cin >> N;
    string s[N];
    rep(i, N) cin >> s[i];
    int ans = 0;
    int A = 0, B = 0, AB = 0;
    rep(i, N) {
        rep(j, (int)s[i].size() - 1) {
            if (s[i][j] == 'A' && s[i][j + 1] == 'B') ans++;
        }
        if (s[i][0] == 'B' && s[i][(int)s[i].size() - 1] == 'A') AB++;
        else if (s[i][0] == 'B') B++;
        else if (s[i][(int)s[i].size() - 1] == 'A') A++;
    }

    ans += max(0, AB - 1);
    if (A != 0 || B != 0) {
        if (AB != 0) {
            A++;
            B++;
        }
    }

    ans += min(A, B);

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 768
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;
 
int main(){
    int N, Q;
    cin >> N >> Q;
    string S;
    cin >> S;

    vector<char> t(Q), d(Q);
    for(int i=0;i<Q;i++){
        cin >> t[i] >> d[i];
    }

    int ans_l, ans_r;
    {
        // [l, r)
        int l = -1, r = N;
        while(r - l > 1){
            int mid = (l + r) / 2;
            int i = mid;
            bool dead = false;
            for(int j=0;j<Q;j++){
                if(S[i] == t[j]){
                    if(d[j] == 'L'){
                        i--;
                    }else{
                        i++;
                    }
                }
                if(i <= -1 || i >= N){
                    dead = true;
                    break;
                }
            }
            if(dead){
                l = mid;
            }else{
                r = mid;
            }
        }
        ans_l = r;
    }

    {
        // [l, r)
        int l = -1, r = N;
        while(r - l > 1){
            int mid = (l + r) / 2;
            int i = mid;
            bool dead = false;
            for(int j=0;j<Q;j++){
                if(S[i] == t[j]){
                    if(d[j] == 'L'){
                        i--;
                    }else{
                        i++;
                    }
                }
                if(i <= -1 || i >= N){
                    dead = true;
                    break;
                }
            }
            if(!dead){
                l = mid;
            }else{
                r = mid;
            }
        }
        ans_r = r;
    }

    cout << max(0, ans_r - ans_l) << endl;

    return 0;
}


Measured memory_bytes: 1024
//write：Sakura
//id：
//problem：
//note：

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long ll;

int n;
int l[505];
ll ans=0;

int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	scanf("%d",&n);
	for(int i=0;i<2*n;i++)scanf("%d",&l[i]);
	sort(l,l+2*n);
	for(int i=0;i<2*n;i+=2){
		ans+=l[i]; 
	}
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 256