akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"
using namespace std;
#define INF 1e9
#define rep(i, n) for(int i = 0; i < n; i++)
#define ALL(x) (x).begin(), (x).end()  //昇順
#define RALL(x) (x).rbegin(), (x).rend() // 降順
const long long mod = 1e9+7;
typedef priority_queue<int> PQ;
typedef vector<long long> VL;
typedef vector<int> VI;  // VI a(n);
typedef vector<string> VS;  
typedef vector<char> VC; 
typedef vector<VS> VSS;
typedef vector<VC> VCC;    
typedef vector<VI> VII;   // VII a(n,vector<int>(m)) n * m 
typedef vector<VL> VLL;
typedef pair<int, int> PII;
typedef map<int, int> MP;   //MP a;
typedef vector<pair<int, int> > PS;
typedef long long ll;  //ll とdoubleは違う
template <class T>  // chmax(max, a);
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> //chmin(min,a)
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
template<typename T>   // operator << (cout,a);
ostream& operator << (ostream& os, vector<T>& v) {
    os << "{";
    rep(i, (int)v.size()) { os << v[i] << (i < v.size() - 1 ? ", " : ""); }
    os << "}";
    return os;
}
template<typename T>
bool all_same(vector<T> v1,vector<T> v2){
    sort(ALL(v1));
    sort(ALL(v2),greater<T>());
    return v1 == v2;
}
 
// g++ -std=c++11 prac.cpp
ll Max=-1e9,Min=1e9+1;
 
int main(){
    ll b,x,c,y=0,n,m,l,r,w,sum=0,sum2=0,ans=0;
    string s,t;
    bool f=true;
    cin >> n >> s;
    map<int,int> a;

    for(int i=0;i<=999;i++){
        t = to_string(i);
        if(t.size()<3){
            while(t.size()!=3){
                t = "0"+t;
            }
        }
        int k=0;
        for(int j=0; j<n;j++){
            if(s[j]==t[k]){
                sum++;
                k++;
            }
            if(k==3){
                break;
            }
        }
        if(sum >= 3 && k == 3){
            ans += 1;
        }
        sum = 0;
        k=0;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)

template<typename T>
void remove(vector<T>& vector, unsigned int index){
vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int n, m;

int main(void){
	while(cin >> n >> m, n||m){
		int cost[120][120];
		int timm[120][120];
		fill_n(*cost, 120*120, 10000000);
		fill_n(*timm, 120*120, 10000000);
		for(int i = 0; i < n; i++){
			int a, b,c,t;
			cin >> a >> b >> c >> t;
			cost[a][b] = c;
			cost[b][a] = c;
			timm[a][b] = t;
			timm[b][a] = t;
		}
		for(int k=1;k<m+1;++k)for(int i=1;i<m+1;++i)for(int j=1;j<m+1;++j){
			cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
			timm[i][j] = min(timm[i][j], timm[i][k] + timm[k][j]);
		}
		int k;
		cin >> k;
		rep(i, k){
			int a, b, c;
			cin >> a >> b >> c;
			if(!c)
				cout << cost[a][b] << endl;
			else
				cout << timm[a][b] << endl;
		}
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

Measured memory_bytes: 1276
n, m = [int(i) for i in input().split()]
ABs = sorted([[int(i) for i in input().split()] for _ in range(n)])
ans, num = 0, 0
for a, b in ABs:
    ans += a * min(b, m - num)
    num += min(b, m - num)
    if num == m:
        break
print(ans)

Measured memory_bytes: 61784
#include <iostream>

using namespace std;
int main() {
    int n, p[5000], cur[5000], ans = 99999999;

    cin >> n;
    for (int i = 0; i < n; i++) cin >> p[i];

    for (int i = 0; i <= p[0]; i++) {
        int count = 0;
        for (int j = 0; j < n; j++) cur[j] = p[j];

        cur[0] -= i;
        count += i;
        for (int j = 0; j < n - 1; j++) {
            count += 2 * cur[j];
            cur[j + 1] = max(cur[j + 1] - cur[j], 0);
            cur[j] = 0;
        }
        count += cur[n - 1];
        ans = min(ans, count);
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3096
import Control.Monad
import Control.Applicative
import Data.List
import Data.Ord



getBysplit = map read . words <$> getLine

main :: IO()
main = do
  n <- readLn :: IO Int
  lst <- getBysplit :: IO [Int]
  let (x:xs) = sortOn Data.Ord.Down lst
  if x <  sum xs
    then putStrLn "Yes"
    else putStrLn "No"


Measured memory_bytes: 508
import java.util.Scanner;

public class Main {
    static int top;
    static int north;
    static int east;
    static int south;
    static int west;
    static int bottom;
    
    public static int North(){
    	int temp = 0;
    	temp = top; top = south; south = bottom; bottom = north; north = temp;
        return top;
    }
    
    public static int East(){
    	int temp = 0;
    	temp = top; top = west; west = bottom; bottom = east; east = temp;
    	return top;
    }
    
    public static int West(){
    	int temp = 0;
    	temp = top; top = east; east = bottom; bottom = west; west = temp;
    	return top;
    }
    
    public static int South(){
    	int temp = 0;
    	temp = top; top = north; north = bottom; bottom = south; south = temp;
    	return top;
    }
    
    public static int Right(){
    	int temp = 0;
    	temp = north; north = west; west = south; south = east; east = temp;
    	return top;
    }
    
    public static int Left(){
    	int temp = 0;
    	temp = north; north = east; east = south; south = west;  west = temp;
    	return top;
    }
    
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		while(true){
			int n = in.nextInt();
			
			if(n == 0) break;
			
			top = 1;
			north = 5;
			south = 2;
			east = 3;
			west = 4;
			bottom = 6;
			int ans = top;
			
			for(int i=0; i <= n; i++){
				String c = in.nextLine();
				if(c.equals("North")){
					ans = ans + North();
				}else if(c.equals("East")){
					ans = ans + East();
				}else if(c.equals("West")){
					ans = ans + West();
				}else if(c.equals("South")){
					ans = ans + South();
				}else if(c.equals("Right")){
					ans = ans + Right();
				}else if(c.equals("Left")){
					ans = ans + Left();
				}
			}
			System.out.println(ans);
		}
	}
}

Measured memory_bytes: 36588
#include <iostream>
#include <math.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)

int main() {
    long long n, j;
    cin >> n;
    long long mul[2] = {};
    for(long long i = 1; i <= sqrt(n); i++){
        if(n % i == 0){
            j = n / i;
            if(mul[0] == 0 || abs(mul[0] - mul[1]) > abs(i - j)){
                if(i - j == 0){
                    cout << i * 2 - 2 << endl;
                    return 0;
                }
                mul[0] = i;
                mul[1] = j;
            }
        }
    }
    cout << mul[0] + mul[1] - 2 << endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1.0)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    cout << (!(a % 3) || !(b % 3) || !((a+b) % 3) ? "Possible" : "Impossible") << endl;
}

Measured memory_bytes: 256
#![allow(unused_imports)]
#![allow(non_snake_case)]
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 reading char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed parsing")
}

use std::cmp::{min};
use std::num;

fn main() {
    
    let n = read::<usize>();
    let mut cnt = 0;
    let mut tmp = 1;
    
    loop {
        if tmp < n {
            cnt += 1;
            tmp += 1;
            if tmp == 10 {
                tmp = 100;
            }
            else if tmp == 1000{
                tmp = 10000;
            }
        }
        else if tmp == n && tmp != 100000{
            cnt += 1;
            break;
        }else{
            break;
        }
    }
    
    println!("{}", cnt);
    
}

Measured memory_bytes: 4352
while str=gets
    @n,@s=str.split(nil).map(&:to_i)
    break if (@n==0)&&(@s==0)
    
    @a=[]
    @c=0
    
    def cal(i)
    if (@a.size==@n)&&(@a.inject(:+)==@s)
       @c+=1
       return
    end
    
    (i..9).each do |j|
        @a.push(j)
        cal(j+1)
        @a.pop
        end
    end
    cal(0)
    puts @c
end

Measured memory_bytes: 6100
r,g,b,n = map(int,input().split())
R = n//r
G = n//g
ans = 0
for i in range(R+1):
    for j in range(G+1):
        if (n - i*r - j* g) >= 0 and ((n - i*r - j* g)%b==0):
            ans += 1
        else:
            continue
print(ans)

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

int main(){
    ll n, d;
    cin >> n >> d;
    ll x[n][d];
    rep(i, 0, n){
        rep(j, 0, d) cin >> x[i][j];
    }
    ll ans = 0;
    rep(i, 0, n-1){
        rep(j, i+1, n){
            ll dist = 0;
            rep(k, 0, d) dist += (x[i][k] - x[j][k])*(x[i][k] - x[j][k]);
            for(ll k = 1; k * k <= dist; k++){
                if(dist == k * k) ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define show(a, n) rep(i,n) {cout<<a[i]<<' ';} cout<<endl;

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

const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 200000;

int main() {
	ll n, d, a;
	cin >> n >> d >> a;
	pll p[N];
	rep(i, n) cin >> p[i].first >> p[i].second;
	sort(p, p + n);

	priority_queue<pll, vector<pll>, greater<pll>> que;
	ll sum_n = 0;
	ll ans = 0;
	rep(i, n) {
		ll x = p[i].first;
		ll h = p[i].second;
		while(!que.empty() && que.top().first < x) {
			sum_n -= que.top().second;
			que.pop();
		}
		h -= a * sum_n;
		if(h > 0) {
			ll add_n = h / a;
			if(h % a != 0) add_n++;
			que.push(pll(x + 2 * d, add_n));
			sum_n += add_n;
			ans += add_n;
		}
	}
	cout << ans << endl;
}


Measured memory_bytes: 5492
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


N, M = map(int, input().split())
uf = UnionFind(N)

for _ in range(M):
  X, Y, Z = map(int, input().split())
  uf.union(X-1, Y-1)

print(uf.group_count())

Measured memory_bytes: 13828
# include <sstream>
# include <stack>
# include <string>
# include <vector>
# include <iomanip>
# include <iostream>
# define rep(i, n) for (int i = 0; i < (int)(n); i++)
# define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
# define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;

int main(){
  int N,W;
  int v[1005];
  int w[1005];
  int dp[1005][1005];
  cin >> N >> W;
  rep(i, N){
    cin >> v[i] >> w[i];
  }

  irep(i, N+1){
    rep(j,W+1){
      if(i == N){
        dp[i][j] = 0;
      }
      if(j == 0){
        dp[i][j] = 0;
      }
      if(j - w[i] > -1){
        if(dp[i+1][j-w[i]]+v[i] > dp[i+1][j]){
          dp[i][j] = dp[i+1][j-w[i]]+v[i];
        }else{
          dp[i][j] = dp[i+1][j];
        }
      }else{
        dp[i][j] = dp[i+1][j];
      }
    }
  }
  cout << dp[0][W] << endl;
}

Measured memory_bytes: 3552
n = int(input())
a = list(map(int, input().split(" ")))

ans = 0
den = int(1e9 + 7)
sum_a = [0 for i in range(len(a))]
sum_a[-1] = a[-1]
for i in range(len(a) - 1, 1, -1):
    sum_a[i - 1] = sum_a[i] + a[i - 1]
for i in range(len(a) - 1):
    ans = (ans + a[i] * sum_a[i + 1]) % den

print(ans)

Measured memory_bytes: 31464
#include <iostream>
#include <cstdio>

#define Inf 0x7fffffff

using namespace std;

const int Maxn = 100010;
int n , k , ans = Inf , minl = 1;
int a[Maxn];

int main() {
	
	scanf("%d%d",&n,&k);
	if(k == n) {
		puts("1");
		return 0;
	}
	k --;
	for(register int i=1; i<=n; i++) {
		scanf("%d",&a[i]);
		if(a[minl] < a[i])
			minl = i;
	}
	
	for(register int i=max(1,minl-k); i<=minl; i++) {
		register int j = min(n,i+k);
		register int tmp = i , sum = 1;
		while(tmp > 1) {
			tmp -= k;
			sum ++;
		}
		tmp = j;
		while(tmp < n) {
			tmp += k;
			sum ++;
		}
		ans = min(ans,sum);
	}
	
	printf("%d\n",ans);
	
	return 0;
	
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

cat pw(cat a, cat e, cat mod) {
	if(e <= 0) return 1;
	cat x = pw(a, e/2, mod);
	x = x * x % mod;
	if(e&1) x = x * a % mod;
	return x;
}

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int N, M;
	cin >> N >> M;
	map< pair<int, int>, int> A;
	for(int i = 0; i < N; i++) A[{i, i}] = -1;
	for(int i = 1; i < N; i++) A[{i-1, i}] = A[{i, i-1}] = -1;
	for(int i = 1; i < N-1; i++) A[{i-1, i+1}] = A[{i+1, i-1}] = -1;
	for(int i = 0; i < M; i++) {
		int x, y, z;
		cin >> x >> y >> z;
		A[{x-1, y-1}] = z;
	}
	set< pair<int, int> > S;
	ALL_THE(A, it) {
		auto jt = A.find({it->ff.ss, it->ff.ff});
		if(abs(it->ff.ss-it->ff.ff) >= 3) S.insert({min(it->ff.ff, it->ff.ss), max(it->ff.ff, it->ff.ss)});
		if(jt != end(A) && abs(it->ff.ss-it->ff.ff) >= 3 && it->ss != -1 && jt->ss != it->ss && jt->ss != -1) {
			cout << "0\n";
			return 0;
		}
	}
	cat free = (N <= 3) ? 0 : (1LL*(N-3)*(N-2)/2-S.size());
	vector<int> pd[2] = {vector<int>(N-1, 0), vector<int>(N-1, 0)};
	for(int i = 0; i < N-1; i++) {
		int x = A[{i, i+1}], y = A[{i+1, i}];
		if(x != -1 && y != -1) pd[x^y][i] = 1;
		else if(x == -1 && y == -1) pd[0][i] = pd[1][i] = 2;
		else pd[0][i] = pd[1][i] = 1;
	}
	vector<int> pdd[2] = {vector<int>(N, 0), vector<int>(N, 0)};
	pdd[0][0] = pdd[1][0] = 1;
	pdd[0][N-1] = pdd[1][N-1] = 1;
	for(int i = 1; i < N-1; i++) {
		int x = A[{i-1, i+1}], y = A[{i+1, i-1}];
		if(x != -1 && y != -1) pdd[x^y][i] = 1;
		else if(x == -1 && y == -1) pdd[0][i] = pdd[1][i] = 2;
		else pdd[0][i] = pdd[1][i] = 1;
	}	
	cat mod = 998244353;
	vector<cat> dp[2] = {vector<cat>(N, 0), vector<cat>(N, 0)};
	if(A[{0, 0}] == -1) dp[0][0] = dp[1][0] = 1;
	else dp[A[{0, 0}]][0] = 1;
	for(int i = 1; i < N; i++) for(int j = 0; j < 2; j++) for(int k = 0; k < 2; k++) {
		auto it = A.find({i, i});
		if(it != end(A) && it->ss != k && it->ss != -1) continue;
		dp[k][i] = (dp[k][i] + pd[j^k][i-1] * pdd[k][i] * dp[j][i-1]) % mod;
	}
	cat ans = (dp[0][N-1] + dp[1][N-1]) % mod;
	ans = ans * pw(2, free, mod) % mod;
	if(ans < 0) ans += mod;
	cout << ans << "\n";
	return 0;
}

// look at my code
// my code is amazing


Measured memory_bytes: 40064
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=NTL_1_C&lang=jp
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;
typedef unsigned long long  ull;


ull gcl(ull a, ull b){
	ull input[2] = {a, b};
	sort(input, input+2);

	ull gcl = 1;
	for(int i=2; i<= input[0]; i++){
		bool ok = true;
		for(int j=0; j<2; j++){
			if(input[j] % i != 0) ok = false;
		}
		if(ok == true) gcl = i;
	}
	//cout << gcl << endl;

	ull lcm = gcl;
	for(int i=0; i<2; i++){
		input[i] /= gcl;
		lcm *= input[i];
	}
	//cout << lcm << endl;
	return lcm;
}


int main(){
	int n;
	cin >>  n;
    ull a[n];
    for(int i=0; i<n; i++) cin >> a[i];
	//input??????

	sort(a,a+n);
	//min???a[0]????????£?????????
	ull g = 1;

	if(n>2){
		for(int i=0; i<n-1; i++){
			g = gcl(g,gcl(a[i],a[i+1]));
		}
	}
	else g = gcl(a[0],a[1]);
	cout << g << endl;
    return 0;
}

Measured memory_bytes: 1184
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/*
    s.find_by_order(k) ---> returns iterator to the element at index(from 0) k
    s.order_of_key(val) ---> number of elements that are strictly smaller than the given value
*/
typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long int> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long int>> vvl;
typedef pair<int, int> pii;
typedef pair<long long int, long long int> pll;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long int, long long int>> vll;
#define fast() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define test() long long int tc; cin >> tc; cin.ignore(); while(tc--) 
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define MOD 1000000007
#define SMOD 998244353
#define pq priority_queue
#define all(x) x.begin(),x.end()
#define SP(x) setprecision(x)
#define sp " "
#define inp(a) ll a;cin>>a;cin.ignore()
#define inps(a,b) ll a,b;cin>>a>>b;cin.ignore()
#define out(a) cout<<SP(20)<<a<<'\n'
#define outs(a,b) cout<<SP(20)<<a<<" "<<b<<'\n'
#define ins insert
#define er erase
#define lb lower_bound
#define ub upper_bound
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define inpa(a,n) ll a[n]; for(int i=0; i<n; i++){cin>>a[i];}
#define inpv(a,n) vl a; for(int i=0; i<n; i++){inp(x);a.pb(x);}
#define outa(a,n) for(int i=0;i<n;i++){cout<<a[i]<<" ";}cout<<'\n';
#define outv(v) for(auto i:v){cout<<i<<" ";}cout<<'\n';
set<char> vow = {'a','e','i','o','u','A','E','I','O','U'};
ll xpow(ll n, ll m){if(m==0){return 1;}else{ll x = xpow(n, m/2);if(m&1){return x*x*n;}else{return x*x;}}}
ll xmod(ll a, ll m){return (((a%m)+m)%m);}
ll xmodpow(ll n, ll m, ll y){if(m==0){return 1;}else{ll x = xmodpow(n, m/2, y);if(m&1){return xmod((xmod((xmod(x,y)*xmod(x,y)),y)*xmod(n,y)),y);}else{return xmod((xmod(x,y)*xmod(x,y)),y);}}}
 
int main() {
	fast();
 
    /*NA – JEONG – MO – SA – JI – MI – DA – CHAE – TZU*/
    
    string s, t, u;
    cin >> s >> t;
    ll a, b;
    cin >> a >> b;
    cin >> u;
    if(u==s){
        a--;
    }
    else{
        b--;
    }
    outs(a, b);
}

Measured memory_bytes: 256
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cinttypes>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<stack>


#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vpii=vector<pii>;

#define PI 3.1415926535897932384626433832795

template<typename X>
bool max_u(X&m, X v)
{
	if(m<v)
	{
		m=v;
		return true;
	}
	return false;
}


template<typename X>
bool min_u(X&m, X v)
{
	if(m>v)
	{
		m=v;
		return true;
	}
	return false;
}


struct solve
{
	solve(){}
	ll operator()() {
		return 0;
	}
};

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

	for(;;)
	{
		vi x(5+1);
		for(int i=1;i<=5;i++) cin >> x[i];
		if(cin.fail()) break;
		for(int i=1;i<=5;i++)
		{
			if(x[i]!=i)
			{
				cout << i << "\n";
				break;
			}
		}
	}
	return 0;
}


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

int main() {
	int r, c, a[100][100],r_sum[100],c_sum[100],a_sum;
	a_sum = 0;
	for (int i = 0;i < 100;i++) {
		r_sum[i] = 0;
		c_sum[i] = 0;
	}
	cin >> r >> c;

	for (int i = 0;i < r;i++) {
		for (int j = 0;j < c;j++) {
			cin >> a[i][j];
		}
	}

	for (int i = 0;i < r;i++) {
		for (int j = 0;j < c;j++) {
			r_sum[i] += a[i][j];
			a_sum += a[i][j];
		}
	}

	for (int i = 0;i < c;i++) {
		for (int j = 0;j < r;j++) {
			c_sum[i] += a[j][i];
		}
	}

	for (int i = 0;i < r;i++) {
		for (int j = 0;j < c;j++) {
			cout << a[i][j] << " ";
		}
		cout << r_sum[i] << endl;
	}

	for (int i = 0;i < c;i++)
		cout << c_sum[i]<<" ";

	cout << a_sum << endl;

	return 0;
}

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

#define ed cout << "\n";
#define speed                         \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define ll long long
#define ffor(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(i, n) ffor(i, 0, n)
#define All(obj) (obj).begin(), (obj).end()
#define INF ((ll)1e9 + 7)
#define c(a) cout << a << "\n";
using namespace std;
const int N = 100005;

int main()
{
    speed;
    cout << fixed << setprecision(6);
    /*start*/
    int n;
    cin >> n;
    string s;
    cin >> s;
    int ans = 0;
    rep(i, n - 2)
    {
        //if(s.substr(i,3)=="ABC")ans++;//true=1,false=0
        ans += s.substr(i, 3) == "ABC";
    }
    string t = s.substr(0, 3);
    // c(t);
    // int test = t == "ABC";
    // c(test);
    c(ans);
    ed;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vs = vector<string>;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e9 + 7;
const int MAX_N = 2e5, MAX_M = 1e5, MAX_K = 1e9;

//qsortでの比較用の関数 cmp
//qsort(配列名,要素数,sizeof(要素型),cmp)
int cmp(const void *p, const void *q) {
	return *(int*)p - *(int*)q;
}

int n, m;
void solve() {
	if (n == m) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
}	
int main() {
	cin >> n >> m;
	solve();
}


Measured memory_bytes: 256
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define FOR(i,a,b) for (int i=(a),for_##i##_max=(b);i<=(for_##i##_max);++i)
#define RFOR(i,a,b) for (int i=(a),rfor_##i##_min=(b);i>=(rfor_##i##_min);--i)
#define REP(i,n) for (int i=0,rep_##i##_len=(n);i<(rep_##i##_len);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)

namespace { // Basics

template<typename T>
void chmin(T &x, T y) {
  x = std::min(x, y);
}

template<typename T>
void chmax(T &x, T y) {
  x = std::max(x, y);
}

template<typename T>
__attribute__((unused))
const auto minimum = static_cast<T const &(*)(T const &, T const &)>(std::min);

template<typename T>
__attribute__((unused))
const auto maximum = static_cast<T const &(*)(T const &, T const &)>(std::max);

template<typename T>
int cmp3(T lhs, T rhs) { return (lhs == rhs) ? 0 : (lhs > rhs) ? 1 : -1; }

template<typename T>
__attribute__((unused))
T constexpr infty = std::numeric_limits<T>::max / 3;
template<>
__attribute__((unused))
auto constexpr infty<int> = 1'100'100'100;
template<>
__attribute__((unused))
auto constexpr infty<long long int> = 100'500'400'300'200'100LL;

using lli = long long int;
using ld = long double;

} // namespace

using namespace std;

int main() {
  // cout << fixed << setprecision(10);

  int X, Y, Z;
  cin >> X >> Y >> Z;
  cout << Z << " " << X << " " << Y << endl;
  return 0;
}


Measured memory_bytes: 888
from sys import stdin, setrecursionlimit
from collections import defaultdict
from math import gcd

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


MOD = 1000000007
N, *AB = map(int, stdin.buffer.read().split())

d = defaultdict(int)
zeros = 0
for a, b in zip(*[iter(AB)] * 2):
    if a == 0 and b == 0:
        zeros += 1
    else:
        g = gcd(a, b)
        a //= g
        b //= g
        if a < 0:
            a *= -1
            b *= -1
        if a == 0 and b == -1:
            b = 1
        d[(a, b)] += 1

ans = 1
free = 0
for (a, b), n in d.items():
    if b > 0:
        if (b, -a) in d:
            m = d[(b, -a)]
            ans = ans * (pow(2, n, MOD) + pow(2, m, MOD) - 1) % MOD
        else:
            free += n
    else:
        if (-b, a) not in d:
            free += n

ans = (ans * pow(2, free, MOD) + zeros - 1) % MOD
print(ans)


Measured memory_bytes: 159800
#include<bits/stdc++.h>
using namespace std;
int main(){
    char a[10000],b[10000];
    int n, i =0;
    scanf("%d",&n);
    cin>> a;
    while(a[i] != '\0'){
        b[i] = a[i] + n;
        if(b[i]>'Z') {
            b[i] = 'A' + (b[i] - 'Z' - 1);
        }
        printf("%c",b[i]);
        i++;
    }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=1LL<<61;

int main() {
  ll n;
  cin >> n;
  vec a(n);
  for(ll i=0;i<n;i++) {
    cin >> a[i];
  }
  ll ans=0;
  ll mx=0;
  for(ll i=0;i<n;i++) {
    ans+=max(0LL,mx-a[i]);
    mx=max(mx,a[i]);
  }
  cout << ans << endl;
}

Measured memory_bytes: 4768
// ABC053 B - A to Z String
'use strict'
function Main(input) {
    const tmp = input.trim().split('\n')
    const str = tmp[0]
    // console.log(str);

    const begin = str.indexOf('A')
    const end = str.lastIndexOf('Z')

    console.log(end - begin + 1);

}

Main(require('fs').readFileSync('/dev/stdin', 'utf8'));

// Main(`QWERTYASDFZXCV`); // 5
// Main(`ZABCZ`);  //4
// Main(`HASFJGHOGAKZZFEGA`);  // 12

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

int main(){
    int N;
    while(cin >> N, N != 0){
        vector<vector<int>> p(21,vector<int>(21,0));
        int X = 10, Y = 10;
        int stone = 0;
        for(int i=0;i<N;i++){
            int x,y;
            cin >> x >> y;
            p[y][x] = 1;
        }
        int M;
        cin >> M;
        for(int i=0;i<M;i++){
            char C;
            int n;
            cin >> C >> n;
            while(n){
                if(C == 'N') Y++;
                if(C == 'E') X++;
                if(C == 'S') Y--;
                if(C == 'W') X--;
                if(p[Y][X]){
                    stone++;
                    p[Y][X] = 0;
                }
                n--;
            }
        }
        if(stone == N) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    
}


Measured memory_bytes: 3064
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

int main() {
#ifdef DEBUG
  ifstream ifs("in.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
  string S;
  while (cin >> S) {
    S = "0" + S;
    reverse(S.begin(), S.end());
    int N = S.size();
    vec<int, 3> dp = make_v(N + 1, 2, 2, 1 << 25);
    dp[0][1][0] = 0;
    for (int i = 0; i < N; i++) {
      int cur = S[i] - '0';
      for (int keep = 0; keep < 2; keep++) {
        for (int carry = 0; carry < 2; carry++) {
          for (int d = 0; d < 10; d++) {
            int add = d;
            int sub = d - cur - carry;
            int ncarry = 0;
            if (sub < 0) {
              ncarry += 1;
              sub += 10;
            }
            add += sub;
            int nkeep = 0;
            if (d > cur) {
              nkeep = 1;
            } else if (d == cur) {
              nkeep = keep;
            }
            chmin(dp[i + 1][nkeep][ncarry], dp[i][keep][carry] + add);
          }
        }
      }
    }
    cout << dp[N][1][0] << endl;
  }
  return 0;
}


Measured memory_bytes: 149680
# -*- coding:utf-8 -*-
"""解説PDF
s1 = "abaaa" をソートして "aaaab" を得る。
s2 = "dddcc" をソートして "ccddd" を得る。
s3 = "baaaa" をソートして "aaaab" を得る。これはすでに一回現れているので答えに1を足す。
s4 = "cdddc" をソートして "ccddd" を得る。これはすでに一回現れているので答えに1を足す。
s5 = "aaaab" をソートして "aaaab" を得る。これはすでに二回現れているので答えに2を足す。

というふうにやる。

「これはすでにK回現れているので」は、ハッシュテーブルでやればよい

"""
def solve():
    N = int(input())
    d = {}
    for i in range(N):
        s = "".join(sorted(input()))
        if not s in d:
            d[s] = 0
        d[s] += 1

    ans = 0
    for s, value in d.items():
        if value >= 2:
            # 和の公式で答えにタス
            ans += (value-1+1)*(value-1)/2
    print(int(ans))


if __name__ == "__main__":
    solve()


Measured memory_bytes: 17852
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>

using namespace std;

int main() {
  pair<pair<string, int>, int> rest_info[110];
  int N;
  cin >> N;
  string rest_name;
  int rest_score;
  for (int i = 0; i < N; i++) {
    cin >> rest_name >> rest_score;
    rest_info[i] = make_pair(make_pair(rest_name, -rest_score), i);
  }
  sort(rest_info, rest_info + N);
  for (int i = 0; i < N; i++) {
    cout << rest_info[i].second + 1 << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#!/usr/bin/env python3
def main():
    from itertools import permutations
    from math import sqrt

    N = int(input())
    xy = [list(map(int, input().split())) for _ in range(N)]

    ans = 0
    cnt = 0
    for can_order in permutations(xy):
        cnt += 1
        for k in range(1, N):
            xi, yi = can_order[k - 1]
            xj, yj = can_order[k]
            ans += sqrt((xi - xj) ** 2 + (yi - yj) ** 2)
    # print(ans / cnt)
    print(f'{ans / cnt:.10f}')


if __name__ == '__main__':
    main()


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

using namespace std;

const long long int MOD=1e9+7;

int main() {
    int N;
    cin >> N;
    string S1, S2;
    cin >> S1 >> S2;
    
    
    long long int ans;
    int n=0;
    bool flag=true;
    if(S1[0]==S2[0]) {
        ans=3;
        n++;
    } else {
        ans=6;
        n++;
        n++;
        flag=false;
    }
    for(int ni=n; ni<N; ni++) {
        if(S1[ni]==S2[ni]) {
            if(flag) {
                ans *= 2;
                ans %= MOD;
            }

            flag = true;
        } else {
            if(flag) {
                ans *= 2;
                ans %= MOD;
            } else {
                ans *= 3;
                ans %= MOD;
            }
            
            ni++;
            flag = false;
        }
    }
    
    cout << ans << endl;


    return 0;
    
}

Measured memory_bytes: 384
#include <iostream>
#include <vector>

int max(std::vector<int> &a)
{
    if (a.size()==0) return 0;
    int m=a[0];
    for (int i=1; i<a.size(); i++)
        if (a[i]>m) m=a[i];
    return m;
}

int main()
{
    int k,n;
    std::cin>>k>>n;
    std::vector<int> a(n);
    for (int i=0; i<n; i++)
        std::cin>>a[i];
    std::vector<int> v;
    for (int i=1; i<n; i++)
        v.push_back(a[i]-a[i-1]);
    v.push_back(k-a[n-1]+a[0]);
    std::cout<<k-max(v);
    return 0;
}

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

using namespace std;

int main() {
	int h, a;
	cin>>h>>a;
	if(h % a == 0) {
		cout<<(h/a)<<endl;
		return 0;
	}
	cout<<(h/a + 1)<<endl;
	return 0;
}


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

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

    for(int i=0;i<n;i++){
        cin>>a>>b>>c;
        if(a*a==b*b+c*c||a*a+b*b==c*c||c*c+a*a==b*b){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
    }
    return 0;
}



Measured memory_bytes: 3100
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
#define int long long
   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; }
    const int MAX =200005;
const int MOD =1e9+7;
using mint = modint998244353;
//using mint = modint;
using S=int;
using F=int;
S op(S a,S b){
  return a+b;
}
S e(){
  return 0;
}
S mapping(F  f,S x){
  return f*x;
}
F composition(F f,F g){
  return f*g;
}
F id(){
  return 1;
}
signed main(){
int n,m;
cin>>n>>m;
dsu d(n);
int a,b;
for(int i=0;i<m;i++){
  cin>>a>>b;a--;b--;
  d.merge(a,b);
}
cout<<d.groups().size()-1<<endl;

}


Measured memory_bytes: 8876
#include <iostream>

using namespace std;

int main()
{
    string sa, sb, sc;
    cin >> sa;
    cin >> sb;
    cin >> sc;
    int flag = 1;
    if(sa.size()<1 || sa.size()>100)flag = 0;
    if(sb.size()<1 || sb.size()>100)flag = 0;
    if(sc.size()<1 || sc.size()>100)flag = 0;

    if(flag)
    {
        for(int i =0; i<sa.size(); i++)
        {
            if(sa[i]!='a' && sa[i] != 'b' && sa[i] != 'c') flag = 0;
        }
        for(int i =0; i<sb.size(); i++)
        {
            if(sb[i]!='a' && sb[i] != 'b' && sb[i] != 'c') flag = 0;
        }
        for(int i =0; i<sc.size(); i++)
        {
            if(sc[i]!='a' && sc[i] != 'b' && sc[i] != 'c') flag = 0;
        }
    }

    int winnn=0;
    int turn = 0;
    while(winnn == 0 && flag == 1)
    {
        if(turn == 0)
        {
           if(sa.size()>0){
                turn = int(sa[0])-97;
                sa.erase(sa.begin());

            }
            else {
                cout << "A" << endl;
                winnn = 1;
            }
        }
        else if(turn == 1)
        {
            if(sb.size()>0){
                turn = int(sb[0])-97;
                sb.erase(sb.begin());

            }
            else {
                cout << "B" << endl;
                winnn = 1;

            }
        }
        else if(turn == 2)
        {
            if(sc.size()>0){
                turn = int(sc[0])-97;
                sc.erase(sc.begin());


            }
            else {
                cout << "C" << endl;
                winnn = 1;

            }
        }
    }
    return 0;
}


Measured memory_bytes: 3656
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B, V):
    V.sort(reverse=True)
    mean = sum(V[:A])/A

    print(mean)

    if A == N:
        return 1

    c = V[:A].count(V[A-1])
    d = V[A:].count(V[A-1])

    if d == 0:
        return 1

    @lru_cache(maxsize=None)
    def comb2(n, r):
        if n < 0 or r < 0 or n < r:
            return 0
        elif n == 0 or r == 0:
            return 1
        return comb2(n-1, r-1)+comb2(n-1, r)

    if V[0] == V[A-1]:
        return sum([comb2(c+d, i) for i in range(A, B+1)])

    return comb2(c+d, c)


def main():
    N, A, B = read_int_n()
    V = read_int_n()

    print(slv(N, A, B, V))


if __name__ == '__main__':
    main()


Measured memory_bytes: 63852
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define PI 3.14159265359
#define INF 1000100100
#define all(x) (x).begin(),(x).end()
#define P pair<int,int>
typedef long long ll;
using namespace std;

int main(){
    int n,k; cin >> n >> k;
    string s; cin >> s;
    s[k-1]=s[k-1]+32;
    cout << s << endl;
return 0;
}


Measured memory_bytes: 256
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = SI()
    x = 0
    for i in N:
        x += int(i)
    if x % 9:
        print('No')
    else:
        print('Yes')



if __name__ == '__main__':
    main()

Measured memory_bytes: 70468
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<ll,ll>;
using um = unordered_map<ll,ll>;
#define fl cout<<flush;
#define endl '\n'
template <typename T> inline void prt(T v){cout<<v<<'\n';}
template <typename T> inline bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> inline bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=(ll)1e9+7;
const ll MOD2=998244353;
const ld pi=3.14159265358979323846;
const ld eps=1e-10;

signed main(void){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    vector<ll> A(N,0);
    for(ll i=0;i<N;++i)cin>>A[i];
    ll now=0;
    ll ans=0;
    for(auto p: A){
        chmax(now,p);
        ans+=now-p;
    }
    prt(ans);

    return 0;
}



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

int main() {
    int a, b, c;
    cin >> a >> b >> c;

    int ans = c - (a - b);
    if (ans < 0) ans = 0;

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


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
 
int h, w;
char s[12][13];
string ss[12];
int a[12];
int b[12];
 
void dfs1(int x, bool f) {
    if (x == w) {
        puts("YES");
        exit(0);
    } else if (b[x] != -1) {
        dfs1(x + 1, f);
    } else {
        int i, j;
        
        for (i = x + 1; i < w; i++) {
            if (b[i] != -1) continue;
            
            for (j = 0; j < h; j++) {
                if (s[j][x] != s[a[j]][i]) break;
            }
            
            if (j == h) {
                b[x] = i;
                b[i] = x;
                
                dfs1(x + 1, f);
                
                b[x] = b[i] = -1;
            }
        }
       
        
        if (!f) {
            for (j = 0; j < h; j++) {
                if (s[j][x] != s[a[j]][x]) break;
            }
            
            if (j == h) {
                b[x] = x;
                
                dfs1(x + 1, true);
                
                b[x] = -1;
            }
        }
    }
}
 
void dfs2(int x, bool f) {
    if (x == h) {
        dfs1(0, w % 2 == 0);
    } else if (a[x] != -1) {
        dfs2(x + 1, f);
    } else {
        int i;
        
        for (i = x + 1; i < h; i++) {
            if (a[i] != -1) continue;
            if (ss[x] != ss[i]) continue;
            
            a[x] = i;
            a[i] = x;
            
            dfs2(x + 1, f);
            
            a[x] = a[i] = -1;
        }
        
        if (!f) {
            a[x] = x;
            
            dfs2(x + 1, true);
            
            a[x] = -1;
        }
    }
}
 
int main() {
    int i;
    
    scanf("%d %d", &h, &w);
    
    for (i = 0; i < h; i++) scanf("%s", s[i]);
    
    for (i = 0; i < h; i++) {
        ss[i] = s[i];
        sort(ss[i].begin(), ss[i].end());
    }
    
    for (i = 0; i < h; i++) a[i] = -1;
    for (i = 0; i < w; i++) b[i] = -1;
    
    dfs2(0, h % 2 == 0);
    
    puts("NO");
    
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N=1e5+10; 
const int lg=18;
int n,q;
int a,b,c,d,y,U,V,fa,ID;
int tmp,num,len1,len2;
int pos[N],ans[N],sum[N];//欧拉序中对应编号 答案 链上的和 
int bit1[N*2],bit2[N*2];//树按颜色建一棵 按查询建一棵 
int dep[N],dis[N];//点的深度 实际边边权
int head[N],v[N*2],nex[N*2],id[N*2],cnt;
int in[N],out[N];
int dfn[N*2],tot;
int dp[N*2][lg];
//欧拉序 dfn点戳 in out边戳 第i条边[in,out)
struct query{int id,y,u,v;};
vector<int>col[N];//把树边放入对应颜色 
vector<query>color[N];//把询问放入对应颜色 
int mn(int u,int v){if(dep[u]<dep[v])return u;else return v;};
void link(int x,int y,int i){v[++cnt]=y;nex[cnt]=head[x];id[cnt]=i;head[x]=cnt;}
void add(int *tr,int x,int v){for(int i=x;i<=tot;i+=i&-i)tr[i]+=v;}
int ask(int *tr,int x){int ans=0;for(int i=x;i>0;i-=i&-i)ans+=tr[i];return ans;}
void dfs(int u,int fa,int d)
{
	dfn[++tot]=u;
	pos[u]=tot;
	dep[u]=d;
	for(int i=head[u];i;i=nex[i])
	{
		int to=v[i],num=id[i],w=dis[num];//边的真实编号 
		if(to==fa)continue;
		in[num]=tot+1;//归远端节点 即下一个搜的节点 
		sum[to]=sum[u]+w;
		dfs(to,u,d+1);
		dfn[++tot]=u;
		out[num]=tot;
	}
}
void init(int up)
{
	for(int i=1;i<=up;++i)
	dp[i][0]=dfn[i];
	for(int len=1;(1<<len)<=up;++len)
	for(int i=1;i+(1<<len)-1<=up;++i)
	dp[i][len]=mn(dp[i][len-1],dp[i+(1<<(len-1))][len-1]);
} 
int lca(int u,int v)
{
	int l=pos[u],r=pos[v];
	if(l==r)return dp[l][0]; 
	if(l>r)swap(l,r);
	int k=log(r-l+1)/log(2);
	return mn(dp[l][k],dp[r-(1<<k)+1][k]);
}
int main()
{
	scanf("%d%d",&n,&q);
	for(int i=1;i<n;++i)
	{
		scanf("%d%d%d%d",&a,&b,&c,&d);
		link(a,b,i);link(b,a,i);
		col[c].pb(i);dis[i]=d;
	}
	dfs(1,-1,0); 
	init(tot);
	for(int i=1;i<=q;++i)
	{
		scanf("%d%d%d%d",&a,&b,&c,&d);
		color[a].pb(query{i,b,c,d});
	}
	for(int i=1;i<n;++i)
	{
		if(!color[i].size())continue;//没有询问 
		len1=col[i].size();
		for(int j=0;j<len1;++j)
		{
			num=col[i][j];
			add(bit1,in[num],1);
			add(bit1,out[num],-1);
			add(bit2,in[num],dis[num]);
			add(bit2,out[num],-dis[num]);
		}
		len2=color[i].size();
		for(int j=0;j<len2;++j)
		{
			ID=color[i][j].id;y=color[i][j].y;
			U=color[i][j].u;V=color[i][j].v;
			fa=lca(U,V);
			tmp=sum[U]+sum[V]-2*sum[fa];
			tmp-=(ask(bit2,pos[U])+ask(bit2,pos[V])-2*ask(bit2,pos[fa]));
			tmp+=y*(ask(bit1,pos[U])+ask(bit1,pos[V])-2*ask(bit1,pos[fa]));
			ans[ID]=tmp;
		}
		for(int j=0;j<len1;++j)
		{
			num=col[i][j];
			add(bit1,in[num],-1);
			add(bit1,out[num],1);
			add(bit2,in[num],-dis[num]);
			add(bit2,out[num],dis[num]);
		}
	}
	for(int i=1;i<=q;++i)
	printf("%d\n",ans[i]);
	return 0;
}

Measured memory_bytes: 35700
n = int(input())
dp = [0]*n
h = list(map(int,input().split()))

dp[1] = abs(h[1]-h[0])

for i in range(2,n):
	dp[i] = min(dp[i-1] + abs(h[i]-h[i-1]) , dp[i-2] + abs(h[i]-h[i-2]))

print(dp[n-1])

Measured memory_bytes: 14696
N, Q = [int(_) for _ in input().split()]
S = input()
l_r_list = [[int(_) for _ in input().split()] for _ in range(Q)]

AC_accumulation = [0]

for i in range(1, N):
    if S[i] == "C" and S[i - 1] == "A":
        AC_accumulation.append(AC_accumulation[-1] + 1)
    else:
        AC_accumulation.append(AC_accumulation[-1])


ans = []
for l, r in l_r_list:
    ans.append(str(AC_accumulation[r - 1] - AC_accumulation[l - 1]))

print("\n".join(ans))


Measured memory_bytes: 31656
import java.util.Scanner;

public class Main {
	static class Point {
		double x;
		double y;
		
		public Point(final double x, final double y) {
			this.x = x;
			this.y = y;
		}
		
		@Override
		public String toString() {
			return x + " " + y;
		}
		
	}
	
	static void kock(int n, Point p1, Point p2) {
		if (n == 0) {
			return;
		}
		
		double sideX = (p2.x - p1.x) / 3.0;
		double sideY = (p2.y - p1.y) / 3.0; 
		
		Point s = new Point(p1.x + sideX, p1.y + sideY);
		Point t = new Point(p2.x - sideX, p2.y - sideY);
		Point u = new Point(s.x + sideX * Math.cos(Math.PI / 3) - sideY * Math.sin(Math.PI / 3),
				s.y + sideX * Math.sin(Math.PI / 3) + sideY * Math.cos(Math.PI/3)); 

		kock(n-1, p1, s);
		System.out.println(s);
		kock(n-1, s, u);
		System.out.println(u);
		kock(n-1, u, t);
		System.out.println(t);
		kock(n-1, t, p2);
	}
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int n = scan.nextInt();

		Point p1 = new Point(0.0, 0.0);
		Point p2 = new Point(100.0, 0.0);
		
		System.out.println(p1);
		kock(n, p1, p2);
		System.out.println(p2);
		
		scan.close();
	}
}

Measured memory_bytes: 29252