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

int main() {
  
  int H, W;
  cin >> H >> W;
  
  for(int a = 0; a < W + 1; a++){
    cout << '#';
  }
  
  cout << '#' << endl;
  
  for(int i = 0; i < H; i++){
    vector<string> vec(H);
    cin >> vec.at(i);
    cout << '#' << vec.at(i) << '#' << endl;
  }
 
  for(int b = 0; b < W + 2; b++){
    cout << '#';
  }
  return 0;    
}

Measured memory_bytes: 256
n = int(input())
fac = [1]
for d in range(1,11):
  fac.append(fac[-1]*d)
#print(fac)
l = fac[n]
fac2 = []
for d in range(n):
  fac2.append(fac[n]//fac[d+1])
for i in range(l):
  tmp = []
  cum = []
  for d in range(n):
    j = i//(fac2[d])%(d+1)
    tmp.append(j)
    if not cum:
      cum = [0]
    else:
      if j > cum[-1] + 1:
        break
      cum.append(max(cum[-1],j))
  #print(tmp)
  #print(cum)
  #for d in range(1,n):
    #if cum[d] > cum[d-1] + 1:
      #break
  else:
    ans = ''
    for d in range(n):
      ans += chr(tmp[d]+97)
    print(ans)

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

int main(){
  int X;
  cin >> X;
  set<int> hoge;
  hoge.insert(1);
  for (int i=2; i<=10; i++){
    for (int j=2; j<=50; j++){
      int tmp = pow(j,i);
      if (tmp>X) break;
      hoge.insert(tmp);
    }
  }
  int ans = 0;
  for (auto k: hoge){
    ans = max(k,ans);
  }
  cout << ans << endl;
}


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

typedef long long ll;
const ll INF = 1e9, MOD = 1e9 + 7;
const double EPS = 1e-9, PI = 3.141592653589793;
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
#define m_p make_pair

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  ll n,T,ans=0;
  cin >> n >> T;
  vector<ll> t(n);
  rep(i,n) cin >> t[i];
  rep(i,n-1){
    ans += min(t[i+1]-t[i], T);
  }
  ans += T;
  cout << ans << "\n";
  return 0;
}

Measured memory_bytes: 1792
/*
   first we need to sort the all edges then updates them one after another .
   we update them according to the adjacent edges then maximize the answer of edge
*/
#include<queue>
#include<map>
#include <string>
#include<bits/stdc++.h>
#define pi 3.141592654
#define forr(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
#define input ios_base::sync_with_stdio(0);cin.tie(0);
//#define x real()
//#define y imag()
using namespace std;
typedef pair<double,double>pdd;
typedef long long  ll;
typedef pair<ll, ll>pii;
//typedef complex<double> point;
//template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
//typedef bitset<30> mask;
//int x[8]={1,0,0,-1,-1,-1,1,1};
//int y[8]={0,1,-1,0,-1,1,-1,1};
//#define var(x) ((x)<<1)
//#define nvar(x) ((x)^1)
int n,m,x;
multiset<int>a;
pair<int,int>b[100010];
ll ans;
 main()
{

//cout<<Pow((ll)1000000,(ll)110);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//    char input[15];
//    scanf("%s", &input);  array of char
//  gets(c+1);  array of char
//printf("%s, ",name[k[i]].c_str());  printf of string
input;

cin>>n>>m;
forr(i,1,n)
{
cin>>x;
ans+=(ll)x;
a.insert(x);
}


forr(i,1,m)
{
  cin>>b[i].S>>b[i].F;
  b[i].F*=-1;
}

sort(b+1,b+1+m);

forr(i,1,m)
{
  int g=b[i].F*-1;
  int c=b[i].S;
  while(c--&&(int)a.size()&&*a.begin()<g)
  {
      ans-=(ll)*a.begin();
      ans+=(ll)g;
      a.erase(a.begin());
  }
}
cout<<ans;
return 0;
}


Measured memory_bytes: 5760
#include <bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int INF=1e9;
int n,sum[maxn],b[maxn];
vector<int> m[maxn];
bool dfs(int u) {
	vector<int> f (sum[u]+1,INF);
	f[0]=0;
	for (int ii=0;ii<m[u].size();ii++) {
		int v=m[u][ii];
		if (!dfs(v)) return 0;
		vector<int> g (sum[u]+1,INF);
		for (int i=0;i+sum[v]<=sum[u];i++)
			g[i+sum[v]]=min(g[i+sum[v]],f[i]+b[v]);
		for (int i=0;i+b[v]<=sum[u];i++)
			g[i+b[v]]=min(g[i+b[v]],f[i]+sum[v]);
		f=g;
	}
	b[u]=*min_element(f.begin(),f.end());
	return b[u]<INF;
}
int main() {
	scanf("%d",&n);
	for (int i=2;i<=n;i++) {
		int x; scanf("%d",&x);
		m[x].push_back(i);
	}
	for (int i=1;i<=n;i++)
		scanf("%d",&sum[i]);
	if (!dfs(1)) printf("IM");
	printf("POSSIBLE\n");
	return 0;
}

Measured memory_bytes: 5756
/* SANDEEP YADAV*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double
#define lld long double
#define umap unordered_map<>
#define sz(x) ((ll)((x).size()))
#define all(x) (x).begin(),(x).end()
#define vi vector<int>
#define pi pair<int, int>
#define setp cout<<fixed<<setprecision(6)
#define loop(i,a,b)  for(int i=a;i<b;i++)
#define rloop(i,b,a) for(int i=b-1;i>=a;i--)
#define vl vector<ll>
#define pl pair<ll, ll>
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
ll gcd(ll a,ll b){ if(a<b) { a=a+b; b=a-b; a=a-b;}while(b!=0){ll r=a%b; a=b; b=r;} return a;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long power(long long a, long long b, long long m) {a %= m;long long res = 1;while (b > 0) {if (b & 1)res = res * a % m;a = a * a % m;b >>= 1;}return res;}
const int N=100005;
void judge()
{
      #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    freopen("error.txt","w",stderr);
    #endif
}
struct myhash
{
    static uint64_t fxn(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
 
    size_t operator ()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return fxn(x + FIXED_RANDOM);
    }
};
 vector<vector<int>>g(N+1);
 bool yvisited[N+1];
 bool visited[N+1];
 ll level[N+1];
 void dfs(int source)
 {
    visited[source]=true;
    for(auto x:g[source])
    {
       if(!visited[x])
       {
        dfs(x);
        }
        level[source]=max(level[x]+1,level[source]);
    }
}
void solve(int tc)
{
    
    int n,m;
    cin>>n>>m;

   
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;

        g[x].pb(y);
        yvisited[y]=true;
    }
    
    for(int i=1;i<=n;i++)
    {
        if(!yvisited[i]&&!visited[i])
            dfs(i);
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
        ans=max(ans,level[i]);
    cout<<ans;
    return;
}
int main()
{
    fastio;
   // judge();

    int T=1;
    // cin>>T;
    for(int tc=1;tc<=T;tc++)
    {
        solve(tc);
    }
    return 0;
}

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

int n;

int main(){
  cin >> n;
  int odd = 0, even2 = 0, even4 = 0;
  rep(i, n){
    int a;
    cin >> a;
    if(a % 4 == 0){
      even4++;
    }else if(a % 2 == 0){
      even2++;
    }else{
      odd++;
    }
  }
  if(even2 == 0){
    if(even4 >= odd - 1){
      cout << "Yes" << endl;
    }else{
      cout << "No" << endl;
    }
  }else if(even2 == 1 && odd == 0 && even4 == 0){
    cout << "No" << endl;
  }else{
    if(even4 >= odd){
      cout << "Yes" << endl;
    }else{
      cout << "No" << endl;
    }
  }
}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const* argv[]){
    char c;
    scanf("%s", &c);

    if (c == 'a' || c == 'i' || c == 'u' || c == 'e' || c == 'o') {
        printf("vowel\n");
    }else{
        printf("consonant\n");
    }

    return 0;
}

Measured memory_bytes: 128
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func getNextLine(scanner *bufio.Reader) string {
	var buffer string
	for {
		line, isPrefix, _ := scanner.ReadLine()
		buffer += string(line)
		if isPrefix == false {
			break
		}
	}
	return buffer
}

func getIntList(scanner *bufio.Reader) []int {
	list := strings.Split(getNextLine(scanner), " ")
	l := len(list)
	result := make([]int, l)
	for i := 0; i < l; i++ {
		result[i], _ = strconv.Atoi(list[i])
	}
	return result
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := bufio.NewReader(fp)
	writer := bufio.NewWriter(os.Stdout)

	var n, m int
	fmt.Sscan(getNextLine(scanner), &n, &m)

	l := 1
	r := n
	for i := 0; i < m; i++ {
		lr := getIntList(scanner)
		if l < lr[0] {
			l = lr[0]
		}
		if r > lr[1] {
			r = lr[1]
		}
	}
	if r-l+1 > 0 {
		fmt.Fprint(writer, r-l+1)
	} else {

		fmt.Fprint(writer, 0)
	}
	writer.Flush()

}


Measured memory_bytes: 5504
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
int main() {
  ll n,k;
  cin>>n>>k;
  ll p[n+5];
  rep(i,n){
    cin>>p[i];
  }
  sort(p,p+n);
  ll money=0;
  rep(j,k){
    money=money+p[j];
  }
  cout<<money<<endl;
}

Measured memory_bytes: 3632
'use strict'
function main(input: string) {
    const n: number = Number(input);
    const sqrt = Math.sqrt(n);
    for (let i = 1; i <= sqrt; i++) {
        if (n % i === 0 && n / i <= 9) {
           console.log("Yes");
           return;
        }
    }
    console.log("No");
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 7372
#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> v;
long long n, a, b;

int main() {
	scanf("%lld %lld %lld",&n,&a,&b);
	if(a>n || b>n) {
		puts("-1");
		return 0;
	}
	if(a+b-1>n || (n+b-1)/b>a) {
		puts("-1");
		return 0;
	}
	for(int i=1;i<=n;i++)
		v.push_back(i);
	reverse(v.begin(),v.begin()+b);
	if(a==1) {
		for(int i=0;i<v.size();i++)
			printf("%d ",v[i]);
		puts("");
		return 0;
	}
	int need=(n-b)%(a-1);
	for(int i=b;i<v.size();) {
		if(need) {
			reverse(v.begin()+i,v.begin()+i+(n-b)/(a-1)+1);
			need--;
			i+=(n-b)/(a-1)+1;
		} else {
			reverse(v.begin()+i,v.begin()+i+(n-b)/(a-1));
			i+=(n-b)/(a-1);
		}
	}
	for(int i=0;i<v.size();i++)
		printf("%d ",v[i]);
	puts("");
	return 0;
}

Measured memory_bytes: 3440
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
int main(void){
    int n,m,sum=0;
    cin>>n>>m;
    vector<int>a(m);
    rep(i,m)cin>>a.at(i);
    rep(i,m){
        sum += a.at(i);
        if(sum>n){
            cout<<"-1";
            return 0;
        }
    }
    cout << n-sum;
    return 0;
}

Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)


int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >>N;
  int at[N];
  int t;
  multiset<int> a;
  int m1=0;
  int m2=0;
  rep(i,N){
    cin >>t;
    at[i]=t;
    a.insert(t);
    if(t<m1) m2=max(t,m2);
    else if(t==m1) continue;
    else{
      m2=m1;
      m1=t;
    }       
  }
  if(a.count(m1)==1){
    rep(i,N){
      if(at[i]==m1) cout << m2 << endl;
      else cout << m1 << endl;
    }
  }
  else{
    rep(i,N) cout << m1 << endl;
  }
  return 0;
    

}


Measured memory_bytes: 11776
def judge():
    for i in range(1, n):
        before = list[i-1]
        l = s[i]
        if l == 'o':
            if list[i] == 'S':
                if before == 'S':
                    list.append('S')
                else:
                    list.append('W')
            else:
                if before == 'S':
                    list.append('W')
                else:
                    list.append('S')
        else:
            if list[i] == 'S':
                if before == 'S':
                    list.append('W')
                else:
                    list.append('S')
            else:
                if before == 'S':
                    list.append('S')
                else:
                    list.append('W')
    if list[0] == list[n]:
        if list[0] == 'S':
            if s[0] == 'o' and list[1] == list[n-1] or s[0] == 'x' and list[1] != list[n-1]:
                list.pop()
                print("".join(list))
                exit()
        elif list[0] == 'W':
            if s[0] == 'x' and list[1] == list[n-1] or s[0] == 'o' and list[1] != list[n-1]:
                list.pop()
                print("".join(list))
                exit()

n = int(input())
s = input()

list = ['S', 'S']
judge()
list.clear()
list = ['S', 'W']
judge()
list.clear()
list = ['W', 'S']
judge()
list.clear()
list = ['W', 'W']
judge()
list.clear()
print('-1')

Measured memory_bytes: 4228
#include<stdio.h>

int main(){
  int i,N,cnt = 0;
  int a[100];

  scanf("%d",&N);

  for(i=0;i<N;i++)
    scanf("%d",&a[i]);

  for(i=0;i<N-1;i++){
    if(a[i] == a[i+1]){
      cnt++;
      i++;
    }
  }

  printf("%d",cnt);

  return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
int main(void){
    int n;
    long long int a,b;
    long long int nowa,nowb;
    cin>>n;
    cin>>nowa>>nowb;
    for(int i=1;i<n;i++){
        cin>>a>>b;
        if(((double)nowa/a)>((double)nowb/b)){
            nowa=a*((nowa/a)+!!(nowa%a));
            nowb=b*((nowa/a)+!!(nowa%a));
        }else{
            nowa=a*((nowb/b)+!!(nowb%b));
            nowb=b*((nowb/b)+!!(nowb%b));
        }
        //cout<<nowa<<" "<<nowb<<endl;
    }
    cout<<nowa+nowb<<endl;
}


Measured memory_bytes: 256
/*
ID: learnin7
TASK: test
LANG: C++                 
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
        
    ios_base::sync_with_stdio(0);
    //ofstream fout ("test.out");
    //ifstream fin ("test.in");
    //fin >> a >> b;
    //fout << a+b << endl;
    int s;
    cin>>s;
    int cnt=1;
    set<int> x;
    x.insert(s);
    while(true){
        if(s%2==0)  s/=2;
        else        s = 3*s+1;
        cnt++;
        if(x.count(s)){
            cout<<cnt<<endl;
            break;
        }
        else{
            x.insert(s);
        }
    }
    return 0;
}



Measured memory_bytes: 256
#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

int main(void){
	string S; cin >> S;
	if (S == "RRR") cout << 3 << endl;
	else if (S == "RRS" || S == "SRR") cout << 2 << endl;
	else if (S == "SSS") cout << 0 << endl;
	else cout << 1 << endl;
	return 0;
}

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

int main(){
  int N;
  cin >>N;
  int P = 0;
  for (int i=0; i<N; i++){
    int x;
    cin >> x;
    P += x*pow(10,N-i-1);
  }
  int Q = 0;
  for (int i=0; i<N; i++){
    int x;
    cin >> x;
    Q += x*pow(10,N-i-1);
  }
  vector<int> v(N);
  for (int i=0; i<N; i++) v[i] = i+1;
  int counter = 0;
  int indexP = 0;
  int indexQ = 0;
  do {
    counter ++;
    int ans = 0;
    for (int i=0; i<N; i++){
      ans += v[i]*pow(10,N-i-1);
    }
    if (ans == P) indexP = counter;
    if (ans == Q) indexQ = counter;
  } while(next_permutation(v.begin(), v.end()));
  cout << abs(indexP-indexQ) << endl;
}

Measured memory_bytes: 256
#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
using namespace std;
void Main();
#ifdef ConanYu
#include "local.hpp"
#else
#define debug(...) do { } while(0)
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  Main();
}
#endif

void Main() {
  int a; string s;
  cin >> a >> s;
  if(a >= 3200) {
    cout << s << "\n";
  } else {
    cout << "red\n";
  }
}


Measured memory_bytes: 256
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <stack>
#include <vector>
#include <set>
#include <tuple>
#include <utility>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
const int INF = 1000000000;
const int MOD = 1000000007;
int main(){
	int n;
	cin >> n;
	vector<int> a(n);
	for(int i = 0;i < n;i++){
		cin >> a[i];
	}

	if(n == 2){
		if(a[0] * a[1] % 4 == 0)cout << "Yes" << endl;
		else cout << "No" << endl;
		return 0;
	}


	int cnt2 = 0;
	int cnt4 = 0;
	for(int i = 0;i < n;i++){
		if(a[i] % 4 == 0){
			cnt4++;
		}
		else if(a[i] % 2 == 0){
			cnt2++;
		}
	}

	if(n - cnt2 - cnt4 == 0)cout << "Yes" << endl;
	else{
		int cnt_odd = n - cnt2 - cnt4;
		if(cnt_odd - cnt4 <= 1 && cnt2 == 0)cout << "Yes" << endl;
		else if(cnt_odd <= cnt4)cout << "Yes" << endl;
		else cout << "No" << endl;
	}
}


Measured memory_bytes: 640
import numpy as np
last=[0]*26
D=int(input())
c=list(map(int, (input().split(' '))))
s=[]
a=[0]*D
b=[0]*D
comsat=[0]*D
comlast=[0]*26
sat=0

for i in range(D):
    s.append(list(map(int, (input().split(' ')))))
    a[i]=s[i].index(max(s[i])) + 1
    last[a[i]-1] = i+1
    comsat[i] = s[i][a[i]-1] - np.dot(c, list(map(lambda x: i+1-x, last)))
for i in range(D):
    for k in range(26):
        last=comlast
        last[k] = i+1
        sat = comsat[i-1] + s[i][k] - np.dot(c, list(map(lambda x: i+1-x, last)))
        if sat > comsat[i]:
            comlast[k] = i+1
            comsat[i] = sat
            a[i] = k+1
for j in range(D):
    print(a[j])

Measured memory_bytes: 27664
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1LL << 60;

int main() {
    int a, b, c, x, y; cin >> a >> b >> c >> x >> y;
    int sum = a * x + b * y;
    chmin(sum, 2 * c * max(x, y));
    if (x > y) {
        chmin(sum, 2 * c * y + a * (x - y));
    } else {
        chmin(sum, 2 * c * x + b * (y - x));
    }
    cout << sum << endl;
    return 0;
}

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

int d[200][200],s[200],n,m,r,M=INT_MAX;
int main() {
	scanf("%d%d%d",&n,&m,&r);
	memset(d,0x3f,sizeof(d));
	rep(i,r)scanf("%d",&s[i]),s[i]--;sort(s,s+r);
	rep(i,m){
		int a,b,c;scanf("%d%d%d",&a,&b,&c);d[--a][--b]=d[b][a]=c;
	}
	rep(k,n)rep(i,n)rep(j,n)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
	do{
		int c=0;
		rep(i,r-1)c+=d[s[i]][s[i+1]];
		M=min(M,c);
	}while(next_permutation(s,s+r));
	printf("%d",M);
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N = 105;

int n, m;
char a[N][N];
int cache[N][N][2];

int dp(int i, int j, int cur)
{
	if(i > n)
		return 1e9;
	if(j > m)
		return 1e9;
	if(i == n && j == m)
	{
		int cost = 0;
		if(!cur && a[i][j] == '#')
			cost++;
		return cost;
	}
	int &ans = cache[i][j][cur];
	if(ans != -1)
		return ans;
	ans = 0;
	if(a[i][j] == '#')
	{
		int x = min(!cur + dp(i, j + 1, 1), !cur + dp(i + 1, j, 1));
		int y = min(!cur + dp(i, j + 1, 0), !cur + dp(i + 1, j, 0));
		ans = min(x, y);
	}
	else
		ans = min(cur + dp(i, j + 1, 0), cur + dp(i + 1, j, 0));
	return ans;
}

int32_t main()
{
	IOS;
	memset(cache, -1, sizeof(cache));
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
			cin >> a[i][j];
	int ans = dp(1, 1, 0);
	cout << ans;
	return 0;
}

Measured memory_bytes: 512
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <queue>
#define MOD (1000000007l)
#define rep(i, n) for(long i = 0; i < n; i++)

using namespace std;

// A: true
// BC: false
vector<vector<bool>> foo;

long calc(vector<bool> arg) {
  long size = arg.size();
  long count = 0;
  long result = 0;
  rep (i, size) {
    if (arg[i]) {
      result += size - 1 - i - count;
      count++;
    }
  }
  return result;
}

int main(void) {
  cout.precision(12);

  string str;
  cin >> str;

  long i = 0;
  vector<bool> current;
  while (true) {
    if (i == str.size()) break;
    if (str[i] == 'A') {
      current.push_back(true);
      i++;
    } else if (i < str.size() - 1 and str[i] == 'B' and str[i+1] == 'C') {
      current.push_back(false);
      i++;
      i++;
    } else {
      foo.push_back(current);
      current = vector<bool>();
      i++;
    }
  }
  foo.push_back(current);

/*
  for (auto it: foo) {
    for (auto it2: it) cout << it2 << " ";
    cout << " : ";
    cout << calc(it);
    cout << endl;
  }
*/

  long result = 0;
  for (auto it: foo) result += calc(it);

  cout << result << endl;

  return 0;
}


Measured memory_bytes: 16656
#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){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}

int main(){
  ll a,b;cin >> a >> b;
  cout << lcm(a,b) << endl;
  return 0;
}

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

int main(){
  int n;
  cin >> n;
  n%2==0 ? cout << n/2 : cout << n/2+1;
}

Measured memory_bytes: 256
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
 
 
int main(){
    int n;
    cin >> n;
    int a[n+1];
    for(int i = 0;i < n;i++){
        cin >> a[i];
    }
    a[n] = 0;
    int sum = 0;
    int ans;
    bool start = true;
    for(int i = 0;i < n+1;i++){
        if(start){
            sum += abs(a[i]);
            start = false;
        }
        else{
            sum += abs(a[i]-a[i-1]);
        }
    }
    
    int x,y;
    for(int i = 0;i < n;i++){
        if(i == 0){
            x = abs(a[0]) + abs(a[1]-a[0]);
            y = abs(a[1]);
        }
        else{
            x = abs(a[i]-a[i-1]) + abs(a[i+1]-a[i]);
            y = abs(a[i+1]-a[i-1]);
        }
        cout << sum-x+y << endl;
    }
    
    
    return 0;
}

Measured memory_bytes: 1664
#include<bits/stdc++.h>
using namespace std;
int i,j,n,m,c,d,x;
string s,a,b,y;
int main()
{
  for(cin>>n>>s,i=1;i<n;i++){
    a=s.substr(0,i);
    b=s.substr(i,n-i);
    for(j=0,c=0;j<i;j++){
      y=a.substr(0,j);
      if(count(y.begin(),y.end(),a[j])!=0)continue;
      if(count(b.begin(),b.end(),a[j])!=0)c++;
    }
    m=max(m,c);
  }
  cout<<m;
}

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

template <class T, class U>
inline bool chmax(T& t, const U& f) { return t < f ? t = f, true : false; }

const int d[] = {1, -1, -1, 1};

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

    int N; cin >> N;
    map<pair<int, int>, int> fox;
    rep(i, N) {
        int x, y, w; cin >> x >> y >> w;
        fox[make_pair(x, y)] = w;
    }
    int maxi = 0;
    for (const auto& e : fox) {
        pair<int, int> t; int x1, y1, w;
        tie(t, w) = e; tie(x1, y1) = t;
        rep(k, 4) {
            w = e.second;
            int x2 = x1 + d[k], y2 = y1 + d[(k + 1) % 4];
            t = make_pair(x2, y2);
            if (fox.count(t)) w += fox[t];
            t = make_pair(x1, y2);
            if (fox.count(t)) w += fox[t];
            t = make_pair(x2, y1);
            if (fox.count(t)) w += fox[t];
            chmax(maxi, w);
        }
    }
    cout << maxi << " / 1" << endl;
}

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

int dp[1001][1001];
int main() {
	int q; scanf("%d", &q);
	rep(i, q) {
		string x, y; cin >> x >> y;
		rep(i, x.size())rep(j, y.size()) {
			if (x[i] == y[j])dp[i + 1][j + 1] = dp[i][j] + 1;
			else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
		}
		printf("%d\n", dp[x.size()][y.size()]);
	}
}

Measured memory_bytes: 7220
#include <cstdio>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>

#ifdef _DEBUG
#define DMP(x) cerr << #x << ": " << x << "\n"
#else
#define DMP(x) ((void)0)
#endif

using namespace std;
using lint = long long;

#pragma GCC optimize ("-O3", "unroll-loops")
const int nmax = 204800, block = 8192, unrl = 8;
int a[nmax], b[nmax];

int main() {

	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) scanf("%d", &a[i]);
	for (int i = 0; i < N; i++) scanf("%d", &b[i]);

	int ans = 0;
	for (int i = 0; i + unrl <= N; i += unrl) {
		for (int j = 0; j + block <= N; j += block) {
			for (int k = j; k < block + j; k++) {

				ans ^= (a[i + 0] + b[k]) ^ (a[i + 1] + b[k]);
				ans ^= (a[i + 2] + b[k]) ^ (a[i + 3] + b[k]);
				ans ^= (a[i + 4] + b[k]) ^ (a[i + 5] + b[k]);
				ans ^= (a[i + 6] + b[k]) ^ (a[i + 7] + b[k]);

			}
		}
	}

	for (int i = 0; i < N / unrl * unrl; i++) {
		for (int j = N / block * block; j < N; j++) {
			ans ^= a[i] + b[j];
		}
	}	
	for (int i = N / unrl * unrl; i < N; i++) {
		for (int j = 0; j < N; j++) {
			ans ^= a[i] + b[j];
		}
	}

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

	return 0;
}


Measured memory_bytes: 1792
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007;

template<class _T> static void getint(_T& a) {
	const char* fmt = " %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld";
	}

	if (scanf(fmt, &a) < 0) {
		printf("g1int Error\n");
	}
}
template<class _T> static void getint(_T& a, _T& b) {
	const char* fmt = " %d %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld %lld";
	}

	if (scanf(fmt, &a, &b) < 0) {
		printf("g2int Error\n");
	}
}
template<class _T> static void getint(_T& a, _T& b, _T& c) {
	const char* fmt = " %d %d %d";
	if (sizeof(_T) == sizeof(long long)) {
		fmt = " %lld %lld %lld";
	}

	if (scanf(fmt, &a, &b, &c) < 0) {
		printf("g3int Error\n");
	}
}

static void ABC153E();

#if 1
int main()
{
	ABC153E();
	fflush(stdout);
	return 0;
}
#endif

static void ABC153E()
{
	int H, N;
	getint(H, N);

	std::vector<std::pair<int, int>> magics(N);
	for (int i = 0; i < N; i++) {
		getint(magics[i].first, magics[i].second);
	}

	std::vector<std::vector<int>> dp(H + 1, std::vector<int>(N + 1, (int)MOD_NUM));
	std::fill(dp[0].begin(), dp[0].end(), 0);
	for (int h = 1; h <= H; h++) {
		for (int m = 1; m <= N; m++) {
			if ((magics[m - 1].first >= h) && (magics[m - 1].second < dp[h][m])) {
				dp[h][m] = magics[m - 1].second;
			}
			if (h >= magics[m - 1].first) {
				dp[h][m] = std::min(dp[h - magics[m - 1].first][m] + magics[m - 1].second, dp[h][m]);
			}
			dp[h][m] = std::min(dp[h][m], dp[h][m - 1]);
		}
	}
	printf("%d\n", dp[H][N]);
}


Measured memory_bytes: 39680
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); i++)
#define REP(i, d, n) for(int i=(d); i<(n); i++)
typedef long long ll;
const int mod = 1e9 + 7;
int main() {

    int n;
    cin >> n;
    vector<ll> v(n);
    rep(i, n) cin >> v[i];
    
    sort(v.begin(), v.end());

    ll ans = 0;
    ll cnt = 1;
    ll tmp = v[0];
    REP(i, 1, n){
        if(v[i] == tmp){
            cnt++;
        }
        else{
            if(tmp > cnt){
                ans += cnt;
            }
            else if(tmp < cnt){
                ans += cnt - tmp;
            }
            cnt = 1;
        }
        tmp = v[i];
    }
    
    if(tmp > cnt){
        ans += cnt;
    }
    else if(tmp < cnt){
        ans += cnt - tmp;
    }      

    cout << ans << endl;

    return 0;
}

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

int main()
{
  int n;

  while (scanf("%d", &n), n){
    int cnt = 0;
    int num[4];
    int L, S;
    if (n % 1111 == 0){
      printf("NA\n");
      continue;
    }
    while (n != 6174){
      cnt++;
      for (int i = 0; i < 4; i++){
        num[i] = n % 10;
        n /= 10;
      }
      sort(num, num + 4);
      L = num[3] * 1000 + num[2] * 100 + num[1] * 10 + num[0];
      S = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
      n = L - S;
    }
    printf("%d\n", cnt);
  }

  return (0);
}

Measured memory_bytes: 1208
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
#include <bitset>
#include <numeric>
#include <math.h>
#include <cmath>

using namespace std;

long gcd(long a, long b) {
    
    if(b){
        return gcd(b, a % b);
    }else{
        return a;
    }

}
 

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

    int N;
    cin >> N;
    vector<long> A(N);
    for(int i = 0; i < N; ++i) cin >> A[i];

    long gcd_A = A[0];

    for(int i = 1; i < N; ++i){
        gcd_A = gcd(gcd_A, A[i]);
    }

    cout << gcd_A << endl;

    return 0;
}


Measured memory_bytes: 1024
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#define MAXN 20
#define ll long long
using namespace std;

int G[MAXN][MAXN];
int vis[MAXN];
int dis[MAXN];
int cnt[MAXN];
int x[MAXN];
int y[MAXN];

int main(){
	int a,b;
	scanf("%d%d",&a,&b);
	printf("%d",a * b - (a + b)  + 1);
	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
#define mem(a) memset(a,0,sizeof(a))
#define pd(a) printf("%.10f\n",a)
#define pb(a) push_back(a)
#define in(a) insert(a)
#define pi M_PI
#define R cin>>
#define F first
#define S second
#define C class
#define ll long long
#define ln cout<<'\n'
template<C T>void pr(T a){cout<<a;ln;}
template<C T,C T2>void pr(T a,T2 b){cout<<a<<' '<<b;ln;}
template<C T,C T2,C T3>void pr(T a,T2 b,T3 c){cout<<a<<' '<<b<<' '<<c;ln;}
template<C T>void PR(T a,int n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
bool check(int n,int m,int x,int y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1000000007,MAXL=1LL<<60,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

void Main() {
  string r;
  cin >> r;
  int n=r.size();
  ll ans=0;
  rep(t,(1<<(n-1))) {
    string s="";
    rep(i,r.size()) {
      s+=r[i];
      if(t&(1<<i)) s+='+';
    }
    s+='+';
    ll sum=0,d=0;
    for(int i=0; i<s.size(); i++) {
      if(isdigit(s[i])) {
        d*=10;
        d+=s[i]-'0';
      } else {
        sum+=d;
        d=0;
      }
    }
    ans+=sum;
  }
  pr(ans);
}

int main() {
  ios::sync_with_stdio(0);cin.tie(0);
  Main();return 0;
}


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

int main(){
  int a, N;
  cin >> N;
  int au[N+1] = {0}, ad[N+1] = {0};
  rep(i, N){
    cin >> a;
    au[i+1] = au[i] + a;
  }
  rep(i, N){
    cin >> a;
    ad[i+1] = ad[i] + a;
  }
  int ans = 0;
  for(int i = 1; i <= N; i++){
    ans = max(ans, au[i] + ad[N] - ad[i-1]);
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long pw(long long x, long long sum) {
	long long res = 1, power = sum, val = x;
	while (power) {
		if (power & 1) {
			res *= val;
			res %= mod;
		}
		val *= val;
		val %= mod;
		power >>= 1ll;
	}
	return res;
}
long long f[200100];
long long cal(int n , int m)
{
    long long a = f[n+m];
    long long b = (f[n] * f[m]) % mod;
    return (a*pw(b , mod-2))%mod;
}

int main()
{
    f[0] = 1;
    for(int i = 1 ; i<=200000 ; ++i)
    {
        f[i] = f[i-1]*i;
        f[i] %= mod;
    }
    int n,m,a,b;
    cin>>n>>m>>a>>b;
    long long ans = cal(n-1,m-1);
    for(int i = 0 ; i<b ; ++i)
    {
        int x = n - a - 1;
        int y = i;
        long long f = cal(x,y);
        x = a-1;
        y = m - i - 1;
        f = (f*cal(x,y))%mod;
        ans -= f;
        if(ans < 0) ans+=mod;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 1792
package main

import "fmt"

func main() {
	var N int
	fmt.Scanf("%d", &N)
	D := make([][]int, N)
	for i := 0; i < N; i++ {
		D[i] = make([]int, 3)
		fmt.Scanf("%d %d %d", &D[i][0], &D[i][1], &D[i][2])
	}
	H := make([][]int, N)
	for i := 0; i < N; i++ {
		H[i] = make([]int, 3)
	}
	for j := 0; j < 3; j++ {
		H[0][j] = D[0][j]
	}
	for i := 1; i < N; i++ {
		for j := 0; j < 3; j++ {
			H[i][j%3] = H[i-1][(j+1)%3] + D[i][j%3]
			chmax(&H[i][j%3], H[i-1][(j+2)%3]+D[i][j%3])
		}
	}
	max := 0
	for j := 0; j < 3; j++ {
		if H[N-1][j] > max {
			max = H[N-1][j]
		}
	}
	fmt.Println(max)
}

func abs(v int) int {
	if v < 0 {
		return -v
	}
	return v
}

func chmin(v1 *int, v2 int) {
	if *v1 > v2 {
		*v1 = v2
	}
}

func chmax(v1 *int, v2 int) {
	if *v1 < v2 {
		*v1 = v2
	}
}


Measured memory_bytes: 12100
#include <bits/stdc++.h>
using namespace std;
int N, A, B, C, D;
char S[200010];

bool canGo(int A, int B) {
	for (int i = A+1; i <= B; i++) {
		if (i == B) {
			S[i] = '#';
			return true;
		}
		if (i != N - 1) {
			if (S[i] == '#' && S[i + 1] == '#') return false;
		}
	}
	return false;
}
bool canSwitch() {
	for (int i = B - 1; i <= D-1; i++) {
		if (i < N - 2) {
			if (S[i] == '.' && S[i + 1] == '.' && S[i + 2] == '.') {

				return true;
			}
		}
	}
	return false;
}

int main() {

	scanf("%d %d %d %d %d", &N, &A, &B, &C, &D);
	A--;
	B--;
	C--;
	D--;
	scanf(" %s", S);
	if (C < D) {
		if (canGo(B, D) && canGo(A,C)) {
			puts("Yes");
		}
		else {
			puts("No");
		}
	}
	else if(canSwitch()){
		if (canGo(A, C) && canGo(B, D)) {
			puts("Yes");
		}
		else {
			puts("No");
		}
	}
	else {
		puts("No");
	}
}

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

int n, a[105];
long long ans, cur;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
 	
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	ans = 1e18;
	for (int i = -100; i <= 100; i++) {
		cur = 0;
		for (int j = 0; j < n; j++) {
			cur += (i - a[j]) * (i - a[j]);
		}
		ans = min(ans, cur);
	}
	cout << ans << '\n';

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
#include <deque>
#include <complex>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORE(i,a,b) for(int i=(a);i<=(b);i++)
#define REP(i,a) FOR(i,0,a)
const int INF=1<<30;
using namespace std;
typedef pair<int,int> pr;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int sy,sx;
int W,H;
bool maps[9][9]={};
int shared[9][9]={};
bool bridged[9][9][2]={};
int frees=0;
bool able(int y,int x)
{
	return (!maps[y][x])&&shared[y][x]<2;
}
void final(int y,int x,bool ok[9][9]){
	ok[y][x]=true;
	REP(i,2) if(bridged[y][x][i]&&(!ok[y+dy[i]][x+dx[i]])) final(y+dy[i],x+dx[i],ok);
	if(bridged[y-1][x][0]&&(!ok[y-1][x])) final(y-1,x,ok);
	if(bridged[y][x-1][0]&&(!ok[y][x-1])) final(y,x-1,ok);
	return;
}
bool check(){
	bool ok[9][9]={};
	int ans=0;
	REP(i,H+2)
		REP(j,W+2)
			if(!maps[i][j]){
				final(i,j,ok);
				goto ends;
			}
	ends:;
	REP(i,H+2)
		REP(j,W+2)
			ans+=ok[i][j];
	return ans==frees;
}
bool search(int y,int x){
	if(y>H)
	return check();
	
	if(!able(y,x))
		return search(y+x/W,x%W+1);
	bool ans=false;
	int cans=able(y+1,x)+able(y,x+1);
	if(cans+shared[y][x]<2) return false;
	if(shared[y][x]==0){
		shared[y][x+1]++;
		shared[y+1][x]++;
		REP(i,2) bridged[y][x][i]=true;
		ans=search(y+x/W,x%W+1);
		REP(i,2) bridged[y][x][i]=false;
		shared[y][x+1]--;
		shared[y+1][x]--;
		return ans;
	}
	if(shared[y][x]==1){
		REP(i,2){
			if(able(y+dy[i],x+dx[i])){
				shared[y+dy[i]][x+dx[i]]++;
				bridged[y][x][i]=true;
				ans|=search(y+x/W,x%W+1);
				bridged[y][x][i]=true;
				shared[y+dy[i]][x+dx[i]]--;
			}
		}
		return ans;
		
	}
	
}
int main() {
	
	// your code goes here
	while(cin >> W >> H && W){
	frees=0;
	REP(i,H+2)
		REP(j,W+2){
			maps[i][j]=1;
			shared[i][j]=0;
			REP(k,2) bridged[i][j][k]=0;
		}
	FORE(i,1,H)
		FORE(j,1,W){
			cin >> maps[i][j];
			if(!maps[i][j]) frees++;
		}
		
	if(frees&&search(1,1))
	cout <<"Yes" << endl;
	else 
	cout <<"No" << endl;
	label:;
	}
	return 0;
}

Measured memory_bytes: 1160
#![allow(non_snake_case, unused_imports)]

// Input
macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); let mut next = || { iter.next().unwrap() }; input_inner!{next, $($r)*} }; ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect() }; input_inner!{next, $($r)*} }; }
macro_rules! input_inner { ($next:expr) => {}; ($next:expr, ) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => { let mut $var = read_value!($next, $t); input_inner!{$next $($r)*} }; }
macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ( $(read_value!($next, $t)),* ) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, [ $t:tt ]) => { { let len = read_value!($next, usize); (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() } }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, bytes) => { read_value!($next, String).into_bytes() }; ($next:expr, usize1) => { read_value!($next, usize) - 1 }; ($next:expr, $t:ty) => { $next().parse::<$t>().expect("Parse error") }; }

// Module
use std::cmp::{min,max};
use std::collections::{HashSet,HashMap,BTreeMap,VecDeque};

// Functions

// Main
fn main(){
    input!{
        S: chars,
    }
    let mut ans:i32 = 1 << 30;
    for i in 0..S.len()-2{
        let x = (S[i] as i32 - 48)* 100 + (S[i+1] as i32 - 48)* 10 + (S[i+2] as i32 -48);
        if (753-x).abs() < (753-ans).abs(){
            ans = x;
        }
    }
    println!("{}", (753-ans).abs());
}

Measured memory_bytes: 4352
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0519

"""
import sys
from sys import stdin
from collections import deque
input = stdin.readline


def bfs(s):
    global Colors
    global Output
    global In_degrees
    global Is_unique

    q = deque()
    q.append(s)
    Colors[s] = GRAY

    while q:
        u = q.popleft()
        Output.append(u)

        pending = []
        for e in Out_edges[u]:
            In_degrees[e] -= 1
            if In_degrees[e] == 0 and Colors[e] == WHITE:
                Colors[e] = GRAY
                pending.append(e)
        if len(pending) > 1:
            Is_unique = False
        for e in pending:
            q.append(e)


def topologial_sort(n):
    global Colors
    global In_degrees
    global Is_unique

    for u in range(1, n+1):
        if In_degrees[u] == 0 and Colors[u] == WHITE:
            if In_degrees.count(0) > 1:
                Is_unique = False
            bfs(u)


BLACK, GRAY, WHITE = 0, 1, 2
Colors = []
Output = []
In_degrees = []
Out_edges = []
Is_unique = True

def main(args):
    global Out_edges
    global Colors
    global In_degrees
    global Is_unique

    n = int(input())            #  ???????????°
    m = int(input())            #  ???????????±?????°
    Out_edges = [[] for _ in range(n+1)]
    In_degrees = [0] * (n + 1)
    In_degrees[0] = -1
    Colors = [WHITE] * (n + 1)

    scores = [[int(x) for x in input().split()] for _ in range(m)]

    for w, l in scores:
        In_degrees[l] += 1
        Out_edges[w].append(l)

    topologial_sort(n)

    for d in Output:
        print(d)
    if Is_unique:
        print(0)
    else:
        print(1)



if __name__ == '__main__':
    main(sys.argv[1:])
    

Measured memory_bytes: 27784
N = int(input())
p = list(map(int, input().split()))
p_t = [i for i in range(1, N+1)]
count = 0
for i in range(N):
    if p[i] != p_t[i]:
        count += 1

if count == 2 or count == 0:
    print('YES')
else:
    print('NO')

Measured memory_bytes: 3060
def powmod(b,r,mod)
  a = 1
  while r > 0
    if r%2 == 1
      a = a * b % mod
    end
    b = b * b % mod
    r /= 2
  end
  a
end

def matmul(r, z, mod)
  return [0,0,0] if z < 0
  return [1,0,1] if z == 0
  n = 1
  abc = [0,0,0]
  mul = [
    [r, 0, 1],
    [r, r, 0],
    [0, 0, 1],
  ]
  sat = [
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1],
  ]
  while n <= z do
    if (n & z) > 0
      sat = (0...3).map{ |i|
        si = sat[i]
        (0...3).map{ |j|
          (0...3).reduce(0){ |a,k|
            (a + si[k] * mul[k][j] % mod) % mod
          }
        }
      }
    end
    n *= 2
    mul = (0...3).map{ |i|
      mi = mul[i]
      (0...3).map{ |j|
        (0...3).reduce(0){ |a,k|
          (a + mi[k] * mul[k][j] % mod) % mod
        }
      }
    }
  end
  (0...3).map{ |i| 
    si = sat[i]
    v = (0...3).reduce(0){ |a,j| (a + si[j] * [1,0,1][j] % mod) % mod }
    (abc[i] + v) % mod
  }
end

L,A,B,M = gets.split.map &:to_i

H = (0..18).map{ |k|
  [[(10**k - A - 1)/B, -1].max, L-1].min
}
da = 0
p (1..18).reduce(0){ |a,k|
  z = H[k] - H[k-1] - 1
  next a if z < 0
  x = (A + H[k]*B)
  y = B
  d = (k+1..18).reduce(0){ |a,l| a + l*(H[l] - H[l-1]) }
  k10 = powmod(10, k, M)
  d10 = powmod(10, d, M)
  xm,ym,_ = matmul(k10, z, M)
  v = (x*xm - y*ym) % M * d10 % M

  (a + v) % M
}


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

using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef long double ld;
typedef vector<ld> vd;
typedef bool bl;
typedef vector<bl> vb;
typedef unordered_map<ll,unordered_map<ll,ll>> graph;

const ll e5 = 1 << 20;
const ll mod = 1000000007;
const ll e3 = 1 << 13;
const ll INF = 1ll << 60;

ll n;
string s[e5];
ll ans;
ll cnt[256];

int main(){
  cin >> n;
  for(ll i = 0;i < n;i++) cin >> s[i];
  for(ll i = 0;i < n;i++){
  cnt[s[i][0]]++;
  }
  vl c = vl({'M', 'A', 'R', 'C', 'H'});
  for(ll i = 0;i < 5;i++){
    for(ll j = i+1;j < 5;j++){
      for(ll k = j+1;k < 5;k++){
        ans += cnt[c[i]]*cnt[c[j]]*cnt[c[k]];
      }
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 13568
n, a, b, c, d = map(int, input().split())
s = input()

if '##' in s[a:c-1]:
  print('No')
  exit()
if '##' in s[b:d-1]:
  print('No')
  exit()
if c > d:
  if '...' not in s[b-2:d+1]:
    print('No')
    exit()

print('Yes')


Measured memory_bytes: 3572
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll MOD=(ll)1e9+7;
const ll MOD2=(ll)998244353;
const ll INF=(ll)1e18+10;
const double PI = 2.0*acos(0);

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 (a>b) { a=b; return 1; } return 0; }
int dy[]={-1,1,0,0};
int dx[]={0,0,-1,1};

ll N,K;
int A[200010];
int pre[200010];
int vis[200010];


int backfrom,backto;

void dfs(int n){
    if(pre[A[n]]>=0){
        backfrom=n;
        backto=A[n];
        return;
    }
    vis[A[n]]=vis[n]+1;
    pre[A[n]]=n;
    dfs(A[n]);
}

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

    memset(pre,-1, sizeof(pre));
    for (int i = 0; i < N; ++i) {
        cin>>A[i]; --A[i];
    }
    vis[0]=0;
    dfs(0);

    if(vis[backfrom]>=K){
        for (int i = 0; i < N; ++i) {
            if(vis[i]==K){
                cout<<i+1<<endl;
                return;
            }
        }
    }

    K-=vis[backto]-1;
    ll tmp=K%(vis[backfrom]-vis[backto]+1);
    if(tmp==0){
        cout<<backfrom+1<<endl;
        return;
    }

    tmp=vis[backto]-1+tmp;

    for (int i = 0; i < N; ++i) {
        if(vis[i]==tmp){
            cout<<i+1<<endl;
            return;
        }
    }

}

//#define MULTI
int main() {
#ifdef MULTI
    int N; cin>>N;
    for (int i = 0; i < N; ++i) {
        solve(i+1);
    }
#else
    solve(0);
#endif
    return 0;
}


Measured memory_bytes: 5984
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n;
    cin >> n;
    ll h[n];
    rep(i,n) cin >> h[i];
    ll dp[n+2];

    rep(i,n+2)
    {
        dp[i] = longinf;
    }
    dp[0] = 0;
    rep(i,n)
    {
        dp[i+1] = min(dp[i+1], dp[i]+abs(h[i]-h[i+1]));
        dp[i+2] = min(dp[i+2], dp[i]+abs(h[i]-h[i+2]));
    }
    cout << dp[n-1] << endl;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 1e9
#define All(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;


int main() {
  string s;
  cin>>s;
  sort(All(s));
  for(int i=0;i<s.size()-1;i++){
    if(s[i]==s[i+1]){
      cout<<"no\n";
      return 0;
    }
  }
  cout<<"yes\n";

  return 0;
}


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

const int MaxN=2e6+5;
const int Mod=1e9+7;

int X1,Y1,X2,Y2,X3,Y3,X4,Y4,X5,Y5,X6,Y6,Ans;
int Fac[MaxN],Inv[MaxN];

int C(int n,int m){
	return 1llu*Fac[n]*Inv[m]%Mod*Inv[n-m]%Mod;
}

int F(int x,int y){
	return C(x+y,y);
}

int Calc1(int x,int y){
	return ((F(x-X1+1,y-Y1+1)+F(x-X2,y-Y2))%Mod-(F(x-X1+1,y-Y2)+F(x-X2,y-Y1+1))%Mod+Mod)%Mod;
}

int Calc2(int x,int y){
	return ((F(X6-x+1,Y6-y+1)+F(X5-x,Y5-y))%Mod-(F(X6-x+1,Y5-y)+F(X5-x,Y6-y+1))%Mod+Mod)%Mod;
}

int main(){
	int i;
	scanf("%d%d%d%d%d%d",&X1,&X2,&X3,&X4,&X5,&X6);
	scanf("%d%d%d%d%d%d",&Y1,&Y2,&Y3,&Y4,&Y5,&Y6);
	Fac[0]=Inv[0]=Fac[1]=Inv[1]=1;
	for(i=2;i<=X6+Y6;i++){
		Fac[i]=1llu*Fac[i-1]*i%Mod;
		Inv[i]=1llu*(Mod-Mod/i)*Inv[Mod%i]%Mod;
	}
	for(i=2;i<=X6+Y6;i++)
		Inv[i]=1llu*Inv[i-1]*Inv[i]%Mod;
	for(i=X3;i<=X4;i++){
		Ans=(Ans-1llu*Calc1(i,Y3-1)*Calc2(i,Y3)%Mod*(i+Y3)%Mod+Mod)%Mod;
		Ans=(Ans+1llu*Calc1(i,Y4)*Calc2(i,Y4+1)%Mod*(i+Y4+1))%Mod;
	}
	for(i=Y3;i<=Y4;i++){
		Ans=(Ans-1llu*Calc1(X3-1,i)*Calc2(X3,i)%Mod*(X3+i)%Mod+Mod)%Mod;
		Ans=(Ans+1llu*Calc1(X4,i)*Calc2(X4+1,i)%Mod*(X4+i+1))%Mod;
	}
	printf("%d\n",Ans);
	return 0;
}

Measured memory_bytes: 15872
#include <iostream>

int main(){
   int N, K;
   std::cin >> N >> K;
   int x[N];
   for(int i=0; i<N; ++i)
      std::cin >> x[i];

   int ans = 0;
   for(int i=0; i<N; ++i){
      ans += std::min(abs(x[i]-K),x[i])*2;
   }
   std::cout << ans << std::endl;
   return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
template<typename T> inline void chkmin(T &a, const T &b) { a = a < b ? a : b; }
template<typename T> inline void chkmax(T &a, const T &b) { a = a > b ? a : b; }

const int MAXN = 105;
int f[MAXN][MAXN][MAXN], row[MAXN][MAXN], col[MAXN][MAXN], n, m, ex, ey, ans;
char mp[MAXN][MAXN];

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%s", mp[i] + 1);
		for (int j = 1; j <= m; j++) {
			row[i][j] = row[i][j - 1] + (mp[i][j] == 'o');
			col[j][i] = col[j][i - 1] + (mp[i][j] == 'o');
			if (mp[i][j] == 'E') ex = i, ey = j;
		}
	}
	for (int i = ex; i > 0; i--)
	for (int j = ex; j <= n; j++)
	for (int k = ey; k > 0; k--)
	for (int l = ey; l <= m; l++) {
		int cur = f[j][k][l];
		chkmax(ans, cur);
		if (i > 1 && i - 1 > j - ex)
			chkmax(f[j][k][l], cur + row[i - 1][min(l, m - ey + k)] - row[i - 1][max(k - 1, l - ey)]);
		if (j < n && ex - i < n - j)
			chkmax(f[j + 1][k][l], cur + row[j + 1][min(l, m - ey + k)] - row[j + 1][max(k - 1, l - ey)]);
		if (k > 1 && k - 1 > l - ey)
			chkmax(f[j][k - 1][l], cur + col[k - 1][min(j, n - ex + i)] - col[k - 1][max(i - 1, j - ex)]);
		if (l < m && ey - k < m - l)
			chkmax(f[j][k][l + 1], cur + col[l + 1][min(j, n - ex + i)] - col[l + 1][max(i - 1, j - ex)]);
	}
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 4608