akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>


using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(ll i = 0; i < n; i++)








int main() {
    ll n, a, b;
    cin >> n >> a >> b;
    cout << min(a, b) << ' ' << max(a + b - n, 0LL) << endl;
}

Measured memory_bytes: 256
//itp1_topic6_a
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  int n;
  cin>>n;

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



  for(int i=0;i < n-1-i;i++) swap(a[i], a[n-1-i]);

  for(int i=0;i<n;i++){
    if(i) cout<<" ";
    cout<<a[i];
  }
  cout<<endl;

  return 0;

}


Measured memory_bytes: 3072
n, m = map(int, input().split())
sa = set()
sb = set()
for i in range(m):
    a, b = map(int, input().split())
    if a == 1:
        sb.add(b)
    if b == n:
        sa.add(a)
print('IMPOSSIBLE' if len(sa & sb) == 0 else 'POSSIBLE')

Measured memory_bytes: 57944
n = int(input())
a = 0
w = 0
t = 0
r = 0
for i in range(n):
    s = input()
    if s == "AC":
        a = a + 1
    elif s == "WA":
        w = w + 1
    elif s == "TLE":
        t = t + 1
    else:
        r = r + 1

print("AC x", a)
print("WA x", w)
print("TLE x", t)
print("RE x", r)


Measured memory_bytes: 9104
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <iomanip>
#include <numeric>
 
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
string S,T;


ll N,K,M;

int main() {
    cin >> S >> T;
    cout << T << S << endl;
}


Measured memory_bytes: 3528
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>

using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
typedef pair<int,int> P;
const int inf=1000000007;
const int MOD=1000000007;
signed main(){
  int n,m;cin>>n>>m;
  string s;cin>>s;
  reverse(ALL(s));
  vector<bool> dp(n+m+10,false);
  dp[n]=true;
  for(int i=n-1;i>=0;i--){
    if(s[i]=='1'){
      dp[i]=false;
      continue;
    }
    else{
      int sup=min(n-i,m);
      rep1(j,sup){
        dp[i]=dp[i]||dp[i+j];
        if(dp[i])break;
      }
    }
  }
  if(!dp[0]){
    cout<<-1<<"\n";
  }
  else{
    vector<int> res;
    rep(i,n){
      for(int j=m;j>0;--j){
        if(dp[i+j]){
          i+=j-1;
          res.push_back(j);
          break;
        }
      }
    }
    int num=res.size();
  rep(i,num){
    cout<<res[num-i-1]<<" ";
  }
  cout<<endl;
  }
  
  return 0;
}


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

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

ll N;
string S;

ll dp[401][401][401][3];

vector<ll> idx[3]; // R,G,Y;
vector<ll> back[3];

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

    cin >> N >> S;

    back[0] = vector<ll>(N);
    back[1] = vector<ll>(N);
    back[2] = vector<ll>(N);

    back[0][N-1] = 0;
    back[1][N-1] = 0;
    back[2][N-1] = 0;

    if(S[N-1] == 'R') back[0][N-1] = 1;
    if(S[N-1] == 'G') back[1][N-1] = 1;
    if(S[N-1] == 'Y') back[2][N-1] = 1;

    for (int i = N-2;i >= 0;i--){
        for (int j = 0;j < 3;j++){
            back[j][i] = back[j][i+1];
        }
        if(S[i] == 'R') back[0][i]++;
        if(S[i] == 'G') back[1][i]++;
        if(S[i] == 'Y') back[2][i]++;
    }

    for (int i = 0;i < N;i++){
        if(S[i] == 'R') idx[0].push_back(i);
        if(S[i] == 'G') idx[1].push_back(i);
        if(S[i] == 'Y') idx[2].push_back(i);
    }

    for (int i = 0;i <= idx[0].size();i++){
        for (int j = 0;j <= idx[1].size();j++){
            for (int k = 0;k <= idx[2].size();k++){
                for (int l = 0;l < 3;l++){
                    dp[i][j][k][l] = LLONG_MAX;
                }
            }
        }
    }

    dp[0][0][0][0] = 0; //i,j,k:次の位置
    dp[0][0][0][1] = 0;
    dp[0][0][0][2] = 0;

    for (int i = 0;i <= idx[0].size();i++){
        for (int j = 0;j <= idx[1].size();j++){
            for (int k = 0;k <= idx[2].size();k++){
                for (int l = 0;l < 3;l++){
                    //cout << i << " " << j << " " << k << " " << dp[i][j][k] << endl;
                    if(dp[i][j][k][l] == LLONG_MAX) continue;
                    //cout << i << " " << j << " " << k << " " << dp[i][j][k][l] << endl;
                    if(i != idx[0].size() && l != 0)dp[i+1][j][k][0] = min(dp[i+1][j][k][0], dp[i][j][k][l]+max<ll>(idx[1].size()-j-back[1][idx[0][i]],0LL)+max<ll>(idx[2].size()-k-back[2][idx[0][i]],0LL));
                    if(j != idx[1].size() && l != 1)dp[i][j+1][k][1] = min(dp[i][j+1][k][1], dp[i][j][k][l]+max<ll>(idx[0].size()-i-back[0][idx[1][j]],0LL)+max<ll>(idx[2].size()-k-back[2][idx[1][j]],0LL));
                    if(k != idx[2].size() && l != 2)dp[i][j][k+1][2] = min(dp[i][j][k+1][2], dp[i][j][k][l]+max<ll>(idx[0].size()-i-back[0][idx[2][k]],0LL)+max<ll>(idx[1].size()-j-back[1][idx[2][k]],0LL));
                }
            }
        }
    }

    ll ans = min({ dp[idx[0].size()][idx[1].size()][idx[2].size()][0] ,dp[idx[0].size()][idx[1].size()][idx[2].size()][1], dp[idx[0].size()][idx[1].size()][idx[2].size()][2]});
    if(ans == LLONG_MAX) ans = -1;

    cout << ans << endl;

    return 0;

}


Measured memory_bytes: 166224
#include <iostream>

using namespace std;

typedef long long ll;

int main() {
    ll n;  cin >> n;
    ll *f = new ll[n+1];
    for (ll i = 0; i < n+1; i++)
        f[i] = 0;

    for (ll i = 1 ;i <= n; i++) {
        for (ll j = i; j <= n; j += i) {
            f[j]++;
        }
    }

    long long ans = 0;
    for (int i = 1; i <= n; i++) {
        ans += (long long)(i * f[i]);
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 81772
#import<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long LL;
LL i,j,k,m,n,s,v,x,y,e[3018][3018],f[3018][3018],g[3018][3018],h[3018][3018],p[3018][3018];
main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>k;
	for(;i++<k;)
	{
		cin>>x>>y>>v;
		p[x][y]=v;
	}
	for(i=0;i++<n;)
	{
		for(j=0;j++<m;)
		{
			v=max(max(e[i-1][j],f[i-1][j]),max(g[i-1][j],h[i-1][j]));
			if(p[i][j])
			{
				e[i][j]=max(e[i][j-1],v);
				f[i][j]=max(f[i][j-1],e[i][j]+p[i][j]);
				g[i][j]=max(g[i][j-1],f[i][j-1]+p[i][j]);
				h[i][j]=max(h[i][j-1],g[i][j-1]+p[i][j]);
			}
			else
			{
				e[i][j]=max(e[i][j-1],v);
				f[i][j]=f[i][j-1];
				g[i][j]=g[i][j-1];
				h[i][j]=h[i][j-1];
			}
		}
	}
	cout<<max(max(e[n][m],f[n][m]),max(g[n][m],h[n][m]));
}

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

using namespace std;

int main() {
    long n;
    long MAX = 1000000000000001;
    cin >> n;

    long index = 26;
    int keta = 1;
    long last = 1;
    long now;
    while (true) {
        if (index >= n) {
            now = index;
            break;
        } else {
            last = index + 1;
            index = index * 26 + 26;
            keta++;
        }
    }

    //cout << last << ", " << now << ", " << keta << endl;

    long syou = n / 26;
    long amari = n % 26;
    long right = now;
    long left = last;
    string result = "";
    // 一番上のけた
    for (int i = 1; i <= keta; i++) {
        long tmplong = (right - left + 1) / 26;
        char tmp = 'a' + ((n - left) / tmplong);
        result += tmp;
        left += tmplong * ((n - left) / tmplong);
        right = left + tmplong ;
        //cout << left << ", " << right << endl;
    }

    cout << result << endl;
}

Measured memory_bytes: 3616
def main():
    X,N = map(int, input().split())
    l = [int(x) for x in input().split()]
    
    ans=0
    if N == 0:
        ans = X
    
    for i in range(100000):
        if not (X-i in l):
            ans=X-i
            break
        elif not(X+i in l):
            ans=X+i
            break
    print(ans)
main()

Measured memory_bytes: 9184
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>

int n;
int a[100002];
int m,ans;

int main(void){
	scanf("%d", &n);
	a[0]=0;
	for(int i=1;i<=n;i++)	scanf("%d", &a[i]);
	a[n+1] = 0;

	for(int i=0;i<=n;i++)	m+=abs(a[i]-a[i+1]);

	for(int i=1;i<=n;i++){	//iは抜かす位置
		ans = m - abs(a[i-1]-a[i]) - abs(a[i]-a[i+1]) + abs(a[i-1]-a[i+1]);
		printf("%d\n", ans);
	}
}


Measured memory_bytes: 1536
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
using ll = long long;
#define rep(i, j, n) for(int i = j; i < (int)n; ++i)
constexpr ll INF = 1LL << 60;

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

    int n, m, s; cin >> n >> m >> s;
    vector<ll> c(n), d(n);
    vector<vector<tuple<int, ll, ll>>> g(n);
    rep(i, 0, m) {
        int u, v, a, b; cin >> u >> v >> a >> b;
        --u, --v;
        g[u].emplace_back(v, a, b);
        g[v].emplace_back(u, a, b);
    }
    rep(i, 0, n) cin >> c[i] >> d[i];


    const int MAXS = 50 * 50;
    vector<vector<ll>> dp(n + 1, vector<ll>(MAXS, INF));
    // 無向グラフ上でDPしたいのでダイクストラを使う
    s = min(s, MAXS);
    dp[0][s] = 0;
    priority_queue<tuple<ll, int, ll>, vector<tuple<ll, int, ll>>, greater<tuple<ll, int, ll>>> pq;
    pq.emplace(0, 0, s);
    while(pq.size()) {
        ll dist, coin;
        int v;
        tie(dist, v, coin) = pq.top();
        pq.pop();
        if (dp[v][coin] != dist) continue; 
        // 最小距離確定はpqに入れる直前
        // dp[v][coin] != distとは、この要素が入った後にdp[v][coin]が更新された場合
      
        // 移動
        int nv, a, b;
        for(auto t : g[v]) {
            tie(nv, a, b) = t;
            if (coin - a >= 0 && dp[nv][coin - a] > dist + b) {
                dp[nv][coin - a] = dist + b;
                pq.emplace(dp[nv][coin - a], nv, coin - a);
            }
        }
        // コイン生産
        if (coin + c[v] < MAXS && dp[v][coin + c[v]] > dp[v][coin] + d[v]) {
            dp[v][coin + c[v]] = dp[v][coin] + d[v];
            pq.emplace(dp[v][coin + c[v]], v, coin + c[v]);
        }
    }

    rep(i, 1, n) {
        ll ans = INF;
        rep(j, 0, MAXS) ans = min(ans, dp[i][j]);
        cout << ans << '\n';
    }
    return 0;
}

Measured memory_bytes: 7352
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
/* main */
int main(){
    vector<vector<int> > c(3, vector<int>(3));
    for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) cin >> c[i][j];

    vector<vector<int> > v(2, vector<int>(3));

    for (int a = 0; a <= c[0][0]; a++) {
        int b = c[0][0] - a;
        v[0][0] = a;
        v[1][0] = b;
        v[0][1] = c[1][0] - b;
        v[0][2] = c[2][0] - b;
        v[1][1] = c[0][1] - a;
        v[1][2] = c[0][2]- a;
        bool can = true;
        for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) {
            if (c[i][j] != v[0][i] + v[1][j]) can = false;
        }
        if (can) {
            cout << "Yes" << '\n';
            return 0;
        }
    }
    cout << "No" << '\n';
}


Measured memory_bytes: 256
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		//?????°?¨????
		int numSet = sc.nextInt(); //???????????????????????°
		String num1;
		String num2;
		int advance = 0;
		int num;

		for(int count = 0; count < numSet; count++) { //???????????????????????°????????°?????????
			List<Integer> ans = new ArrayList<Integer>();

			num1 = sc.next();
			num2 = sc.next();

			String[] numbersStr1 = num1.split("");
			String[] numbersStr2 = num2.split("");

			numbersStr1 = reverse(numbersStr1);
			numbersStr2 = reverse(numbersStr2);

			int max = Math.max(numbersStr1.length, numbersStr2.length);


			for(int i = 0; i < max; i++) {
				if(numbersStr1.length - i > 0 && numbersStr2.length - i > 0) {
					num = Integer.parseInt(numbersStr1[i]) +  Integer.parseInt(numbersStr2[i]) + advance;
					advance = 0;

					if(num >= 10) { //?¶?????????????2????????¨???
						num -= 10;
						advance = 1;
					}

					//???????????\??????
					ans.add(i, num);

				} else if(numbersStr1.length - i > 0) {
					num = Integer.parseInt(numbersStr1[i]) + advance;
					advance = 0;

					if(num >= 10) { //?¶?????????????2????????¨???
						num -= 10;
						advance = 1;
					}

					//???????????\??????
					ans.add(i, num);

				} else if(numbersStr2.length - i > 0) {
					num = Integer.parseInt(numbersStr2[i]) + advance;

					advance = 0;

					if(num >= 10) { //?¶?????????????2????????¨???
						num -= 10;
						advance = 1;
					}

					//???????????\??????
					ans.add(i, num);
				}
			}

			if(advance > 0) {
				ans.add(1);
				advance = 0;
			}

			if(ans.size() > 80) {
				System.out.println("overflow");
			} else {
				for(int i = ans.size() - 1; i >= 0; i--) {
					System.out.print(ans.get(i));
				}
				System.out.println("");
			}
		}
	}

	public static String[] reverse(String[] array) {
		int len = array.length;
		String box;

		for(int i = 0; i < len / 2; i++) {
			box = array[i];
			array[i] = array[len - 1 - i];
			array[len - 1 - i] = box;
		}

		return array;
	}

}

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

int main() {
	ios_base::sync_with_stdio( false );

	int A, B, C;
	cin >> A >> B >> C;

	bool result = false;

	for( int i = 1; i <= 100; ++i ) {
		if( (B*i + C) % A == 0 ) {
			result = true;
			break;
		}
	}


	cout << (result ? "YES" : "NO") << endl;

	return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
string t[2][2][2];
string u[192];
bool used[8];
int n,flg;

int checkch='A'-'a';

bool input();
string ra(string,int);
string rb(string,int);
string rc(string,int);
void dfs(int,int,int);

inline bool check_ch(char x,char y){
  return (x+checkch==y||x-checkch==y);
}
inline bool check_x(string a,string b){
  return check_ch(a[2],b[3]);
}
inline bool check_y(string a,string b){
  return check_ch(a[4],b[1]);
}
inline bool check_z(string a,string b){
  return check_ch(a[0],b[5]);
}


int main(){

  while(input()){
    for(int i=0;i<8;i++)used[i]=false;
    
    flg=0;
    dfs(0,0,0);
    if(flg)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
  }
  return 0;
}

void dfs(int x,int y,int z){
  //cout<<x<<' '<<y<<' '<<z<<endl;
  if(x==2){x=0;y++;}
  if(y==2){y=0;z++;}
  if(x==0&&y==0&&z==2)flg=1;

  for(int j=0;j<8;j++){
    if(used[j])continue;
    for(int i=j*24;i<j*24+24;i++){
      if(flg)return;
      if(x>0)if(check_ch(t[x-1][y][z][2],u[i][3])==false)continue;
      if(y>0)if(check_ch(t[x][y-1][z][4],u[i][1])==false)continue;
      if(z>0)if(check_ch(t[x][y][z-1][0],u[i][5])==false)continue;
      /*
      if(x>0)if(check_x(t[x-1][y][z],u[i])==false)continue;
      if(y>0)if(check_y(t[x][y-1][z],u[i])==false)continue;
      if(z>0)if(check_z(t[x][y][z-1],u[i])==false)continue;
      */
      used[j]=true;
      t[x][y][z]=u[i];
      dfs(x+1,y,z);
      used[j]=false;
    }
  }
}



bool input(){
  string in,str,str2;
  n=0;
  for(int i=0;i<8;i++){
    cin>>in;if(in=="0")return false;
    for(int j=0;j<6;j++){
      if(j<4)str=ra(in,j);
      else if(j==4)str=rb(in,1);
      else str=rb(in,3);
      for(int k=0;k<4;k++){
	str2=rc(str,k);
	u[n++]=str2;
      }
    }
    
  }
  return true;
}

string ra(string str,int x){
  char tmp;
  for(int i=0;i<x;i++){
    tmp=str[3];
    str[3]=str[0];
    str[0]=str[2];
    str[2]=str[5];
    str[5]=tmp;
  }
  return str;
}
string rb(string str,int x){
  char tmp;
  for(int i=0;i<x;i++){
    tmp=str[5];
    str[5]=str[4];
    str[4]=str[0];
    str[0]=str[1];
    str[1]=tmp;
  }
  return str;
}
string rc(string str,int x){
  char tmp;
  for(int i=0;i<x;i++){
    tmp=str[1];
    str[1]=str[2];
    str[2]=str[4];
    str[4]=str[3];
    str[3]=tmp;
  }
  return str;
}

Measured memory_bytes: 1196
use std::vec::Vec;

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()
}

fn main() {
    let s: String = read();
    let s1: i64 = s[0..2].parse().ok().unwrap();
    let s2: i64 = s[2..4].parse().ok().unwrap();
    let yymm: bool = 1 <= s2 && s2 <= 12;
    let mmyy: bool = 1 <= s1 && s1 <= 12;

    if yymm && mmyy {
      println!("AMBIGUOUS");
    }
    else if yymm && !mmyy {
      println!("YYMM");
    }
    else if !yymm && mmyy {
      println!("MMYY");
    }
    else {
      println!("NA");
    }
}


Measured memory_bytes: 4476
#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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, 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")
    };
}

fn main() {
    input! {
        k: i64,
        a: i64,
        b: i64,
    }

    let ans = if b - a < 2 {
        k + 1
    } else {
        if k <= a - 1 {
            k + 1
        } else {
            let base = a;
            let rest = k - (a - 1);
            let reps = rest / 2;
            base + (b - a) * reps + rest - reps * 2
        }
    };

    println!("{}", ans);
}


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

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repr(i, n) for(int i = (n-1); i >= 0; --i)

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

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

int main(){
    int N;
    string S;
    cin >> N >> S;

    ll ans = 0;
    rep(i, N) {
        if (S[i] == 'R') {
            int gl = 0, bl = 0;
            rep(j, i) {
                if (S[j] == 'G') gl++;
                if (S[j] == 'B') bl++;
            }
            int gr = 0, br = 0;
            for (int j = i + 1; j < N; ++j) {
                if (S[j] == 'G') gr++;
                if (S[j] == 'B') br++;
            }
            ll res = gl * br + bl * gr;
            for (int j = 1; i-j >= 0 && i + j < N; ++j) {
                if (S[i-j] == 'G' && S[i+j] == 'B') res--;
                if (S[i-j] == 'B' && S[i+j] == 'G') res--;
            }
            ans += res;
        }
        if (S[i] == 'G') {
            int rl = 0, bl = 0;
            rep(j, i) {
                if (S[j] == 'R') rl++;
                if (S[j] == 'B') bl++;
            }
            int rr = 0, br = 0;
            for (int j = i + 1; j < N; ++j) {
                if (S[j] == 'R') rr++;
                if (S[j] == 'B') br++;
            }
            ll res = rl * br + bl * rr;
            for (int j = 1; i-j >= 0 && i + j < N; ++j) {
                if (S[i-j] == 'R' && S[i+j] == 'B') res--;
                if (S[i-j] == 'B' && S[i+j] == 'R') res--;
            }
            ans += res;
        }
        if (S[i] == 'B') {
            int gl = 0, rl = 0;
            rep(j, i) {
                if (S[j] == 'G') gl++;
                if (S[j] == 'R') rl++;
            }
            int gr = 0, rr = 0;
            for (int j = i + 1; j < N; ++j) {
                if (S[j] == 'G') gr++;
                if (S[j] == 'R') rr++;
            }
            ll res = gl * rr + rl * gr;
            for (int j = 1; i-j >= 0 && i + j < N; ++j) {
                if (S[i-j] == 'G' && S[i+j] == 'R') res--;
                if (S[i-j] == 'R' && S[i+j] == 'G') res--;
            }
            ans += res;
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 3636
N = int(input())
A=[]
B=[]
for i in range(N):
    a,b=[int(n) for n in input().split()]
    A.append(a)
    B.append(b)

cnt=0

# print(list(reversed(range(N))))
for i in reversed(range(N)):
    if (A[i]+cnt)%B[i]==0:
        d=0
    else:
        d=B[i]-(A[i]+cnt)%B[i]
    cnt+=d
#    A=[A[n]+r if n<=i else A[n] for n in range(N)]

print(cnt)


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

using namespace std;

#define FOR(i,a,b)  for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n)    FOR(i,0,n)
#define ALL(a)      a.begin(),a.end()
#define vi          vector<int>
#define vvi         vector<vi>
#define P           pair<int,int>
#define vP          vector<P>
#define vvP         vector<vP>

int main(){
    int n;cin>>n;
    vi dat(n);
    REP(i,n){
        cin>>dat[i];
    }
    sort(ALL(dat));
    int before=dat[0];
    int res=0;
    int num=0;
    REP(i,n){
        if(before!=dat[i]){
            if(num<before){
                res+=num;
            }else if(before<num){
                res+=num-before;
            }
            num=0;
            before=dat[i];
        }
        num++;
    }
    if(num<before){
        res+=num;
    }else if(before<num){
        res+=num-before;
    }
    cout<<res<<"\n";
    return 0;
}


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

using namespace std;

//#define double long double

typedef pair<int,int> ii;
typedef pair<int,long long> il;
typedef pair<long long,long long> ll;
typedef pair<ll,int> lli;
typedef pair<long long,int> li;
typedef pair<double,double> dd;
typedef pair<ii,int> iii;
typedef pair<double,int> di;
typedef pair<int,ii> iii2;
typedef pair<ii,ii> iiii;
long long mod = 1000000007LL;
long long large = 2000000000000000000LL;



int main(){

	int n;
	cin>>n;
	vector<int> cnt(26,0);
	string s;
	cin>>s;
	for(int i=0;i<n;i++){
		cnt[(int)(s[i]-'a')]++;
	}

	long long ans = 1;
	for(int i=0;i<26;i++){
		ans*=1LL*(cnt[i]+1);
		ans%=mod;
	}
	cout<<(ans+mod-1)%mod<<endl;



	return 0;
}


Measured memory_bytes: 512
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

lli n;
mat g;
vll d;
vll f;
vbl vis;

lli u,k,v;
lli t = 0;

lli dfs(lli x,lli t){
    d[x] = t;
    vis[x] = true;
    for(lli i = 0;i < g[x].size();i++){
        if(!vis[g[x][i]]) t = dfs(g[x][i],t+1);
    }
    return f[x] = t + 1;
}

int main(){
    cin >> n;
    g = mat(n+1);
    d = vll(n+1);
    f = vll(n+1);
    vis = vbl(n+1);
    for(lli i = 0;i < n;i++){
        cin >> u >> k;
        for(lli i = 0;i < k;i++){
            cin >> v;
            g[u].push_back(v);
        }
    }
    for(lli i = 1;i <= n;i++){
        if(!vis[i]) t = dfs(i,t+1);
    }
    for(lli i = 1;i <= n;i++){
        cout << i << " " << d[i] << " " << f[i] << endl;
    }
    return 0;
}

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

struct node{
	struct node *right;
	struct node *left;
	struct node *parent;
	int key;
};

typedef struct node * Node;
#define NIL NULL

Node root;

Node treeMinimum (Node x ){

}

Node treeSearch (Node u, int k ){
	if (u == NIL || u->key == k ) return u;
	else
	if (u->key < k ){
		return treeSearch (u->right, k );
	}else{
		return treeSearch (u->left, k );
	} // end if 
}

Node treeSuccessor (Node x ){

}

void treeDelete (Node z ){
	Node y;	// node to be deleted
	Node x;	// child of y
}

void insert (int k ){
	Node y = NIL;
	Node x = root;
	Node z;

	z = malloc (sizeof (struct node ) );
	z->key = k;
	z->left = NIL;
	z->right = NIL;

	if (x == NIL ){
		root = z;
	}else{
		do{
			if (x->key < k ){
				y = x->right;
			}else{
				y = x->left;
			} // end if

			if (y != NIL ){
				y->parent = x;
			}else{
				if (x->key < k )
					x->right = z;
				else
					x->left = z;
			} // end if
			x = y;
		}while (x != NIL ); // end while
	} // end if
}

void inorder (Node u ){
	if (u == NIL ) return;
	inorder (u->left );
	printf (" %d", u->key );
	inorder (u->right );
}

void preorder (Node u ){
	if (u == NIL ) return;
	printf (" %d", u->key );
	preorder (u->left );
	preorder (u->right );
}

int main()
{
	int n, i, x;
	char com[20];

	root = NIL;
	scanf ("%d", &n );
	for (i = 0; i < n; i++ ){
		scanf ("%s", com );
		if (com[0] == 'i' ){	// case insert 
			scanf ("%d", &x );
			insert (x );
		}else
		if (com[0] == 'p' ){ // case print
			inorder (root );
			printf ("\n" );
			preorder (root );
			printf ("\n" );
		}else
		if (com[0] == 'f' ){	// case find
			scanf ("%d", &x );
			Node t = treeSearch (root, x );
			if ( t != NIL ) printf ("yes\n" );
			else printf ("no\n" );
		} // end if
	} // end for

	return 0;
}

Measured memory_bytes: 19364
#include <iostream>

using namespace std;
int main() {
	int n;
	int h,w;

	int count = 0;

	cin >> n >> h >> w;
	for (int i = 0; i < n; i++) {
		int a,b;
		cin >> a >> b;
		if (a >= h && b >= w) {
			count ++;
		}
	}
	cout << count << endl;
	return 0;
}

Measured memory_bytes: 256
import java.util.*;
import java.io.*;
import java.math.*;
 
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int n = sc.nextInt();
        HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
        for(int i = 0; i < n; i++){
            int k = sc.nextInt();
            int v = sc.nextInt();
            if(map.containsKey(k)){
                if(map.get(k) > v){
                    map.put(k,v);   
                }
            }else{
                map.put(k,v);
            }
        }
        long[][] dp = new long[h+1][map.size()+1];
        
        for(int i = 0; i <= h; i++){
            Arrays.fill(dp[i],Integer.MAX_VALUE);
        }
        dp[0][0] = 0;
        int j = 1;
        for(Map.Entry<Integer,Integer> entry : map.entrySet()){
            int k = entry.getKey();
            int v = entry.getValue();
            for(int i = 0; i <= h; i++){
                if(i < k){
                    dp[i][j] = Math.min(dp[i][j-1], v);
                }else{
                    dp[i-k][j] = Math.min(dp[i-k][j], dp[i-k][j-1]);
                    dp[i][j] = Math.min(dp[i][j-1], dp[i-k][j] + v);
                }
            }
            j++;
        }
        System.out.println(dp[h][map.size()]);
    }
}

Measured memory_bytes: 128940
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,k,frd,sum=0;
	cin >> n >> k;
	for(int i=1;i<=n;i++){
		cin>>frd;
		if(frd>=k) sum++;
	}
	cout << sum << endl;
	return 0; 
} 

Measured memory_bytes: 256
n = int(input())
s = input()

c = 0
x = ""
for i in range(n):
    a = 0
    x = s[0:i]
    y = s[i:n]
    for j in x:
        if j in y:
            y = y.replace(j, "")
            a += 1

        if a > c:
            c = a

print(c)

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

int main() {
	while (1) {
		int i, j, indx, p, size;
		string s, tmp;
		vector<string> vs;

		cin >> s;
		if (s[0] == '#')break;

		vs.push_back(s);

		for (i = 'z'; i >= 'b'; i--) {
			size = vs.size();

			for (j = 0; j < size; j++) {
				s = vs.front();
				vs.erase(vs.begin());

				p = s.find(i);
				if (p == -1) {
					//not found
					vs.push_back(s);
					p = s.size();
				}

				indx = -1;
				while (1) {
					indx = s.find(i - 1, indx + 1);
					if (indx == -1 || indx >= p)break;
					tmp = s;
					tmp[indx] = i;
					vs.push_back(tmp);
				}
				
			}
			//sort(vs.begin(), vs.end());
			//vs.erase(unique(vs.begin(), vs.end()), vs.end());
		


		}

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

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

		if (vs.size() < 10) {
			for (i = 0; i < vs.size(); i++) {
				cout << vs[i] << endl;
			}
		} else {
			for (i = 0; i < 5; i++) {
				cout << vs[i] << endl;
			}
			for (i = vs.size() - 5; i < vs.size(); i++) {
				cout << vs[i] << endl;
			}
		}



	}
	return 0;
}

Measured memory_bytes: 2348
def P(a,b,mod):
    r=1
    for i in range(b):
        r = (r*(a-i)) % mod
    return r

N, K = [int(c) for c in input().split()]
E = [[] for n in range(N)]
mod = 1000000007

for i in range(N-1):
    a, b = [int(c) for c in input().split()]
    E[a-1].append(b-1)
    E[b-1].append(a-1)


n = K * P(K-1, len(E[0]), mod) % mod
for i in range(1, N):
    n = (n * P(K-2, len(E[i])-1, mod)) % mod

print(n)

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

using namespace std;

int main()
{
	vector<int> Enum;
	vector<int> Wnum;
	int n;
	string str;
	int min;
	
	cin >> n >> str;
	
	for (int i = 0; i < (int)str.size(); i++) {
		if (str[i] == 'W') {
			if (i != 0) {
				Wnum.push_back(Wnum[i - 1] + 1);
				Enum.push_back(0);
			}
			else {
				Wnum.push_back(1);
				Enum.push_back(0);
			}
		}
		else {
			if (i != 0) {
				Enum.push_back(1);
				Wnum.push_back(Wnum[i - 1]);
			}
			else {
				Enum.push_back(1);
				Wnum.push_back(0);
			}
		}
	}
	
	for (int i = (int)str.size(); i > 0; i--) {
		Enum[i - 1] += Enum[i];
	}
	
	min = Enum[0] + Wnum[0] - 1;
	
	for (int i = 1; i < (int)str.size(); i++) {
		if (min > (Enum[i] + Wnum[i] - 1)) {
			min = Enum[i] + Wnum[i] - 1;
		}
	}
	
	cout << min << endl;
}

Measured memory_bytes: 3960
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dd;
const int N = 2e5+5;
const double eps = 1e-8;

string s;
int k;
int dp[105][5][2];

int main(){
  // freopen("input.txt","r",stdin);
  cin>>s>>k;
  dp[0][0][0] = 1;
  for(int i = 0;i < s.size();i++){
    for(int j = 0;j <= k;j++){
      int nd = s[i] - '0';
      for(int m = 0;m < 2;m++){
	for(int n = 0;n < 10;n++){
	  int nj = j,nk = m;	  
	  if(n != 0) nj++;
	  if(nj > k) continue;
	  if(m == 0){
	    if(n > nd) continue;
	    if(n < nd) nk = 1;
	  }
	  dp[i+1][nj][nk] += dp[i][j][m];
	}
      }
    }
  }
  cout<<dp[s.size()][k][0]+dp[s.size()][k][1]<<endl;
  return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
       Scanner in=new Scanner(System.in);
       int a,b;
       a=in.nextInt();
       b=in.nextInt();
       if(a==b){
           System.out.println("Yes");
       }
       else{
           System.out.println("No");
       }
    }
    
}

Measured memory_bytes: 22612
package main

import "fmt"

func main() {
  var s string
  dream := []string{"maerd", "remaerd", "esare", "resare"}

  fmt.Scan(&s)

  tmp := ""
  for i := 0; i < len(s); i++ {
    tmp += s[len(s)-i-1:len(s)-i]

    for j := 0; j < len(dream); j++ {
      if dream[j] == tmp {
        tmp = ""
      }
    }
  }

  if tmp == "" {
    fmt.Println("YES")
  } else {
    fmt.Println("NO")
  }
  
}


Measured memory_bytes: 7680
#include<bits/stdc++.h>
#include <math.h>
#include <algorithm>
#include <functional>
#include<iostream>
#include <string>
using namespace std;
typedef long long ll;
#define pb pushback
#define fr(i,n) for(int i=0;i<n;i++)
#define fro(i,j,n) for(int i=j;i<n;i++)
#define ifr(i,n) for(int i=n-1;i>=0;i--)


int main() {
    int a;
    string x,y;
    cin >> a;
	cin >> x>> y;
    string ans="";
    fr(i,a){
      ans+=x[i];
      ans+=y[i];
    }
    cout << ans << endl;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long int
#define ld long double
#define yorn(f) cout<<((f)?"Yes":"No")<<endl;
#define YORN(f) cout<<((f)?"YES":"NO")<<endl;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, n) for (ll i = 1; i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) cout << x << endl;
#define println(x) cout << x << endl;
using namespace std;
// using namespace atcoder;
void Main()
{
    ll n;
    cin >> n;

    vector<vector<ll>> c(10, vector<ll>(10));

    repi(x, n + 1) {
        ll j = x % 10;
        ll i = x;
        while (i / 10 > 0) {
            i /= 10;
        }
        c[i][j]++;
    }

    ll res = 0;
    rep(i, 10) {
        rep(j, 10) {
            res += c[i][j] * c[j][i];
        }
    }

    put(res);

}
signed main(){ Main();return 0;}

Measured memory_bytes: 3584
using System;
using System.Linq;
using System.Collections.Generic;
using static System.Math;
using static System.Console;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        solve();
    }
    static Scanner cin;

    static int N;
    static int K;
    static int[] A;
    static void input()
    {
        cin = new Scanner();
        N = cin.nextInt();
        K = cin.nextInt();
        A = cin.ArrayInt(N);
    }
    static void solve()
    {
        input();
        WriteLine(1 + (N - 2) / (K - 1));
    }
    class Scanner
    {
        string[] s;
        int i;

        char[] cs = new char[] { ' ' };

        public Scanner()
        {
            s = new string[0];
            i = 0;
        }
        
        public string next()
        {
            if (i < s.Length) return s[i++];
            string st = Console.ReadLine();
            while (st == "") st = Console.ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            if (s.Length == 0) return next();
            i = 0;
            return s[i++];
        }

        public int nextInt()
        {
            return int.Parse(next());
        }

        public int[] ArrayInt(int N, int add = 0)
        {
            int[] Array = new int[N];
            for(int i = 0; i < N; i++)
            {
                Array[i] = nextInt() + add;
            }
            return Array;
        }

        public long nextLong()
        {
            return long.Parse(next());
        }

        public long[] ArrayLong(int N, long add = 0)
        {
            long[] Array = new long[N];
            for(int i = 0; i < N; i++)
            {
                Array[i] = nextLong() + add;
            }
            return Array;
        }

        public double nextDouble()
        {
            return double.Parse(next());
        }

        public double[] ArrayDounble(int N, int add = 0)
        {
            double[] Array = new double[N];
            for (int i = 0; i < N; i++)
            {
                Array[i] = nextDouble() + add;
            }
            return Array;
        }
    }
}

Measured memory_bytes: 19808
#include<stdio.h>

int main(){
    int h,n,sum=0;
    scanf("%d%d",&h,&n);
    int a[n];
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    if(sum>=h){
        printf("Yes\n");
    }else{
        printf("No\n");
    }
    return 0;
}

Measured memory_bytes: 1976
import java.util.*;
public class Main {
	public static void main(String[] args){
      	
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
			System.out.println(C+" "+A+" "+B);
		
		
      	sc.close();
    }
		
		
}


Measured memory_bytes: 22228
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	vector<int>a(n);
	for(int i=0;i<n;++i)cin>>a[i];
	for(int i=0;i<n-1;++i)
	{
		if(a[i]>a[i+1])
		{
			swap(a[i],a[a[i]-1]);
			break;
		}
	}
	if(is_sorted(a.begin(),a.end()))
	cout<<"YES";
	else
	cout<<"NO";
	return 0;
}

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

static const int MAX_N = 200000;

struct Relay_List {

	Relay_List *next;
	Relay_List *prev;
	bool live;

};

Relay_List RL[MAX_N];

int main()
{

	int N, M, Q, a, q;

	cin >> N >> M >> Q;
	for( int i = 0; i < N; i++ ) {

		RL[i].next = &RL[i == N - 1 ? 0		: i + 1];
		RL[i].prev = &RL[i == 0		? N - 1 : i - 1];
		RL[i].live = true;

	}

	Relay_List *checkRL1, *checkRL2;

	checkRL1 = &RL[0];
	checkRL2 = checkRL1;

	for( int i = 0; i < M; i++ ) {

		bool OEflg = false;	// ODD : false, EVEN : true

		cin >> a;
		if( !( a % 2 ) ) {
			for( int j = 0; j < a; j++ ) checkRL1 = checkRL1->next;
			checkRL2 = checkRL1->prev;
			OEflg = true;
		}
		else {
			for( int j = 0; j < a; j++ ) checkRL1 = checkRL1->prev;
			checkRL2 = checkRL1->prev;
		}

		checkRL1->live = false;
		checkRL1 = checkRL1->next;

		checkRL1->prev = checkRL2;
		checkRL2->next = checkRL1;

	}

	for( int i = 0; i < Q; i++ ) {

		cin >> q;
		cout << ( RL[q].live ? 1 : 0 ) << endl;

	}

	return 0;

}

Measured memory_bytes: 5848
package main

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

var sc = bufio.NewScanner(os.Stdin)

func Scanner() string {
	sc.Scan()
	return sc.Text()
}

func intScanner() int {
	n, _ := strconv.Atoi(Scanner())
	return n
}

func floatScanner() float64 {
	n, _ := strconv.ParseFloat(Scanner(), 64)
	return n
}
func stringToInt(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}
func stringSliceScanner(n int) []string {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		a[i] = Scanner()
	}
	return a
}
func intSliceScanner(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = intScanner()
	}
	return a
}
func larger(a, b int) int {
	if a < b {
		return b
	} else {
		return a
	}
}
func smaller(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}
func max(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x < a[i] {
			x = a[i]
		}
	}
	return x
}
func min(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x > a[i] {
			x = a[i]
		}
	}
	return x
}
func abs(x int) int {
	if x < 0 {
		x = -x
	}
	return x
}

var mod int = 1000000007

func main() {
	buf := make([]byte, 0)
	sc.Buffer(buf, mod)
	sc.Split(bufio.ScanWords)
	a, b := intScanner(), intScanner()
	fmt.Println((a - 1) * (b - 1))
}


Measured memory_bytes: 1772
import java.util.Scanner;
     
    public class Main {
    	public static void main (String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int N = sc.nextInt();
    		
    		if (N < 1200) {
    			System.out.println("ABC");
    		} else if (N < 2800) {
    			System.out.println("ARC");
    		} else {
    			System.out.println("AGC");
    		}
    		
    	}
    }

Measured memory_bytes: 21844
S = input()
L = int(S[:2])
R = int(S[2:])

if 0 < L <= 12:
    if 0 < R <= 12:
        print("AMBIGUOUS")
    else:
        print("MMYY")

elif 0 < R <= 12:
    print("YYMM")

else:
    print("NA")

Measured memory_bytes: 3060
#include<iostream>
#include<math.h>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<bitset>
#include <climits>
using namespace std;
#define _GLIBCXX_DEBUG
#define pq priority_queue
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vit vector<lint>::iterator
#define all(x) (x).begin(),(x).end()
#define rep(x) for(int i = 0;i < (x);i++)
#define repj(x) for(int j = 0;j < (x);j++)
#define REP(i,x) for(int (i) = 0;(i) < (x);(i)++)
#define rrep(x) for(int i = (x);i >= 0;i--)
#define mins(x,y) x = min(x,y)
#define maxs(x,y) x = max(x,y)
#define mod 1000000007
#define graph_set(n) struct edge{int to,int cost;} vector<edge> G((n))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define range(x,y) ((x) >= 0 && (x) < h && (y) >= 0 && (y) < w)
#define pyes cout << "Yes" << endl
#define pno cout << "No" << endl
#define fin return 0
typedef vector<long long> vi;
typedef vector<pair<long long, long long>> vp;
typedef vector<vector<long long>> vvi;
typedef pair<long long, long long> pi;
typedef long long lint;
typedef long long ll;
lint qp(ll a, ll b) { ll ans = 1; do { if (b & 1)ans = 1ll * ans*a; a = 1ll * a*a; } while (b >>= 1); return ans; }
lint qp(ll a, ll b, int mo) { int ans = 1; do { if (b & 1)ans = 1ll * ans*a%mo; a = 1ll * a*a%mo; } while (b >>= 1); return ans; }
lint gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
long long INF = 1000000000000000;

int r[100], l[100];
int a, b;

#define MAX 9223372036854775807
/*********************** */
int main(void){
	int n,k;cin>>n>>k;
	vector<int> a(n);rep(n)cin>>a[i];
	long long sum = 0LL;
	rep(n)sum+=a[i];
	priority_queue<long long> q;
	//cout << 1 << endl;
	for(ll i=1;i<=sqrt(sum);i++){
		if(sum %i == 0){
			//cout << i << endl;
			q.push(i);
			q.push(sum/i);
		}
	}
	//cout << q.size() << endl;
	while(!q.empty()){
		long long d = q.top();q.pop();
		vector<int> r;
		for(int i=0;i<n;i++){
			int x = a[i]%d;
			if(x != 0) r.push_back(x);
		}
		sort(r.begin(),r.end());
		int m = r.size();
		vector<ll> cum_minus(m+1);
		vector<ll> cum_plus(m+1);
		cum_minus[0] = 0;
		cum_plus[0] = 0;
		
		for(int i=0;i<r.size();i++){
			cum_minus[i+1] = (cum_minus[i] + r[i]);
			cum_plus[i+1] = (cum_plus[i] + (d - r[i]));
		}
		for(int i=0;i<r.size();i++){
			bool j1 = (cum_minus[i+1] <= k);
			bool j2 = ((cum_plus.back() - cum_plus[i+1]) <= k);
			bool j3 =  (cum_minus[i+1] == (cum_plus[m] - cum_plus[i+1]));
			if(j1&&j2&&j3){
				cout << d << endl;
				return 0;
			}
		}
	}
	cout << 1 << endl;
	//cout << "no" << endl;
	return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main 
{

	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		int d = sc.nextInt();
		
		int[] posx = new int[w*h];
		int[] posy = new int[w*h];
		for(int y = 0; y < h; y++)
		{
			for(int x = 0; x < w; x++)
			{
				int cur = sc.nextInt()-1;
				posx[cur] = x;
				posy[cur] = y;
			}
		}
		
		for(int i = w*h-1; i>=0;i--)
		{
			
		}
		
//		long[][] sumdis = new long[d][((h*w)/d)+1];
		long[] sumdis = new long[h*w];
		for(int offset = 0; offset < d; offset++)
		{
			long curdis = 0;
			for(int x = offset+d; x < h*w; x += d)
			{ //distance to here from 0!!!
				int disx = posx[x] - posx[x-d];
				if(disx<0)disx*=-1;
				int disy = posy[x] - posy[x-d];
				if(disy<0)disy*=-1;
				
				curdis += disx + disy;
				sumdis[x] = curdis;
			}
		}
		
		int q = sc.nextInt();
		StringBuilder sb = new StringBuilder();
		
		for(int i = 0; i < q; i++)
		{
			int l = sc.nextInt()-1;
			int r = sc.nextInt()-1;
			long an = sumdis[r] - sumdis[l];
			sb.append(an + "\n");
		}
		System.out.print(sb);
	}

}


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

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

const int offset=30;
int xl[60];
int xr[60];

int main(void){
	int r,n;
	while(cin >> r >> n){
		if(r==0)
			break;
		rep(i,60) xl[i]=xr[i]=0;
		rep(i,n){
			int l,r,h;
			cin >> l >> r >> h;
			l+=offset,r+=offset;
			for(int j=l;j<=r;++j){
				if(j!=l)
					xl[j]=max(xl[j],h);
				if(j!=r)
					xr[j]=max(xr[j],h);
			}
		}
		double cmin=inf;
		for(int x=-r;x<=r;++x){
			int h=inf;
			if(x!=-r)h=min(h,xl[x+offset]);
			if(x!=r)h=min(h,xr[x+offset]);

			int diff=r*r-x*x;
			double y=sqrt(diff);
			double t=h-y+r;
			cmin=min(cmin,t);
		}
		cout.precision(9);
		cout << fixed << max(cmin,0.0) << endl;
	}
	return 0;
}

Measured memory_bytes: 1236
//g++ test.cpp -o test.out
//入出力：http://kaitei.net/cpp/iostream/
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
using namespace std;

int N;
int A[15];
int x[15][15];
int y[15][15];
int max_honester_num = 0;

int bit_digit(int X,int digit){//Xのdigit目を返す関数
	//2真数で表した数の、右の桁から順に0桁目、1桁目、2桁目...と指定する
	return (X >> digit) & 1;
}

int bitcounter(int X){//Xの立っているビットの数を調べる
	int counter = 0;
	for(int i=0;i<N;i++){
		if(bit_digit(X,i)) counter++;
	}
	return counter;
}

//最初に決めてかかってそれが矛盾していないかどうかを全探索
void solve(){
	//二進数でのhonest_flagのj桁目はj人目が正直者と仮定されているかを表す
	for(int honest_flag=1;honest_flag<(1 << N);honest_flag++){
		bool ok = true;//矛盾なしをtrueで表す
		//正直者と仮定されているj人目について調べたい
		for(int j=0;j<N;j++){
			if(bit_digit(honest_flag,j)){//j人目が正直者と仮定されているなら
				for(int k=0;k<A[j];k++){
					//仮定と入力内容が一致しないなら
					if(bit_digit(honest_flag,x[j][k]-1)!=y[j][k]){
						ok = false;//矛盾ありとする
						break;
					}
				}
				if(!ok) break;
			}
		}
		if(ok) max_honester_num = max(max_honester_num,bitcounter(honest_flag));
	}
}

int main(){
	cin >> N;
	for(int i=0;i<N;i++){
		cin >> A[i];
		for(int j=0;j<A[i];j++){
			cin >> x[i][j] >> y[i][j];
		}
	}

	solve();

	cout << max_honester_num;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main()
{
	int n,m,t[111],p,x,s=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>t[i];
		s+=t[i];
	}
	cin>>m;
	for(int i=0;i<m;i++)
	{
		cin>>p>>x;
		cout<<s-t[p]+x<<endl;
	}
	return 0;
}

Measured memory_bytes: 256
N, M = map(int, raw_input().split())
A = []
B = []

for _ in xrange(N):
    A.append(raw_input())
for _ in xrange(M):
    B.append(raw_input())

def match(x, y):
    for i in xrange(M):
        for j in xrange(M):
            if A[x+i][y+j] != B[i][j]: return None
    return 1

def main():
    for x in xrange(N-M+1):
        for y in xrange(N-M+1):
            if match(x, y):
                return 'Yes'
    return 'No'

if __name__ == '__main__':
    print main()

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

using namespace std;


int main(){
    long long i,j;
    long long a,b;
    cin >> a >> b;
    cout << 100 << " " << 100 << endl;
    vector<vector<char> > ans(100,vector<char>(100));
    for(int i=0;i<50;i++){
        for(int j=0;j<100;j++){
            ans[i][j]='#';
        }
    }
    for (int i=50;i<100;i++){
        for(int j=0;j<100;j++){
            ans[i][j]='.';
        }
    }
    bool flag=false;
    for(int i=1;i<50;i+=2){
        for(int j=1;j<100;j+=2){
            if(a==1){
                flag=true;
                break;
            }
            ans[i][j]='.';
            --a;
        }
        if(flag) break;
    }
    flag=false;
    for(int i=51;i<100;i+=2){
        for(int j=1;j<100;j+=2){
            if(b==1){
                flag=true;
                break;
            }
            ans[i][j]='#';
            --b;
        }
        if(flag) break;
    }
    for(int i=0;i<100;i++){
        for(int j=0;j<100;j++){
            cout << ans[i][j];
        }
        cout <<endl;
    }
}



Measured memory_bytes: 256