akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

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

string getMax(string a, string b)
{
	if(a.size() != b.size()) return (a.size() < b.size() ? b : a);
	for(int i=0; i<a.size(); i++) 
		if(a[i] != b[i]) return (a[i] < b[i] ? b : a);

	return a;
}

string add(string a, char b)
{
	string res;
	if(a.size() == 1 && a[0] == '0') res += b;
	else res += a + b;

	return res;
}

int main()
{
	int W,H;

	while(cin >> W >> H, (W||H)) {
		string field[70];
		for(int i=0; i<H; i++) 
			cin >> field[i];

		string ans[70][70];
		string res;

		for(int i=0; i<H; i++)
		for(int j=0; j<W; j++) {
			if(!isdigit(field[i][j])) continue;
			string tmp;

			for(int k=0; k<2; k++) {
				int tx = j + dx[k];
				int ty = i + dy[k];

				if(tx<0||ty<0) continue;

				tmp = getMax(tmp, ans[ty][tx]);
			}

			tmp = add(tmp, field[i][j]);

			ans[i][j] = tmp;

			res = getMax(res, tmp);
		}

		cout << res << endl;
	}
}

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

int n,k,x,a[10];

bool f(int n){
    while(n>0){
        if(a[n%10])return true;
        n/=10;
    }
    return false;
}

int main(){
    cin>>n>>k;
    for(int i=0;i<k;i++){
        cin>>x;
        a[x]=true;
    }
    while(f(n))n++;
    cout<<n<<endl;
}

Measured memory_bytes: 256
#!/usr/bin/env python3
MOD = 10**9 + 7
s = list(input())[::-1]
n = len(s)
dp = [[0] * 13 for _ in range(len(s) + 1)]
if s[0] == '?':
    for i in range(10):
        dp[0][i] = 1
else:
    dp[0][int(s[0])] = 1
for i in range(n - 1):
    if s[i + 1] == '?':
        for k in range(13):
            for l in range(10):
                dp[i + 1][(k + l * 10**((i + 1) % 6)) % 13] += dp[i][k]
                dp[i + 1][(k + l * 10**((i + 1) % 6)) % 13] %= MOD

    else:
        for k in range(13):
            dp[i + 1][(k + int(s[i + 1]) * 10**((i + 1) % 6)) % 13] += dp[i][k]
            dp[i + 1][(k + int(s[i + 1]) * 10**((i + 1) % 6)) % 13] %= MOD
print(dp[n - 1][5])


Measured memory_bytes: 67500
#include <iostream>
using namespace std;
typedef long long ll;
ll mod = 1000000007;

int main(){
  int n;
  cin >> n;
  string s1, s2;
  cin >> s1 >> s2;
  int cnt = 0;
  ll ans = 1;
  bool stand;
  if(s1[cnt] == s2[cnt]){
    ans *= 3;
    stand = true;
    cnt++;
  }else{
    ans *= 6;
    stand = false;
    cnt += 2;
  }
  while(cnt < n){
    if(s1[cnt] == s2[cnt]){
      if(stand){
        ans *= 2;
        ans %= mod;
      }else{
        
      }
      stand = true;
      cnt++;
    }else{
      if(stand){
        ans *= 2;
        ans %= mod;
      }else{
        ans *= 3;
        ans %= mod;
      }
      stand = false;
      cnt += 2;
    }
  }
  cout << ans << endl;
  return 0;
}
  
  

Measured memory_bytes: 3620
N = int(input())
S = list(map(int, input().split()))

S.sort(reverse = True)
L = []

cnt = 1
for i in range(1, len(S)):
  if S[i] == S[i - 1]:
    cnt += 1
  else:
    L.append(cnt)
    cnt = 1
L.append(cnt)

if L[0] != 1:
  print("No")
  exit()

DP = [[0] * N for _ in range(len(L))]
S = [0] * len(L)

for i in range(N):
  DP[0][i]= 1

for i in range(1, len(L)):
  for j in range(N):
    DP[i][j] = DP[i - 1][j]
  
  t = L[i]
  k = 0
  P = [0] * N
  for j in range(N):
    P[j] = k
    if DP[i][j] > t:
      DP[i][j] -= t
      k += t
      t = 0
    else:
      t -= DP[i][j]
      k += DP[i][j]
      DP[i][j] = 0
  if t != 0:
    print("No")
    exit()
  for j in range(N):
    DP[i][j] += P[j]

print("Yes")

Measured memory_bytes: 125996
h, w = gets.split.map(&:to_i)
grids = []
h.times { grids << gets.chomp.split("") }
dp = Array.new(h) { Array.new(w, 10 ** 6) }
# dp.each { |r| p r }
cnt = 0
prev = "."
grids.first.each_with_index do |column, i|
  if i == 0
    prev = "."
  else
    prev = grids.first[i - 1]
  end
  if column == "#" && prev == "."
    cnt += 1
  end
  dp[0][i] = cnt
end

(1...h).each do |row|
  (0...w).each do |column|
    if column == 0
      dp[row][column] = dp[row - 1][column]
      dp[row][column] += 1 if grids[row - 1][column] == "." && grids[row][column] == "#"
    else
      op_1 = dp[row - 1][column]
      op_1 += 1 if grids[row - 1][column] == "." && grids[row][column] == "#"
      op_2 = dp[row][column - 1]
      op_2 += 1 if grids[row][column - 1] == "." && grids[row][column] == "#"
      dp[row][column] = [op_1, op_2].min
    end
  end
end
# dp.each { |r| p r }
puts dp.last.last


Measured memory_bytes: 2812
#include"bits/stdc++.h"


#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x,"r",stdin)
#define fw(x) freopen(x,"w",stdout)
#define iout(x) printf("%d\n",x)
#define lout(x) printf("%lld\n",x)
#define REP(x,l,u) for(ll x = (l);x<=(u);x++)
#define RREP(x,l,u) for(ll x = (l);x>=(u);x--)
#define mst(x,a) memset(x,a,sizeof(x))
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define MP make_pair
#define se second
#define fi first
#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
#define sz(x) ((int)x.size())
#define cl(x) x.clear()

typedef  long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
using namespace std;

const int maxn = 100010;
const int mod = 1e9+7;
const double eps = 1e-6;
const double PI = acos(-1);

template<typename T> inline void read(T &x){
x=0;T f=1;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do x=x*10+ch-'0',ch=getchar();while(ch<='9'&&ch>='0');x*=f;
}

template<typename A,typename B> inline void read(A&x,B&y){read(x);read(y);}
template<typename A,typename B,typename C> inline void read(A&x,B&y,C&z){read(x);read(y);read(z);}
template<typename A,typename B,typename C,typename D> inline void read(A&x,B&y,C&z,D&w){read(x);read(y);read(z);read(w);}
template<typename A,typename B> inline A fexp(A x,B p){A ans=1;for(;p;p>>=1,x=1LL*x*x%mod)if(p&1)ans=1LL*ans*x%mod;return ans;}
template<typename A,typename B> inline A fexp(A x,B p,A mo){A ans=1;for(;p;p>>=1,x=1LL*x*x%mo)if(p&1)ans=1LL*ans*x%mo;return ans;}

int n,M;ll K;

int F[maxn],Ans[maxn],Tmp[maxn];

ll X[maxn],A[maxn];

void calc(int *A,int *B){
	REP(i,2,n)Tmp[i]=B[A[i]];
	REP(i,2,n)A[i]=Tmp[i];
}

void Work(){
	while(K){
		if(K&1)calc(Ans,F);
		calc(F,F);
		K>>=1;
	}
	A[1]=X[1];REP(i,2,n)A[i]=A[i-1]+X[Ans[i]];
	REP(i,1,n)printf("%lld\n",A[i]);
}

void Init(){
	read(n);
	REP(i,1,n)read(X[i]);
	RREP(i,n,2)X[i]-=X[i-1];
	read(M,K);
	REP(i,2,n)F[i]=Ans[i]=i;
	REP(i,1,M){
		int x;read(x);
		swap(F[x],F[x+1]);
	}
}

int main(){
	Init();
	Work();
	return 0;
}


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

vector<int> Z_Algorithm(string &s) {
    vector<int> ret(s.size());
    for (int i = 1, j = 0; i < s.size(); i++) {
        if (i + ret[i-j] < j + ret[j]) {
            ret[i] = ret[i-j];
        } else {
            int k = max(0, j + ret[j] - i);
            while (i+k < s.size() && s[k] == s[i+k]) k++;   
            ret[i] = k; j = i;
        }
    }
    ret[0] = s.size();
    return ret;
}

// one-based numbering
struct UnionFind {
    vector<int> data;
    // i: (data[i] < 0) -> group size, (data[i] > 0) -> parent;
    UnionFind(int n) {
        data.resize(n+1, -1);
    }
    int find(int x) {
        if(data[x] < 0) return x;
        else return data[x] = find(data[x]);
    }
    int size(int x) {
        return -data[find(x)];
    }
    bool unite(int x, int y) {
        x = find(x); y = find(y);
        if(x == y) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y];
        data[y] = x;
        return true;
    }
    bool same(int x, int y) {
        x = find(x); y = find(y);
        return x == y;
    }
};

int main() {
    string s, t; cin >> s >> t;
    string r = t + s + s;
    while (r.size() < t.size() * 3) {
        r.append(s);
    }
    vector<int> z = Z_Algorithm(r);
    UnionFind uf(s.size());
    for (int i = 0; i < s.size(); i++) {
        if (z[t.size()+i] >= t.size()) {
            int u = i+1, v = ((i+t.size()) % s.size()) + 1;
            if (u == v || uf.same(u, v)) {
                cout << -1 << endl;
                return 0;
            }
            uf.unite(u, v);
        }
    }
    int ans = 0;
    for (auto x: uf.data) {
        if (x < 0) ans = max(ans, -x-1);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 13108
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <iomanip>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

#define dump(x)  cout << #x << " = " << (x) << endl;

#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define shandom_ruffle random_shuffle

const int MOD = 1000000007;
const ll INF = 1e18;
//             0123456789 - digits. check the limits, int vs long
const int MX = 100001;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout << std::setprecision(0);
    cout << std::fixed;
    // freopen("ex.in","r",stdin);
    // freopen("ex.out","w",stdout);
    string s[7] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
    string t;
    cin >> t;
    for (int i = 0; i < 7; i++) {
        if (s[i] == t) {
            cout << 7 - i << "\n";
        }
    }
    return 0;
}

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

//long long ccw(const Point& A, const Point& B, const Point& C) {
//	return 1LL * (B.x - A.x)*(C.y - A.y) - 1LL * (B.y - A.y)*(C.x - A.x);
//}

int main() {
	// 오늘 할 것!  종만북 보기! 
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	ll n;
	cin >> n;
	if (n & 1) {
		cout << 2 * n << '\n';
	}
	else {
		cout << n << '\n';
	}




}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define ins insert
#define vi vector<int>
const int mxN=2e9;
const int mxS=2e5;

int maxAbsValExpr(vector<int>& x, vector<int>& y) {
        int res = 0, n = x.size(), smallest, cur;
        for (int p : {1, -1}) {
            for (int q : {1, -1}) {
                smallest = p * x[0] + q * y[0] + 0;
                for (int i = 1; i < n; ++i) {
                    cur = p * x[i] + q * y[i];
                    res = max(res, cur - smallest);
                    smallest = min(smallest, cur);
                }
            }
        }
        return res;
    }

int main()
{
    int n;
    cin >> n;
    vector<int> x;
    vector<int> y;
    for(int i=0;i<n;i++)
    {
        ll a,b;
        cin >> a >> b;
        x.pb(a);
        y.pb(b);
    }
    cout << maxAbsValExpr(x,y);
}

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

int main() {
  const int M = 300010;
  bool ismsp[M]; // monday-saturday prime
  fill(ismsp, ismsp+M, true);
  ismsp[0] = ismsp[1] = false;
  for (int i=0; i<M; i++) {// 7で割って1,6余らない奴は全部ダメ
    if (7*i < M) {
      ismsp[7*i] = false;
    } else {
      goto ENDMS;
    }
    for (int j=2; j<=5; j++) {
      if (7*i+j < M) {
	ismsp[7*i+j] = false;
      } else {
	goto ENDMS;
      }
    }
  }
 ENDMS:
  for (int i=0; i<M; i++) { // ふるい
    if (ismsp[i]) {
      int j=2;
      while (true) {
	if (j * i < M) {
	  ismsp[j*i] = false;
	} else {
	  break;
	}
	j++;
      }
    }
  }
  int n;
  while (scanf("%d", &n) != EOF && n != 1) {
    printf("%d:", n);
    for (int i=2; i<=n; i++) {
      if (n%i == 0 && ismsp[i]) {
	printf(" %d", i);
      }
    }
    printf("\n");
  }
}

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

int main(){
  int H,W;
  cin >> H >> W;
  
  for(int i=0;i<W+2;i++){
    cout << '#';
  }
  
  cout << ' ' << endl;
  
  for(int i=0;i<H;i++){
    string str;
    cin >> str;
    cout << '#' << str << '#' << endl;
  }
  
  for(int i=0;i<W+2;i++){
    cout << '#';
  }
}

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

template <typename T>
vector<T> uni(vector<T> v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    return v;
}

#include <iomanip>
int main() {
    int n, m; cin >> n >> m;
    vector<int> p(m), y(m);
    for (int i = 0; i < m; i++) cin >> p[i] >> y[i], p[i]--;
    vector<vector<int>> vals(n);
    for (int i = 0; i < m; i++) vals[p[i]].emplace_back(y[i]);
    for (int i = 0; i < n; i++) vals[i] = uni(vals[i]);
    for (int i = 0; i < m; i++) {
        int v = p[i];
        int id = lower_bound(begin(vals[v]), end(vals[v]), y[i]) - begin(vals[v]);
        cout << setfill('0') << setw(6) << v + 1;
        cout << setfill('0') << setw(6) << id + 1 << endl;
    }
    return 0;
}

Measured memory_bytes: 7808
s = input()
h_else_i = True
 
ret = True
for char in s:
  if h_else_i:
    if char == "h":
      h_else_i = not h_else_i
      continue
    else:
      ret = False
      break
  else:
    if char == "i":
      h_else_i = not h_else_i
      continue
    else:
      ret = False
      break
if ret and h_else_i:
  print("Yes")
else:
  print("No")

Measured memory_bytes: 3060
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iterator>
#include<sstream>
#include<iomanip>

#define rep(i, n) for (int i = 0; i < n; i++) 
#define rrep(i, n) for(int i = n-1; i>=0; i--)
#define range(i, n, m) for (int i = n; i < m; i++) 
#define rrange(i, n, m) for (int i = n - 1; i => m; i--) 
#define MOD 1000000007

typedef long long int ll;
typedef unsigned long long int ull;


using namespace std;

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

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

	ll N, M;
	string S, T;

	vector<ll> IN(5, 0);
	ll ret = 0;

	cin >> N;
	rep(i, 5) {
		cin >> IN[i];
	}

	ll min = *min_element(IN.begin(), IN.end());

	ll dst;
	dst = N / min;
	if (N % min == 0)dst -= 1;

	cout << dst+5 << endl;



	
	return 0;
}


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

a_list = []
tmp_start = []
tmp_goal = []
for i in range(m):
    a, b = map(int, input().split())
    
    # 島1から出ている船の行き先
    if a == 1:
        tmp_start.append(b)
    
    if b == n:
        tmp_goal.append(a)
    

    # 島1から意外の運航便のリスト
    #else:
    #    a_list.append([a, b])

agree = set(tmp_start) & set(tmp_goal)

if len(agree) == 0:
    print('IMPOSSIBLE')
else:
    print('POSSIBLE')

#for j in tmp_start:
#    if j in tmp_goal:
#        print('POSSIBLE')
#        break

#else:
#    print('IMPOSSIBLE')

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

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

int main() {
  string s;
  cin >> s;
  if (s.size() == 1) {
    cout << 0 << endl;
    return 0;
  }
  string s1, s2;
  ll count1 =0;
  ll count2 = 0;
  rep(i, s.size()) {
    if (i % 2 == 0) {
      s1 += '0';
      s2 += '1';
    }else {
      s1 += '1';
      s2 += '0';
    }
  }
  //cout << "s1: " << s1 << endl;
  //cout << "s2: " << s2 << endl;
  rep(i, s.size()) {
    if (s[i] != s1[i]) count1++;
    if (s[i] != s2[i]) count2++;
  }
  cout << min(count1, count2) <<endl;
  return 0;
}

Measured memory_bytes: 768
let modulo = 1000000007

let ( +% ) a b = let c = a + b in if c > modulo then c - modulo else c
let ( -% ) a b = if a >= b then a - b else modulo + a - b
let ( *% ) a b = a * b mod modulo

let pow k =
  let rec aux accu k = function
  | 0 -> accu
  | n when n mod 2 = 0 -> aux accu (k *% k) (n/2)
  | n -> aux (accu *% k) (k *% k) (n/2) in
  aux 1 k

let inv k = pow k (modulo-2)

let n, k = Scanf.scanf "%d %d" (fun x y -> x, y)

let () = if k = 1 then begin print_int 1; exit 0 end

let smax = n * k
let factbl = Array.make (smax+1) 1
let ifactbl = Array.make (smax+1) 0
let () = for i = 1 to smax do factbl.(i) <- factbl.(i-1) *% i done;
         ifactbl.(smax) <- inv factbl.(smax);
         for i = smax-1 downto 0 do ifactbl.(i) <- ifactbl.(i+1) *% (i+1) done

let binom n k = factbl.(n) *% ifactbl.(k) *% ifactbl.(n-k)


let dp = Array.make (n+1) 0

let () = dp.(0) <- 1;
         for i = 0 to n-1 do for j = i+1 to n do
           dp.(j) <- dp.(j) +% dp.(j-1) *% binom (i+j*(k-1)-1) (k-2) done done;
         print_int (factbl.(n) *% dp.(n))

Measured memory_bytes: 63608
#[macro_use]
extern crate text_io;

fn main() {
    let n: i32 = read!();
    let mut num = vec![];

    for _ in 0..n {
        let input: u128 = read!();
        num.push(input);
    }

    num.sort();
    let mut ans: u128 = 1;
    let overlimit: u128 = 1000000000000000000;

    for n in num {
        if n == 0 {
            ans = 0;
            break;
        } 
        ans *= n;
        if ans > overlimit {
            println!("-1");
            return;
        }
    }

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


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

#define sz(s)               (int)s.size()
using namespace std;
int main()
{
	int n; cin >> n;
	vector<int>v(n);
	for (int i = 0; i < n; ++i)cin >> v[i];
	int m; cin >> m;
	int x, y;
	for (int i = 0; i < m; ++i)
	{
		cin >> x >> y;
		long long ans = 0;
		for (int j = 0; j < n; ++j)
		{
			if (x == j + 1) ans += y;
			else ans += v[j];
		}
		cout << ans << endl;
	}

	 
}

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

int main() {
  int K, S;
  cin >> K >> S;

  int ans = 0;

  for (int i = 0; i <= K; i++) {
    for (int j = 0; j <= K; j++) {
      int x = S - i - j;
      if (0 <= x && x <= K) ans++;
    }
  }

  cout << ans << "\n";
}

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

using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b == 2 ? a : gcd(b, a % b);};
ll poww(ll a, ll b, ll md) {
   return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}
const long long MOD = 1e9+7;
const int SIZE = 1e5+10;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    double n,m,k;
    cin>>n>>m>>k;
    double div = n/k;
    if(div<=m) cout<<"Yes\n";
    else cout<<"No\n";










    return 0;
}




Measured memory_bytes: 3736
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
#define int long long
signed main() {
	int n; string s; cin >> n >> s;
	int cnt = 0, ans = n;
	for (int i = 1; i < n; i++)if (s[i] == 'E')cnt++;
	ans = min(ans, cnt);
	for (int i = 1; i < n; i++) {
		if (s[i - 1] == 'W')cnt++;
		if (s[i] == 'E')cnt--;
		ans = min(ans, cnt);
	}
	cout << ans << endl;
}

Measured memory_bytes: 900
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
using namespace std;
 
const int MAXN = 310;
 
int f[MAXN][MAXN][MAXN];
 
int main() {
#ifdef BZ
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
    string s;
    int k;
    cin >> s >> k;
    string t(s.rbegin(), s.rend());
    int n = s.length();
    s += '#';
    t += '#';
 
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n; ++j) {
            for (int l = 0; l <= k; ++l) {
                int c = f[i][j][l];
                f[i + 1][j][l] = max(f[i + 1][j][l], c);
                f[i][j + 1][l] = max(f[i][j + 1][l], c);
                int& nv = f[i + 1][j + 1][l + (s[i] != t[j])];
                nv = max(nv, c + 1);
            }
        }
    }
 
    int ans = 0;
 
    for (int i = 0; i < n; ++i) {
        ans = max(ans, f[i][n - 1 - i][k] * 2 + 1);
    }
 
    for (int i = 0; i <= n; ++i) {
        ans = max(ans, f[i][n - i][k] * 2);
    }
 
    cout << ans << "\n";
}

Measured memory_bytes: 114944
eval'H,W='+gets.split*?,;s=$<.read.tr".#\n","10";c=s.count ?1;W+=t=1;l=1<<H*W-1;c,r,t=c-1,t,(t|t<<1|t>>1|t<<W|t>>W)&s.to_i(2)while t<l&&t!=r;p(r<t ?c:-1)

Measured memory_bytes: 2044
"use strict";
    
const main = arg => {
    arg = arg.trim().split("\n");
    const S = arg[0].split("");
    const Srev = arg[0].split("").reverse();
    
    let diff = 0;
    
    for(let i in S) {
        if(S[i] !== Srev[i]) diff++;
    }
    
    // console.log(S);
    // console.log(Srev);
    
    console.log(diff / 2)
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 21064
#pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;
constexpr double pi = acos(-1);
constexpr double EPS = 1e-10;

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

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

    string s; cin>>s;
    ll k; cin>>k;
    int n = s.size();

    for(int i=0; i<min(k, (ll)n); i++){
        if(s[i] != '1'){
            cout << s[i] << endl;
            return 0;
        }
    }

    cout << 1 << endl;
}

Measured memory_bytes: 256
import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class Main {

	static final Scanner in = new Scanner(System.in);
	static final PrintWriter out = new PrintWriter(System.out,false);
	static boolean debug = false;

	public static double area(double a, double b, double c) {
		double s = (a + b + c)/2;
		return Math.sqrt(s*(s - a)*(s - b)*(s - c));
	}

	static boolean solve() {
		int x = in.nextInt();
		int y = in.nextInt();
		int z = in.nextInt();
		if (x + y + z == 0) return false;
		int n = in.nextInt();

		if (x < y) {
			int t = x; x = y; y = t;
		}
		if (x < z) {
			int t = x; x = z; z = t;
		}

		while (n-- > 0) {
			int r = in.nextInt()*2;
			out.println(r > Math.sqrt(y*y + z*z) ? "OK" : "NA");
		}

		return true;
	}

	public static void main(String[] args) {
		debug = args.length > 0;
		long start = System.currentTimeMillis();

		while(solve());
		out.flush();

		long end = System.currentTimeMillis();
		dump((end-start) + "ms");
		in.close();
		out.close();
	}

	static void dump(Object... o) { if (debug) System.err.println(Arrays.deepToString(o)); }
}

Measured memory_bytes: 25884
import java.util.*;
import java.util.Scanner;
import java.io.*;
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.Scanner; 
import java.util.StringTokenizer;

import javax.lang.model.util.ElementScanner6; 

public class Main
{
    static class FastReader
    { 
        BufferedReader br; 
        StringTokenizer st; 

        public FastReader() 
        { 
            br = new BufferedReader(new
                    InputStreamReader(System.in)); 
        } 

        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 

        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 

        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 

        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 

        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    } 
    
    

    public static void main(String args[])
    {  
        FastReader in=new FastReader();
        int t=1;
        //t=in.nextInt();
        while(t-->0)
        {
            long n=in.nextInt();
            long ans=0;
            for(long i=1;i<=n;i++)
            {
                long y=(long)Math.floor(n/(double)i);
                ans+=((y*(y+1))*i)/2;
            }
            System.out.println(ans);
        
        }

    }
}

Measured memory_bytes: 25696
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+2;
const int S=5e5+2;
int arr[N];
int L[S];
int R[S];
int seg[4*N];
int ans[S];
vector<pair<pair<int,int>,int> >Q;
map<int,int>lst;
bool cmp(pair<pair<int,int>,int>A,pair<pair<int,int>,int>B)
{
    if(A.first.second==B.first.second)
        return A.first.first<B.first.first;
    return A.first.second<B.first.second;
}
void update(int pos,int val,int st,int en,int idx)
{
    if(st==en&&st==pos)
    {
        seg[idx]=val;
        return;
    }
    int mid=st+(en-st)/2;
    if(pos<=mid)
        update(pos,val,st,mid,2*idx);
    else
        update(pos,val,mid+1,en,2*idx+1);
    seg[idx]=seg[2*idx]+seg[2*idx+1];
}
long long query(int l,int r,int st,int en,int idx)
{
    //cout<<l<<" "<<r<<endl;
    if(l>en||r<st||st>en)return 0;
    if(l<=st&&r>=en)
        return seg[idx];
    int mid=st+(en-st)/2;
    return query(l,r,st,mid,2*idx)+query(l,r,mid+1,en,2*idx+1);
}
int main()
{
    ios_base::sync_with_stdio(0);
    int n,q,l,r;
    //cin>>n;
    scanf("%d%d",&n,&q);
    for(int i=0;i<n;i++)
        scanf("%d",&arr[i]);
        //cin>>arr[i];

    for(int i=0;i<q;i++)
    {
        //cin>>l>>r;
        scanf("%d%d",&l,&r);
        l--,r--;
        L[i]=l;
        R[i]=r;
        Q.push_back({{l,r},i});
    }
    sort(Q.begin(),Q.end(),cmp);
    memset(seg,0,sizeof(seg));
    int j=0;
    int pos;
    for(int i=0;i<n;i++)
    {
        pos=Q[j].second;
        l=L[pos];
        r=R[pos];
        if(lst.count(arr[i])>0)
        update(lst[arr[i]],0,0,n-1,1);
        update(i,1,0,n-1,1);
        lst[arr[i]]=i;
        while(r==i&&j<q)
        {
            ans[pos]=query(l,r,0,n-1,1);
            j++;
            if(j==q)break;
            pos=Q[j].second;
            l=L[pos];
            r=R[pos];
        }
    }
    for(int i=0;i<q;i++)
        printf("%d\n",ans[i]);
        //cout<<ans[i]<<"\n";
    return 0;
}


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

int main()
{
	string S;
	int N;
	cin >> S >> N;
	int l = S.size();
	int res = 0;
	for (int t = 0; t < N; t++) {
		string T;
		cin >> T; T += T;
		bool ok = false;
		int x = T.size();
		for (int i = 0; i < x; i++) {
			if (T.substr(i, l) == S) {
				ok = true;
				break;
			}
		}
		res += ok;
	}
	cout << res << endl;
	return 0;
}



Measured memory_bytes: 3176
<?php
$ans = "No";

fscanf(STDIN, "%d", $N);
$a = array_map('intval', explode(" ", trim(fgets(STDIN))));

$mmin = min($a);
$mmax = max($a);
$sum = [];

foreach ($a as $v) {
    if (!isset($sum[$v])) {
        $sum[$v] = 1;
    } else {
        $sum[$v]++;
    }
}
krsort($sum);

switch (count($sum)) {
    case 1:
        if ($mmin === 0 && $mmax === 0) {
            $ans = "Yes";
        }
        break;
    case 2:
        $keys = array_keys($sum);
        $vals = array_values($sum);
        
        if ($keys[1] === 0) {
            $x = [
                intdiv($vals[0] + 1, 2),
                intdiv($vals[0], 2),
                $vals[1]
            ];
            if (max($x) - min($x) === 0) {
                $ans = 'Yes';
            }
        }
        break;
    case 3:
        $keys = array_keys($sum);
        $vals = array_values($sum);
        if (max($vals) - min($vals) === 0) {
            if (($keys[0] ^ $keys[1]) === $keys[2]) {
                $ans = 'Yes';
            }
        }
        break;
    default:
        
}
echo $ans;


Measured memory_bytes: 19188
A,B,C,D=map(int,input().split())
E=A//D
F=C//B
if  E > F:
	print("Yes")
elif E == F and A%D != 0:
	print("Yes")
elif E == F and C%B == 0 and A%D == 0:
	print("Yes")
elif E+1 == F and C%B == 0 and A%D != 0:
	print("Yes")    
else:
	print("No")

Measured memory_bytes: 9192
import sys
input = sys.stdin.readline
from heapq import heappop, heappush
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
q = []
for i,b in enumerate(B):
    heappush(q,(-b,i))
answer = 0
while len(q)>0:
    b,i = heappop(q)
    b = -b
    if b < A[i]:
        answer = -1
        break
    sub = B[(i-1)%N] + B[(i+1)%N]
    t = ( b - A[i]) // sub
    if t == 0:
        if b == A[i]:
            continue
        answer = -1
        break
    B[i] -= sub * t
    answer += t
    heappush(q,(-B[i],i))
print(answer)



Measured memory_bytes: 127308
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
int t[1000];
int p[56];
int r[1000];
int o;
int book[1000000];
int main()
{

    int u=0;
    for(int i=2; i<10000; i++)
    {
        if(book[i]==0)
            for(int j=2; j<10000; j++)
            {
                if(i*j>=10000)
                    break;
                book[i*j]=1;
            }
    }
    int n;
    int f[56];
    int v=0;
    for(int i=2;i<=10000;i++)
    {
        if(book[i]==0&&i%10==1)
        {
            f[v]=i;
            v++;
            if(v==57)
                break;
        }
    }
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; i++)
            {
                if(i==0)
                    cout<<f[i];
                else
                    cout<<" "<<f[i];
            }
            cout<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int inf = 1000000;
int R, C;
int sx, sy, gx, gy;
char c[22][22];
int d[22][22];

int bfs() {
  queue<P> que;
  for (int y = 0; y < R; y++)
    for (int x = 0; x < C; x++)
      d[x][y] = inf;
  que.push(P(sx, sy));
  d[sx][sy] = 0;

  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.first == gx && p.second == gy)
      break;

    for (int i = 0; i < 4; ++i) {
      int nx = p.first + dx[i], ny = p.second + dy[i];

      if (0 <= nx && nx < C && 0 <= ny && ny < R && c[nx][ny] != '#' &&
          d[nx][ny] == inf) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[gx][gy];
}

int main() {
  cin >> R >> C;
  for (int y = 0; y < R; y++) {
    for (int x = 0; x < C; x++) {
      cin >> c[x][y];
    }
  }
  int count = 0;
  int ans = 0;
  for (int ssx = 0; ssx < C; ssx++) {
    for (int ssy = 0; ssy < R; ssy++) {
      for (int ggx = 0; ggx < C; ggx++) {
        for (int ggy = 0; ggy < R; ggy++) {
          if ((c[ssx][ssy] == '#' || c[ggx][ggy] == '#')) {
            continue;
          }
          count++;
          sx = ssx;
          sy = ssy;
          gx = ggx;
          gy = ggy;
          int res = bfs();
          if (d[gx][gy] == inf) {
            break;
          }
          ans = max(ans, res);
        }
      }
    }
  }
  // cout << count << endl;
  cout << ans << endl;
}


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

int main()
{
	int n;
	cin>>n;
	int a[n],b[n];
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	int sum=0;
	for(int i=0;i<n;i++)
	{
		cin>>b[i];
		if(a[i]>b[i])
		{
			sum+=a[i]-b[i];
		}
	}
	cout<<sum<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll,ll>;
const ll mod = 1e9+7;
#define ALL(x) (x).begin(),(x).end()
#define pow(x,y) modpow(x,y)
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define REPS(i,n) for(ll (i)=1;(i)<=(n);(i)++)
#define RREP(i,n) for(ll (i)=(n-1);(i)>=0;(i)--)
#define RREPS(i,n) for(ll (i)=(n);(i)>0;(i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());

template <class T = int>T in(){
    T x;
    cin >> x;
    return (x);
}

template<class T> inline void chmin(T& a, T b){
    if (a > b){
        a = b;
    }
}

template<class T> inline void chmax(T& a, T b){
    if (a < b){
        a = b;
    }
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<fixed<<setprecision(10);
    int N = in();
    ll sum = 0;
    REPS(i,N){
        if(i % 3 != 0 && i % 5 != 0)sum+=i;
    }
    cout << sum << "\n";
}

Measured memory_bytes: 3608
#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using IP = pair<int, int>;

const ll INF = 1LL << 62;
#define atcoder(int)1e9+7
#define Endl endl
#define mp make_pair
#define all(v) v.begin(),v.end()
#define pb push_back

ll gcd(ll a, ll b) {//最大公約数
	if (a%b == 0)return b;
	else gcd(b, a%b);
}

ll lcm(ll a, ll b) {//最小公倍数
	return (a / gcd(a, b))*b;
}

int main(){

	char a, b;
	cin >> a >> b;

	if (a < b)cout << "<" << endl;
	else if (a > b)cout << ">" << endl;
	else cout << "=" << endl;

	return 0;
}

Measured memory_bytes: 256
N = int(raw_input())
m = N
for i in range(N+1):
    c = 0
    six = i
    nine = N-i
    while six > 0:
        c += six % 6
        six /= 6
    while nine > 0:
        c += nine % 9
        nine /= 9
    if m > c:
        m = c
print m

Measured memory_bytes: 5764
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <list>
#include <set>
#include <utility>
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <fstream>
using namespace std;
#define pi acos(-1)
#define N 1000000007
#define ll long long
#define ull unsigned long long
#define mem(a) memset(a,0,sizeof(a))
int r[4010];
int g[4010];
int b[4010];
string s;
int l;
int jud(int a, int b)
{
    int q[4];
    q[0] = a;
    q[1] = b;
    sort(q,q+2);
    int x = q[0]-q[1]+q[0];
    int y = q[1]+q[1]-q[0];
    int z = (q[0]+q[1])/2;
    int cnt = 0;
    if(x>=0&&s[x]=='B') cnt++;
    if(y<l&&s[y]=='B') cnt++;
    if(z*2==x+y&&s[z]=='B') cnt++;
    return cnt;
}

int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    cin >> n;
    cin >> s;
    l = s.length();
    ll sum = 0;
    int k1 = 0;
    int k2 = 0;
    int k3 = 0;
    for(int i = 0; i < l; i++){
        if(s[i]=='R') r[k1++] = i;
        if(s[i]=='G') g[k2++] = i;
        if(s[i]=='B') b[k3++] = i;
    }
    for(int i = 0; i < k1; i++){
        for(int j = 0; j < k2; j++){
            sum += k3;
            if(jud(r[i],g[j])==1) sum--;
            if(jud(r[i],g[j])==2) sum -= 2;
            if(jud(r[i],g[j])==3) sum -= 3;
        }
    }
    cout << sum << endl;
    return 0;
}


Measured memory_bytes: 3664
def main():
    import sys
    readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    from collections import deque
    from bisect import bisect_left

    N, M = map(int, input().split())
    tate = []
    yoko = []
    X = set()
    Y = set()
    for _ in range(N):
        A, B, C = map(int, readline().split())
        X.add(A)
        X.add(B)
        Y.add(C)
        tate.append((C, A, B))
    for _ in range(M):
        D, E, F = map(int, readline().split())
        X.add(D)
        Y.add(E)
        Y.add(F)
        yoko.append((D, E, F))

    X = sorted(list(X))
    Y = sorted(list(Y))
    H = len(X)
    W = len(Y)

    wall = [[15] * W for _ in range(H)]

    for c, a, b in tate:
        ia = bisect_left(X, a)
        ib = bisect_left(X, b)
        jc = bisect_left(Y, c)
        for i in range(ia, ib):
            wall[i][jc - 1] &= 15 - (1 << 1)
            wall[i][jc] &= 15 - (1 << 3)
    for d, e, f in yoko:
        id = bisect_left(X, d)
        je = bisect_left(Y, e)
        jf = bisect_left(Y, f)
        for j in range(je, jf):
            wall[id - 1][j] &= 15 - (1 << 2)
            wall[id][j] &= 15 - (1 << 0)

    q = deque()
    x0 = bisect_left(X, 0) - (0 not in X)
    y0 = bisect_left(Y, 0) - (0 not in Y)
    if x0 == H - 1 or y0 == W - 1:
        print('INF')
        exit()

    path = [True] * (W * H)
    path[x0 * W + y0] = False
    q.append((x0, y0))
    direction = ((-1, 0), (0, 1), (1, 0), (0, -1))
    cnt = 0
    while q:
        x, y = q.popleft()
        cnt += (X[x + 1] - X[x]) * (Y[y + 1] - Y[y])
        for i, (dx, dy) in enumerate(direction):
            if wall[x][y] & (1 << i):
                nx = x + dx
                ny = y + dy
                if 0 <= nx < H - 1 and 0 <= ny < W - 1:
                    if path[nx * W + ny]:
                        path[nx * W + ny] = False
                        q.append((nx, ny))
                else:
                    print('INF')
                    exit()
    print(cnt)


if __name__ == "__main__":
    main()


Measured memory_bytes: 353468
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#define pb push_back
using namespace std;
typedef pair<int,int> pii;

int main(){
  int n,h,w;
  cin>>n>>h>>w;
  cout<<(n-h+1)*(n-w+1)<<endl;
  
  return 0;
}


Measured memory_bytes: 256
def main():

    i = list(map(int, input().split()))
    a = i[0]
    b = i[1]
    c = i[2]

    if  not (1 <= b and b <= a and a <= 20 and 1 <= c and c <= 20):      
        return

    if c >= a - b:
        k = c - a + b
    else:
        k = 0

    print(k)


if __name__ == "__main__":
    main()


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

long long cnt;
int l;
int A[1000000];
int n;
vector<int> G;

void insertionSort(int A[],int n,int g){
	for(int i=g;i<n;i++){
		int v=A[i];
		int j=i-g;
		while(j>=0&&A[j]>v){
			A[j+g]=A[j];
			j-=g;
			cnt++;
		}
		A[j+g]=v;
	}
}

void shellSort(int A[],int n){
	for(int h=1;;){
		if(h>n)break;
		G.push_back(h);
		h=3*h+1;
	}
	
	for(int i=G.size()-1;i>=0;i--){
		insertionSort(A,n,G[i]);
	}
}

int main() {
	
	cin>>n;
	for(int i=0;i<n;i++) cin>>A[i];
	cnt=0;
	
	shellSort(A,n);
	
	cout<<G.size()<<endl;
	
	for(int i=G.size()-1;i>=0;i--){
		cout<<G[i];
		if(i) cout<<" ";
	}
	cout<<endl;
	
	cout<<cnt<<endl;
	
	for(int i=0;i<n;i++){
		cout<<A[i]<<endl;
	}
	
	return 0;
}

Measured memory_bytes: 7008
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<map>
#include<set>

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

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

int main() {
    int K; cin >> K;
    int A; cin >> A;
    int B; cin >> B;

    bool can = false;
    rrep(i, 1, 1000) {
        int kb = i * K;
        if (A <= kb && kb <= B) {
            can = true;
            break;
        }
    }

    if (can) cout << "OK" << endl;
    else cout << "NG" << endl;
    return 0;
}


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

int main(){
	int n;
	while(cin >> n){
		bool* p = new bool[n+1];
		int cou = 0;
		for(int i = 0; i < n+1; ++i){
			p[i] = true;
		}
		p[0] = false; p[1] =false;
		for(int i = 2; i*i <= n; ++i){
			for(int j = 2;i*j <= n ; ++j) {
				p[i*j] = false;
			}
		}

		for(int i = 0; i < n+1; ++i ){
			if(p[i] == true) ++cou;
		}
		cout << cou << endl;
	}
    return 0;
}

Measured memory_bytes: 15460
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();
    String[]S = new String[h];
         
    for(int i=0; i<h; i++) {
    	S[i] = sc.next();
    }
    for(int i=0; i<w+1; i++) {
        System.out.print('#');
    }
    	System.out.println('#');
    for(int i=0; i<h; i++) {
    	System.out.println('#' + S[i] + '#');
    }
    for(int i=0; i<w+2; i++) {
        System.out.print('#');
}
}
    }

Measured memory_bytes: 39336
N, M = map(int, input().split())
D = [None] * M
for i in range(M):
  a, b = map(int, input().split())
  D[i] = (a, b)
D.reverse()

P = list(range(N))
C = [1] * N
R = [1] * N
def root(x):
  if P[x] == x:
    return x
  else:
    rx = root(P[x])
    P[x] = rx
    return rx

def find(x, y):
  return root(x) == root(y)

def union(x, y):
  rx = root(x)
  ry = root(y)
  if rx != ry:
    if R[ry] > R[rx]:
      rx, ry = ry, rx
    P[ry] = rx
    C[rx] += C[ry]
    R[rx] = max(R[rx], R[ry] + 1)

def size(x):
  return C[root(x)]

f = N * (N - 1) // 2
F = []
for a, b in D:
  a -= 1
  b -= 1
  F.append(f)
  if not find(a, b):
    f = max(0, f - size(a) * size(b))
    union(a, b)

F.reverse()
for f in F:
  print(f)

Measured memory_bytes: 25484
#include <bits/stdc++.h>
    
#define int long long

#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define FORR(i,m,n) for(int i=m;i>=n;i--)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y) 
#define SORT(x) sort((x).begin(),(x).end())
#define INF 99999999999
    
using namespace std;

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

int32_t main(){
    int n, m;
    cin >> n >> m;
    vector<pair<int, int> > v[n];
    REP(i, m){
        int a, b;
        cin >> a >> b;
        a--; b--;;
        v[a].pb(mp(b, i));
        v[b].pb(mp(a, i));
    }

    int ans=0;
    REP(i, m){
        vector<bool> f(n, false);
        queue<int> q;
        q.push(0);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            f[u]=true;

            REP(j, v[u].size()){
                if(v[u][j].second!=i && !f[v[u][j].first]){
                    q.push(v[u][j].first);
                }
            }
        }

        REP(i, n){
            if(!f[i]){
                ans++;
                break;
            }
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include<string>
using namespace std;
int main(void){
    int N,r,b;
    cin>>N;
    string s;
    cin>>s;
    r=0;
    b=0;
    for(int i=0;i<N;i++){
        if(s[i]=='R'){
        r=r+1;

        }else{
            b=b+1;
        }
    }
    if(r>b){
         cout<<"Yes"<<endl;
    }else{
         cout<<"No"<<endl;
    }
    return 0;
}

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

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<pi> vpi;
typedef vector<bool> vbool;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define forall(it,s) for(auto it = s.begin(); it != s.end(); ++it)
#define F0(i,n) for(ll i = 0; i < n; i++)
#define F1(i,n) for(ll i = 1; i <= n; i++)
#define rep(i,a,b) for(ll i = a; i <= b; i++)
#define per(i,a,b) for(ll i = b-1; i >= a; i--)
#define FIN ios::sync_with_stdio(false);cin.tie(0);
#define db(x) cout<<#x<<" = "<<x<<"\n"
#define all(v) v.begin(),v.end()
#define modulo(a,b) ((a%b)+b)%b
#define F first
#define S second
#define INF 1000000010
#define INFLL 1000000000000000000
#define eps 0.000000000001
#define mod 1000000007
template<class C> void mini(C&a, C b){ a=min(a, b);}
template<class C> void maxi(C&a, C b){a=max(a,b);}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}

//=======================//
const int maxn = 101, maxk = 1e5+2;
int n;
ll dp[maxn][maxk], prefix_sum[maxn][maxk], k;
vll a;


int main () {
	//FIN

	cin >> n >> k;
	a = vll(n);
	F0(i,n) cin >> a[i];
	F0(i,n) F0(j,k+1) {dp[i][j] = 0; prefix_sum[i][j] = 0;}
	F0(j,k+1) {
		if (j<=min(a[0],k)) dp[0][j] = 1;
		else dp[0][j] = 0;
	}
	prefix_sum[0][0] = 1;
	F1(j,k) {
		prefix_sum[0][j] = dp[0][j] + prefix_sum[0][j-1];
		prefix_sum[0][j] = modulo(prefix_sum[0][j], mod);
	}
	rep(i, 1, n-1) rep(j, 0, k) {
		ll mink = 0;
		ll maxk = min(a[i], j);
		dp[i][j] = prefix_sum[i-1][j-mink];
		if (j-maxk-1 >= 0) dp[i][j] -= prefix_sum[i-1][j-maxk-1];
		dp[i][j] = modulo(dp[i][j],mod);
		//db(i); db(j); db(dp[i][j]);
		if (j == 0) prefix_sum[i][j] = dp[i][j];
		else prefix_sum[i][j] = modulo(prefix_sum[i][j-1] + dp[i][j],mod);
	}

	cout << dp[n-1][k] << "\n";

	return 0;
}










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

int main() {
    int N;
    cin >> N;
    vector<bool> dp(N+1);
    dp[0] = true;

    rep(i,N+1){
        if(dp[i]){
            rep(j,6){
                if(i+100+j<=N)
                    dp[i + 100 + j] = true;
            }
        }
    }

    dp[N] ? cout << 1: cout << 0;
    cout << endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int P[10010];
int n,q;
int comp[100000 + 10];
int x[100000 + 10];
int y[100000 + 10];
void init(int N)
{
    for(int i = 1; i <= N; ++i)
    {
        P[i] = i;
    }
}

int root(int a)
{
    if(P[a] == a)
    {
        return a;
    }
    return (P[a] = root(P[a]));
}

bool is_same_set(int a, int b)
{
    return root(a) == root(b);
}

void unite(int a, int b)
{
    P[root(a)] = root(b);
}
int main()
{
    cin >> n >> q;
    for(int i = 1; i <= q; ++i)
    {
        cin >> comp[i] >> x[i] >> y[i];
    }
    init(n);
    for(int j = 1; j <= q; ++j)
    {
        if(comp[j] == 0)
        {
            unite(x[j], y[j]);
        }
        else if(comp[j] == 1)
        {
            cout << is_same_set(x[j], y[j]) << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 4180
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <functional>
#include <ccomplex>
#include <unordered_map>
#include <cstring>
#include <iostream>
#include <numeric>
#include <sstream>
#include <set>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <map>

using namespace std;
#define MAX 1LL << 60

typedef long long ll;
typedef unsigned long long ull;

#define rep(i, s, e) for(ll i = s; i < e; i++)
#define repR(i, s, e) for(ll i = s; i > e; i--)
#define reft0(i, j) setfill('0') << setw(i) << j

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

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

    int ans;
    bool flg = false;
    repR(i, S.size(), -1){
        rep(ii, 0, S.size() - i+1){
            string tmp = S.substr(ii, i);
            ans = tmp.size();
            string::iterator itr;
            while ((end(tmp) != (itr = find(begin(tmp), end(tmp), 'A')))
                || (end(tmp) != (itr = find(begin(tmp), end(tmp), 'T')))
                || (end(tmp) != (itr = find(begin(tmp), end(tmp), 'G')))
                || (end(tmp) != (itr = find(begin(tmp), end(tmp), 'C')))){
                    tmp.erase(itr);
            }
            if (tmp.size() == 0) {
                flg = true;
                break;
            }
        }
        if (flg) break;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
from collections import defaultdict
n = int(input())
arr = list(map(int, input().split()))
# n = len(arr)

graph = defaultdict(int)
for i in range(n):
	graph[i+1] = arr[i]

root_arr = [i for i in range(n+1)]

def dfs(src):
	global visited
	stack = [src]
	visited.add(src)
	cycle_size = 0
	while stack:
		v = stack.pop()
		if not graph[v] in visited:
			visited.add(graph[v])
			stack.append(graph[v])
			cycle_size += 1
	return cycle_size

visited = set()
count = 0
for ele in arr:
	if not ele in visited:
		cs = dfs(ele)
		count += cs
print("YES" if count <= 1 else "NO")

Measured memory_bytes: 38640