akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
#define puts(i) cout<<i<<endl
int main(){
	string a,b;
	cin>>a>>b;
	if(a.size()>b.size())puts("GREATER");
	else if(a.size()<b.size())puts("LESS");
	else{
		rep(i,a.size()){
			if(a[i]>b[i]){
				puts("GREATER");
				return 0;
			}else if(a[i]<b[i]){puts("LESS");
			return 0;
			}
		}
		puts("EQUAL");
	}
}


Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10000)
d = [-1,0,1]
def search(x,y,m):
    if not(0<=x<len(m[0]) and 0<=y<len(m)) or m[y][x]=='0': return
    m[y][x] = '0'
    for dx,dy in [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]]:
        search(x+dx,y+dy,m)
while True:
    w,h = map(int,input().split())
    if w==h==0: break
    m = [input().split() for _ in range(h)]
    c = 0
    for y in range(h):
        for x in range(w):
            if m[y][x]=='1':
                c += 1
                search(x,y,m)
    print(c)

Measured memory_bytes: 11736
import java.util.*

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)

    val k = sc.nextInt()
    println(listOf(1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51)[k-1])
}


Measured memory_bytes: 35656
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>

using ll = long long;

#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 100;
std::vector<int> g[1 + nmax];

std::pair<int,int> dfs(int node, int parent){
  std::pair<int,int> sol(0, node);
  for(int h = 0; h < g[node].size(); h++) {
    int to = g[node][h];
    if(to != parent) {
      std::pair<int,int> sol2 = dfs(to, node);
      sol2.first++;
      sol = std::max(sol, sol2);
    }
  }
  return sol;
}

void mark(int node, int parent, std::vector<int> &dist) {
  for(int h = 0; h < g[node].size(); h++) {
    int to = g[node][h];
    if(to != parent) {
      dist[to] = dist[node] + 1;
      mark(to, node, dist);
    }
  }
}

std::pair<ll,ll> solve_specific(int mid, int mid2, int total, int n) {
  std::vector<int> _count(1 + n);
  std::vector<int> new_dist(1 + n), new_dist2(1 + n);
  mark(mid, 0, new_dist);
  mark(mid2, 0, new_dist2);

  ll result = 1;
  if(mid != mid2)
    result = 2;

  for(int i = 1;i <= n; i++) {
    if(new_dist[i] <= new_dist2[i]) {
      int sum = 0;
      for(int h = 0; h < g[i].size(); h++){
        int to = g[i][h];
        if(new_dist[to] == new_dist[i] + 1)
          sum++;
      }
      _count[new_dist[i]] = std::max(_count[new_dist[i]], sum);
    } else { 
      int sum = 0;
      for(int h = 0; h < g[i].size(); h++){
        int to = g[i][h];
        if(new_dist2[to] == new_dist2[i] + 1)
          sum++;
      }
      _count[new_dist2[i]] = std::max(_count[new_dist2[i]], sum);
    }
  }
  if(mid != mid2)
    _count[0]--; 

  for(int i = 0;i < n; i++)
    if(0 < _count[i])
      result = result * _count[i];
  return {total / 2 + 1, result};
}

std::pair<ll, ll> solve(int from, int to, int n) {
  std::vector<int> dist(1 + n), dist2(1 + n);
  mark(from, 0, dist);
  mark(to, 0, dist2);
  int total = dist[to];
 
  int mid = 0, mid2 = 0;
  for(int i = 1;i <= n; i++)
    if(dist[i] + dist2[i] == total){
      if(dist[i] == dist2[i])
        mid = mid2 = i;
      else if(dist[i] + 1 == dist2[i])
        mid = i;
      else if(dist2[i] + 1 == dist[i])
        mid2 = i;
    }
  if(mid != mid2)
    return solve_specific(mid, mid2, total, n);
  else {
    std::pair<ll, ll> sol = solve_specific(mid, mid, total, n);
    for(int h = 0; h < g[mid].size(); h++) {
      int to = g[mid][h];
      sol = std::min(sol, solve_specific(mid, to, total, n));
    }
    return sol;
  }
}

int main() {
  int n;
  std::cin >> n;
  for(int i = 1;i < n; i++) {
    int x, y;
    std::cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  int from = dfs(1, 0).second;
  int to = dfs(from, 0).second;
  std::pair<ll, ll> sol = solve(from, to, n);
  std::cout << sol.first << " " << sol.second;
  return 0;
}


Measured memory_bytes: 3652
#include <stack>
using namespace std;

struct Rectangle {
  int height;
  int pos;
};
 

#include <stdio.h>
#define SIZ 1400
int m[SIZ+1][SIZ+1];
int st[SIZ];
char z[9999999];
int get(){
	static int input_count=0;
	int r=0;
	for(;'0'<=z[input_count]&&z[input_count]<='9';)r=r*10+z[input_count++]-'0';
	input_count++;
	return r;
}
int main(){
	fread(z,1,sizeof(z),stdin);
	int i,j,k,y=get(),x=get(),r,ptr;
	for(j=0;j<y;j++)for(r=i=0;i<x;m[j][i]=r,i++){
		int t=get();
		r=t==0?r+1:0;
	}
	r=0;
/*
	for(i=0;i<x;i++)for(ptr=j=0;j<=y;j++){
		//printf("%d %d %d %d\n",i,j,ptr,m[j][i]);
		if(!ptr || st[ptr-1]<m[j][i])st[ptr++]=m[j][i];
		else{
			for(;ptr && st[ptr-1]>m[j][i];){
				int _y=(j-(ptr-1)),_x=st[--ptr];
				//printf("%d %d\n",_x,_y);
				if(r<_x*_y)r=_x*_y;
			}
			st[ptr++]=m[j][i];
		}
	}
*/
	Rectangle rect;
	for(int i=0;i<x;i++){
		stack<Rectangle> stk;
		int pop=0;
		for(int j=0;j<=y;j++){
			rect.height = m[j][i];
			rect.pos = j;
			if(stk.empty() || stk.top().height < rect.height){
				stk.push(rect);
			}else{
				//printf("[-] %d %d\n",i,j);
				int target = j;
				for(;!stk.empty() && stk.top().height >= rect.height;){
					pop++;
					Rectangle pre = stk.top(); stk.pop();
					int area = pre.height * (j - pre.pos);
					r = max(r, area);
					//printf("%d %d\n",target,pre.pos);
					target = pre.pos;
				}
				rect.pos = target;
				stk.push(rect);
			}
		}
	}

	printf("%d\n",r);
	return 0;
}

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

#define each(i, c) for (auto& i : c)
#define unless(cond) if (!(cond))

using namespace std;

typedef long long int lli;
typedef unsigned long long ull;
typedef complex<double> point;

template<typename P, typename Q>
ostream& operator << (ostream& os, pair<P, Q> p)
{
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

const int N = 2000 + 10;
vector<int> g[N];

int diameter(const int root = 0)
{
  function<int(int)> bfs = [&] (int src) {
    queue<int> q;
    map<int, int> cost;
    cost[root] = 0;
    for (q.push(root); q.size(); q.pop()) {
      int curr = q.front();
      each (next, g[curr]) {
        if (!cost.count(next)) {
          cost[next] = cost[curr] + 1;
          q.push(next);
        }
      }
    }
    pair<int, int> x = *max_element(
      cost.begin(),
      cost.end(),
      [] (pair<int, int> a, pair<int, int> b) { return a.second < b.second; })
    ;
    return x.first;
  };
  return bfs(bfs(root));
}

set<int> reachable(int src, int k, int p = -1)
{
  set<int> vis;
  if (p != -1) vis.insert(p);
  function<void(int, int)> rec = [&] (int curr, int depth) {
    vis.insert(curr);
    each (next, g[curr]) {
      if (!vis.count(next) && depth) rec(next, depth - 1);
    }
    return ;
  };
  rec(src, k);
  return vis;
}

int main(int argc, char *argv[])
{
  int n, k;
  while (cin >> n >> k) {
    fill(g, g + N, vector<int>());
    vector<pair<int, int>> es;
    for (int i = 0; i < n - 1; ++i) {
      int a, b;
      cin >> a >> b;
      --a;
      --b;
      g[a].push_back(b);
      g[b].push_back(a);
      es.push_back(make_pair(a, b));
    }
    int mn = 1 << 29;
    if (k % 2) {
      each (e, es) {
        set<int> a = reachable(e.first, k / 2, e.second);
        set<int> b = reachable(e.second, k / 2, e.first);
        int m = n;
        for (int i = 0; i < n; ++i) {
          m -= a.count(i) | b.count(i);
        }
        mn = min<int>(mn, m);
      }
    } else {
      for (int i = 0; i < n; ++i) {
        set<int> x = reachable(i, k / 2);
        mn = min<int>(mn, n - x.size());
      }
    }
    cout << mn << endl;
  }
  return 0;
}


Measured memory_bytes: 512
//#pragma GCC target("avx2")
//#pragma GCC optimization("O3")
//#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define ub upper_bound
#define lb lower_bound
#define bit __builtin_popcount
#define memset(x) memset(x,0,sizeof(x))

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<vector<int>> matrix;

inline void boost(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

const ll maxn=2e5+123;
const ll inf=1e9+123;
const ll linf=1e18+123;
const ll mod=1e9+7;
const double eps=1e-9;
const double pi=acos(-1);
int dx[8]={0,1,-1,0,1,-1,1,-1};
int dy[8]={1,0,0,-1,1,1,-1,-1};
ll a[maxn];
int main(){
    boost();
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n;
    cin>>n;
    ll cntr=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        cntr+=a[i];
    }
    ll mn=linf;
    ll cur=0;
    for(int i=1;i<=n-1;i++){
        cur+=a[i];
        mn=min(mn,abs(cntr-cur-cur));
    }
    cout<<mn;
	exit(0);
}


Measured memory_bytes: 1792
int M[10][10];
int x,y,s,i,j;

int main(){
	while(~scanf("%d,%d,%d",&x,&y,&s)){
		for(i=0;i<10;i++){
			for(j=0;j<10;j++){
				if(s==2){
					if(fmax(abs(i-x),abs(j-y))<=1)M[i][j]+=1;
				}else{
					if(abs(i-x)+abs(j-y)<=(s+1)/2)M[i][j]+=1;
				}
			}
		}
	}
	int ac=0,ans=0;
	for(i=0;i<10;i++){
		for(j=0;j<10;j++){
			if(M[i][j]==0)ac++;
			ans=fmax(ans,M[i][j]);
		}
	}
	srand((unsigned)time());
	if(rand()%100<50)return !puts("0\n0\n");
	return !printf("%d\n%d\n",ac,ans);
}

Measured memory_bytes: 0
a, b = gets.split.map(&:to_f)
tapnum = 0
count = 1

if b > 1
  (1..b).each do |i|
    tapnum = (a - 1) * i + 1
    if tapnum < b
      count += 1
    end
  end
else
  count = 0
end

puts count

Measured memory_bytes: 1788
#include<iostream>
#include<vector>
#include<queue>
#include<string.h>
using namespace std;
vector<int> sons[100001];
queue<int> q; 
int da[100001],db[100001];
bool v[100001];
int main(){
    int n,a,b,x,u;
    cin >> n;
    for(int i=1;i<=n-1;i++){
    cin >> a >> b;
    sons[a].push_back(b);
    sons[b].push_back(a);
    }
    a=1;
    b=n;    
    memset(v,0,sizeof(v));
    for(int i=1;i<=n;i++) da[i]=1e9;
    da[a]=0; v[a]=1; q.push(a);
    while(!q.empty()){
        x=q.front();
        for(int i=0;i<sons[x].size();i++){
                u=sons[x][i];
                if(!v[u]){
                                da[u]=da[x]+1;
                                v[u]=1;
                                q.push(u);
                                }}
                q.pop();
                }
    memset(v, 0, sizeof(v));
    for(int i=1;i<=n;i++) db[i]=1e9;
    db[b]=0; v[b]=1; q.push(b);
    while(!q.empty()){
        x=q.front();    
        for(int i=0;i<sons[x].size();i++){
        u=sons[x][i];
        if(!v[u]){
                db[u]=db[x]+1;
                v[u]=1;
                q.push(u);
        }}
        q.pop();
        }
        int snuke=0, fennec=0;        
    for(int i=1;i<=n;i++){
        if(da[i]>db[i]) snuke++;
        else if(da[i]<=db[i]) fennec++;
        }
    if(snuke>=fennec) cout << "Snuke" << endl;  
    else cout << "Fennec" << endl;
    //system("pause");
    return 0;
}

Measured memory_bytes: 6784
package main

import "fmt"

func main() {
	var n, m int
	fmt.Scan(&n, &m)

	uf := newUnionFind(n)
	var x, y, z int
	for i := 0; i < m; i++ {
		fmt.Scan(&x, &y, &z)
		x--
		y--
		uf.unite(x, y)
	}

	used := make(map[int]bool)
	ans := 0
	for i := 0; i < n; i++ {
		r := uf.root(i)
		if used[r] {
			continue
		}
		ans++
		used[r] = true
	}

	fmt.Println(ans)
}

// UnionFind .
type UnionFind struct {
	par   []int
	rank  []int
	sizes []int
}

func newUnionFind(n int) UnionFind {
	u := UnionFind{}
	u.par = make([]int, n)
	u.rank = make([]int, n)
	u.sizes = make([]int, n)
	for i := 0; i < n; i++ {
		u.par[i] = i
		u.rank[i] = 0
		u.sizes[i] = 1
	}
	return u
}

func (u UnionFind) unite(x, y int) {
	x = u.root(x)
	y = u.root(y)
	if x == y {
		return
	}

	if u.rank[x] < u.rank[y] {
		u.par[x] = y
		u.sizes[y] += u.sizes[x]
	} else {
		u.par[y] = x
		u.sizes[x] += u.sizes[y]
		if u.rank[x] == u.rank[y] {
			u.rank[x]++
		}
	}
}

func (u UnionFind) root(x int) int {
	if u.par[x] == x {
		return x
	}
	u.par[x] = u.root(u.par[x])
	return u.par[x]
}

func (u UnionFind) same(x, y int) bool {
	return u.root(x) == u.root(y)
}

func (u UnionFind) size(x int) int {
	return u.sizes[x]
}


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

using namespace std;
typedef long long ll;

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))

#define MOD 1000000007
#define INF 1000000000

ll N, K;
set<ll> A;
vector<ll> cand;
vector<ll> cand2;

int main()
{
    cin >> N >> K;
    bool isEnough = false;
    REP(i, N) {
        ll tmp;
        cin >> tmp;
        A.insert(tmp);
        if(tmp == K) {
            printf("POSSIBLE\n");
            return 0;
        }
        else if(tmp > K) { isEnough = true; }
    }
    if(!isEnough) {
        printf("IMPOSSIBLE\n");
        return 0;
    }
    ll a_min;
    while(1) {
        a_min = *(A.begin());
        // 1 番小さいものを更新
        auto it = A.begin();
        while(it != A.end()) {
            ll tmp = min(a_min, (*it) % *(A.begin()));
            if(tmp != 0) { a_min = tmp; }
            it++;
        }
        if(a_min == *(A.begin())) { break; }
        A.insert(a_min);
    }
    if(a_min == 1) {
        printf("POSSIBLE\n");
        return 0;
    }
    auto it = A.begin();
    while(it != A.end()) {
        if(K % *it == 0) { cand.push_back(*it); }
        if(K < *it) { cand2.push_back(*it); }
        ++it;
    }
    REP(i, cand2.size()) {
        REP(j, cand.size()) {
            if(cand2[i] % cand[j] == 0) {
                printf("POSSIBLE\n");
                return 0;
            }
        }
    }

    printf("IMPOSSIBLE\n");
    return 0;
}


Measured memory_bytes: 4992
var n,i,j:longint;
    a:array[0..100]of string;
    t:string;
begin
  readln(n);
  for i:=1 to n do
    readln(a[i]);
  for i:=1 to n do
    for j:=i+1 to n do
      if a[i]>a[j] then
      begin
        t:=a[i];a[i]:=a[j];a[j]:=t;
      end;
  for i:=1 to n do
    write(a[i]);
end.

Measured memory_bytes: 128
K,T = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse=True)
a = A[0]
if a<=(K+1)//2:
    ans = 0
elif K%2==1:
    ans = (a-(K+1)//2)*2
else:
    ans = (a-(K+1)//2)*2-1
print(ans)

Measured memory_bytes: 38256
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <queue>
#include <deque>
#include <list>
#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;


#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repr(i,n) for(int (i)=(n); (i)>=(0); (i)--)
#define SIZE(x) ((int)(x).size())
#define GCD(a, b) int gcd(int a,int b){return b?gcd(b,a%b):a;}
#define QP(a, b) int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(v) (v).begin(),(v).end()


template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
template<typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

typedef vector<int, int> vint;
typedef pair<int,int> pint;
typedef vector<pint>vpint;
typedef long long ll;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
const ll INF = 1LL << 60;
const ll MOD = 1000000007LL;


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll h, w; cin >> h >> w;
    vector<string> s(h);
    for(ll i=0;i<h;i++) cin >> s[i];
    for(ll y=0;y<h;y++){
        for(ll x=0;x<w;x++){
            if(s[y][x] == '#'){
                bool f = false;
                for(ll i=0;i<4;i++){
                    ll nx = x+dx[i], ny = y+dy[i];
                    if(nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
                    if(s[ny][nx] == '#') f = true;
                }
                if(!(f)){
                    cout << "No" << endl;
                    return 0;
                }
            }
        }
    }
    cout << "Yes" << endl;
    return 0;
}



Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<limits>
#include<set>
#include<queue>
using namespace std;
#define int long  long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
typedef pair<int, int>P;
const int INF = 1e15;
const int MOD = 1e9 + 7;

signed main() {
	int x;
	cin >> x;
	if (x < 1200)cout << "ABC";
	else cout << "ARC";
}

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

using namespace std;

using ll = long long;

int main (void){
  ll h, w;
  cin >> h >> w;
  cin.ignore();

  if((h % 3 == 0) || (w % 3 == 0)){
    cout << "0" << endl;
    return 0;
  }

  // 横をすべて１つめが取る
  // 3つはなるべく長さ均等になる

  ll p1, p2, p3, p4, p5, p6;

  if(h % 3 != 0) p1 = w;
  if(w % 3 != 0) p2 = h;

  // 横に均等になろうとする場合

  ll l;
  ll al;
  ll am;
  ll cs, stmp;
  ll smin = 0, smax = 0;

  cs = 1LL << 50;
  ll css = 0, cst = 0, csu = 0;
  for (int i = 1; i <= h - 1; i++){
    cst = i * w;
    // まず横切り
    l = h - i;
    al = l / 2;
    if(al != 0){
      am = l % 2;
      css = (al + am) * w;
      smax = (css > cst) ? css : cst; 
      css = al * w;
      smin = (css < cst) ? css : cst;
      stmp = smax - smin;
      if(cs > stmp) cs = stmp;
      //cout << " aa " << cs << endl;
    }
    //そして縦切り
    l = w;
    al = l / 2;
    if(al == 0) continue;
    am = l % 2;
    css = (al + am) * (h - i);
    smax = (css > cst) ? css : cst; 
    css = al * (h - i);
    smin = (css < cst) ? css : cst;
    stmp = smax - smin;
    if(cs > stmp) cs = stmp;
    //cout << " aa " << cs << endl;
  }

  for (int i = 1; i <= w - 1; i++){
    cst = i * h;
    // まず縦切り
    l = w - i;
    al = l / 2;
    if(al != 0){
      am = l % 2;
      css = (al + am) * h;
      smax = (css > cst) ? css : cst; 
      css = al * h;
      smin = (css < cst) ? css : cst;
      stmp = smax - smin;
      if(cs > stmp) cs = stmp;
    }
    //そして横切り
    l = h;
    al = l / 2;
    if(al == 0) continue;
    am = l % 2;
    css = (al + am) * (w - i);
    smax = (css > cst) ? css : cst; 
    css = al * (w - i);
    smin = (css < cst) ? css : cst;
    stmp = smax - smin;
    if(cs > stmp) cs = stmp;
  }

  cout << cs << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i <= b; i++)
#define revrep(i, s) for (int i = s-1; i > 0; i--)
#define revrrep(i, s) for (int i = s-1; i >= 0; i--)
#define all(a)  begin((a)),end((a))
#define uniq(c) (c).erase(unique(all((c))), end((c)))

int main()
{
    int N, K; cin >> N >> K;
    int ans = 1;
    while (true) {
        if (N == 0) break;
        ans = min(ans*2, ans+K);
        N--;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Math;

namespace AtCoder
{
	class Program
	{
		static void Main(string[] args)
		{
			Joker.Run();
		}

		public static class Joker
		{
			public static void Run()
			{
				var n = int.Parse(Console.ReadLine());
				var permutation = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();

				var unactive = new bool[n + 2, n + 2];

				var ar = new int[n + 2, n + 2];
				for (int i = 1; i <= n; i++)
					for (int j = 1; j <= n; j++)
					{
						var toBot = n - i;
						var toRight = n - j;

						ar[i, j] = Min(Min(i - 1, toBot), Min(j - 1, toRight));
					}

				var queue = new Queue<IntPair>();

				long score = 0;
				foreach (var p in permutation)
				{
					var pp = p - 1;
					var x = pp % n + 1;
					var y = pp / n + 1;
					unactive[y, x] = true;

					score += ar[y, x];
					var currentP = ar[y, x];

					Append(ar, queue, x, y, currentP, 0, 1);
					Append(ar, queue, x, y, currentP, 0, -1);
					Append(ar, queue, x, y, currentP, 1, 0);
					Append(ar, queue, x, y, currentP, -1, 0);

					while (queue.Count != 0)
					{
						var p2 = queue.Dequeue();
						var currentP2 = ar[p2.First, p2.Second];
						var isUnactive = unactive[p2.First, p2.Second];
						if (!isUnactive)
							currentP2 += 1;
						
						Append(ar, queue, p2.Second, p2.First, currentP2, 0, 1);
						Append(ar, queue, p2.Second, p2.First, currentP2, 0, -1);
						Append(ar, queue, p2.Second, p2.First, currentP2, 1, 0);
						Append(ar, queue, p2.Second, p2.First, currentP2, -1, 0);
					}
				}

				Console.WriteLine(score);
			}

			private static void Append(int[,] ar, Queue<IntPair> queue, int x, int y, int currentP, int mx, int my)
			{
				if (ar[y + my, x + mx] > currentP)
				{
					ar[y + my, x + mx] = currentP;
					queue.Enqueue(new IntPair(y + my, x + mx));
				}
			}
		}

		public struct IntPair
		{
			public int First;
			public int Second;

			public IntPair(string s)
			{
				var split = s.Split();
				First = int.Parse(split[0]);
				Second = int.Parse(split[1]);
			}

			public IntPair(int first, int second)
			{
				First = first;
				Second = second;
			}

			public static bool operator ==(IntPair p1, IntPair p2) => p1.First == p2.First && p1.Second == p2.Second;
			public static bool operator !=(IntPair p1, IntPair p2) => p1.First != p2.First || p1.Second != p2.Second;
			public override string ToString() => $"{First} {Second}";
			public override bool Equals(object obj)
			{
				var p = obj as IntPair?;
				return p.HasValue && p.Value.First == First && p.Value.Second == Second;
			}

			public override int GetHashCode()
			{
				uint rol5 = ((uint)First << 5) | ((uint)First >> 27);
				return ((int)rol5 + First) ^ Second;
			}
		}

	}
}


Measured memory_bytes: 47528
A = int(input())
B = int(input())
C = int(input())
D = int(input())
E = int(input())
total_time = 0
if A > 0:
    total_time = (B - A) * E
elif A == 0:
    total_time += D
    total_time += B * E
else:
    total_time += -A * C
    total_time += D
    total_time += B *E

print(total_time)


Measured memory_bytes: 5588
words = lambda t : list(map(t, input().split()))
n,m,x,y = words(int)
xs = words(int)
ys = words(int)

x_max = max(xs)
y_min = min(ys)

if x_max < y_min and x_max < y and y_min > x :
    print("No War")
else:
    print("War")


Measured memory_bytes: 38384
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <iostream>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <algorithm>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <string>

#ifndef ONLINE_JUDGE
	#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#else
	#define DEBUG(x) do {} while(0);
#endif

#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define FOR(i, A, N) for(int (i) = (A); (i) < (N); (i)++)
#define REP(i, N) for(int (i) = 0; (i) < (N); (i)++)

using namespace std;
typedef long long ll;
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }

int n,m;
ll dp[1111][1<<12];
typedef struct {
	int cost;
	int msk;
} box;
box kes[1111];
ll solve(int t, int msk) {
	if(msk == (1<<n)-1) {
		return 0;
	}
	if(t >= m) {
		return 1LL<<50;
	}
	ll& ans = dp[t][msk];
	if(ans == -1) {
		ans = solve(t+1, msk);
		ans = min(ans, kes[t].cost+solve(t+1, msk|kes[t].msk));
	}
	return ans;
}
int main() {
	memset(dp, -1, sizeof(dp));
	scanf("%d%d", &n, &m);
	REP(i, m) {
		int cnt;
		scanf("%d%d", &kes[i].cost, &cnt);
		REP(j, cnt) {
			int x;
			scanf("%d", &x);
			kes[i].msk |= 1<<(x-1);
		}
	}
	ll ans = solve(0, 0);
	if(ans > 1LL<<40) {
		printf("-1\n");
	} else {
		printf("%lld\n", ans);
	}
	return 0;
}


Measured memory_bytes: 35840
#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int n;
    string s;
    cin>>n>>s;
    int num=0;
    int ans=0;
    for(int i=0;i<n;i++){
    	if(num==0){
    		if(s[i]=='A')num++;
    	}else if(num==1){
    		if(s[i]=='B')num++;
    		else if(s[i]=='A')num=1;
    		else num=0;
    	}else{
    		if(s[i]=='C'){
    			ans++;
    			num=0;
    		}
    		else if(s[i]=='A')num=1;
    		else num=0;
    	}
    }
    cout<< ans<<endl;
    return 0;
}

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

signed main() {
  int N, K; cin >> N >> K;
  
  if(K%2 == 1) {
    if(K > N) {
      cout << 0 << endl;
      return 0;
    }
    int a = N/K;
    cout << a*a*a << endl;
  } else {
    K /= 2;
    if(K > N) {
      cout << 0 << endl;
      return 0;
    }
    int a = N/K;
    if(a%2 == 0) {
      a /= 2;
      cout << a*a*a*2 << endl;
    } else {
      a /= 2;
      cout << a*a*a + (a+1)*(a+1)*(a+1) << endl;
    }
  }

}

Measured memory_bytes: 3572
/*  CREATED BY
    STREAM_CIPHER
    7-aug-2020
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define double long double
#define debug1(a) cout<<#a<<"="<<(a)<<"\n"
#define debug2(a,b) cout<<#a<<"="<<(a)<<","<<#b<<"="<<(b)<<"\n"
#define debug3(a,b,c) cout<<#a<<"="<<(a)<<","<<#b<<"="<<(b)<<","<<#c<<"="<<(c)<<"\n"
#define fix_precision(n) cout << fixed << setprecision(n)
#define all(a) a.begin(),a.end()
const double pi=acos(-1.0);
int inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int mx=5*1000000;
char mat[2000+9][2000+9];
int dp[2000][2000];
int solve(int x,int y)
{
    if(x<1||y<1)
        return 0;
    if(x==1&&y==1)
    {
        dp[x][y]=1;
        return 1;
    }
    if(dp[x][y]!=-1)
        return dp[x][y];
    int val=0;
    if(mat[x-1][y]!='#')
        val+=solve(x-1,y)%mod;
    if(mat[x][y-1]!='#')
        val=(val+solve(x,y-1)%mod)%mod;
    dp[x][y]=val;
    return val;

}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        // freopen("output.txt","w",stdout);
    #endif
        int r,c;
        cin>>r>>c;
        for(int i=0;i<r+9;i++)
            for(int j=0;j<c+9;j++)
                mat[i][j]='!';
        for(int i=1;i<=r;i++)
            for(int j=1;j<=c;j++)
                cin>>mat[i][j];
        memset(dp,-1,sizeof dp);
        solve(r,c);
        cout<<max(0ll,dp[r][c])<<endl;
}

Measured memory_bytes: 36976
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)
S = str(input())
N = len(S)
M = 13
#c = [0]*(N)
#for i in range(N):
#    c[i] = (10**i)%M
#    c[i] = pow(10,i,M)
#print(c)
#c.reverse()
#print(c)
"""
dp = [[0]*M for _ in range(N+1)]
i = int(0)
for j in range(M):
    dp[0][j] = 1
for s in S:
    if s == "?":
        for l in range(10):
            m = l * c[i] % M
            dp[i+1][(M - m) % M] += dp[i][j]
    else:
        m = int(s) * c[i] % M
        dp[i][(M - m) % M] +=dp[i][j]
    i +=1




dp = [[0]*M for _ in range(N)]
c = (10**(N-1))%M
if S[0] == "?":
    for l in range(10):
        dp[0][l*c%M] += 1
else:
    m = int(S[0]) * c%M
    dp[0][m] = 1
S = S[1:]
i = int(1)
for s in S:
    c = (10 ** (N - 1-i)) % M
    if s == "?":
        for j in range(13):
            for l in range(10):
                dp[i][j] += dp[i-1][(j-l*c)%13]
    else:
        st = int(s)
        for j in range(13):
            dp[i][j] = dp[i-1][(j-st*c)%13]
    i +=1


dp = [[0]*M for _ in range(N)]
if S[0] == "?":
    for l in range(10):
        m = l * c[0]%M
        dp[0][m] += 1
else:
    m = int(S[0]) * c[0]%M
    dp[0][m] = 1

for i in range(1,N):
    if S[i] == "?":
        for j in range(M):
            for l in range(10):
                m = l*c[i]%M
                dp[i][j] += dp[i-1][(M+j-m)%M]
    else:
        for j in range(M):
            m = int(S[i])*c[i]%M
            dp[i][j] = dp[i-1][(M+j-m)%M]

#print(dp)
print(dp[-1][5]%mod)
"""

dp = [[0]*M for _ in range(N)]
for i in range(N):
    if i == 0:
        if S[-i - 1] == "?":
            for j in range(10):
                dp[i][j] += 1
        else:
            dp[i][int(S[-i - 1])] += 1
    else:
        mult = pow(10, i, 13)
        if S[-i - 1] == "?":
            for j in range(13):
                for k in range(10):
                    dp[i][(k * mult + j) % 13] += dp[i - 1][j]
        else:
            st = int(S[-i - 1])
            for j in range(13):
                dp[i][(st * mult + j) % 13] += dp[i - 1][j]
    for j in range(13):
        dp[i][j] = dp[i][j] % mod

#print(dp)
print(dp[-1][5]%mod)


Measured memory_bytes: 61788
'''
参考
https://marco-note.net/abc-175-work-log/
'''
N, K = map(int, input().split())        # コマ数、最大移動可能回数
P = list(map(int, input().split()))     # 移動先のコマ番号
C = list(map(int, input().split()))     # そのコマに立ち寄った時に加算される得点

ans = -float('inf')             # 解を-∞で初期化

for piece in range(N):          # 各コマについて、スタート地点とした場合の得点を調べる
    S = []                      # 累積和
    mov = P[piece] - 1          # 1回目の移動：移動先（indexなので-1する）
    S.append(C[mov])            # 1回目の移動：得点

    while mov != piece:             # 2回目以降の移動（スタート地点に戻ったらループ終了）
        mov = P[mov] - 1            # 2回目以降：移動先
        S.append(S[-1] + C[mov])    # 2回目以降：得点（最終要素に加算した値をリストに追加）
    
    if K <= len(S):             # 【パターン1】与えられた移動可能回数では一巡できない
        score = max(S[:K])      # 得点=移動可能回数までの最大得点
    elif S[-1] <= 0:            # 【パターン2】一巡できるが一巡してしまうと得点が減る
        score = max(S)          # 得点=一巡するまでの最大得点
    else:
        score_a = S[-1] * (K // len(S))     # 【パターン3-1】与えられた移動可能回数で廻れるだけ廻る
        rem = K % len(S)
        if rem != 0:
            score_a += max(0, max(S[:rem]))
        score_b = S[-1] * (K // len(S) - 1) # 【パターン3-2】1周少なく廻りあとは最高得点のところで止める
        score_b += max(S)
        score = max(score_a, score_b)
    
    ans = max(ans, score)

print(ans)

Measured memory_bytes: 80628
from itertools import product

N = int(input())
S = input()
ans = 0
cand = '0123456789'
for num1, num2 in product(cand, repeat=2):
    phase = 0
    for i, s in enumerate(S):
        if phase == 0 and s == num1:
            phase = 1
        elif phase == 1 and s == num2:
            ans += len(set(S[i+1:]))
            break
print(ans)

Measured memory_bytes: 3064
n = int(input())
a = sorted([(v,k) for k,v in enumerate(list(map(int, input().split())))], reverse=True)
dp = [[-(10 ** 18)] * (n+1) for i in range(n+1)]
dp[0][0] = 0
for i,(v,k) in enumerate(a):
  for j in range(i+1):
    left = j
    right = n-1-(i-j)
    dp[i+1][j] = max(dp[i+1][j], dp[i][j] + v * abs(k-right))
    dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + v * abs(k-left))
print(max(dp[i+1]))

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

const long long MOD = 1000000007;
const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

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

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, X, Y;
  cin >> N >> X >> Y;
  X--, Y--;
  vector<int> ans(N);
  for (int i = 0; i < N; i++)
  {
    for (int j = i + 1; j < N; j++)
    {
      if (i == j)
        continue;
      else
      {
        ans[min(j - i, abs(X - i) + abs(j - Y) + 1)]++;
      }
    }
  }
  for (int i = 1; i < N; i++)
  {
    cout << ans[i] << endl;
  }
  return 0;
}

// iとjが重ならないように
// i < j, X + 1 < Yとなるからiとjの最短距離はj - i, iとX，jとY，がつながっている場合を考えてmin(j - i, abs(i - X) + abs(j - Y) + 1)となる
// 何も考えずにBFS，アドホックに求める


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int A,B,K;
  cin>>A>>B;
  if(A>B & ((A%2==0 && B%2==0) ||(A%2!=0 & B&2!=0))){
    K = (A+B)/2;
    cout<<K<<endl;
  }
  else if(B>A && ((A%2==0 & B%2==0) ||(A%2!=0 & B&2!=0))){
    K = (B+A)/2;
    cout<<K<<endl;
  }
  else{
    cout<<"IMPOSSIBLE"<<endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <tuple>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring> //memset(dp,0,sizeof(dp))
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define repp(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
//小数点以下　cout << fixed << setprecision(10) << num << endl;
//桁数はlog10(mid)でやれ　log(mid)/log(10)はWA
//long*long<0で判定するな
//開区間と閉区間
//アルファベットが何番目かは x=s[i]-'a'
//bfsはmain()内で　rep(i,h)rep(j,w)rep(i,4)でバグ
//切り上げ：(a+(b-1))/b
//32bit以上は 1ull<<j]
// map 初期値0 for(auto itr=mp.begin();itr!=mp.end();++itr) itr->fi itr->se で全探索
// for(auto u:mp) u.fi u.se も可能
//n個のgcd,lcmは一つずつみて更新していく
const ll MOD = 1000000007;

int main(){
    int n,m; cin >> n >> m;
    int pen=0;
    vector<bool> ac(n,0);
    int p[m];
    string s[m];
    rep(i,m){
        cin >> p[i] >> s[i];
        p[i]--;
        if(s[i]=="AC") ac[p[i]]=1;
    }
    int ans=0;
    rep(i,n){
        if(ac[i]) ans++;
    }
    vector<bool> acc(n,0);
    rep(i,m){
        if(s[i]=="AC") acc[p[i]]=1;
        if(s[i]=="WA" && acc[p[i]]==0 && ac[p[i]]==1) pen++;
    }
    cout << ans << " " << pen << endl;
}


Measured memory_bytes: 6144
#!/usr/bin/env python3
import sys


def solve(n: str):
    print(n.replace("9","t").replace("1","9").replace("t","1"))



# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = next(tokens)  # type: int
    solve(n)

if __name__ == '__main__':
    main()


Measured memory_bytes: 2940
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;

template<class T,class U>constexpr bool chmin(T&a,const U b){if(a<=b)return false;a=b;return true;}
template<class T,class U>constexpr bool chmax(T&a,const U b){if(a>=b)return false;a=b;return true;}
#define bit(n,k) ( (n>>k)&1 )

//デバッグ
template<class T>
void Vout(vector<T> &V){
    cout<<"\nstart\n";
    const int sz=V.size();
    for(int i=0;i<sz;i++){
        cout<<i<<" "<<V[i]<<'\n';
    }
    cout<<"end\n"<<endl;
}

template<class T>
void VPout(vector<T> &V){
    cout<<"\nstart\n";
    const int sz=V.size();
    for(int i=0;i<sz;i++){
        cout<<i<<" "<<V[i].first<<" "<<V[i].second<<'\n';
    }
    cout<<"end\n"<<endl;
}


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


constexpr int MAX=1<<30;
constexpr ll INF=1LL<<62;
constexpr int MOD=1e9+7;
int dx[]={1,-1,0,0},dy[]={0,0,-1,1};

//__builtin_popcount(S);
//#define int ll
//vector<vector<int>> data(3, vector<int>(4));
//vector.resize(a,vector<int>(b,-1));
//vector<vector<vector<要素の型>>> 変数名(要素数1, vector<vector<要素の型>>(要素数2, vector<要素の型>(要素数3, 初期値)));

const int MAX_C=100;
ll Com[MAX_C][MAX_C];

void calc_com(){
    memset(Com,0,sizeof(Com));
    Com[0][0]=1;
    for(int i=1;i<MAX_C;i++){
        Com[i][0]=1;
        for(int j=1;j<MAX_C;j++){
            Com[i][j]=(Com[i-1][j-1]+Com[i-1][j]);
        }
    }
}



signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr); 
    cout<<fixed<<setprecision(10);

    int N,A,B;
    cin>>N>>A>>B;
    vector<ll> v(N);
    for(int i=0;i<N;i++) cin>>v[i];
    v.push_back(-1);
    N++;
    sort(v.rbegin(),v.rend());
    ll now=v[0],count=1,pre=0,sum=v[0];
    for(int i=1;i<A;i++){
        sum+=v[i];
        if(now!=v[i]){
            now=v[i];
            pre+=count;
            count=1;
        }else count++,pre;
    }
    int i=A;
    ll ans=0;
    calc_com();
    while(now==v[i]){
        i++;
        count++;
    }
    if(pre==0){
        for(int i=A;i<=B;i++){
            ans+=Com[count][i];
        }
    }else{
        ans+=Com[count][A-pre];
    }
    cout<<(double)sum/A<<endl;
    cout<<ans<<endl;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
#define endl '\n'
#define maxn 200005
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbg2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl;
#define dbg3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl;

int n,q,d[maxn];
struct block
{
    int s,t,x;
}b[maxn];

bool operator < (block a, block b) { return a.x > b.x;}

priority_queue<block> PQ;

bool cmp(block a, block b)
{
    return a.s < b.s;
}

int main()
{
    FASTIO
    /*
    //double start_time = clock();
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
#endif
//*/
    cin >> n >> q;
    for(int i = 1; i <= n; i++){
        cin >> b[i].s >> b[i].t >> b[i].x;
        b[i].t -= 1;
        b[i].s = b[i].s - b[i].x;
        b[i].t = b[i].t - b[i].x;
    }

    sort(b+1, b+1+n, cmp);

    for(int i = 1; i <= q; i++) cin >> d[i];
    int t = 1;
    for(int i = 1; i <= q; i++){
        while(b[t].s <= d[i] && t <= n)PQ.push(b[t]),t++;
        if(PQ.empty()){
            cout << "-1\n";
            continue;
        }

        bool flag = false;
        while(!PQ.empty()){
            block u = PQ.top();
            if(u.t < d[i]) {PQ.pop(); continue;}
            flag = true;
            cout << u.x << endl;
            break;
        }
        if(!flag) cout << "-1\n";
    }
    return 0;

    //double end_time = clock();
    //printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000);

    return 0;
}


Measured memory_bytes: 7156
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
int n,c[10001],c2[10001];
int main(){
    while(true){
        cin >> n;
        if(n == 0)
            break;
        REP(i,n)
            cin >> c[i];
        int mini = n;
        REP(i,n){
            int s = c[i];
            FOR(j,1,4){
                if(s == j)
                    continue;

                c[i] = j;

                int l = i;
                int r = i;
                int cou = 0;
                int center = c[i];
                while(true){

                    while(l - 1 >= 0 && c[l - 1] == center)l--;
                    while(r + 1 < n && c[r + 1] == center)r++;

                    if(r - l + 1  - cou < 4)break;

                    cou = r - l + 1;

                    if(l == 0 || r == n - 1 || c[l - 1] !=  c[r + 1])
                        break;
                    center = c[l - 1];
                }
                mini = min(mini,n - cou);
            }
            c[i] = s;
        }
        cout << mini << endl;
    }
    return 0;
}

Measured memory_bytes: 3036
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <cmath>
using namespace std;
#define MOD 1000000007
#define INF 1<<30
#define LINF (ll)1<<62
#define MAX 510000
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define all(x) (x).begin(),(x).end()
#define uni(q) unique(all(q)),q.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<pair<ll, ll>> vpl;

int main(){
    string a,b,c; cin >> a >> b >> c;
    if(a.back()==b[0] && b.back()==c[0]) puts("YES");
    else puts("NO");
}


Measured memory_bytes: 256
import sys

# sys.stdin = open('c1.in')


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


def read_str_list():
    return input().split()


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


def read_str():
    return input()


def solve(s):
    words = ['dream', 'dreamer', 'erase', 'eraser']
    n = len(s)
    m = n
    while m > 0:
        found = False
        for w in words:
            stop = m
            start = m - len(w)
            if start >= 0:
                if s[start:stop] == w:
                    m = start
                    found = True
                    break
        if not found:
            return False
    return True


def main():
    s = read_str()
    res = solve(s)
    if res:
        print('YES')
    else:
        print('NO')


main()


Measured memory_bytes: 3188
#include <bits/stdc++.h>
#define IO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)

using namespace std;
int N, M, T, F[31];

int main(void){
	//IO("test.in", "test.out");
	scanf("%d%d", &N, &M);
	for(int i = 0; i < N; i++){
		int k, w;
		scanf("%d", &k);
		for(int j = 0; j < k; j++) scanf("%d", &w), F[w]++;
	}
	for(int i = 1; i <= M; i++)
		if(F[i] == N) T++;
	printf("%d\n", T);
}

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

int i,j,k,h,r,mat[200][2][2],stk,ans;
int n,m;

int main(){
	while(scanf("%d",&n)*n){
		for(i=0;i<n;i++)scanf("%d %d",&mat[i][0][0],&mat[i][0][1]);
		mat[n][0][0]=9999;mat[n][0][1]=9999;
		scanf("%d",&m);
		for(i=0;i<m;i++)scanf("%d %d",&mat[i][1][0],&mat[i][1][1]);
		mat[m][1][0]=9999;mat[m][1][1]=9999;

		ans=n;h=0;r=0;
		for(i=0,j=0;j<m;j++){
			while(mat[j][1][0]>=mat[i][0][0] || mat[j][1][1]>=mat[i][0][1]){
				h=mat[i][0][0];r=mat[i][0][1];
				i++;
			}
			if(mat[j][1][0]>h && mat[j][1][1]>r){
				ans++;
				//printf("--%d %d\n",j,i);
				h=mat[j][1][0];r=mat[j][1][1];
			}
		}

		stk=m;h=0;r=0;
		for(i=0,j=0;i<n;i++){
			while(mat[i][0][0]>=mat[j][1][0] || mat[i][0][1]>=mat[j][1][1]){
				h=mat[j][1][0];r=mat[j][1][1];
				j++;
			}
			if(mat[i][0][0]>h && mat[i][0][1]>r){
				stk++;
				//printf("**%d %d\n",i,j);
				h=mat[i][0][0];r=mat[i][0][1];
			}
		}

		printf("%d\n",stk>ans?stk:ans);
	}
	return 0;
}

Measured memory_bytes: 600
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
int DP[100001][2];
int main()
{
	int N;
	cin>>N;
	vector<int> arr(N);
	int mvalue=-1;
	int negativecount=0;
	for(int i=0; i<N; i++)
	{
		cin>>arr[i];
		if(arr[i]<0)
		{
			negativecount++;
		}
		
		if(mvalue==-1)
		{
			mvalue=abs(arr[i]);
		}
		else
		{
			mvalue=min(mvalue,abs(arr[i]));
		}
		
	}
	long long ans=0;
	for(int i=0; i<N; i++)
	{
		ans+=abs(arr[i]);
	}
	if(negativecount%2!=0)
	{
		ans-=(2*abs(mvalue));
	}
	cout<<ans<<endl;
	
}

Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef unsigned long ul;
typedef pair<ul, ul> P;
#define REP(i,n) for(int i=0;i<n;i++)

int N, M;


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int R;
    cin >> R;
    if(R < 1200){
      cout << "ABC"<<endl;
    }else if (R<2800){
      cout << "ARC"<<endl;
    }else {
      cout << "AGC"<<endl;
    }
}


Measured memory_bytes: 256
N, M = map(int, input().split())
inf = float("inf")
dp = [[inf]*N for _ in [0]*N]
for i in range(N):dp[i][i] = 0
d = [list(map(int, input().split())) for i in range(M)]
ans = 0
 
for a,b,c in d:
    dp[a-1][b-1] = c
    dp[b-1][a-1] = c
for k in range(N):
    for i in range(N):
        for j in range(N):
            dp[i][j] = min(dp[i][j],dp[i][k]+dp[k][j])
for a,b,c in d:
        if dp[a-1][b-1] != c:
            ans += 1
print(ans)


Measured memory_bytes: 3572
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

ll train[500][500] = {0,};
ll train_sum[501][501] = {0,};

int main() {
    ll n, m, q;
    cin >> n >> m >> q;

    //vector<vector<ll>> train(n,vector<ll>(n,0));
    //vector<vector<ll>> train_sum(n+1,vector<ll>(n+1,0));


    for(int i = 0; i < m; i++){
        int l, r;
        cin >> l >> r;
        l--; r--;
        train[l][r]++;
    }
    for(int i = 1; i < n+1; i++){
        for(int j = 1; j < n+1; j++){
            train_sum[i][j] = train_sum[i][j-1] + train[i-1][j-1];
        }
    }
    for(int i = 1; i < n+1; i++){
        for(int j = 0; j < n+1; j++){
            train_sum[i][j] += train_sum[i-1][j];
        }
    }
    for(int i = 0; i < q; i++){
        int l, r;
        cin >> l >> r;
        cout << train_sum[r][r] - train_sum[l-1][r]  << endl;
    }

    return 0;
}




Measured memory_bytes: 4864