akhauriyash/Code-Regression huggingface dataset:
using System;

namespace Test
{
    
    class MainClass
    {
        public static void Main(string[] args)
        {
            const string Print = "print";
            const string Reverse = "reverse";
            const string Replace = "replace";
            var str = Console.ReadLine().ToCharArray();
            var num = int.Parse(Console.ReadLine());
            for (int i = 0; i < num; i++)
            {
                var commandLine = Console.ReadLine().Split();
                var command = commandLine[0];
                var paramA = int.Parse(commandLine[1]);
                var paramB = int.Parse(commandLine[2]);
                switch (command)
                {
                    case Print:
                        for (int j = paramA; j <= paramB; j++)
                        {
                            Console.Write(str[j]);
                        }
                        Console.WriteLine();
                        break;
                    case Reverse:
                        Array.Reverse(str, paramA, paramB - paramA + 1);
                        break;
                    case Replace:
                        var index=0;
                        for (int j = paramA; j <= paramB; j++)
                        {
                            str[j] = commandLine[3][index++];
                        }
                        break;
                }
            }
        }
    }
}

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

using namespace std;

typedef long long ll;
typedef long double ld;

#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(v) v.begin(),v.end()

const int maxn = 1e5 + 100;
const ll inf = 2e18, mod = 1e9 + 7;

int n;
string s, t, ans;

bool good(string S)
{
	if (S.substr(0, n) == s && S.substr(S.size() - n, n) == t)
		return true;
	return false;
}

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

	cin >> n >> s >> t;
	if (s == t)
	{
		cout << n << endl;
		return 0;
	}
	for (int i = 0; i < n; i++)
	{
		ans += s[i];
		if (good(ans + t))
		{
			ans = ans + t;
			break;
		}
		if (i == n - 1)
			ans = ans + t;
	}
	cout << ans.size() << endl;
}



Measured memory_bytes: 256
n=int(input())
l=list(map(int,input().split()))
c=0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            if l[i]!=l[j] and l[k]!=l[j] and l[i]!=l[k] and abs(l[i]-l[j])< l[k] <l[i]+l[j]:
                c+=1
print(c)
        

Measured memory_bytes: 9100
import std.stdio;
import std.string;
import std.conv;
import std.typecons;
import std.algorithm;
import std.functional;
import std.bigint;
import std.numeric;
import std.array;
import std.math;
import std.range;
import std.container;
import std.ascii;

void main(){
	auto ip = readln.split.to!(int[]);
	if(ip[0] > ip[2]){
		"NO".writeln;
	}
	else if(ip[2] - ip[0] <= ip[1]){
		"YES".writeln;
	} else {
		"NO".writeln;
	}
}

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

int main() {
  std::int_fast64_t k, s;
  std::cin >> k >> s;
  std::cout << (s < k ? (s + 1) * (s + 2) / 2 : s < 2 * k ? (k + 1) * (k + 1) - (2 * k - s) * (2 * k - s + 1) / 2 - (s - k) * (s - k + 1) / 2 : (3 * k - s + 1) * (3 * k - s + 2) / 2) << std::endl;
  return 0;
}

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

using namespace std;

typedef long long ll;

const ll MOD = 1000000007;
vector<ll> sil(200000);

ll r(ll x)
{
    if (x >= 0)
        return x % MOD;
    return (x % MOD + MOD) % MOD;
}

ll Pot(ll p, ll w)
{
    if (w == 0)
        return 1;
    if (w % 2 == 0)
    {
        ll pol = Pot(p, w / 2);
        return pol * pol % MOD;
    }
    return Pot(p, w - 1) * p % MOD;
}

ll Inv(ll n)
{
    return Pot(n, MOD - 2);
}

ll C(ll n, ll k)
{
    return sil[n] * Inv(sil[n - k] * sil[k] % MOD) % MOD;
}

int main()
{
    ios_base::sync_with_stdio(0);
    sil[0] = 1;
    for (ll i = 1; i < 200000; ++i)
        sil[i] = (sil[i - 1] * i) % MOD;
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    if (k == 1)
    {
        cout << "0";
        return 0;
    }
    ll odp = 0;
    for (int i = 0; i + k <= n; ++i)
        odp = (odp + r(a[i]) * C(n - i - 1, k - 1)) % MOD;
    for (int i = n - 1; i - k + 1 >= 0; --i)
    {
        odp -= (r(a[i]) * C(i, k - 1) % MOD);
        if (odp < 0)
            odp += MOD;
    }
    cout << odp;
    return 0;
}


Measured memory_bytes: 2560
#include<bits/stdc++.h>
using namespace std;
static const int mod=1000000007;
int main(){
int H,W;
  cin>>H>>W;
  vector<vector<char>>A(1+H,vector<char>(1+W,'#'));
  for(int i=1;i<=H;i++){
  string s;
    cin>>s;
    for(int j=1;j<=W;j++){
    A[i][j]=s.at(j-1);
  }
  }vector<vector<int>>dp(1+H,vector<int>(1+W,0));
   dp[1][0]=1;
  for(int i=1;i<=H;i++){
  for(int j=1;j<=W;j++){
  if(A[i][j]=='.')
    dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;
  
  
  }
  
  }cout<<dp[H][W]<<endl;
   return 0;
}

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

#define ll long long int
#define vl vector<ll>
#define vvl vector<vl>

ll m = 998244353;

int main() {

    ll N, M, K;
    cin >> N >> M >> K;

    vector<ll> modinv(2e5+9);
    modinv[1] = 1;
    for (int i = 2; i <= 2e5+3; i++) {
        modinv[i] = modinv[m%i] * (m-m/i) % m;
    }

    if (M == 1) {
        if (K == N-1) cout << 1 << endl;
        else cout << 0 << endl;
        return 0;
    }

    ll a = M;
    for (int i = 0; i < N-1; ++i) {
        a = (a * (M-1)) % m;
    }
    ll b = 1;

    ll ans = (a * b) % m;
    for (int k = 1; k <= K; ++k) {
        a = (a*modinv[M-1]) % m;
        b = (b*(N-k)%m) * (modinv[k]%m) % m;
        ans = (ans + a*b) % m;
    }

    cout << ans << endl;


    return 0;
}


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

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

  map<char, int> p;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'N')
      p['N']++;
    else if (s[i] == 'S')
      p['S']++;
    else if (s[i] == 'W')
      p['W']++;
    else if (s[i] == 'E')
      p['E']++;
  }

  if (((p['N'] == 0 && p['S'] == 0) || p['N'] * p['S'] >= 1) &&
      ((p['W'] == 0 && p['E'] == 0) || p['W'] * p['E'] >= 1))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <cstring>
 
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i, n) for(int i = 0; i < (n); i++)
#define MP make_pair
#define X first
#define Y second
#define all(v) v.begin(), v.end()
#define rev(v) v.rbegin(), v.rend()

using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;

const int INF = 1<<29;

int dp[3005];
int v[3005];
int w[3005];

int main(){
	int N, T, S;
	cin >> N >> T >> S;
	rep(i, N) cin >> v[i] >> w[i];

	rep(i, N){
		for(int j = T-w[i]; j >= 0; j--){
			if(j < S && S < j+w[i]) continue;
			dp[j+w[i]] = max(dp[j+w[i]], dp[j]+v[i]);
		}
	}
	int ans = 0;
	rep(i, T+1){
		ans = max(ans, dp[i]);
	}
	cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1196
all = [x for x in range(1,53)]

N = int(input())
i = 1

while i <= N:
	M, num = input().split()
	num = int(num)
	if M == 'S':
		all.remove(num)
	elif M == 'H':
		all.remove(num+13)
	elif M == 'C':
		all.remove(num+26)
	elif M == 'D':
		all.remove(num+39)
	
	i += 1

sN = 52 - N

for j in range(sN):
	if all[j] // 13 == 0 or (all[j] // 13 == 1 and all[j] % 13 == 0):
		print("S {}".format(all[j]))
	elif all[j] // 13 == 1 or (all[j] // 13 == 2 and all[j] % 13 == 0):
		print("H {}".format(all[j]-13))
	elif all[j] // 13 == 2 or (all[j] // 13 == 3 and all[j] % 13 == 0):
		print("C {}".format(all[j]-26))
	else:
		print("D {}".format(all[j]-39))

Measured memory_bytes: 7660
import bisect
# import heapq
# from collections import deque
# from collections import Counter
# from fractions import gcd # >=Python3.5 # lcm（最小公倍数） = (a*b)//gcd(a,b)
# from fractions import Fraction
# from math import gcd # <Python3.5
# from math import sqrt

#input = sys.stdin.readline
#def main():

#---------------------------------------------------------

n = int(input())
 
tmp = 0
li1 = []
li2 = []
 
alp = [chr(i) for i in range(97, 97+26)]
 
for i in range(200):
    tmp += 26**i
    li1.append(tmp)
    if tmp > 10**16:
        break
 
keta = bisect.bisect_right(li1,n)
 
m = n-li1[keta-1]
 
for i in range(200):
    a = m//26
    b = m%26
    li2.append(chr(97+b))
    if a==0:
        break
    if a<26:
        li2.append(chr(97+a))
        break
    m -= b
    m = m//26
 
li2.reverse()
l = len(li2)
 
for i in range(keta):
    if l < keta:
        li2.insert(0,'a')
        l+=1
    else:
        break
 
print(*li2, sep='')

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

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(),x.end()
#define REACH cerr<<"reached line "<<__LINE__<<endl
#define DBG(x) cerr<<"line "<<__LINE__<<" "<<#x<<":"<<x<<endl

using pi=pair<int,int>;
using vi=vector<int>;
using ld=long double;

template<class T,class U>
ostream& operator<<(ostream& os,const pair<T,U>& p){
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator <<(ostream& os,const vector<T>& v){
	os<<"[";
	REP(i,(int)v.size()){
		if(i)os<<",";
		os<<v[i];
	}
	os<<"]";
	return os;
}

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

void printSpace(){
	printf(" ");
}

void printEoln(){
	printf("\n");
}

void print(int x,int suc=1){
	printf("%lld",x);
	if(suc==1)
		printEoln();
	if(suc==2)
		printSpace();
}

string readString(){
	static char buf[3341919];
	scanf("%s",buf);
	return string(buf);
}

char* readCharArray(){
	static char buf[3341919];
	static int bufUsed=0;
	char* ret=buf+bufUsed;
	scanf("%s",ret);
	bufUsed+=strlen(ret)+1;
	return ret;
}

template<class T,class U>
void chmax(T& a,U b){
	if(a<b)
		a=b;
}

template<class T,class U>
void chmin(T& a,U b){
	if(a>b)
		a=b;
}

template<class T>
T Sq(const T& t){
	return t*t;
}

const int inf=LLONG_MAX/3;

int p(int a,int b,int c){
	return (a+b)%c;
}
int m(int a,int b,int c){
	return (a-b%c+c)%c;
}

signed main(){
	int n=read(),l=read(),t=read();
	assert(1<=n&&n<=100000);
	assert(n<=l&&l<=1000000000);
	assert(0<=t&&t<=1000000000);
	vi x,w;
	int last=-1;
	REP(i,n){
		x.PB(read()),w.PB(read());
		assert(last<x[i]&&x[i]<l);
		assert(w[i]==1||w[i]==2);
		last=x[i];
	}
	vi res;
	REP(i,n)res.PB(w[i]==1?p(x[i],t,l):m(x[i],t,l));
	int root=0,pos=res[0];
	sort(ALL(res));
	if(w[0]==1){
		REP(i,n)if(w[i]==2){
			int a=x[i]-x[0],g=2*t;
			if(g>a){
				g-=a;
				root=p(root,(g-1)/l+1,n);
			}
		}
	}else{
		REP(i,n)if(w[i]==1){
			int a=l+x[0]-x[i],g=2*t;
			if(g>=a){
				g-=a;
				root=m(root,g/l+1,n);
			}
		}
	}
	pos=lower_bound(ALL(res),pos)-res.begin();
	vi ans(n);
	FOR(i,pos,n)ans[(root++)%n]=res[i];
	REP(i,pos)ans[(root++)%n]=res[i];
	REP(i,n)print(ans[i],i==n-1?1:2);
}


Measured memory_bytes: 4460
INF = 10 ** 9

n, m = map(int, input().split())
a = [[INF] * n for _ in range(n)]
e = [None] * m
for i in range(n): a[i][i] = 0
for i in range(m):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    e[i] = (u, v, w)
    a[u][v] = w
    a[v][u] = w
for k in range(n):
    for i in range(n):
        for j in range(n):
            a[i][j] = min(a[i][j], a[i][k] + a[k][j])
c = 0
for u, v, w in e:
    if a[u][v] != w: c += 1
print(c)

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define sf scanf
#define pf printf
#define pb push_back
#define mp make_pair
#define PI ( acos(-1.0) )
#define mod 1000000007LL
#define IN freopen("pieaters.in","r",stdin)
#define OUT freopen("pieaters.out","w",stdout)
#define FOR(i,a,b) for(i=a ; i<=b ; i++)
#define DBG pf("Hi\n")
#define i64 long long int
#define eps (1e-8)
#define xx first
#define yy second
#define ln 17
#define off 2

using namespace __gnu_pbds;
using namespace std ;

typedef tree< i64, null_type, less<i64>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef pair<i64, i64> pii;

#define INF 2000000000
#define alpha 26

#define maxn 100010
#define LOG 18

vector <int> g[maxn] ;

int dfs(int u)
{
    vector <int> depths ;
    for(int v:g[u])
    {
        depths.pb( dfs(v) ) ;
    }
    sort( depths.begin() , depths.end() ) ;
    int sz = (int)depths.size() ;
    int res = 0 ;
    for(int i=sz-1 , j=1 ; i>=0 ; j++,i--)
    {
        res = max( res , depths[i]+j ) ;
    }
    return res ;
}

int main()
{
    int n ;
    scanf("%d",&n) ;

    for(int i=2 ; i<=n ; i++)
    {
        int u ;
        scanf("%d",&u) ;
        g[u].pb(i) ;
    }

    printf("%d\n",dfs(1)) ;

    return 0 ;
}


Measured memory_bytes: 13568
A,B,AB,X,Y= list(map(int, input().split()))
ans = (X * A) + (Y * B)
max_ab_cnt = max(X,Y)
for ab_cnt in range(max_ab_cnt+1):
    a_cnt = max(X - ab_cnt, 0)
    b_cnt = max(Y - ab_cnt,0)
    tmp = (a_cnt *A) + (ab_cnt * 2 * AB) + (b_cnt * B)
    ans = min(ans, tmp)
print(ans)

Measured memory_bytes: 3064
#include <algorithm>
#include <bits/stdc++.h>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <vector>

// lib/util.hpp
#ifndef UTIL_HPP
#define UTIL_HPP


typedef ::std::int_fast64_t i64;
typedef ::std::uint_fast64_t u64;
typedef ::std::int_fast32_t i32;
typedef ::std::uint_fast32_t u32;

namespace tools {
  template <typename T>
  void read(::std::istream& is, ::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    vector.reserve(size);
    ::std::copy_n(::std::istream_iterator<T>(is), size, ::std::back_inserter(vector));
  }
  template <typename T>
  void read(::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    ::tools::read(::std::cin, vector, size);
  }

  template <typename T, ::std::size_t N>
  void read(::std::istream& is, ::std::array<T, N>& array) {
    ::std::copy_n(std::istream_iterator<T>(is), N, array.begin());
  }
  template <typename T, ::std::size_t N>
  void read(::std::array<T, N>& array) {
    ::tools::read(::std::cin, array);
  }
}

#endif

// main.cpp

int main() {
  i64 A, B, C;
  std::cin >> A >> B >> C;
  std::cout << (A == B && B == C ? "Yes" : "No") << std::endl;
  return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int x,n,y=200;
    scanf("%d",&n);

    if(n>=15)
        x=(800*n)-(((n-(n%15))/15)*y);
    else
        x=800*n;
    printf("%d",x);
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <cassert>

using namespace std;

#define print(x) cout << x << endl
#define input(x) cin >> x

const int N = 2020;
const int OFFSET = N * 5;

char buffer[N];

bool match(char a, char b) {
    return (a == '?' || b == '?' || a == b);
}

void find_diff(vector<bool>& diff, const string& A, const string& B) {
    for (int i = 0; i < A.size(); i++) {
        for (int j = 0; j < B.size(); j++) {
            if (!match(A[i], B[j])) {
                assert(i - j + OFFSET >= 0);
                diff[i - j + OFFSET] = false;
            }
        }
    }
}

int main() {
    string A, B, C;

    scanf("%s", buffer);
    A = string(buffer);
    scanf("%s", buffer);
    B = string(buffer);
    scanf("%s", buffer);
    C = string(buffer);

    vector<bool> diff_ab(OFFSET * 2, true);
    vector<bool> diff_bc(OFFSET * 2, true);
    vector<bool> diff_ac(OFFSET * 2, true);
    find_diff(diff_ab, A, B);
    find_diff(diff_ac, A, C);
    find_diff(diff_bc, B, C);

    int ans = A.size() + B.size() + C.size();
    for (int i = -N * 2; i <= N * 2; i++) {
        for (int j = -N * 2; j <= N * 2; j++) {
            assert(i + OFFSET >= 0 && i + OFFSET < diff_ab.size());
            assert(j + OFFSET >= 0 && j + OFFSET < diff_ac.size());
            assert(j - i + OFFSET >= 0 && j - i + OFFSET < diff_bc.size());
            if (diff_ab[i + OFFSET] && diff_ac[j + OFFSET] && diff_bc[j - i + OFFSET]) {
                int L = min(0, min(i, j));
                int R = max(int(A.size()), max(int(B.size()) + i, int(C.size()) + j));
                // print(i << ' ' << j << ' ' << L << ' ' << R << " " << R - L);
                ans = min(ans, R - L);
            }
        }
    }
    print(ans);
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<iomanip>
//#include<cstdio>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cassert>
using namespace std;
typedef long long ll;
ll N, K;

int main(){
	cin >> N >> K;
	
	ll ans = 0;
	for(int b=K+1; b<=N; b++){
		ll q = (N+1-(N+1)%b)/b;
		ll a0 = (K==0)?b-K-1:b-K;
		ll a1 = (b-K)*(q-1);
		ll a2 = max(0LL, (N+1)%b-K);
		//cout << b << " " << a0 << " " << a1 << " " << a2 << endl;
		ans += a0 + a1 + a2;
	}

	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vi vector<int>
#define vs vector<string>
#define vll vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define pcc pair<char, char>
#define pdd pair<double, double>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define flsh fflush(stdout)
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1000000000007
 

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> o_tree;

const int N=2e5+5;
ll fac[N];

void compute()
{
    fac[0]=1;
    fac[1]=1;
    ll i;
    for(i=2;i<N;i++)
    {
        fac[i]=(fac[i-1]*i);
        fac[i]%=mod;
    }
}



long long power(long long x, long long y, long long p)
{
long long res = 1;


x = x % p;


while (y > 0)
{
if (y & 1)
res = (res*x) % p;


y = y>>1;
x = (x*x) % p;
}
return res;
}

long long modInverse(long long n, long long p)
{
return power(n, p-2, p);
}


long long ncrp(long long n, long long r, long long p)
{


if (r==0)
return 1;




 return (fac[n]* modInverse(fac[r], p) % p *modInverse(fac[n-r], p) % p) % p;
}

void solve()
{
    ll n,k;
    cin>>n>>k;
    ll res=0;
    ll i,j;
    for(i=0;i<n&&i<=k;i++)
    {
       ll tans = (ncrp(n,i,mod));
       tans*=(ncrp(n-i-1+i,i,mod));
       tans%=mod;
       res+=tans;
       res%=mod;
        
    } 
    cout<<res<<"\n";

}


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


if (fopen("input.txt","r" ))
  {
     freopen("input.txt", "r", stdin);
     freopen("output.txt", "w", stdout);
  }
cout<<setprecision(20);


ll t=1;

compute();
// cin>>t;


while(t--)
{
solve();
} 


return 0;
}

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

int main() {
    int n, m, a, b, d[1005], ans=0;
    cin >> n >> m >> a >> b;
    REP(i,n){
        cin >> d[i];
    }
    while(ans<n && d[ans]>=a) ans++;
    if(n-ans<m){
        while(ans>0 && d[ans-1]<=b) ans--;
    }
    cout << ans << endl;
    return 0;
}



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

const int inf = 1e9;

int h,w,n;
string g[10];
char c[10];
bool use[10];
int l[10][10], r[10][10], u[10][10], d[10][10];

inline bool valid(const string &s){
  rep(i,s.size()){
    if(s[i]=='0' && (i==0 || !isdigit(s[i-1])) && (i+1<(int)s.size() && isdigit(s[i+1]))){
      return false;
    }
    if(!isdigit(s[i]) && (i==0 || !isdigit(s[i-1]))){
      return false;
    }
  }
  return isdigit(s.back());
}

int parse(int L, int R, const string &s){
  for(int i=R-1;i>=L;i--){
    if(s[i] == '+' || s[i] == '-'){
      int a = parse(L,i,s), b = parse(i+1,R,s);
      if(a==inf || b==inf)return inf;
      if(s[i] == '+')return a+b;
      if(s[i] == '-')return a-b;
    }
  }

  for(int i=R-1;i>=L;i--){
    if(s[i] == '*' || s[i] == '/'){
      int a = parse(L,i,s), b = parse(i+1,R,s);
      if(a==inf || b==inf)return inf;
      if(s[i] == '*')return a*b;
      if(s[i] == '/'){
	if(b==0 || a%b)return inf;
	return a/b;
      }
    }
  }

  int res = 0;
  for(int i=L;i<R;i++)res = res*10 + (int)(s[i]-'0');
  return res;
}

inline bool check(const string &s){
  if(!valid(s))return false;

  int p = 0;
  while(s[p]!='=')p++;
  assert(p!=(int)s.size());
  int a = parse(0,p,s);
  int b = parse(p+1,s.size(),s);
  if(a==inf || b==inf)return false;
  return a==b;
}

bool solve(int y, int x){
  if(y==h)return true;

  int ny = y, nx = x+1;
  if(nx==w)ny++, nx=0;

  if(g[y][x] != '.')return solve(ny,nx);

  rep(i,n){
    if(!use[i]){
      use[i] = 1;
      g[y][x] = c[i];

      bool f = true;
      if(l[y][x]>=0){
	string s;
	for(int j=l[y][x];j<r[y][x];j++)s += g[y][j];
	if(!check(s))f = false;
      }

      if(f && u[y][x]>=0){
	string s;
	for(int j=u[y][x];j<d[y][x];j++)s += g[j][x];
	if(!check(s))f = false;
      }

      if(f && solve(ny,nx))return true;

      g[y][x] = '.';
      use[i] = 0;
    }
  }
  return false;
}

int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  while(cin >> h >> w, h){
    rep(i,h)cin >> g[i];
    cin >> n;
    rep(i,n)cin >> c[i];

    memset(l,-1,sizeof(l)); memset(r,-1,sizeof(r));
    memset(u,-1,sizeof(u)); memset(d,-1,sizeof(d));
    rep(i,h)rep(j,w){
      if(g[i][j] == '.'){
	int cnt = 0;
	for(int k=j+1;k<w;k++){
	  if(g[i][k] == '.')cnt++;
	  if(g[i][k] == '#')break;
	}
	if(cnt==0){
	  l[i][j] = 0; r[i][j] = w;
	  for(int k=j-1;k>=0;k--){
	    if(g[i][k] == '#'){
	      l[i][j] = k+1;
	      break;
	    }
	  }
	  for(int k=j+1;k<w;k++){
	    if(g[i][k] == '#'){
	      r[i][j] = k;
	      break;
	    }
	  }
	  if(r[i][j]-l[i][j]<3)r[i][j] = l[i][j] = -1;
	}

	cnt = 0;
	for(int k=i+1;k<h;k++){
	  if(g[k][j] == '.')cnt++;
	  if(g[k][j] == '#')break;
	}
	if(cnt==0){
	  u[i][j] = 0; d[i][j] = h;
	  for(int k=i-1;k>=0;k--){
	    if(g[k][j] == '#'){
	      u[i][j] = k+1;
	      break;
	    }
	  }
	  for(int k=i+1;k<h;k++){
	    if(g[k][j] == '#'){
	      d[i][j] = k;
	      break;
	    }
	  }
	  if(d[i][j]-u[i][j]<3)d[i][j] = u[i][j] = -1;
	}
      }
    }
    
    memset(use,0,sizeof(use));
    if(solve(0,0))cout << "Yes" << endl;
    else cout << "No" << endl;
  }
}

Measured memory_bytes: 1208
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll,ll> P;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod=1e9+7;
const ll inf=1e16;

#define rep(i,m,n) for(ll i=m;i<n;i++)
#define repr(i,m,n) for(ll i=m-1;i>=n;i--)
#define fi first
#define se second
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define eb(x) emplace_back(x)
#define all(x) x.begin(),x.end()
#define sum(x) accumulate(all(x),0)
#define gll greater<ll>()

struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
 
 

const ll nmax=1000000;


ll fac[nmax],finv[nmax],inv[nmax];

void comint(){
 fac[0]=fac[1]=1;
 finv[0]=finv[1]=1;
 inv[1]=1;
 for(ll i=2;i<nmax;i++){
  fac[i]=fac[i-1]*i%mod;
  inv[i]=mod-inv[mod%i]*(mod/i)%mod;
  finv[i]=finv[i-1]*inv[i]%mod;
}
}

ll com(ll n, ll k){
 if(n<k||n<0||k<0) return 0;
 return fac[n]*(finv[k]*finv[n-k]%mod)%mod;
}

ll mod_pow(ll x, ll n){
 ll res=1;
  while(n>0){
   if(n&1) res=res*x%mod;
   x=x*x%mod;
   n>>=1;
}
return res;
}




void solve(){
 ll n,m;
  cin >> n >> m;
  comint();
  
  mint ans=0,f=1;
  rep(k,0,n+1){
   mint tot=com(n,k);
   tot*=com(m,k)*fac[k];   
   mint perm=com(m-k,n-k)*fac[n-k];
   perm=perm*perm;
    tot*=perm;
    tot*=f;
    
    ans+=tot;
    f*=-1;
  }
  cout << ans << endl;
}

int main(){
   cin.tie(nullptr);
  ios::sync_with_stdio(false);
  solve();
}

Measured memory_bytes: 27080
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
constexpr ll mod = 1000000007;
constexpr ll INF = (1ll << 60);
constexpr double pi = 3.14159265358979323846;
template <typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return 1;}return 0;}
template <typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return 1;}return 0;}

int main(){
    ll s;cin>>s;
    vector<ll> a(1000001);
    vector<bool> check(5000000,false); 
    a[1]=s;
    for(ll i=2;i<=1000000;i++){
        if(a[i-1]%2==0){
            a[i]=a[i-1]/2;
        }else{
            a[i]=3*a[i-1]+1;
        }
    }

    ll ans=0;
    for(ll i=1;i<=1000000;i++){
        if(check[a[i]]==false){
            check[a[i]]=true;
        }else{
            ans=i;
            break;
        }
    }

    //rep(i,10) cout<<a[i]<<endl;

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

Measured memory_bytes: 11708
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math;

void main()
{
    auto N = readln.chomp.to!int;
    auto AS = readln.split.to!(int[]);
    sort(AS);
    auto n = AS[$-1];
    auto kc = n/2;

    int k;
    foreach (a; AS[0..$-1]) {
        if (a == kc) {
            k = a;
            break;
        } else if (a > kc) {
            k = kc-k < a-kc ? k : a;
            break;
        }
        k = a;
    }
    writefln("%d %d", n, k);
}

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

using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int main(){
  int N; cin >> N;
  if (N >= 3) cout << 0 << endl;
  else if (N == 1) cout << 2 << endl;
  else cout << 1 << endl;
  return 0;
}



Measured memory_bytes: 3088
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <ctime>
#include <deque>
#include <ctime>
#include <string>
#include <algorithm>
#include <vector>
#include <bitset>
#include <cassert>
#include <cstring>
#include <queue>
#include <random>

#define fi first
#define se second
#define ll long long
#define pii pair<int, int>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define mp(x, y) make_pair((x), (y))

using namespace std;

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

    int n;
    cin >> n;
    if (n % 2 == 0) {
        cout << (1.0 / 2) << endl;
    } else {
        cout << (((double)n + 1) / 2 / n) << endl;
    }
    
    return 0;
}

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

//省略形
#define rep(i,n) for(auto i=0; i<n ; ++i)
#define REP(i,a,b) for(auto i=a; i<b; ++i)
#define all(v) (v).begin() , (v).end()
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvc = vector<vector<char>>;
using pii = pair<int , int>;
using pll = pair<long long , long long>;

//よく使う定数
const int INF = 1e9;
const int MOD = 1e9+7;
//逆元
const int INV = 1e9+5;

//UnionFindTree
struct union_find{
  vector<int> data;
  union_find(int size) : data(size , -1) { }

  void merge(int x , int y){
    x = root(x); y = root(y);
    if(x == y) return;
    if(data[y] < data[x]) swap(x , y);
    data[x] += data[y];
    data[y] = x;
    return; 
  }

  bool same(int x , int y){
    x = root(x) , y = root(y);
    return x == y;
  }

  int root(int x){
    return data[x] < 0 ? x : (data[x] = root(data[x]));
  }

  int size(int x){
    return -data[root(x)];
  }
};

//プロトタイプ宣言
void faster(void);
template <class T>
T get_digent(T);
template <class T>
T POW(T , T);
template <class T>
T modpow(T , T , T);
bool is_palindrome(string);

//メモ
//最大公約数:__gcd　|　最小公倍数:__lcm
//回文は反転させて一致すればよい

int main(){
  faster();
  int n;
  cin >> n;
  string s;
  s = to_string(n);
  if(s[0]=='7'||s[1]=='7'||s[2]=='7') cout << "Yes";
  else cout << "No";
}























//同期解除
void faster(void){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

//桁数取得
template <class T>
T get_digent(auto x){
  string s;
  s = to_string(x);
  ll digent = s.size();
  return digent;
} 

//繰り返し自乗法
template <class T>
T POW(T x, T y){
  if(y==0) return 1;
  if(y%2 == 1){
    ll temp = POW(x,y-1);
    return (temp*x);
  }
  else{
    ll temp = POW(x,y/2);
    return temp*temp;
  }
} 

//繰り返し自乗法(剰余)
template <class T>
T modpow(T x , T y , T mod){
  if(y == 0) return 1;
  if(y%2 == 1){
    ll temp = modpow(x,y-1,mod);
    return temp*x%mod;
  }
  else{
    ll temp = pow_mod(x,y/2,mod);
    return temp*temp%mod;
  }
}

bool is_palindrome(string s){
    string t(s.rbegin(),s.rend());
    return s == t;
}


Measured memory_bytes: 3644
// --------------
// Tejas Pandey  |
// 02 - 08 - 20  |
// Atcoder BC 174|
// --------------

#include <bits/stdc++.h>
using namespace std;
     
#define mod 1000000007
#define vi vector<int>
typedef long long int ll;
     
int main (void){
  //freopen("in.txt","r",stdin);
  //freopen("out.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll k;
  cin >> k;
  ll ans = -1,cur = 0,ad = 7;
  for(int i = 1;i <= 50000000;i++){
	  cur += ad;
	  cur %= k;
	  if(!cur){
		ans = i;
		break;
	  }
	  ad *= 10;
	  ad %= k;
  }
  cout << ans << "\n";
  return 0;
}


Measured memory_bytes: 3640
import java.util.Scanner;

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

        System.out.println(nC2(n) + nC2(m));
    }
    static int nC2(int n) {
        return n * (n - 1) / 2;
    }
}


Measured memory_bytes: 25172
import Control.Applicative ((<$>), (<*>))
import Control.Monad
import Data.List

main :: IO ()
main = do
  n <- getl toInt
  solve <$> getl toInt <*> replicateM n (getl $ wrds toInt) >>= print

solve :: Int -> [[Int]] -> Int
solve m = fst . foldl' f (m,m)
  where
    f (s,t) [i,o]
      | t == (-1) = (0,-1)
      | t + i - o < 0 = (0,-1)
      | t + i - o > s = (t + i - o, t + i - o)
      | otherwise = (s, t + i - o)

toInt :: String -> Int
toInt = read

wrds :: (String -> a) -> String -> [a]
wrds f = map f . words

getl :: (String -> a) -> IO a
getl f = f <$> getLine

Measured memory_bytes: 8740
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
 
#define rp(i, k, n) for (int i = k; i < n; i++)
using ll = long long;
using ld = double;

template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7ll;
// const ll MOD = 998244353;
const double PI=3.14159265358979323846;



int main() {
    int n; cin >> n;
    priority_queue<pair<ll, pair<ll, ll>> > q;
    rp(i, 0, n) {
        ll a, b; cin >> a >> b;
        q.push(make_pair(llabs(a+b), make_pair(a, b)));
    }
    ll res = 0;
    rp(i, 0, n) {
        auto x = q.top(); q.pop();
        if(i%2 == 0) res += x.second.first;
        else res -= x.second.second;
    }
    cout << res << endl;
    return 0;
}


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

int task(int n){
  int sum=0;
  if(n>36) return 0; if(n==36) return 1;

  for(int a=0;a<10;a++){
    if(n>(a+9+9+9)) continue;
    for(int b=0;b<10;b++){
      if(n>(a+b+9+9)) continue;
      for(int c=0;c<10;c++){
        if(n>(a+b+c+9))continue;
        for(int d=0;d<10;d++){
          if(n==(a+b+c+d)) sum++;
        }
      }
    }
  }
  return sum;
}

int main(){
  int n;
  while(cin>>n){
    int sum=0;
    sum=task(n);
    cout<<sum<<"\n";
  }
}




Measured memory_bytes: 3084
def resolve():
    h, w, k = list(map(int, input().split()))
    c = []
    for _ in range(h):
        row = input()
        c.append(list(row))

    ans = 0
    for a in range(2**h):
        for b in range(2**w):
            black_sum = 0
            row = list(format(a,'0'+str(h)+'b'))
            column = list(format(b,'0'+str(w)+'b'))
            for i in range(h):
                if row[i] == '1':
                    continue
                for j in range(w):
                    if column[j] == '1':
                        continue
                    elif c[i][j] == "#":
                        black_sum += 1
            if black_sum == k:
                ans += 1
        
    print(ans)

resolve()

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

int main() {
  int a[5],ans = 0;
  for(int i=0 ;i<3 ;i++){
    cin >> a[i];
  }
  sort(a,a+3);
  for(int i=0 ;i<2 ;i++){
    ans += a[i+1] - a[i];
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
#include <ctype.h>

int main(){
    char text[100]; // 一行に入力される文字列数は最大で100文字
    char pp1[] = {"END_OF_TEXT"};

    char eof[12] = "";
    int count=0;
    scanf("%s",eof);

    while(1){
        scanf("%s", text);
        if(strcmp(text,"END_OF_TEXT") == 0){
            break;
        }
        for(int i = 0; text[i] != '\0'; i++){
            text[i] = tolower(text[i]);
        }
        for(int i = 0; pp1[i] != '\0'; i++){
            pp1[i] = tolower(pp1[i]);
        }
        if(strcmp(text, eof) == 0){
            count++;
        }

        
    }
    printf("%d\n", count);

    return 0;
}


Measured memory_bytes: 2044
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = sum(a)
    l = [0]*10**5
    for i in a:
        l[i-1] += 1
    q = int(input())
    for i in range(q):
        b,c = map(int, input().split())
        ans += (c-b)*l[b-1]
        l[c-1] += l[b-1]
        l[b-1] = 0
        print(ans)

if __name__=="__main__":
    main()


Measured memory_bytes: 86608
/*
    ( ´-ω･)︻┻┳══━一一一一一一一				(× _ ×)
	
	Stay Low. Go Fast
	Kill First. Die Last
	One Shot. One Kill
	No Luck. Pure Skill 
*/

#include<bits/stdc++.h>
using namespace std;

/*************************** MACROS **************************/
#define ld long double
#define ll long long  
#define ull unsigned long long
#define loop(i,x,y) for(ll i=x;i<y;i++)
#define rloop(i,x,y) for(ll i=x;i>=y;i--)
#define vll vector<ll>
#define pb push_back
#define S second
#define F first
#define B begin()
#define E end()
#define W(x) while(x--)
#define pll pair< ll,ll >
#define vpll vector< pll >
#define qll queue<ll>
#define dqll deque<ll>
#define qpll queue<pll>
#define dqpll deque<pll>
#define pqll priority_queue<ll>
#define pqpll priority_queue<pll>
#define Line cout<<"\n**************************\n\n";

/*************************** CONST ******************************/

const ll MOD = 1000000007;
const ll MOD1 = 998244353;
const ll MIN_LL = -1e18;
const ll MAX_LL = 1e18;
const int MAX_INT = 1e9;
const int MIN_INT = -1e9;
const ld pie = 3.1415926535;
 
/************************** POW UTIL *****************************/
 
ll fpow(ll n,ll p){;ll m=1;while(p){if(p%2)m*=n;p>>=1;n*=n;}return m;}
ll mfpow(ll n,ll p,ll M){ll m=1;n%=M;while(p){if(p%2)m=(m*n)%M;n=(n*n)%M;p>>=1;}return m%M;}
 
/************************* PRINT UTIL *****************************/

template<class T> inline ostream& operator<<(ostream& o,vector<T> v){for(auto i:v)o<<i<<" ";return o;};
template<class T> inline ostream& operator<<(ostream& o,set<T> s){for(auto i:s)o<<i<<" ";return o;}
template<class T> inline ostream& operator<<(ostream& o,multiset<T> s){for(auto i:s)cout<<i<<" ";return o;}
template<class T1,class T2> inline ostream& operator<<(ostream& o,pair<T1,T2> p){o<<p.F<<" "<<p.S<<"\n";return o;}
template<class T1,class T2> inline ostream& operator<<(ostream& o,map<T1,T2> m){for(auto p:m)o<<p.F<<" "<<p.S<<"\n";return o;}

/*************************** VARIABLES **************************/

const vll days={31,28,31,30,31,30,31,31,30,31,30,31};
 
bool br,br1,br2; 
char ch,ch1,ch2;
 

ll n,m,k,x,y,z,mini,maxi,l,r,sum,t,w;
pll p,p1,p2;

string s,s1,s2;

set<ll> st,st1,st2;
multiset<ll> mst; 

map<ll,ll> mp;

/**************************** FXNS ****************************/

ll spf[(int)1e7+100];

void solve()
{

	cin>>n;	

	loop(i,0,n+1)spf[i] = i;
	loop(i,2,n+1)if(spf[i] == i)
	{
		for(ll j=i*i;j<=n;j+=i)spf[j] = min(spf[j],i);
	}

	loop(i,1,n+1)
	{
		k = 1;
		x = i;

		while(x != 1)
		{
			y = spf[x];
			z = 0;

			while(x%y == 0)
			{
				z++;
				x /= y;
			}

			k *= z+1;
		}

		// cout<<"@ "<<i<<" "<<k<<endl;

		sum += k*i;
	}

	cout<<sum<<endl;

}
 
/*************************** MAIN ****************************/
 
int main(){
 
 
	// #ifndef ONLINE_JUDGE
	        // freopen("input.txt", "rt", stdin);
	        // freopen("output.txt", "wt", stdout);
	// #endif
 
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);cout.tie(NULL);

	solve();
 
	return 0;
}


Measured memory_bytes: 81756
#include <iostream>

int main(){
	int N;
	std::cin >> N;
	int p[N];
	for(int i = 0;i < N;i++){
		std::cin >> p[i];
	}
	int count = 0;
	for(int i = 1;i < N-1;i++){
		if((p[i-1] > p[i] && p[i] > p[i+1])||
			(p[i-1] < p[i] && p[i] < p[i+1]))
			count++;
	}
	std::cout << count;
	return 0;
}

Measured memory_bytes: 256
N = int(input())

t = str(input())
t_old = t[-1]
cc = 0
word = [t]

for i in range(N-1):
  t = str(input())
  t_new = t[0]
  if t_new == t_old:
    if t in word:
      cc += 1
      break
    else:
      t_old = t[-1]
      word.append(t)
  else:
    cc +=1
    break

if cc == 0:
  print("Yes")
else:
  print("No")

Measured memory_bytes: 3064
def main():
    from bisect import bisect_right

    N = int(input())
    *e, = map(int, input().split())

    e.sort()

    ans = 0
    for j in range(2, N):
        for i in range(1, j):
            d = e[j] - e[i]
            ans += max(0, i - bisect_right(e, d))

    print(ans)


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
#
# sys.setrecursionlimit(10 ** 7)
#
# (int(x)-1 for x in input().split())
# rstrip()


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

int main () {
  int n;
  cin >>n;
  vector<int>a(n);
  vector<int>b(n);
  vector<int>c(n);

  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;i++)cin>>c[i];

  int cnt =0;
  for(int i=0;i<n;i++){
    cnt+=b[i];
    if(a[i] +1 ==a[i+1])cnt +=c[a[i]-1];
  }

  cout<<cnt <<endl;
}






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

int N;
long long ans;
int As[200005];

int main() {
  cin >> N;
  int a;
  rep(i, N) {
    scanf("%d", &a);
    As[i] = (a - (i + 1));
  }
  sort(As, As+N);
  int b = As[N/2];
  rep(i, N) {
    ans += abs(As[i] - b);
  }
  cout << ans << endl;
  return 0;
}



Measured memory_bytes: 1024
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
char digit[4]={'m','c','x','i'};
void to_num(int nums[4],char s[10]){
    for(int j=0;j<strlen(s);j++){
        char c=s[j];
        switch(c){
            case 'm':
                if(j==0) nums[0]=1;
                if(j-1<0) break;
                if(isdigit(s[j-1])) nums[0]=s[j-1]-'0';
                else nums[0]=1;
                break;
            case 'c':
                if(j==0) nums[1]=1;
                if(j-1<0) break;
                if(isdigit(s[j-1])) nums[1]=s[j-1]-'0';
                else nums[1]=1;
                break;
            case 'x':
                if(j==0) nums[2]=1;
                if(j-1<0) break;
                if(isdigit(s[j-1])) nums[2]=s[j-1]-'0';
                else nums[2]=1;
                break;
            case 'i':
                if(j==0) nums[3]=1;
                if(j-1<0) break;
                if(isdigit(s[j-1])) nums[3]=s[j-1]-'0';
                else nums[3]=1;
                break;
            default : break;
        }
    }
}
void sum(int nums[],int numt[],int ans[]){
    for(int i=0;i<4;i++){
        ans[i]=nums[i]+numt[i];
    }
    for(int i=3;i>=0;i--){
        if(ans[i]>9){
            ans[i]=ans[i]%10;
            ans[i-1]++;
        }
    }
}
int main(void){
    int n;
    scanf("%d",&n);
    getchar();
    for(int i=0;i<n;i++){
        char s[10],t[10];
        scanf("%s%s",s,t); getchar();
        //printf("|%s %s|\n",s,t); printf("%lu\n",strlen(s));
        int nums[4]={0},numt[4]={0};
        to_num(nums,s);
        to_num(numt,t);
        //for(int j=0;j<4;j++) printf("%d",nums[j]); puts("");
        //for(int j=0;j<4;j++) printf("%d",numt[j]); puts("");
        int ans[4]={0};
        sum(nums,numt,ans);
        //for(int j=0;j<4;j++) printf("%d",ans[j]); puts("");
        for(int j=0;j<4;j++){
            if(ans[j]>1){
                printf("%d%c",ans[j],digit[j]);
            }
            else if(ans[j]==1){
                printf("%c",digit[j]);
            }
        }puts("");
    }
}



Measured memory_bytes: 2016
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}


int main(){
  int n , k;
  cin >> n >> k;
  if(n <= k) {
    cout << 1 << endl;
    return 0;
  }
  n -= k;
  if(n%(k-1) == 0) cout << n/(k-1)+1 << endl;
  if(n%(k-1) != 0) cout << n/(k-1)+2 << endl;

  return 0;
}


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

int main() {

	int n; cin >> n;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		a[i]--;
	}
	int ans = 0;
	int now = 0;
	bool can = true;
	while (true) {
		if (now == 1) {
			break;
		}
		now = a[now];
		ans++;
		if (ans == n) {
			can = false;
			break;
		}
	}
	if (can) cout << ans << endl;
	else cout << -1 << endl;
	return 0;
}

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

using ll = long long; 

int main() {
  int A, B;
  cin >> A >> B;

  
  int ans = -1;
  for (int i=1; i<=1250; i++){
    if (i*8/100 ==A && i*10/100 == B){
      ans = i;
      break;      
    }
  }

  cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define each(e,v) for(auto&& e:(v))
#define DEBUG(x) cout<<#x<<": "<<x<<endl
using namespace std;
using vint = vector<int>;
using vdouble = vector<double>;
using vstring = vector<string>;

int main() {
    string s; cin >> s;
    int i = 0;
    for(char c: s) {
        if(c!='C') i++;
        else break;
    }
    if(i == s.length()) {
        cout << "No" << endl;
        return 0;
    }
    for(; i < s.length(); i++) {
        if(s[i] == 'F') {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
    return 0;
}


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

int main() {
  int A, B, i, j = 0;
  cin >> A >> B;
  for(i = A; i <= B; i++) {
    if (i / 10000 == i - i / 10 * 10 && (i - i / 10000 * 10000) / 1000 == (i - i / 100 * 100) / 10) {
      j++;
    }
  }
  cout << j;
}


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

using namespace std;
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(),a.end()
#define rrng(a) a.end(),a.begin()
#define endl "\n"

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

  vector<int>a(4);
  for(int i=0;i<4;i++)cin>>a[i];
  sort(rng(a));
  int Min=INT_MAX;
  do{
    Min=min(Min,abs(a[0]+a[1]-(a[2]+a[3])));
  }while(next_permutation(rng(a)));
  cout<<Min<<endl;
  return 0;
}



Measured memory_bytes: 3188
/*
知らなかったこと
"あるグラフが２部グラフであるためには,
グラフに奇数本の辺からなる閉路（奇サイクル）が含まれないことが
必要十分である"

長さ３に着目する理由

二部グラフでは同じグループの頂点間に辺は無い
奇数回の移動では同じグループの頂点にはたどり着けないため,辺が追加されることも無い

二部グラフでないならば, 任意の２頂点の間に奇数長のパスが存在する
・・・グラフが完全グラフになるまで辺を追加する

二部グラフを分けたときに同じグループの頂点同士の距離は必ず偶数になる
*/

// http://ronly.hatenablog.com/entry/2017/10/15/134854

#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
using namespace std;

const long long inf = 1e10;
const long long mod = 1e9+7;

using G = vector<vector<int>>;

G graph;

int color[101010];

bool dfs(int v, int c){
    
    color[v] = c;
    
    for (int i = 0; i < graph[v].size(); i++){
        
        if (color[graph[v][i]] == c) return false;
        if (color[graph[v][i]] == 0 && !dfs(graph[v][i],-c)) return false; //???????????? why !(dfs(-c)
    }
    
    return true;
}


int main(){
    long long n, m;
    cin >> n >> m;
    
    graph = G(n);
    
    for (int i = 0; i < m; i++){
        int x, y;
        cin >> x >> y;
        
        x--, y--;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    
    long long B = 0, W = 0;
    
    if(dfs(0,1)){
        
        for (int i = 0; i < n; i++){
            if (color[i] == 1) B++;
            else W++;
        }
        
        cout << B * W - m << endl;
    }
    else{
        cout << (n * (n-1)) / 2 - m << endl;
    }
}








Measured memory_bytes: 6528