akhauriyash/Code-Regression huggingface dataset:
function Main(input){
  input = input.split("\n");
  var mem = input[0].split(" ").map(Number);

  var p;
  eval("p = /[0-9]{"+mem[0]+"}-[0-9]{"+mem[1]+"}$/g");
  var n = input[1].match(p);
  console.log(n?"Yes":"No")

}Main(require("fs").readFileSync("/dev/stdin", "utf8").trim());

Measured memory_bytes: 7500
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dd;
const int N = 1e6+5;

int n,m;
string p1,p2;
int main(){
  // freopen("input.txt","r",stdin);
  cin>>n>>m;
  for(int i = 0;i < m;i++) p1 += n + '0';
  for(int i = 0;i < n;i++) p2 += m + '0';
  if(p1 <= p2) cout<<p1<<endl;
  else cout<<p2<<endl;
  return 0;
}



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

#整数値龍力　複数の入力
def input_multiple_number():
    return map(int, input().split())

n,m = input_multiple_number()

ans = [-1]*n

for i in range(m):
    s,c = input_multiple_number()
    if ans[s-1] != -1 and ans[s-1] != c:
        print(-1)
        exit()

    ans[s-1]=c

if ans[0] == 0 and n != 1:
    print(-1)
    exit()

if ans[0] == -1:
    ans[0] = 1

for j in range(n-1):
    if ans[j+1] == -1:
        ans[j+1] = 0

numans = 0
for j in range(n):
    numans += pow(10,n-j-1)*ans[j]

if n ==1 and m==0:
    numans = 0

print(numans)


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

int main() {
  ll N;
  cin >> N;
  vector<pair<ll, ll>> A(N);

  for (ll i = 0; i < N; i++) {
    ll tmp;
    cin >> tmp;
    A[i] = make_pair(tmp, i);
  }

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

  for (ll i = 0; i < N; i++) {
    if (i > 0) cout << " ";
    cout << A[i].second + 1;
  }
  cout << endl;
  return 0;
}


Measured memory_bytes: 2432
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long int lli;
typedef pair<int, int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#define pque(type) priority_queue<type,vector<type>,greater<type> >
#define memst(a,b) memset(a,b,sizeof(a))

#define MOD 1000000007

int n, c;
lli f[401][401];
lli s[401][401];
vector<vector<lli>> d(401, vector<lli>(401, -1));
vector<int> a, b;

void fs(void){
	rep(i, 401) f[0][i] = f[i][0] = 1;
	for(lli i = 1; i <= 400; i++) rep(j, 400) f[i][j+1] = (f[i][j]*i)%MOD;
	rep(i, 401) s[0][i] = 0;
	rep(i, 401) rep(j, 400) s[j+1][i] = (s[j][i] + f[j+1][i])%MOD;
	return;
}

lli g(int m, int c){
	return (s[b[m-1]][c] - s[a[m-1]-1][c] + MOD)%MOD;
}

lli dfs(int m, int k){
	if(m == n) return g(n, k);
	if(d[m][k] != -1) return d[m][k];
	lli ret = 0;
	reg(i, 0, k) ret = (ret + g(m, i)*dfs(m+1, k-i))%MOD;
	return d[m][k] = ret;
}

int main(void){
	cin >> n >> c;
	a.resize(n), b.resize(n);
	rep(i, n) cin >> a[i];
	rep(i, n) cin >> b[i];
	fs();
	cout << dfs(1, c) << endl;	
	return 0;
}

Measured memory_bytes: 4096
//Author- Vaibhav singh
//will be expert soon 
// *#####################################################################################*
#include<bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_multiset tree<long long , null_type,less_equal<long long >, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_set tree<long long , null_type,less<long long >, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#define ll long long int
#define YES cout<<"YES"<<"\n";
#define NO cout<<"NO"<<"\n";
#define ld     long double
#define yes cout<<"yes"<<"\n";
#define no cout<<"no"<<"\n";
#define No cout<<"No"<<"\n";
#define Yes cout<<"Yes"<<"\n";
#define f(i,a) for(i=0;i<a;i++)
#define fo(i,a) for(i=1;i<=a;i++)
#define fa(i,a) for(auto i:a)
#define r(i,a) for(auto i=a.rbegin();i!=a.rend();i++)
#define en cout<<"\n";
#define ull unsigned long long int
#define o(x) cout<<x<<"\n";
#define o1(x) cout<<x<<" ";
#define pb push_back
#define F first
#define in insert
#define mp make_pair
#define S second
#define pre(n) cout<<fixed<<setprecision(n);
#define      gcd(a,b)    __gcd(a,b)
#define bs            binary_search
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;cin>>t;while(t-->0)
const ll Mod = 998244353;
#define mod 1000000007
#define pi 3.14159265358979323846
#define re return 0;
// *#######################################################################################*
// *#########################################################################################*
    //      ll ans=0,b[5000005]={0},a[5000005],i,n,x=0;                
    

int main()
{
      
      fast
     
   ll a[3],i,s=0,s1=0;;
   f(i,3)
   {
         cin>>a[i];
         if(a[i]==7)
         {
               s++;
         }
         else if(a[i]==5)
         {
               s1++;
         }
   }

      if(s1==2&&s==1)
      {
            YES
      }
else
{
      NO
}


    re
}

Measured memory_bytes: 256
#!/usr/bin/env python3


def main():
    n, m = map(int, input().split())
    range_list = [list(map(int, input().split())) for _ in range(m)]
    min_list = [x[0] for x in range_list]
    max_list = [x[1] for x in range_list]
    min_id = max(min_list)
    max_id = min(max_list)
    answer = max_id - min_id + 1
    answer = max(answer, 0)
    print(answer)


if __name__ == '__main__':
    main()


Measured memory_bytes: 29028
#include <iostream>
using namespace std;
#define rep(i,x,to) for(int i=x;i<to;i++)

struct Card {char mark,num;};

void bubble (struct Card a[],int n){
	rep(i,0,n){
		for(int j=n-1;j>=i+1;j--){
			if(a[j].num<a[j-1].num){
				Card t = a[j];
				a[j] = a[j-1];
				a[j-1] = t;
			}
		}
	}
}

void select(Card a[],int n){
	rep(i,0,n){
		int min = i;
		rep(j,i,n){
			if(a[j].num<a[min].num) min = j;
		}
		Card t = a[i];
				a[i] = a[min];
				a[min] = t;
	}
}

bool Stable(struct Card a[],struct Card b[],int n){
	rep(i,0,n){
		if (a[i].mark!=b[i].mark) return false;
	}
	return true;
}

int main() {
	int n; cin >> n;
	Card a[37],b[37];
	rep(i,0,n){
		cin >> a[i].mark >> a[i].num;
	}
	rep(i,0,n){
		b[i]=a[i];
	}
	
	bubble(a,n);
	select(b,n);
	
	rep(i,0,n){
		if(i==0) cout << a[i].mark<<a[i].num;
		else cout << " " << a[i].mark<<a[i].num;
	}
	cout <<endl;
	cout << "Stable" << endl;
	
	rep(i,0,n){
		if(i==0) cout << b[i].mark<<b[i].num;
		else cout << " " << b[i].mark<<b[i].num;
	}
	cout <<endl;
	if(Stable(a,b,n)) cout << "Stable" << endl;
	else cout << "Not stable" << endl;
	return 0;
}


Measured memory_bytes: 3120
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int main() {
  ios::sync_with_stdio(false);
  int x;
  cin>>x;
  if(x<=599)cout<<"8";
  else if(x<=799)cout<<7;
  else if(x<=999)cout<<6;
  else if(x<=1199)cout<<5;
  else if(x<=1399)cout<<4;
  else if(x<=1599)cout<<3;
  else if(x<=1799)cout<<2;
  else if(x<=1999)cout<<1;
  return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int N;
    cin >> N;
    string S;
    cin >> S;
    if (N & 1) {
        cout << "No" << endl;
        return 0;
    }
    for (int i = 0; i < N / 2; i++) {
        if (S[i] != S[i + N / 2]) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 256
<?php
fscanf(STDIN, "%d%d", $N, $M);
$A = array_map("intval", explode(" ", trim(fgets(STDIN))));
sort($A);

//累積和を作る
$AS[-1] = 0;
foreach($A as $i => $a){
    $AS[$i] = ($AS[$i-1] ?? 0) + $a;
}

//満足度がi以上の握手のパターン表を作っておく
for($i = 0; $i < 10**5*2+1; $i++){
    $lbmemo[$i] = lower_bound($A, $i);
}

//めぐる式二分探索 -- $k以上の値の個数がMを超えるなかで最大のkを求める
$ok = 0; 
$ng = 10**5*2+1;
while(abs($ok - $ng) > 1){
    $mid = intdiv($ok+$ng, 2);
    $sum = 0;
    $count = 0;
    if(isOK($mid)){
        $ok = $mid;
    }else{
        $ng = $mid;
    }
}
echo $sum - $ok * ($count - $M);
function isOK($bound){
    global $M, $A, $AS, $N, $sum, $count, $lbmemo;
    foreach($A as $a){
        $i = $lbmemo[$bound - $a] ?? 0;
        $ok = $N - $i;
        $count += $ok;
        $sum += $AS[$N - 1] - $AS[$i - 1] + $a * $ok;
    }
    return $count >= $M;
}
function lower_bound($array, $key){
    global $N;
    $min = -1;
    $max = $N;
    while($max - $min > 1){
        $c = intdiv($min + $max, 2);
        if($array[$c] >= $key){
            $max = $c;
        }else{
            $min = $c;
        }
    }
    return $max;
}


Measured memory_bytes: 27120
//  Created by conan1024hao in 2019.
//  Copyright © 2019 conan1024hao. All rights reserved.
//  専用ライブラリです、自由にコピーして構いません。
//  感谢看我的代码！Wechat:conan1024hao QQ:810396815
#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define INF 10e16
#define MOD 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
using namespace std;
typedef long long int ll;
typedef pair<ll,ll>P;
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
ll cmp(pair<ll,string>a,pair<ll,string> b){
        if(a.fi!=b.fi)
        return a.fi<b.fi;
        else
        return a.se<b.se;
}

//---------------------------------------------------------------------------
int counter(int x){
    if(!x)return 0;
    return counter(x>>1)+(x&1);
}
//---------------------------------------------------------------------------
int main(){//問題をちゃんと見ろ!!!!!!!!!!!!!!!!!      llか??????????
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    int n;cin>>n;
    vector<vector<P>>an(n);
    for(int i=0;i<n;i++){
        int a;cin>>a;
        while(a--){
            int x,y;cin>>x>>y;
            x--;
            an[i].pb(mp(x,y));
        }
    }
    int maxm=-1;
    for(int bit=0;bit<(1<<n);bit++){
        bool abc=true;
        for(int i=0;i<n;i++){
            if(bit&(1<<i)){
                for(int j=0;j<siz(an[i]);j++){
                   // cout<<(bit>>(an[i][j].fi-1))<<" "<<((bit>>(an[i][j].fi-1))&1)<<endl;
                    if((bit&(1<<an[i][j].fi))&&an[i][j].se==0)//
                    abc=false;
                    else if(!(bit&(1<<an[i][j].fi))&&an[i][j].se==1)
                        abc=false;
                }
              
            }
        }
        if(abc==true)
            chmax(maxm,counter(bit));
    }
    //cout<<cnt<<endl;
    cout<<maxm<<endl;
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------


Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){
    char ch;
    int a[26]={};
    while(cin>>ch){
        if(islower(ch)){
            int num=ch-'a';
            a[num]++;
        }
        else if(isupper(ch)){
            int num=ch-'A';
            a[num]++;
        }
    }
    for(int i=0;i<26;i++){
        char ch1=i+'a';
        cout<<ch1<<" "<<":"<<" "<<a[i]<<endl;
    }
    return 0;
}


Measured memory_bytes: 3104
#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 100000000

int main(){
	
	int W,a,b;
	cin>>W>>a>>b;
	
	int ans = Inf;
	
	for(int i=-1000000;i<1000000;i++){
		if(abs(a+i-b)<=W){
			ans=min(ans,abs(i));
		}
	}
	
	cout<<ans<<endl;
	
    return 0;
}



Measured memory_bytes: 256
#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<utility>
#include<queue>
#include<cmath>
using namespace std;
#define REP(i, a,b) for(i=a;i<b;i++) 
#define REP1(i,b,a) for(i=b-1;i>=a;i--)
#define output(x) cout<<x<<endl;
#define pb(x) push_back(x);
typedef long long int ll;
const int MAX = 510000;
const int MOD = 1000000007;


int main()
{
	ll i, j, k, l, m, sum, flag, N, M;
	sum = 0;
	flag = 0;
	cin >> N;
	vector<double>a(N);
	REP(i, 0, N) {
		cin >> a[i];
		sum += a[i];
	}
	double ave;
	ave = sum / (double)N;
	double min;
	min = MOD;
	ll ans;
	ans = N;
	for (i = N - 1; i >= 0; i--) {
		if (abs(a[i] - ave) <= min) {
			min = abs(a[i] - ave);
			ans = i;
		
		}
	}
	
	output(ans);
	return 0;
}



Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <utility>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <istream>
#include <ostream>

#include <cstdlib>
#include <cmath>
#include <cstdio>

using namespace std;

#define fi first
#define se second
#define mkp make_pair
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define rep(i,n) for(ll i=0; i < (n); ++i)
#define rrep(i,n) for(ll i=((n)-1); i >= 0; --i)

#define OPLT(T) bool operator<(const T & lop_, const T & rop_)
#define OPEQ(T) bool operator==(const T & lop_, const T & rop_)

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

istream& operator>>(istream& istr, __float128& obj) { double d; istr >> d; obj = d; return istr; };
ostream& operator<<(ostream& ostr, __float128& obj) { ostr << static_cast<double>(obj); return ostr; };

int cnt[26];

int main() {
	int N, M;
	cin >> N >> M;
	int A, B, C;
	string s;
	rep(i,N) {
		cin >> s;
		rep(j,M)
			cnt[s[j]-'a']++;
	}
	A = B = C = 0;
	rep(i,26) {
		if(cnt[i] % 4 == 0) {
			A++;
		}
		else if(cnt[i] % 2 == 0) {
			B++;
		}
		else
			C++;
	}

	bool f = true;
	if(N % 2 == 0 && M % 2 == 0) {
		if(B || C) f = false;
	}
	else if(N % 2 == 1 && M % 2 == 1) {
		if(C != 1 || B > (N+M-2)/2)
			f = false;
	}
	else {
		int t = (M % 2 == 0) ? (M/2) : (N/2);
		if(C != 0 || B > t) f = false;
	}

	if(f)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define f first
#define s second
#define pb push_back

main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	string s;
	cin>>s;
	int cnt=0;
	for(int i=0; i<s.size(); i++){
		if(s[i]=='1'){
			cnt++;
		}
	}
	if(cnt==0){
		for(int i=1; i<=n; i++){
			cout<<1<<endl;
		}
		return 0;
	}
	int cntm=cnt+1;
	int cntl=cnt-1;
	if(cntl<1){
		cntl=1;
	}
	if(cnt<1){
		cnt=1;
	}
	int pol[200005];
	int po[200005];
	int pom[200005];
	pom[0]=1;
	for(int i=1; i<=200000; i++){
		pom[i]=(2*pom[i-1])%cntm;
	}
	po[0]=1;
	for(int i=1; i<=200000; i++){
		po[i]=(2*po[i-1])%cnt;
	}
	pol[0]=1;
	if(cntl==-1){
		cntl=0;
	}
	
	for(int i=1; i<=200000; i++){
		pol[i]=(pol[i-1]*2)%cntl;
	}
	int ans=0;
	int ansl=0;
	int ansm=0;
	for(int i=0; i<s.size();i++){
		ans=(ans+po[s.size()-i-1]*(s[i]-'0'))%cnt;
		ansl=(ansl+pol[s.size()-i-1]*(s[i]-'0'))%cntl;
		ansm=(ansm+pom[s.size()-i-1]*(s[i]-'0'))%cntm;
	}
	

	for(int i=0; i<s.size(); i++){
		
		if(s[i]=='1'){
			if(cnt==1){
				cout<<0<<endl;
				continue;
			}
			int val=ansl;
			val=(val-pol[s.size()-i-1]+cntl)%cntl;
			int num=val;
			int ans=1;
			while(num!=0){
				num=num%(__builtin_popcount(num));
				ans++;
			}
			cout<<ans<<endl;
		}
		else{
			
			int val=ansm;
			
			val=(val+pom[s.size()-i-1])%cntm;
			
			int num=val;
			int ans=1;
			while(num!=0){
				num=num%(__builtin_popcount(num));
				ans++;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}

Measured memory_bytes: 6888
#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() {

    vector<ll> dp;
    dp.push_back(1);
    dp.push_back(1);
    ll n;
    cin >> n;
    for(ll i=0;i<n;i++) {
        dp.push_back(dp[i]+dp[i+1]);
    }
    cout << dp[n] << endl;;
}



Measured memory_bytes: 3136
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

runrun = [None for _ in range(100)]
runrun[1] = (1, 2, 3, 4, 5, 6, 7, 8, 9)


def gen_runrun_keta(k):
    for x in runrun[k - 1]:
        r = x % 10
        if r > 0:
            yield 10 * x + (r - 1)
        yield 10 * x + r
        if r < 9:
            yield 10 * x + (r + 1)


for keta in range(2, 11):
    runrun[keta] = tuple(gen_runrun_keta(keta))


rest = int(read())

answer = 0
for n in range(1, 11):
    if rest <= len(runrun[n]):
        answer = runrun[n][rest - 1]
        break
    rest -= len(runrun[n])
print(answer)


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

using namespace std;


void solve() {
    int n; cin >> n;
    double pi = acos(-1.0);
    cout << fixed << setprecision(10) << 2*pi*n;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    return 0;
}


Measured memory_bytes: 3800
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for(ll i=0; i<n; i++)
#define Rep(i, j, n) for(ll i=j; i<n; i++)
#define all(vec) vec.begin(), vec.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;}
const ll INF = 1LL << 60;

int main() {
    int n, k; cin >> n >> k;
    vector<int> p(n);
    rep(i, n) cin >> p[i];
    sort(all(p));
    int ans = 0;
    rep(i, k) ans += p[i];
    cout << ans << endl;
}

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

using namespace std;
typedef long long ll;
const int INF = 1e9;

string s;

void solve(){
    for(int i=0;i<4;i++){
        cout << s[i];
    }
    cout <<" ";
    for(int i=4;i<s.size();i++){
        cout << s[i];
    }
    cout << endl;
}

int main() {
    cin >> s;
    solve();
    return 0;
}

Measured memory_bytes: 256
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

#define SORT(c) sort((c).begin(),(c).end())
#define ANS(ans) cout<<(ans)<<endl;


//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  for(int i=0;i<(n);++i)
#define VI(v,n) vector<int> v(n)



int main() {
	int n;
	cin >> n;
	VI(L, 2 * n);
	REP(i, 2 * n) {
		cin >> L[i];
	}

	SORT(L);

	int sum = 0;
	REP(i, n) {
		sum += L[2 * i];
	}

	ANS(sum);
	

	
	return 0;
}



Measured memory_bytes: 256
/*input

*/
#include <bits/stdc++.h>
#define up(i,a,b) for(int (i) = (a);(i)<=(b);++(i))
#define down(i,b,a) for(int (i) = (b);i>=(a);--i)
#define debug(x) cerr << (x) << '\n';
#define bits(x,i) ((x >> i) & 1)
#define mid ((l+r)/2)
#define ll long long
#define pr pair<ll,ll>
using namespace std;
const int N = 200005;
const ll mod = 1e9 + 7;
int a[N], child[N],n;
vector<int> G[N];
// template 
ll power(ll x, ll n){
	ll ans = 1;
	for (int i=0;i<30;++i){
		if (bits(n, i)){
			ans = (ans * x) % mod;
		}
		x = (x * x) % mod;
	}
	return ans;
}
ll inv(ll num,ll den){
	return (num * power(den, mod - 2)) % mod;
}
ll ans = 0;
void dfs(int u,int pre){
	child[u] = 1;
	for (auto v : G[u]){
		if (v == pre) continue;
		dfs(v, u);
		child[u] += child[v];
		ans += (power(2, child[v]) - 1LL) * (power(2, n - child[v]) - 1LL);
		ans %= mod;
	}
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for(int i=1;i<n;++i){
		int u,v; cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	ans = (power(2, n) - 1LL * n * power(2, n-1) - 1LL) % mod;
	dfs(1, 0);
	cout << inv(ans, power(2, n));
}

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

int main(void){
  int N,K,i,j,l;
  long h[100005],hmin,hdiff,buff;
  char str[100],*p;
  
  fgets(str,sizeof(str),stdin);
  p=strtok(str," \n");
  N=atoi(p);
  p=strtok(NULL," \n");
  K=atoi(p);
  
  for(i=0;i<N;i++){
    fgets(str,sizeof(str),stdin);
    h[i]=atol(str);
  }
  
  for(l=1;l<N/9;l=3*l+1);
  
  for(;l>0;l/=3){
    for(i=l;i<N;i++){
      j=i;
      while(j>=l && h[j-l]>h[j]){
        buff=h[j-l],h[j-l]=h[j],h[j]=buff;
        j-=l;
      }
    }
  }
  hmin=h[N-1]-h[0];
  for(i=K-1;i<N;i++){
    hdiff=h[i]-h[i-K+1];
    if(hmin>hdiff){
      hmin=hdiff;
    }
  }
  
  printf("%ld\n",hmin);
  
  
  
  return 0;
}

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

typedef long long ll;
#define pii pair<int , int>
#define _FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define MAXX 105

ll n;
ll l[MAXX];

int main()
{
    _FastIO;

    cin >> n;
    for(ll i = 0; i < n; i++){
        cin >> l[i];
    }
    sort(l , l + n);
    ll ans = 0;
    for(ll i = 0; i < n - 2; i++){
        for(ll j = i + 1; j < n - 1; j++){
            for(ll k = j + 1; k < n; k++){
                ll x = l[i];
                ll y = l[j];
                ll z = l[k];
                if((x + y) > z && (z - y) < x && x != y && x != z && y != z)
                    ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3656
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main(){
    string s;
    cin >> s;
    if ( s == "SUN" ) {
        cout << 7 << endl;
    }
    else if ( s == "MON" ) {
        cout << 6 << endl;
    }
    else if ( s == "TUE" ) {
        cout << 5 << endl;
    }
    else if ( s == "WED" ) {
        cout << 4 << endl;
    }
    else if ( s == "THU" ) {
        cout << 3 << endl;
    }
    else if ( s == "FRI" ) {
        cout << 2 << endl;
    }
    else if ( s == "SAT" ) {
        cout << 1 << endl;
    }
    return 0;
}


Measured memory_bytes: 256
from collections import deque
n, k = input().rstrip().split(" ")
n = int(n)
k = int(k)
w_list = deque()
for _ in range(n):
    w_list.append(int(input().rstrip()))

def get_num_of_w(p, w_list, k):
    w_cnt = 0
    k_cnt = k
    temp_p = p
    temp_w_list = w_list.copy()

    while k_cnt > 0 and len(temp_w_list) > 0:
        w = temp_w_list[0]
        temp_p -= w
        if temp_p >= 0:
            temp_w_list.popleft()
            w_cnt += 1
        else:
            temp_p = p
            k_cnt -= 1
    return w_cnt  

    
left = 0
right = 10000 * 100000
min_p = 10000 * 100000
while left < right:
    mid = (left+right) // 2
    if get_num_of_w(mid, w_list, k) >= n:
        right = mid
        min_p = mid
    else:
        left = mid + 1
    
print(min_p)
    
    
    
    

        
    
    

        
    


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

using namespace std;
int i, n, m, out = 0;

int main() {
    scanf(" %d %d", &n, &m);
    if (n >= 2) out += n * (n-1) / 2;
    if (m >= 2) out += m * (m-1) / 2;
    printf("%d\n", out);
    return 0;
}

Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<functional>
#include<iomanip>
#include<iostream>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<utility>
#include<vector>

#define ll long long
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define FORR(i,a,b) for(int i=(a);i>=(b);--i)
#define REPR(i,n)   FORR(i,n,0)

using namespace std;

int main() {
	int H, W, c[10][10], A[200][200];
	cin >> H >> W;
	REP(i, 10) {
		REP(j, 10) {
			cin >> c[i][j];
		}
	}
	REP(i, H) {
		REP(j, W) {
			cin >> A[i][j];
		}
	}


	REP(k, 10) {
		REP(i, 10) {
			REP(j, 10) {
				if (c[i][j] > c[i][k] + c[k][j]) {
					c[i][j] = c[i][k] + c[k][j];
				}
			}
		}
	}

	int ans = 0;
	REP(i, H) {
		REP(j, W) {
			if (A[i][j] != 1 && A[i][j] != -1) {
				ans += c[A[i][j]][1];
			}
		}
	}

	cout << ans << endl;


	return 0;
}

Measured memory_bytes: 384
package main
import . "fmt"
func main() {
  var a,b,c,d int
  Scan(&a,&b,&c)
  d = gcd(a,b)
  if c%d == 0 {
    Println("YES")
  } else {
    Println("NO")
  }
}
func gcd(x int,y int) int {
  if x < y { return gcd(y,x) }
  if x%y == 0 { return y }
  return gcd(y,x%y)
}

Measured memory_bytes: 512
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <unordered_map>
#include <map>

using namespace std;

#define ull unsigned long long


ull pow(ull a, ull e){
    ull tmp = 1;
    while(e > 0){
        if( e & 1ll){
            tmp = tmp * a;
        }
        a = a * a;
        e >>= 1; 
    }
    return tmp;
}

class Conbination
{
public:
	long long size;
	vector<vector<long long> > nck;
	Conbination(long long n);
	~Conbination();
	void setnCk();
	long long fetchnCk(long long n, long long k);
};

Conbination::Conbination(long long n){
	this->size = n;
	setnCk();
}

Conbination::~Conbination(){}

void Conbination::setnCk(){
	vector<long long> start;
	start.push_back(1.0);
	nck.push_back(start);

	for (long long i = 0; i <= size; ++i)
	{
		vector<long long> next;
		for (long long j = 0; j <= i+1; ++j)
		{
			next.push_back(0);
		}

		for (long long j = 0; j < i+1; ++j)
		{
			next[j] += nck[i][j];
			next[j+1] += nck[i][j];
		}
		nck.push_back(next);
	}
}

long long  Conbination::fetchnCk(long long n, long long k){
	return nck[n][k];
}


class MaximumAverageSets
{
public:
	long long n;
	long long a;
	long long b;
	vector<double> v;
	map<long long, long long> hash;
	MaximumAverageSets(){
		cin >> n >> a >> b;
		for (long long i = 0; i < n; ++i)
		{
			double tmp;
			cin >> tmp;
			v.push_back(tmp);
			hash[tmp]++;
		}

	}

	void exec(){
		double sum = 0;
		sort(v.begin(), v.end(), greater<long long>());
		for (long long i = 0; i < a; ++i)
		{
			sum += v[i]; 
		}


		double ave  = sum / (a * 1.0);
		printf("%.10f\n", ave);

		Conbination c = Conbination(hash[v[a-1]]);
		long long ans = 0;
		// cout << a<<" : " << hash[v[a-1]] << endl;
		// if(a - hash[v[a-1]] >= 0){
		if(v[a-1] != v[0]){
			long long tmp_count = 0;
			for (auto itr = hash.end(); itr != hash.find(v[a-1]);)
			{
				itr--;
				if(itr == hash.find(v[a-1])) break;
				// cout << itr->first << " " << itr->second <<endl;
				tmp_count += itr->second;
			}
			// cout << tmp_count << endl;
			ans = c.fetchnCk(hash[v[a-1]], a - tmp_count);
		}else{
			for(long long i = a; i <= min(hash[v[a-1]], b); ++i)
			{
				
				ans += c.fetchnCk(hash[v[a-1]], i);
				// if(v[a] != v[i]) break;

			}
		}
		cout << ans << endl;
	}

};

int main(){
	MaximumAverageSets mas = MaximumAverageSets();
	mas.exec();
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define RFOR(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define REP(i,n) FOR(i,0,(n))
#define REPS(i,n) FOR(i,1,(n)+1)
#define RREP(i,n) RFOR(i,0,(n))
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define DEBUG(x)  cout << #x << " = " << (x) << endl;
#define SORT(x) sort(ALL(x));
#define RSORT(x) sort(RALL(x));
#define SUM(x) accumulate(ALL(x),0);
#define FI first
#define SE second
#define PB push_back
#define EB emplace_back
#define SZ(x) (int)(x).size()
#define BN(x) ((1<<x)-1)
#define DUP(x,y) (((x)+(y)-1)/(y))
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const ll mod = 1e9+7;
const int INF = (1<<30)-1;
const ll INFLL = (1LL<<62)-1;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
//cout << fixed << setprecision(10);

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

ll gcd(ll a, ll b) { return b ? gcd(b,a%b) : a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}

int main () {
  int N; cin >> N;
  vi A(N); REP(i,N) cin >> A[i];

  vector<bool> flag(1e9+1,false);
  int ans = 0;
  REP(i,N) {
    if (!flag[A[i]]) {
      ans++;
      flag[A[i]] = true;
    }
    else {
      ans--;
      flag[A[i]] = false;
    }
  }

  cout << ans << endl;
}

Measured memory_bytes: 122752
#include <stdio.h>

int main()
{
  int n;
  scanf("%d",&n);
  int a[n];
  int i;
  
  i = 0;
  while (i < n)
  {
    scanf("%d",&a[i]);
    i++;
  }
  qsort(n,a,0,n-1);
  i = 1;
  while (i < n)
  {
    if (a[i] == a[i-1])
    {
      printf("NO");
      return (0);
    }
    i++;
  }
  printf("YES");
}

void qsort(int len, int a[len], int f, int l)
{
  int i,j,thr,tmp;
  i = f;
  j = l;
  thr = a[(f+l)/2];
  while (1)
  {
    while (thr > a[i])
      i++;
    while (thr < a[j])
      j--;
    if (i >= j)
      break;
    tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
    i++;
    j--;
  }
  if (f < i - 1)
    qsort(len, a, f, i-1);
  if (l > j+1)
    qsort(len, a, j+1, l);
}

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

int max_gcd(long long x){
  long long gcd;
  for(long long i = 1; i*i <= x; i++){
    if(x%i == 0) gcd = i;
  }
  return gcd;
}

int main(){
  long long N; cin >> N;
  long long x, y;
  x = max_gcd(N);
  y = N/x;
  long long ans = x + y - 2;
  cout << ans << endl;
}

Measured memory_bytes: 256
from collections import Counter

n = int(input())
a_list = list(map(int, input().split()))
q = int(input())

counter = Counter(a_list)
sum_res = sum(a_list)

for _ in range(q):
  b, c = (map(int, input().split()))
  
  sum_res -= b * counter[b]
  sum_res += c * counter[b]
  
  counter[c] += counter[b]
  counter[b] = 0
  
  #print(b, counter[b], c)
  print(sum_res)

Measured memory_bytes: 24000
#include<bits/stdc++.h>
using namespace std;
 
int main () {
  int n;
  cin >>n;
  set<string> s;
  for(int i=0;i<n;i++){
    string ss;
    cin>>ss;
    s.insert(ss);
  }

  cout<<s.size()<<endl;

  return 0;
}

Measured memory_bytes: 19112
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <random>
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (int) n; i++)

int main() {
    string a,b,c; cin >> a >> b >> c;
    char turn = 'a';
    int ind[3] = {0,0,0};
    bool win[3] = {false,false,false};
    while(1) {
        if (turn == 'a') {
            turn = a[ind[0]];
            ind[0]++;
            if (ind[0] == a.length()) win[0] = true; 
        } else if (turn == 'b') {
            turn = b[ind[1]];
            ind[1]++;
            if (ind[1] == a.length()) win[1] = true; 
        } else {
            turn = c[ind[2]];
            ind[2]++;
            if (ind[2] == c.length()) win[2] = true; 
        }
        if (win[turn-'a']) {
            cout << char(turn-0x20) << endl;
            return 0;
        }
    }
    return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long ll;
int n;
ll ans,c[410][4],h[410],v[4],tmp[4];
unordered_map<ll,int>mp;
void upd(ll x,int d){
	for(int i=0;i<4;i++,x=((x&1023ll)<<30ll)|(x>>10ll))
		mp[x]+=d;
	return;
}
ll Hash(ll *a){
	ll ret=0;
	for(int i=0;i<4;i++)
		ret|=(a[i]<<(i*10ll));
	return ret;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld%lld%lld%lld",c[i],c[i]+1,c[i]+2,c[i]+3);
		h[i]=Hash(c[i]);
		upd(h[i],1);
	}
	for(int i=1;i<=n-5;i++){
		upd(h[i],-1);
		for(int j=i+1;j<=n;j++){
			upd(h[j],-1);
			for(int k=0;k<4;k++){
				ll res=1;
				for(int l=0;l<4;l++){
					ll tmp[]={c[i][(l+1)&3],c[i][l],c[j][(3-l+k)&3],c[j][(6-l+k)&3]};
					v[l]=Hash(tmp);
					if(!mp.count(v[l]))
						goto to;
				}
				for(int l=0;l<4;l++){
					res*=mp[v[l]];
					upd(v[l],-1);
				}
				ans+=res;
				for(int l=0;l<4;l++)
					upd(v[l],1);
				to: ;
			}
			upd(h[j],1);
		}
	}
	printf("%lld",ans);
	return 0;
}

Measured memory_bytes: 256
// #include<iostream>
// #include<vector>
// #include<algorithm>
// #include<map>
// #include<cstring>
// #include<math.h>
// #include<bitset>
// #include<queue>
// #include<set>
// #include<iomanip>
// #include<math.h>
// #include<assert.h>
// #include<string>
// #include<cstdlib>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr ll INF = 1LL << 40;
const double INFD = 1e100;
const ll mod = 1000000007;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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;
}
// ----------------------------------------------------------------------------

int main(){
  int N;
  cin >> N;
  vector<int> a(N);
  for(int i=0; i<N; i++){
    cin >> a[i];
  }
  vector<int> color(10,0);
  for(int i=0; i<N; i++){
    if(a[i]>=3200){
      color[9]++;
    }else{
      color[a[i]/400+1]++;
    }
  }
  int var=0;
  for(int i=1; i<=8; i++){
    if(color[i]){
      var++;
    }
  }
  if(var==0){
    cout << 1 << " " << color[9] << endl;
  }else{
    if(color[9]){
      if(color[8]){
        cout << var << " " << var+color[9] << endl;
      }else{
        cout << var+1 << " " << var+color[9] << endl;
     }
    }else{
      cout << var << " " << var << endl;
    }
  }

  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
    int n;
    cin>>n;
     int i = 1;
 CHECK_NUM:
  int x = i;
  if ( x % 3 == 0 ){
    cout << " " << i;
    goto END_CHECK_NUM;
  }
 INCLUDE3:
  if ( x % 10 == 3 ){
    cout << " " << i;
    goto END_CHECK_NUM;
  }
  x /= 10;
  if ( x ) goto INCLUDE3;
 END_CHECK_NUM:
  if ( ++i <= n ) goto CHECK_NUM;

  cout << endl;

    return 0;
}


Measured memory_bytes: 3084
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using Pair = pair<int, int>;
using Vec = vector<int>;
using Graph = vector<Vec>;

int h, n;
Vec a, b;

int		round_up_div(int c, int d)
{
	return ((c + d - 1) / d);
}

int		main(void)
{
	cin >> h >> n;
	a = Vec(n);
	b = Vec(n);
	for (int i = 0; i < n; i ++)
		cin >> a[i] >> b[i];
	vector<Vec> dp(n, Vec(h+1, 0));
	for (int i = 0; i < n; i++)
	{
		for (int j = 1; j <= h; j++)
		{
			if (i == 0)
				dp[i][j] = round_up_div(j, a[i]) * b[i];
			else if (j <= a[i])
				dp[i][j] = min(dp[i-1][j], b[i]);
			else
				dp[i][j] = min(dp[i-1][j], dp[i][j-a[i]] + b[i]);
		}
	}
	cout << dp[n-1][h] << endl;
}


Measured memory_bytes: 39424
#include <bits/stdc++.h>
#define rep(i,n) for(int64_t i=0;i < (int64_t)(n);i++)
#define invrep(i,n) for(int64_t i=n-1;i >= 0L;i--)
#define prep(i,n) for(int64_t i=1;i <= (int64_t)(n);i++)
using namespace std;

//////全探索したいときに開けよ////////
vector<vector<int>> fspattern;
vector<int> fspattern_draft;
int fspcnt = 0;

void setfspattern_core(int floor,int number,int degree){
  if(floor==degree){
    fspattern.at(fspcnt) = fspattern_draft;
    fspcnt++;
  }else{
    rep(i,number){
      fspattern_draft.at(floor) = i;
      setfspattern_core(floor+1,number,degree);
    }
  }
}

void setfspattern(int number,int degree){
  int64_t array_num = 1;
  fspcnt = 0;
  rep(i,degree){
    array_num *= number;
  }
  fspattern = vector<vector<int>>(array_num,vector<int>(degree));
  fspattern_draft = vector<int>(degree);
  setfspattern_core(0,number,degree);
}

//////nCrのmodを求めたいときに開けよ/////////
//int64_t univMod = 1000000007;
//int64_t factnum = 100000
//vector<int64_t> modfact(factnum);
//vector<int64_t> modinvfact(factnum);

/*void setmodfact(){
  for(int i=0;i<factnum;i++){
    if(i == 0){
      modfact.at(i) = 1;
    }else{
      modfact.at(i) = (modfact.at(i-1)*i)%univMod;
    }
  }
}

int64_t calcmodpower(int64_t a,int64_t n){
  int64_t res = 1;
  while(n != 0){
    if(n & 1){
      res = (res * a) % univMod;
    }
    a = (a*a) % univMod;
    n = n >> 1;
  }
  return res;
}

int64_t calcinverse(int64_t n){
	return calcmodpower(n,univMod-2);
}

void setmodinvfact(){
  for(int i=0;i<factnum;i++){
    if(i==0){
      modinvfact.at(i) = 1;
    }else{
      modinvfact.at(i) = calcmodpower(modfact.at(i),univMod-2);
    }
  }
}

int64_t calcmodcomb(int64_t n,int64_t r){
  return (((modfact.at(n)*modinvfact.at(r))%univMod)*modinvfact.at(n-r))%univMod;
}

int64_t calcmodpositive(int64_t n){
  return (n + ((abs(n)+univMod-1)/univMod)*univMod)%univMod;
}
  
    */


/*
int64_t calcGCD(int64_t a,int64_t b){
	int64_t p = a;
	int64_t q = b;
	int64_t c = a%b;
	while(c != 0){
		p = q;
		q = c;
		c = p%q;
	}
	return q;
}
*/

int main(){
  int n;
  cin >> n;
  int64_t res = 1;
  rep(i,n){
    res = (res*(i+1))%1000000007;
  }
  cout << res << endl;
    
}

Measured memory_bytes: 256
n = int(input())
*A, = map(int, input().split())
B = [0]*n
for i in range(n-1, -1, -1):
    B[i] = (sum(B[i::(i+1)])%2+A[i])%2
print(sum(B))
print(*[i+1 for i, b in enumerate(B) if b == 1])

Measured memory_bytes: 13108
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;

struct segtree {
	int n;
	vector<ll> t;
	segtree(int nn) { n = nn; t.assign(n<<1, INF); }
	void update(int i, ll v) {
		for(t[i += n] = v; i > 1; i >>= 1) t[i>>1] = min(t[i], t[i^1]);
	}
	ll query(int l, int r) {
		ll res = INF;
		for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
			if(l & 1) res = min(t[l++], res);
			if(r & 1) res = min(t[--r], res);
		}
		return res;
	}
};

int n, q, a, b;
long long sum, ans = INF;
vector<int> x;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> q >> a >> b;
	a--;
	b--;
	x.resize(q);
	segtree dpp = segtree(n), dpm = segtree(n);
	for(int i = 0; i < q; i++) {
		cin >> x[i];
		x[i]--;
		if(i > 0) sum += abs(x[i]-x[i-1]);
	}
	dpm.update(a, abs(x[0]-b)-a);
	dpm.update(b, abs(x[0]-a)-b);
	dpp.update(a, abs(x[0]-b)+a);
	dpp.update(b, abs(x[0]-a)+b);
	for(int i = 1; i < q; i++) {
		ll res = min(dpm.query(0, x[i]+1)+x[i], dpp.query(x[i], n)-x[i]);
		if(res-x[i-1]-abs(x[i]-x[i-1]) < dpm.t[x[i-1]+n]) {
			dpm.update(x[i-1], res-x[i-1]-abs(x[i]-x[i-1]));
			dpp.update(x[i-1], res+x[i-1]-abs(x[i]-x[i-1]));
		}
	}
	for(int i = 0; i < n; i++) ans = min(dpm.query(i, i+1)+i, ans);
	cout << ans+sum;
}


Measured memory_bytes: 7296
#include<stdio.h>
#ifdef DEBUG
#include<conio.h>
#endif // DEBUG

int main () {
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif // DEBUG

    int a, b;
    while (scanf("%d %d", &a, &b), 0 != a && 0 != b)
    {
        for (size_t j = 0; j < b; j++)
            printf("#");
        printf("\n");

        for (size_t i = 1; i < a - 1; i++)
        {
            printf("#");
            for (size_t j = 1; j < b - 1; j++)
            {
                printf(".");
            }
            printf("#\n");
        }

        for (size_t j = 0; j < b; j++)
            printf("#");
        printf("\n");
        
        printf("\n");
    }

#ifdef DEBUG
    getch();
#endif // DEBUG
    return 0;
}




Measured memory_bytes: 2068
#include <iostream>

int count_pattern( unsigned int const& n )
{
    int ans=0;
    for ( unsigned int a = 0; a < 10; ++a )
    {
        for ( unsigned int b = 0; b < 10; ++b )
        {
            for ( unsigned int c = 0; c < 10; ++c )
            {
                for ( unsigned int d = 0; d < 10; ++d)
                {
                    if ( a+b+c+d == n ) ++ans;
                }
            }
        }
    }
    return ans;
}

int main()
{
    int n;
    while (std::cin >> n)
    {
        std::cout << count_pattern(n) << std::endl;
    }
    return 0;
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
#define int long long
#define range(i, a, b) for(auto i = a; i < b; i++)
#define rep(i, a) range(i, 0, a)
#define all(a) (a).begin(),(a).end()
using namespace std;
const int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
const int MOD = 1000000007;
const long double EPS = 1e-8;
using vi = vector <int>;
using vvi = vector <vi>;

signed main() {
	int h, w, d;
	cin >> h >> w >> d;
	int a[h][w];
	map <int, pair <int, int>> mp;
	map <pair <int, int>, int> cost;
	rep (i, h) rep (j, w) {
		cin >> a[i][j];
		mp[a[i][j]] = {i, j};
	}

	cout << endl;
	range (i, 1, d + 1) {
		for (int j = 0; d * j < h * w; j++) {
			cost[make_pair(i, i + d * (j + 1))] += (cost[make_pair(i, i + d * j)]
																			+ abs(mp[i + d * j].first - mp[i + d * (j + 1)].first)
																			+ abs(mp[i + d * j].second - mp[i + d * (j + 1)].second));
		}
	}
	int q;
	cin >> q;
	rep (i, q) {
		int l, r;
		cin >> l >> r;
		int pear = (r - l) / d;
		pair <int, int> now = mp[l];
		int tmp = l % d;
		if (!tmp) tmp = d;
		cout << cost[make_pair(tmp, r)] - cost[make_pair(tmp, l)] << endl;
	}
	return 0;
}

Measured memory_bytes: 25600
module Main where
-- import Debug.Trace
import Data.List
import qualified Data.Sequence as S
import qualified Data.ByteString.Char8 as B
import Data.Int
import Data.Char
import Data.Array
import Data.Maybe
-- import qualified Data.Map as M
import Data.Ord
import Data.Functor
import Control.Applicative
import Control.Monad

solve :: Int -> [Int] -> [Int] -> Int
solve n s = iter 0 where
    iter ac [] = ac
    iter ac (t:ts) = iter (ac + if find t then 1 else 0) ts
    tableS = listArray (0, n-1) s
    find t = iter 0 (n-1) where
        iter l r 
            | l >  r    = False
            | v == t    = True
            | v <  t    = iter (d+1) r
            | otherwise = iter l (d-1) where
                d = div (l+r) 2
                v = tableS ! d

readLine :: IO [Int]
readLine = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

main :: IO() 
main = do
    n <- readLn :: IO Int
    s <- readLine
    q <- readLn :: IO Int
    t <- readLine
    print $ solve n s t



Measured memory_bytes: 31736
N,M=list(map(int,input().split()))
A=[]
for i in range(N):
    A.append(list(map(int,input().split())))
# print(A)
A.sort()
#print(A)
res,count=0,0
for i in range(N):
    res+=A[i][0]*A[i][1]
    count+=A[i][1]
    if(count==M):
        break 
    if(count>M):
        res-=A[i][0]*A[i][1]
        count-=A[i][1]
        for j in range(1,A[i][1]+1):
            res+=A[i][0]
            count+=1
            if(count==M):
                print(res)
                exit()

print(res)
exit()
                


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

#define rep(i,n) for(int i=0; i<(n); ++i)
#define rrep(i,n) for(int i=1; i<=(n); ++i)
#define drep(i,n) for(int i=(n)-1; i>=0; --i)
#define foreach(it, v) for(typeof(v.begin()) it=v.begin(); it!=v.end(); ++it)
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second

typedef unsigned int uint;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
typedef set<int> si;

//MOD//
const ll MOD = 1000000007;
struct big;
big ex(big a, ll tms);
typedef struct big{
  ll val;
  big():val(0){}
  big(ll val):val((val%MOD+MOD)%MOD){}
  big& operator += (const big& obj){ (val+=obj.val)%=MOD; return *this;}
  big& operator -= (const big& obj){ (val+=MOD-obj.val)%=MOD; return *this;}
  big& operator *= (const big& obj){ (val*=obj.val)%=MOD; return *this;}
  big& operator /= (const big& obj){ (val*=ex(obj,MOD-2).val)%=MOD; return *this;}
  big operator + (const big& obj)const{ return big(*this) += obj;}
  big operator - (const big& obj)const{ return big(*this) -= obj;}
  big operator * (const big& obj)const{ return big(*this) *= obj;}
  big operator / (const big& obj)const{ return big(*this) /= obj;}
  bool operator < (const big& obj)const{ return val < obj.val;}
  bool operator == (const big& obj)const{ return val == obj.val;}
} big;
istream& operator >> (istream& i, big& obj){ i>>obj.val; return i;}
ostream& operator << (ostream& o, const big& obj){ o<<obj.val; return o;}
big ex(big obj, ll tms){
  if(!tms) return 1;
  big ret = ex(obj, tms/2);
  ret *= ret;
  return (tms&1) ? ret*obj : ret;
}
//MOD//

ll pow(ll n, int m){
  if(!m) return 1;
  ll ret = pow(n, m/2);
  ret *= ret;
  return (m&1) ? ret*n : ret;
}

template <class F, class T>
void convert(const F &f, T &t){
  stringstream ss;
  ss << f;
  ss >> t;
}

int N;
char s[200000];
int cnt[26];

int main(){

  scanf("%d",&N);
  scanf("%s",&s);

  big ans = 1;
  int n;
  rep (i, N) ++cnt[s[i]-'a'];
  rep (i, 26) ans = ans * big(cnt[i]+1);
  ans -= 1;
  cout << ans << endl;

  return 0;
}



Measured memory_bytes: 384
#include <iostream>
using namespace std;
int main(){
  long long k; cin >> k;
  long long r=7;
  long c=1;
  while(c<=1000000){
    r=r%k;
    if(r==0){
      cout << c << endl;
      return 0;
    }
    r=10*r+7;
    c++;
  }
  cout << -1 << endl;
}

Measured memory_bytes: 3588
#include<bits/stdc++.h>
using namespace std;
#define  ll long long
int main()
{
	
    ll n,W;
    cin>>n>>W;
    vector<ll>dp(W+1);
    for(ll item=0;item<n;++item)
    {
    	ll weight,value;
    	cin>>weight>>value;
    	for(ll weight_already = W-weight; weight_already>=0;--weight_already)
    	{
    		dp[weight_already+weight]=max(dp[weight_already+weight],dp[weight_already]+value);
    	}
    }
    ll answer=0;
    for(ll i=0;i<=W;i++)
    {
       answer=max(answer,dp[i]);
    }

   cout<<answer;

     		
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> geko;
  for(int i = 0; i < n; i++){
    ll a;
    cin >> a;
    geko.push_back(a);
  }
  sort(geko.begin(), geko.end());
  cout << geko[geko.size()-1] - geko[0] << endl;
}

Measured memory_bytes: 256
import sys

def main():
    a=[]
    b=[]
    c=[]
    for i in range(3):
        tmp = input().replace('\n','').split(' ')
        tmp = list(map(int, tmp))
        a.append([tmp[0],False])
        a.append([tmp[1],False])
        a.append([tmp[2],False])
        c.append(tmp[0])
        c.append(tmp[1])
        c.append(tmp[2])
    n = int(input().replace('\n',''))
    for i in range(n):
        try:
            tmp = c.index(int(input().replace('\n','')))
            a[tmp][1] = True
        except ValueError:
            pass
    for i in range(3):
        if a[i][1] and a[i+3][1] and a[i+6][1]:
            res = 'Yes'
            break
        elif a[i*3][1] and a[i*3+1][1] and a[i*3+2][1]:
            res = 'Yes'
            break
        elif a[0][1] and a[4][1] and a[8][1] or a[2][1] and a[4][1] and a[6][1]:
            res = 'Yes'
            break
        else:
            res = 'No'
    print(res)
if __name__ == '__main__':
    input = sys.stdin.readline
    main()

Measured memory_bytes: 3064
n, a = map(int, input().split())

for x in range(n+1):
  for y in range(n-x+1):
      if 10000 * x + 5000 * y + 1000 * (n-x-y) == a:
        print("{} {} {}".format(x,y,(n-x-y)))
        exit()

print("-1", "-1", "-1")

Measured memory_bytes: 40684