akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
const int N=30;
char a[N][N];
int dist[N][N];bool vis[N][N];
int dx[4]={-1,1, 0, 0};
int dy[4]={0,0,1,-1};
int n,m;
int bfs(int x, int y, int n, int m)
{
    memset(vis, 0, sizeof vis);
    queue<pair<int, int> > q;
    q.push({x,y});
    vis[x][y]=1;
    dist[x][y]=0;
    int mx=0;
    while(!q.empty())
    {
        int x=q.front().first;
        int y=q.front().second;
        q.pop();
        for(int dir=0;dir<4;dir++)
        {
            int nx=x+dx[dir];
            int ny=y+dy[dir];
            if(nx<0 or ny<0 or ny>=m or nx>=n or a[nx][ny]=='#')
                continue;
            if(!vis[nx][ny])
            {
                dist[nx][ny]=dist[x][y]+1;
                mx=max(mx, dist[nx][ny]);
                vis[nx][ny]=1;
                q.push({nx,ny});
            }
        }
    }

    return mx;
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)cin>>a[i][j];

     int ans=0;
     for(int i=0;i<n;i++)
         for(int j=0;j<m;j++)
             if(a[i][j]=='.')
                 ans=max(ans, bfs(i,j,n,m));

    cout<<ans;


}

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

int main() {
	int N, X;
	int m[1000];
	int ans = 0;
	int i;
	cin >> N >> X;
	for (i = 0;i < N;i++) {
		cin >> m[i];
	}
	for (i = 0;i < N;i++) {
		X -= m[i];
		ans++;
	}
	int min = m[0];
	for (i = 1;i < N;i++) {
		if (min > m[i])min = m[i];
	}
	while (1) {
		X -= min;
		if (X >= 0)ans++;
		if (X <= 0)break;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
int a[51],b[51],n,maxmin=0x7fffffff,maxmax,dp[51],num;
long long ans;
bool flag,bjuse[51];
bool check()
{
	for(int i=1;i<=n;i++)
	{
		memset(dp,0,sizeof(dp));
		dp[a[i]]=1;
		flag=0;
		for(int j=50;j>=1;j--)
			if(bjuse[j])
				for(int k=j;k<=50;k++)
					if(dp[k])
					{
						dp[k%j]=1;
						if(k%j==b[i])
						{
							flag=1;
							break;
						}
					}
		if(!flag&&!dp[b[i]])return 0;
	}
	return 1;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		scanf("%d",&b[i]);
	for(int i=1;i<=50;i++)
		bjuse[i]=1;
	if(!check())
	{
		printf("-1");
		return 0;
	}
	for(int i=50;i>=1;i--)
	{
		bjuse[i]=0;
		if(!check())bjuse[i]=1;	
	}
	for(int i=1;i<=50;i++)
		if(bjuse[i])ans|=1ll<<i;
	printf("%lld",ans);
	return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <cstdio>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
# define ll int64_t
# define str string
# define rep(i,n) for(ll i=0;i<n;i++)
# define rrep(i,n) for(ll i=1;i<=n;i++)
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define mod 1000000007
# define PI 3.141592653589793
# define vec vector
#define dump(x) cerr<<#x<<"="<<x<<endl
using namespace std;



#define INF 2000000000
#define MAX_V 10
const int MAX_N =10000000;


bool compare_by_b(pair<string,ll> a,pair<string,ll> b){
    if(a.second != b.second) return a.second<b.second;
    else return a.first<b.first;
}

bool my_compare(pair<ll,ll> a,pair<ll,ll> b){
    if(a.first != b.first) return a.first<b.first;
    if(a.second != b.second) return a.second>b.second;
    else return true;
}

ll modpow(ll a,ll n,ll mod1) {
    ll res=1;
    while(n>0){
        if(n&1) res=res*a%mod1;
        a = a*a%mod1;
        n >>= 1;
    }
    return res;
}

ll factorial(ll n){
    ll x=1;
    rrep(i,n) x*=i;
    return x;
}

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

ll func(ll a,ll b,ll& ans_x,ll& ans_y){
    if(b == 0){
        ans_x = 1;
        ans_y = 0;
        return a;
    }else{
        ll ret = func(b,(a%b),ans_y,ans_x);
        ans_y -= (a/b)*ans_x;
        return ret;
    }
}

ll par[MAX_N],ranking[MAX_N];

void init(ll n){
    rep(i,n){
        par[i]=i;
        ranking[i]=0;
    }
}

ll find(ll x){
    if(par[x]==x) return x;
    else return par[x]=find(par[x]);
}

void unite(ll x,ll y){
    x=find(x);
    y=find(y);
    if(x==y) return;
    
    if(ranking[x]<ranking[y]) par[x]=y;
    else{
        par[y]=x;
        if(ranking[x]==ranking[y]) ranking[x]++;
    }
}

bool same(ll x,ll y){
    return find(x)==find(y);
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll n,m;
    cin>>n>>m;
    init(n);
    rep(i,m){
        ll q,a,b;
        cin>>q>>a>>b;
        if(q==0) unite(a,b);
        else{
            if(same(a,b)) cout<<1<<endl;
            else cout<<0<<endl;
        }
    }
    return 0;
}



Measured memory_bytes: 3336
n, m = map(int, input().split())

state = {}
wa_count = {}
wa = 0
ac = 0
for i in range(m):
  no, result = input().split()
  if result == 'AC':
    if not state.get(no):
      ac += 1
      wa += wa_count.get(no, 0)
    state[no] = True
  elif result == 'WA':
    if not state.get(no):
      if no in wa_count.keys():
        wa_count[no] += 1
      else:
        wa_count[no] = 1

print(str(ac) + ' ' + str(wa))
 

Measured memory_bytes: 17116
#include <bits/stdc++.h>
#define REP(i, n) for(long long i=0; i<n; i++)
#define REPR(i, n) for(long long i=n-1; i>=0; i--)
#define FOR(i, m, n) for(long long i=m; i<=n; i++)
#define FORR(i, m, n) for(long long i=m; i>=n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(),(v).end()
#define FIN ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9+7;
const ll inf = 1e15;
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}


ll a[305][305];

int main(){FIN

    ll h,w,d;cin>>h>>w>>d;
    vector<pair<ll,ll>> v(h*w+1);
    REP(i,h) REP(j,w) {
        cin>>a[i][j];
        v[a[i][j]] = make_pair(i,j);
    }

    vll ans(h*w+1,0);
    REP(i,d){
        ll start = i+1;
        while(start+d<=h*w){
            ans[start+d] = ans[start] + abs(v[start].first-v[start+d].first) + abs(v[start].second-v[start+d].second);
            start+=d;
        }
    }
    ll q;cin>>q;

    REP(i,q){
        ll l,r;cin>>l>>r;
        cout<<ans[r]-ans[l]<<endl;
    }
    return 0;
}

Measured memory_bytes: 6020
#include <iostream>
#include <cstdio>
#include <bitset>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <utility>
#include <string>
#include <algorithm>
#include <cmath>
#include <limits>

using namespace std;
using ll=long long;
using vint=vector<int>;
using pint=pair<int,int>;

const double EPS = 1e-9;
const int inf = numeric_limits<int>::max();

#define rep(i,n) for(int i=0;i<int(n);++i)
#define rrep(i,n) for(int i=int(n-1);i>=0;--i)
#define REP(i,a,b) for(int i=int(a);i<int(b);++i)
#define RREP(i,a,b) for(int i=int(b-1);i>=int(a);--i)
#define SHOW(a) cout << #a << " = " << a << endl
#define ARR(a,n) for(int i=0;i<int(n);++i) cout << #a << "[" << i << "]" << " = " << a[i] << endl

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

  int s = 0;
  int n = N;

  while(n){
    s+=(n%10);
    n /= 10;
  }

  cout << ((N%s)?"No":"Yes") << endl;
}

int main(){
  solve();
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>//本当は必要なヘッダファイルだけインクルードするべきです
using namespace std;//本当は個別にusingするか変数や関数ごとにstd::をつけるべきです
//本当は関数内に宣言し明示的に初期化するべきです
int n,i,a[1000];
double s,ave,b;
int main(void){
  while(cin>>n){
    if(n==0)break;
    memset(a,0,sizeof(0));
    s=0;
    for(i=0;i<n;i++)cin>>a[i],s+=a[i];
    ave=s/n;
    b=0;
    for(i=0;i<n;i++)b+=pow(ave-a[i],2);
    printf("%f\n",sqrt(b/n));
  }
  return 0;
}



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

const int MAX_N = 1000005;
int N;
bool found_divisor[MAX_N];
int prime_counts[MAX_N], vals[MAX_N], prime_chain[MAX_N];

int main() {
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    int v;
    scanf("%d", &v);
    vals[v]++;
  }
  for (int64_t i = 2; i < MAX_N; ++i) {
    if (found_divisor[i]) continue;
    found_divisor[i] = true;
    prime_chain[i] = i;
    for (int64_t j = i * i; j < MAX_N; j += i) {
      if (found_divisor[j] == 0) prime_chain[j] = i;
      found_divisor[j] = true;
    }
  }
  bool pairwise = true, setwise = true;
  for (int i = 0; i < MAX_N; ++i) {
    for (int k = 0; k < vals[i]; ++k) {
      int v = i;
      while (v > 1) {
        int p = prime_chain[v];
        bool had_prime = false;
        while (v % p == 0) {
          v /= p;
          had_prime = true;
        }
        if (had_prime) {
          prime_counts[p] += 1;
          if (prime_counts[p] >= 2) pairwise = false;
          if (prime_counts[p] == N) setwise = false;
        }
      }
    }
  }
  if (pairwise) printf("pairwise coprime\n");
  else if (setwise) printf("setwise coprime\n");
  else printf("not coprime\n");
}


Measured memory_bytes: 16388
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

ans=0

for i in range (N):
    ans+=min(A[N-i],B[N-1-i])
    if  B[N-i-1]>A[N-i]:
        ans+=min(A[N-i-1],B[N-1-i]-A[N-i])
        A[N-i-1]-=min(A[N-i-1],B[N-1-i]-A[N-i])

print(ans)

Measured memory_bytes: 24048
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
int m[maxn];
int a[10][10];

int main(){
	int n;
	cin >> n;
	int cnt = 0;
	for(int i = 1;i <= 9;i++){
		for(int j = 1;j <= 9;j++){
			if(i == j && i <= n) a[i][j]++;
			if(j * 10 + i <= n) a[i][j]++;
			for(int k = 0;k <= 9;k++){
				int nu = j * 100 + k * 10 + i;
				if(nu <= n) a[i][j]++;
				else break;
			}
			for(int k = 0;k <= 99;k++){
				int nu = j * 1000 + k * 10 + i;
				if(nu <= n) a[i][j]++;
				else break;
			}
			for(int k = 0;k <= 999;k++){
				int nu = j * 10000 + k * 10 + i;
				if(nu <= n) a[i][j]++;
				else break;
			}
			for(int k = 0;k <= 9999;k++){
				int nu = j * 100000 + k * 10 + i;
				if(nu <= n) a[i][j]++;
				else break;
			}
		}
	}
	for(int i = 1;i <= 9;i++)
		m[i] = m[i-1] + a[i][i];
	for(int i = 10;i <= n;i++){
		if(i % 10 == 0){
			m[i] = m[i-1];
			continue;
		}
		int x = i%10,y;
		int t = i;
		while(t){
			y = t%10;
			t/=10;
		}
		m[i] = m[i-1]+a[y][x];
	}
	cout<<m[n]<<endl;
	return 0;
}

Measured memory_bytes: 1024
import sys
sys.setrecursionlimit(10000000)

def m():
    def dfs(v, p, c, depth):
        depth[v] = c
        for child in graph[v]:
            if child == p:
                continue
            dfs(child, v, depth[v]+1, depth)
    n = int(input())
    graph = [[] for _ in range(n + 1)]
    depth_from1 = [0] * (n+1)
    depth_fromN = [0] * (n+1)
    depth_from1[1] = 0
    depth_fromN[n] = 0

    for _ in range(n-1):
        a, b = map(int, input().split())
        graph[a].append(b)
        graph[b].append(a)

    dfs(1, -1, 0, depth_from1)
    dfs(n, -1, 0, depth_fromN)
    fene = 0
    sn = 0

    for i in range(1,n+1):
        if depth_from1[i] <= depth_fromN[i]:
            fene += 1
        else:
            sn += 1
    if fene > sn:
        return "Fennec"
    else:
        return "Snuke"


print(m())

Measured memory_bytes: 143280
/*
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
typedef mp::cpp_int mint; //*/
#include <bits/stdc++.h>
#define  re(i,j,n) for(li i=(j);i<(n);i++)
#define  rep(i,n) re(i,0,(n))
#define  red(i,n) for(li i=n-1;i>=0;i--)
#define  all(a) begin(a),end(a)
#define  pb(a) push_back(a)
using namespace std;
typedef long int li;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<li> vli;
typedef vector<string> vs;


int main()
{
    int a,b;
    cin>>a>>b;
    if(a+b>=10) cout<<"error"<<endl;
    else cout<<a+b<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> G;
int ans;
void dfs(int i, vector<int> use){
    use[i] = 1;
    int flag = 0;
    for(int j:G[i]){
        if(use[j] == 0) {
            dfs(j, use);
            flag = 1;
        }
    }
    if(flag == 0){
        int flag2 = 0;
        for(int i = 0; i < (int)use.size(); i++){
            if(use[i] == 0) flag2 = 1;
        }
        if(flag2 == 0) ans++;
    }
}
int main(){
    int N, M;
    cin >> N >> M;
    G.resize(N);
    for(int i = 0; i < M; i++){
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    ans = 0;
    vector<int> used(N, 0);
    dfs(0, used);
    cout << ans << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];
  int nxt = 1, ans = 0;
  rep(i,n) {
    if (nxt == a[i]) {
      ++nxt;
    } else {
      ++ans;
    }
  }
  if(ans==n) ans = -1;
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 1152
#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;

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

edge ed[2010];
ll d[1010];      //d[i]は始点からiまでの最短距離
int V,E;  
ll INF=pow(10,18);          //Vは頂点数、Eは辺数
bool NeCyEx=false;

void shortest_pass(int s){
    for(int i=1;i<=V;i++)d[i]=INF;
    d[s]=0;
    ll x;
    for(int i=0;i<=V+E;i++){
        if(i==V)x=d[V];
        if(i==V+E && x!=d[V]){
            NeCyEx=true;
            break;
        }
        bool update=false;
        for(int j=0;j<E;j++){
            edge e=ed[j];
            if(d[e.from]!=INF && d[e.to]>d[e.from]+e.cost){
                d[e.to]=d[e.from]+e.cost;
                update=true;
            }
        }
        if(!update)break;
    }
}

int main(){
    cin>>V>>E;
    for(int i=0;i<E;i++){
        int a,b;ll c;
        cin>>a>>b>>c;
        ed[i]=(edge){a,b,-c};
    }
    shortest_pass(1);
    if(NeCyEx){
        cout<<"inf"<<endl;
        return 0;
    }
    else{
        cout<<-d[V]<<endl;
        return 0;
    }
}

Measured memory_bytes: 256
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>

using namespace std;
int main(void)
{
	string a;
	cin >> a;
	for (int i = 0; i < a.size() - 8; i++)
	{
		cout << a[i];
	}
	cout << "\n";
}

Measured memory_bytes: 256
from collections import Counter


def solve(s):
    c = Counter(s)

    ans = 0
    for i in c.values():
        ans += i * (i - 1) // 2

    return ans


n = int(input())
s = []

for i in range(n):
    tmp = sorted(input())
    s.append("".join(tmp))

print(solve(s))


Measured memory_bytes: 68312
#include<bits/stdc++.h>
using namespace std;
#define ll        long long
#define  sci(x)   scanf("%d", &x)
#define  scl(x)   scanf("%lld", &x)
#define  scd(x)   scanf("%lf", &x)
#define l0(i,n)   for(ll i=0; i<n; i++)

int main ()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	//CODE TONOY
	ll n,k;
	cin >> n >> k;
	cout<<(ll) min(n%k,k-(n%k));
	return 0;
}



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

int main()
{
    char A, B;
    A = fgetc(stdin); fgetc(stdin); B = fgetc(stdin);
    if(A < B) cout << "<" << endl;
    else if(A > B) cout << ">" << endl;
    else cout << "=" << endl;
    return 0;
}

Measured memory_bytes: 256
S = input()
ans = 'Yes'
if len(S)%2 != 0:
    print('No')
    exit()
for i in range(0,len(S),2):
    if S[i] != 'h':
        print('No')
        exit()
    if S[i+1] != 'i':
        print('No')
        exit()
print('Yes') 


Measured memory_bytes: 2940
N=int(input())
p=list(map(float,input().split()))
dp=[[0 for i in range(N+1)] for j in range(N+1)]
dp[0][0]=1
for i in range(1,N+1):
    for j in range(N+1):
        if j>0:
            dp[i][j]+=dp[i-1][j]*(1-p[i-1])+dp[i-1][j-1]*p[i-1]
        else:
            dp[i][j]+=dp[i-1][j]*(1-p[i-1])


print(sum(dp[N][N//2+1:]))

Measured memory_bytes: 260104
//ABC150C
#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <math.h>
#include <stdio.h>
#include <climits>
#include <cfloat>
#include <string>
#include <unordered_map>
using namespace std;

int main(){
	int n;
	cin >> n;
	vector<int> x(n), p(n), q(n);
	int a, b;
	for(int i=0;i<n;i++){
		x.at(i) = i+1;
	}
	for(int i=0;i<n;i++){
		cin >> p.at(i);
	}
	for(int i=0;i<n;i++){
		cin >> q.at(i);
	}
	int count = 0;
	do{
		bool flaga = true, flagb = true;
		for(int i=0;i<n;i++){
			if(x.at(i) != p.at(i)){
				flaga = false;
			}
		}
		if(flaga){
			a = count;
		}
		for(int i=0;i<n;i++){
			if(x.at(i) != q.at(i)){
				flagb = false;
			}
		}
		if(flagb){
			b = count;
		}
		count++;
	}while(next_permutation(x.begin(),x.end()));
	cout << abs(a-b) << endl;
	return 0;
}


Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main(){
        bool list[50001];
        list[0] = list[1] = true;
        for(int i = 2; i <= 50000; i++){
                list[i] = false;
        }   
        for(int i = 2; i <= 50000; i++){
                if(!list[i]){
                        for(int j = 2; i*j <= 50000; j++){
                                list[i*j] = true;
                        }   
                }   
        }   
        int n;
        cin >> n;
        while(n){
                if(n > 50000) for(;;);
                int sum = 0;
                for(int i = 2; i*2 <= n; i++){
                        if(!list[i] && !list[n-i]) sum++;
                }   
                cout << sum << endl;
                cin >> n;
        }   
}

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

int main()
{
    long n, a, b; cin>>n>>a>>b;
    if ((b-a)%2==0) {
        cout<<(b-a)/2<<endl;
    }
    else {
        auto x=a-1;
        auto y=b-x;
        auto p=n-b;
        auto q=a+p;
        cout<<min(x+y/2, p+(n-q+1)/2)<<endl;
    }
}


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

using namespace std;

vector<int> pre, in, hint;

void postorder(vector<int>& res, int pl, int pr, int il, int ir) {
    if(pl==pr) return;
    int root = pre[pl];
    int im = hint[root];
    int ll = im - il;
    postorder(res, pl+1, pl+1+ll, il, im);
    postorder(res, pl+1+ll, pr, im+1, ir);
    res.push_back(root);
}


int main() {
    int n;
    cin >> n;
    pre.resize(n);
    in.resize(n);
    hint.resize(n+1);
    for(int i=0; i<n; i++) {
        cin >> pre[i];
    }
    for(int i=0; i<n; i++) {
        cin >> in[i];
        hint[in[i]] = i;
    }
    vector<int> post;
    postorder(post, 0, n, 0, n);
    for(int i=0; i<n; i++) {
        cout << post[i] << (i==n-1 ? "\n" : " ");
    }
    
    
}


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

int main ()
{
    int n, k;
    string s;
    cin >> n >> k >> s;
    k--;
    if (s[k] >= 'A' && s[k] <= 'Z') {
        s[k] = s[k] - 'A' + 'a';
    }
    cout << s;
}


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


int main(){
    string s;cin>>s;
    int length =s.size();
    int o,x;
    rep(i,length){
        if(s[i]=='o'){
            ++o;
        }
        else {
            ++x;
        }

    }
    int v=15-length;
    if(o+v>=8)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;


}


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

int main(){
    int A,B;
    cin >> A >> B;
    if(A%3==0||B%3==0||(A+B)%3==0){
        cout << "Possible" << endl;
    }else{
        cout << "Impossible" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <map>
using namespace std;

int main()
{
    long long int ans = 0;
    {
        int N;
        cin >> N;
        string s;
        map<string, int> mp;
        for (int i = 0; i < N; i++)
        {
            cin >> s;
            sort(s.begin(), s.end());
            if (mp[s] == 0)
            {
                mp[s] = 1;
            }
            else
            {
                ans += mp[s];
                mp[s]++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
/* 注意すべき点
グローバル以外の配列は初期化すべし
*/

Measured memory_bytes: 11136
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <functional>
#include <tuple>
#include <vector>
#include <sstream>
using namespace std;

int main() {
  string s;
  cin >> s;
  int total;
  for (int i=0; i<s.size(); i++){
    total = total%9 + (s[i]-'0');
  }
  if (total%9 == 0) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}

Measured memory_bytes: 3332
import Control.Arrow
import Data.List
import qualified Data.ByteString.Char8 as B

main :: IO ()
main = breads >>= print . solve 

solve :: [Int] -> Int
solve [n, m]
    | v == 0 = r
    | otherwise = head $ filter calc [r, (r - 1)..1]
    where
        (r, v) = m `divMod` n 

        calc ans = y == 0 && x > n + 1
            where
                (x, y) = m `divMod` ans

breads :: IO [Int]
breads = unfoldr (\s -> second (maybe B.empty snd . B.uncons) <$> B.readInt s) <$> B.getLine

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

ll MOD = 1e9+ 7;

int main() {
    int n;
    cin >> n;
    vector<ll>a(n);
    rep(i, n) cin >> a[i];
    ll sum = 0;
    rep(i, n) {
        sum += a[i];
        sum %= MOD;
    }
    ll ans = 0;
    rep(i, n - 1) {
        sum -= a[i];
        sum %= MOD;
        if (sum < 0) sum += MOD;
        ans += sum * a[i] % MOD;
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 4840
#include<bits/stdc++.h>
#define ci const int&
using namespace std;
struct edge{
	int t,nxt;
}e[200010];
int n,m,u,v,be[100010],cnt,d[100010],t4,li,vis[100010];
void add(ci x,ci y){
	e[++cnt]=(edge){y,be[x]},be[x]=cnt;
}
void dfs(ci x){
	vis[x]=1;
	for(int i=be[x];i;i=e[i].nxt)if(!vis[e[i].t])dfs(e[i].t);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;++i)scanf("%d%d",&u,&v),++d[u],++d[v],add(u,v),add(v,u);
	for(int i=1;i<=n;++i)if(d[i]&1)return puts("No"),0;
	for(int i=1;i<=n;++i)if(d[i]>4)return puts("Yes"),0;
	for(int i=1;i<=n;++i)if(d[i]==4){
		if(t4==2)return puts("Yes"),0;
		if(!t4)li=i,vis[i]=1;
		else{
			vis[i]=1;
			dfs(i);
			for(int j=be[li];j;j=e[j].nxt)if(!vis[e[j].t])return puts("Yes"),0;
		}
		++t4;
	}
	puts("No");
	return 0;
}

Measured memory_bytes: 7140
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
  int N; cin >> N;
  int sum = 0;
  int X = N;
  for ( int i = 0; i < 11; i++ ) {
    sum += X % 10;
    X /= 10;
  }
  if ( N % sum == 0 ) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>

#define NMAX 52
#define XMAX 52

int main(void){


  int n, a;
  std::cin >> n >> a;

  long int dp[NMAX][NMAX][NMAX*XMAX] = {};
  int array[NMAX] = {};

  for(int i=1; i<=n; i++){
    int x;
    std::cin >> x;
    array[i] = x;
  }
  
  for(int i=0; i<=n; i++){
    for(int k=0; k<=i; k++){
      for(int N=0; N<=i*XMAX; N++){
	if(i==0 && k==0 && N==0) dp[i][k][N] = 1;
        else if(i!=0 && N<array[i]) dp[i][k][N] = dp[i-1][k][N];
	else if(i!=0 && k!=0 && N>=array[i]) dp[i][k][N] = dp[i-1][k][N]+dp[i-1][k-1][N-array[i]]; 
	else dp[i][k][N] = 0;
	/*
	if(k==0 && N==0) dp[i][k][N] = 1;
	else if(k==0) dp[i][k][N] = 0;
	else if(i==0 && k==0 && N== 0) dp[i][k][N] = 1;
        else if(i==0) dp[i][k][N] = 0; 
	else if(N-array[i] >= 0) dp[i][k][N] = dp[i-1][k][N]+dp[i-1][k-1][N-array[i]];
        else dp[i][k][N] = dp[i-1][k][N];
	*/
      }
    }
  }

  long long int res = 0;
  for(int k=1; k<=n; k++){
    //std::cout << k << "," << k*a << "," << dp[n][k][k*a] << std::endl;
    res += dp[n][k][k*a];
  }

  std::cout << res << std::endl;
  return 0;
}

Measured memory_bytes: 57344
#include<stdio.h>
int main(void){
    int a,b;
    scanf("%d %d",&a,&b);
    if(a>b){
        printf("a > b\n");
    }
    else if(a<b){
        printf("a < b\n");
    }
    else if(a==b){
        printf("a == b\n");
    }
    return 0;
}

Measured memory_bytes: 580
#include <math.h>
#include <algorithm>  //sort,二分探索,など
#include <array>
#include <bitset>         //固定長bit集合
#include <cmath>          //pow,logなど
#include <complex>        //複素数
#include <deque>          //両端アクセスのキュー
#include <functional>     //sortのgreater
#include <iomanip>        //setprecision(浮動小数点の出力の誤差)
#include <iostream>       //入出力
#include <map>            //map(辞書)s
#include <numeric>        //iota(整数列の生成),gcdとlcm(c++17)
#include <queue>          //キュー
#include <set>            //集合
#include <stack>          //スタック
#include <string>         //文字列
#include <unordered_map>  //イテレータあるけど順序保持しないmap
#include <unordered_set>  //イテレータあるけど順序保持しないset
#include <utility>        //pair
#include <vector>         //可変長配列

using namespace std;
typedef long long ll;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()  // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())  // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000
#define MOD 10000007
#define PB push_back
#define MP make_pair
#define F first
#define S second

int main() {
  ll x;
  cin >> x;
  for (int i = 0; i <= 3760; ++i) {
    ll p = 100;
    ll count = 0;
    while (p < x) {
      p += p / 100;
      count++;
    }
    cout << count << endl;
    return 0;
  }}

Measured memory_bytes: 3640
#include <stdio.h>
#include <math.h>
int main(){
  int n,i,j,flag=0;
  int count=0;
  scanf("%d",&n);
  int sosuu[n];
  for(i=0;i<n;i++){
    scanf("%d",&sosuu[i]);
  }
  for(i=0;i<n;i++){
    if(sosuu[i]==2){
      count++;
    }
    if(sosuu[i]<2 || sosuu[i]%2==0)flag=1;

  
    j=3;
    while(j<=sqrt((double)sosuu[i])){
      if(sosuu[i]%j==0){
	flag=1;
	break;
      }
      j=j+2;
    }
    if(flag!=1)count++;
    flag=0;
  }
  printf("%d\n",count);
  return 0;
}

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

using namespace std;

typedef long long ll;

int bit(int x, int n){
	return (x >> n) & 1;
}

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

	int N, K;
	cin >> N >> K;

	vector<int> A(N);
	vector<ll> B(N);
	rep(i, N) cin >> A[i] >> B[i];

	int l = 32 - __builtin_clz(K);

	ll ans = 0;
	for(int i = 0; i < l; i++){
		if(bit(K, i) == 0) continue;

		ll tmp = 0;
		for(int j = 0; j < N; j++){
			int x = A[j];
			if(bit(x, i) == 1) continue;

			bool ok = true;
			for(int k = 30; k > i; k--){
				if(bit(x, k) > bit(K, k)) {
					ok = false;
					break;
				}
			}

			if(ok) tmp += B[j];
		}

		ans = max(ans, tmp);
	}

	ll tmp2 = 0;
	for(int j = 0; j < N; j++){
		if((A[j] | K) <= K) tmp2 += B[j];
	}

	ans = max(ans, tmp2);

	cout << ans << endl;
}


Measured memory_bytes: 1408
#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

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

Measured memory_bytes: 1164
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace AtCoder.ABC
{
	class QuestionA
	{
		public static void Main(string[] args)
		{
			var t = long.Parse(Console.ReadLine());

			var x = t / 200;

			Console.WriteLine(10 - x);
		}

		public static string[] ReadStrings()
		{
			return Console.ReadLine().Split(' ');
		}

		public static long[] ReadNums()
		{
			return ReadStrings().Select(i => long.Parse(i)).ToArray();
		}
	}
}


Measured memory_bytes: 27468
N,K=map(int,input().split())
R,S,P=map(int,input().split())
T=list(input())

score={'r':P,'s':R,'p':S}

ans=0
for i in range(K):
  ans += score[T[i]]

for i in range(K,N):
  if T[i-K] == T[i]:
    T[i] = ''
    continue
  ans += score[T[i]]

print(ans)

Measured memory_bytes: 4084
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll/*, vl, greater<ll>*/> pql; //降順(/*昇順*/)
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef pair<pl, ll> ppl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++)
#define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--)
#define all(v) (v).begin(), (v).end()
ll mod(ll a, ll b) {if(b == 0) return 0; return (a % b + b) % b;}
bool chmin(ll &a, ll b) {if(b < a) {a = b; return 1;} return 0;}
bool chmax(ll &a, ll b) {if(b > a) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\n';

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

  ll n;
  cin >> n;

  vl x(n), y(n);
  set<pl> balls;
  rep(i, n) {
    cin >> x[i] >> y[i];
    balls.insert(pl (x[i], y[i]));
  }

  ll ans = n;
  rep(i, n) rep(j, n) {
    if(i==j) continue;

    ll cnt=n, p=x[j]-x[i], q=y[j]-y[i];
    rep(k, n) {
      if(balls.count(pl (x[k]-p, y[k]-q))) cnt--;
    }
    chmin(ans, cnt);
  }

  cout << ans << newl;
  return 0;
}

Measured memory_bytes: 3648
import java.util.Scanner;
public class Main
{
	static int cost[][];
	static int n;
	public static void main(String[] args) 
	{
		Scanner in=new Scanner(System.in);
		for(;;)
		{
			n=in.nextInt();
			if(n==0)
				return;
			cost=new int[n+1][n+1];
			int ans=0;
			for(int i=0;i<n-1;i++)
			{
				int a=in.nextInt();
				int b=in.nextInt();
				int c=in.nextInt();
				cost[a][b]=cost[b][a]=c;
				ans+=cost[a][b]*2;
			}
			
			for(int i=2;i<=n;i++)
			{
				int reaf=0;
				int ii=0,jj=0;
				for(int j=1;j<=n;j++)
				{
					if(cost[i][j]>0||cost[i][j]==-1)
					{
						reaf++;
						ii=i;
						jj=j;
					}
				}
				if(reaf==1)
				{
					ans-=cost[ii][jj]*2;
					cost[ii][jj]=cost[jj][ii]=-1;
				}
			}
			System.out.println(ans-max(1));
		}
	}
	static int max(int m)
	{
		int cut=0;
		for(int i=2;i<=n;i++)
		{
			if(cost[m][i]>0)
			{
				cost[i][m]=0;
				cut=Math.max(cut, cost[m][i]+max(i));
			}
		}
		return cut;
	}
}

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

int main(){

    int n,total;
    scanf("%d",&n);
    total=1;
    for(int i=0;i<3;i++)
        total=total*n;

    printf("%d\n",total);

    return 0;
}


Measured memory_bytes: 128
#include <iostream>
#include <algorithm>
#include <vector>
#include <complex>
#include <cmath>
using namespace std;

typedef complex<double> P;

const double pi = acos(-1);
const double eps = 1e-4;

bool equals(double a, double b) {
  return abs(a-b) < eps;
}

double dot(P a, P b) {
  return a.real()*b.real() + a.imag()*b.imag();
}

double cross(P a, P b) {
  return a.real()*b.imag() - a.imag()*b.real();
}

inline double toRad(double d) {
  return pi*d/180.0;
}

const int WATASHIUME = 1<<0;
const int UME        = 1<<1;
const int MOMO       = 1<<2;
const int SAKURA     = 1<<3;

struct Tree {
  P pos;
  int type;
  int d;
};

class Field {
  vector<Tree> trees;
public:
  void add(const Tree &t) {
    trees.push_back(t);
  }

  int reach(const int w, const int a, const P &p) {
    int res = 0;
    for(int i = 0; i < trees.size(); ++i) {
      P m = p - trees[i].pos;
      double dist = abs(m);
      if(equals(dist, a) || dist < a) ; else continue;
      P s = P( cos(toRad(w) - toRad(trees[i].d)/2.0),
               sin(toRad(w) - toRad(trees[i].d)/2.0) );
      P t = P( cos(toRad(w) + toRad(trees[i].d)/2.0),
               sin(toRad(w) + toRad(trees[i].d)/2.0) );
      double cs = cross(s, m);
      double ct = cross(t, m);
      if(  ( equals(cs, 0.0) || cs > 0.0 ) &&
           ( equals(ct, 0.0) || ct < 0.0 )  ) {
        res |= trees[i].type;
      }
    }
    return res;
  }
};

int main() {
  int H, R;
  int U, M, S, du, dm, ds;
  while(cin >> H >> R && (H|R)) {
    Field field;
    vector<P> ps(H);
    for(int i = 0; i < H; ++i) {
      cin >> ps[i].real() >> ps[i].imag();
    }
    cin >> U >> M >> S >> du >> dm >> ds;
    field.add((Tree){P(0,0), WATASHIUME, du});
    for(int i = 0; i < U; ++i) {
      P pos;
      cin >> pos.real() >> pos.imag(); 
      field.add((Tree){pos, UME, du});
    }
    for(int i = 0; i < M; ++i) {
      P pos;
      cin >> pos.real() >> pos.imag(); 
      field.add((Tree){pos, MOMO, dm});
    }
    for(int i = 0; i < S; ++i) {
      P pos;
      cin >> pos.real() >> pos.imag(); 
      field.add((Tree){pos, SAKURA, ds});
    }

    int cnt[H];
    fill(cnt, cnt+H, 0);

    while(R--) {
      int w, a;
      cin >> w >> a;
      for(int i = 0; i < H; ++i) {
        if(field.reach(w,a,ps[i]) == WATASHIUME) {
          cnt[i] += 1;
        }
      }
    }

    vector<pair<int,int> > v;
    for(int i = 0; i < H; ++i) {
      if(cnt[i] != 0)
        v.push_back(make_pair(-cnt[i], i+1));
    }
    if(v.size()) {
      sort(v.begin(), v.end());
      for(int i = 0; i < v.size(); ++i) {
        if(i && v[i].first != v[i-1].first) break;
        if(i) cout << " ";
        cout << v[i].second;
      }
      cout << endl;
    } else {
      cout << "NA" << endl;
    }
  }
  return 0;
}

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

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

ll lcm(ll a, ll b) {
	ll ab = a * b;
	return ab / gcd(a, b);
}

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

	ll ans = lcm(A, B);

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout<<fixed<<setprecision(15);
template<class T>bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
using namespace std;

// 連長圧縮（ランレングス圧縮）：sの連長部分を{文字,個数}の配列で返す。
vector<pair<char, long long>> RunLengthEncoding(string s){
    vector<pair<char, long long>> ret;
    long long i = 0, n = s.size();
    while(i<n){
        char tmp = s[i];
        long long cnt = 1;
        while(i<n){
            i++;
            if(tmp == s[i]) cnt++;
            else break;
        }
        ret.push_back(make_pair(tmp, cnt));
    }
    return ret;
}

int main(){
    Init();
    string s; ll k;
    cin >> s >> k;
    
    auto rle = RunLengthEncoding(s);
    if(rle.size() == 1){
        // 全部同じ文字の場合
        cout << (s.size()*k)/2 << endl;
    }
    else{
        ll loopCnt = 0;
        rep(i, s.size()-1){
            if(s[i] == s[i+1]){
                loopCnt++;
                i++;
            }
        }
        if(rle[0].first != rle[rle.size()-1].first){
            // 最初の文字と最後の文字が異なる場合
            cout << loopCnt*k << endl;
        }
        else{
            // 最初の文字と最後の文字が同じ場合
            ll sub = (rle[0].second/2 + rle[rle.size()-1].second/2 - (rle[0].second+rle[rle.size()-1].second)/2)*(k-1);
            cout << loopCnt*k-sub << endl;
        }
    }
}

Measured memory_bytes: 3644
#include<iostream>
#include<vector>
#include<set>
using namespace std;
 
typedef vector<vector<int> > Graph;
typedef pair<int, int> pii;
struct Node{
  int order, min_order;
};
class BridgeFind{
public:
  BridgeFind(Graph _g){
    g = _g;
    node.resize(g.size(), Node{-1, (int)1e9});
  }
  set<pii> find(){
    if(current_order == 0)dfs();
    return brige;
  }
private:
  void dfs(int current  = 0,
           int previous = -1){
    node[current].order   = current_order;
    node[current].min_order = current_order++;
    int to;
    for (int i = 0; i < g[current].size(); i++) {
      to = g[current][i];
      if(node[to].order == -1) dfs(to, current);
      if(previous == to)continue;
      node[current].min_order =
        min(node[current].min_order,
            node[to].min_order);
    }
    if(node[current].min_order == node[current].order and
       previous != -1){
      if(previous < current)
        brige.insert(pii(previous, current));
      else
        brige.insert(pii(current, previous));
    }
  }
  int current_order = 0;
  set<pii> brige;
  Graph g;
  vector<Node> node;
};
 
int main(){
  int n, m;
  std::cin >> n >> m;
  Graph g(n, vector<int>());
  for (int i = 0; i < m; i++) {
    int to, from;
    std::cin >> to >> from;
    g[--to].push_back(--from);
    g[from].push_back(to);
  }
  BridgeFind bf(g);
  set<pii> bridge = bf.find();
  std::cout << bridge.size() << std::endl;
  return 0;
}


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

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

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

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

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

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

int main(){
    string s; cin >> s;
    int x, y; cin >> x >> y;
    vector<int> lis;
    int count = 0;
    REP(i, 0, s.size()){
        if(s[i] == 'T'){
            lis.pb(count);
            count = 0;
            lis.pb(-1);
        }else{
            count++;
        }
    }
    if(count > 0){
        lis.pb(count);
    }
    /*REP(i, 0, lis.size()){
        cout << lis[i];
    }
    cout << endl;*/
    vector<set<int>> xdp(lis.size()+1, set<int>()), ydp(lis.size() + 1, set<int>());
    xdp[0].insert(lis[0]);
    if(lis.size() < 3){
        if(y == 0 && x == lis[0]){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
        return 0;
    }
    ydp[2].insert(lis[2]);
    ydp[2].insert(-1 * lis[2]);
    int xkey = 0, ykey= 2;
    REP(i, 4, lis.size()){
        if(i % 4 == 0){
            //cout << lis[i] << endl;
            for(auto x:xdp[i-4]){
                xdp[i].insert(x + lis[i]);
                xdp[i].insert(x - lis[i]);
            }
            xdp[i-4].clear();
           xkey = i;    
        }else if(i % 2 == 0){
            //cout << lis[i] << endl;
            for(auto x:ydp[i-4]){
                ydp[i].insert(x + lis[i]);
                ydp[i].insert(x - lis[i]);
            }
            ydp[i-4].clear();
            ykey = i;
        }
    }
    //cout << xkey << ykey << endl;
    if(xdp[xkey].find(x) != xdp[xkey].end() && ydp[ykey].find(y) != ydp[ykey].end()){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
    return 0;
}


Measured memory_bytes: 1920
from operator import sub

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

numA = [[0] * K for i in range(K)]
numB = [[0] * K for i in range(K)]

for i in range(N):
    x, y, c = input().split()
    x = int(x) % (2 * K)
    y = int(y) % (2 * K)

    if c == 'B':
        if (x // K == 0 and y // K == 0) or (x // K == 1 and y // K == 1):
            numA[x % K][y % K] += 1
        else:
            numB[x % K][y % K] += 1
    else:
        if (x // K == 1 and y // K == 0) or (x // K == 0 and y // K == 1):
            numA[x % K][y % K] += 1
        else:
            numB[x % K][y % K] += 1


sumXA = list(map(sum, numA))
sumYA = list(map(sum, zip(*numA)))

sumXB = list(map(sum, numB))
sumYB = list(map(sum, zip(*numB)))

sumA = sum(sumXA)
sumB = sum(sumXB)

Arr = [[0] * K for i in range(K)]
numOKA = sumA
numOKB = sumB
for x in range(K):

    tempOKA = numOKA
    tempOKB = numOKB
    for y in range(K):
        Arr[x][y] = max(tempOKA + sumB - tempOKB, sumA - tempOKA + tempOKB)
        tempOKA -= sumYA[y]
        tempOKB -= sumYB[y]

    sumYA = list(map(sub, sumYA, numA[x]))
    sumYA = list(map(sub, sumYA, numA[x]))
    numOKA -= sumXA[x]

    sumYB = list(map(sub, sumYB, numB[x]))
    sumYB = list(map(sub, sumYB, numB[x]))
    numOKB -= sumXB[x]

print(max(map(max, Arr)))


Measured memory_bytes: 58716
import heapq

n = int(input())
array = [int(i) for i in input().split()]
ans=-1e14
l_array = []
r_array = []
l_sum = [0]*(n+1)
r_sum = [0]*(n+1)

# left-n and right-n numbers
for i in range(n):
    heapq.heappush(l_array, array[i])
    heapq.heappush(r_array, (-array[3*n-1-i], array[3*n-1-i]))

l_sum[0] = sum(l_array)
r_sum[n] = sum([i[1] for i in r_array])

for i in range(n, 2*n):
    heapq.heappush(l_array, array[i])
    heapq.heappush(r_array, (-array[3*n-1-i], array[3*n-1-i]))
    l_min = heapq.heappop(l_array)
    r_max = heapq.heappop(r_array)[1]

    # l_sum[i-n+1] = sum(l_array)
    # r_sum[2*n-1-i] = sum([j[1] for j in r_array])
    l_sum[i-n+1] = l_sum[i-n] - l_min + array[i]
    r_sum[2*n-1-i] = r_sum[2*n-i] - r_max + array[3*n-1-i]

for i in range(n+1):
    ans = max(ans, l_sum[i]-r_sum[i])

print(ans)

Measured memory_bytes: 38308
#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <stack>
#include <queue>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <iomanip>
using namespace std;

#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define pb push_back

typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

const int mod = (int)1e9 + 7;
const int mod2 = 998244353;

ll exp(ll taban, ll us, ll md) {
    ll carpan = taban % md;
	if(carpan == 0) return 0;
    ll temp = us;
    ll res = 1;
    while(temp){
        if(temp % 2) res = (res*carpan) % md;
        temp /= 2;
        carpan = (carpan*carpan) % md;
    }
    return res;
}
 
ll ebob(ll a, ll b){
    if(!a)return b;
    return ebob(b%a, a);
}

ll ekok(ll a, ll b){
    return (a*b)/ebob(a, b);
}

ll mul(ll a, ll b, ll md){
	return a*b % md;
}

vector<ll> fact;
vector<ll> inv_fact;
void fact_init(int n){
	fact.resize(n+5);
	inv_fact.resize(n+5);
	fact[0] = inv_fact[0] = 1;
	for(int i = 1; i <= n; i++){
		fact[i] = (fact[i-1] * i) % mod;
		inv_fact[i] = exp(fact[i], mod-2, mod);
	}
}

ll komb(ll a, ll b){
	if(a < b) return 0;
	return fact[a] * (inv_fact[a-b] * inv_fact[b] % mod) % mod;
}


const int N = 2e5+5;

int main(){
	ios::sync_with_stdio(false); cin.tie(NULL);
	int r, c; cin>>r>>c;
	vector<string> vec(r);
	for(int i = 0; i < r; i++) cin>>vec[i];
	queue<pii> q;
	bool vis[r][c] = {};
	int dx[] = {-1, 1, 0, 0};
	int dy[] = {0, 0, -1, 1};
	ll ans = 0;
	for(int i = 0; i < r; i++){
		for(int j = 0; j < c; j++){
			q.push({i, j});
			ll siyah = 0, beyaz = 0;
			while(!q.empty()){
				int cr = q.front().first, cc = q.front().second;
				q.pop();
				if(vis[cr][cc]) continue;
				vec[cr][cc] == '.' ? beyaz++ : siyah++;
				vis[cr][cc] = true;
				for(int d = 0; d < 4; d++){
					int nr = cr + dx[d], nc = cc + dy[d];
					if(nr >= 0 && nr < r && nc >= 0 && nc < c){
						if(vec[nr][nc] != vec[cr][cc]) q.push({nr, nc});
					}
				}
			}
			ans += siyah * beyaz;
		}
	}
	cout<<ans<<"\n";
}

Measured memory_bytes: 640
N[]={6,10,18,14,2,13,22,11,3,5,21,19,1,17,23,7,0,9,20,15,4,12,16,8};
E[]={15,7,11,19,8,3,14,23,16,0,6,22,4,2,18,20,12,1,10,21,9,5,13,17};
W[24],S[24];

n,d,a,i;
char s[9];
main(c)
{
for(c=576;c--;)W[c/24]=E[c%24]==c/24?c%24:W[c/24],S[c/24]=N[c%24]==c/24?c%24:S[c/24];
for(;c-48&&scanf("%s",s);)
c=*s,
c<60&&a?printf("%d\n",a),a=d=0:0,
d=
(c-78?0:N[d])+
(c-69?0:E[d])+
(c-87?0:W[d])+
(c-83?0:S[d])+
(c-82?0:d&28|d%4+1&3)+
(c-76?0:d&28|d%4+3&3),
a+=d/4+1,d%=24;
}

Measured memory_bytes: 0
l = list(map(int, input().split(" ")))
a = l[0]
b = l[1]
t = float(l[2])

ans = 0
i = 0

while i <= (t+0.5):
    if (i%a) == 0 and i != 0:
        ans += b
    
    i += 1

print(ans)

Measured memory_bytes: 3064
use std::io::*;
use std::str::FromStr;

pub fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

use std::cmp::min;

fn main() {
    let n: usize = read();
    let mut map: Vec<Vec<i64>> = vec![vec![1_000_000_000; n]; n];
    let mut ans = 0;
    for i in 0..n {
        for j in 0..n {
            let dist: i64 = read();
            if i < j {
                ans += dist;
            }
            if dist != 0 {
                map[i][j] = dist;
            }
        }
    }

    let mut map_b: Vec<Vec<i64>> = vec![vec![0; n]; n];
    for k in 0..n {
        for i in 0..n {
            for j in 0..n {
                if map[i][j] == map[i][k] + map[k][j] {
                    map_b[i][j] = -1;
                }
                if map[i][j] > map[i][k] + map[k][j] && i != j {
                    println!("-1");
                    return;
                }
            }
        }
    }

    for i in 0..n {
        for j in 0..n {
            if map_b[i][j] == -1 && i < j {
                ans -= map[i][j]
            }
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 4352
gets
a = gets.chomp.split(" ").map(&:to_i)

zero = false
a.each{|e|
  if e == 0
    zero = true
    break
  end
}
if zero
  puts("#{0}")
else
  cut = false
  ans = a.inject{|i,j|
    i = i*j
    if i > 1000000000000000000
      cut = true
      break
    end
    i
  }
  if cut
    puts("#{-1}")
  else
    puts("#{ans}")
  end
end

Measured memory_bytes: 23592