akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)

const int inf=1001001001;

int main ()
{
    int H,N;
    cin >> H >> N;
    vector <int> dp(H+1,inf);
    dp.at(0)=0;
    rep(i,N)
    {
        int a,b;
        cin >> a >> b;

        rep(j,H){
            int x=min(j+a,H);
            dp.at(x)=min(dp.at(x),dp.at(j)+b);
        }
    }
    cout << dp.at(H) << endl;
    return 0;
}

Measured memory_bytes: 256
from sys import stdin, stdout
from math import sin, tan, cos, pi, atan2, sqrt, acos, atan, factorial
from random import randint

n = int(stdin.readline())
a, b = map(int, stdin.readline().split())
cnt = [0, 0, 0]

values = list(map(int, stdin.readline().split()))
for v in values:
    if v <= a:
        cnt[0] += 1
    elif v <= b:
        cnt[1] += 1
    else:
        cnt[2] += 1

stdout.write(str(min(cnt)))

Measured memory_bytes: 5224
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>

using namespace std;

#define all(c) (c).begin(), (c).end()

int N, M;

int solve(vector<tuple<int, int, int> >& record){
    sort(all(record));
    vector<int> infected(N); infected[0] = 1;
    for(auto x: record){
        int src, dst;
        tie(ignore, src, dst) = x;
        infected[dst] |= infected[src];
    }
    return count(all(infected), 1);
}

int main(){
    while(cin >> N >> M, N|M){
        vector<tuple<int, int, int> > record;
        record.reserve(M);
        while(M--){
            int t, s, d;
            cin >> t >> s >> d;
            s--; d--;
            record.emplace_back(t, s, d);
        }
        cout << solve(record) << '\n';
    }
    return 0;
}

Measured memory_bytes: 1508
#include<iostream>
#include<string>
#include<set>
using namespace std;

string s;
int K;

int main(){
  cin>>s;
  cin>>K;
  set<string> st;
  int N=s.size();
  for(int i=0;i<N;i++){
    for(int j=1;j<=N-i&&j<=K;j++){
      st.insert(s.substr(i,j));
    }
  }
  auto it=st.begin();
  for(int i=0;i<K-1;i++) it++;
  cout<<*it<<endl;
  return 0;
}
  

Measured memory_bytes: 1664
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdio>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <string>
#include <string.h>
#include <algorithm>

using namespace std;
#define fast ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define pb push_back
#define sz(s) (int)s.size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)

typedef long long ll;
typedef pair<ll, ll> pii;
//const ll N = (ll)3e5 + 7;
//const ll inf = (ll)1e12 + 7;

int main(){
    fast;
    ll n,m,x,ans;
    cin>>n>>m;
    x=m;
    if(2*n<=m){
        ans=n;
        m-=2*n;
        ans+=m/4;
    }else{
        ans=m/2;
    }
    cout<<max(ans,x/4)<<endl;
    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int n = scan.nextInt();
    int ans = 0;

    for (int i = 1; i <= n; i += 2) {
      int cnt = 0;
      for (int j = 1; (j <= i) && (cnt <= 8); j++) {
        if (i % j == 0) {
          cnt++;
        }
      }
      if (cnt == 8) {
        ans++;
      }
    }

    System.out.println(ans);

  }

}

Measured memory_bytes: 21716
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n,a[105],cnt=0;
  cin>>n;
  for(int i=1;i<=n;i++)
  {
    cin>>a[i];
    if(a[i]%2==1 && i*i%2==1)
    {
      cnt++;
    }
  }
  cout<<cnt<<endl;
}


Measured memory_bytes: 3616
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,N) for(int i = 0; i < (N); i++)
#define erep(i,N) for(int i = N; i >= 0; i--)
const ll INF = 1000000000;
const ll mod = 1000000007;
#define PI 3.1415926535
using Graph = vector<vector<int>>;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true;} return false;}
bool seen[100000];
void dfs(const Graph &G, int v) {
    seen[v] = true; // v を訪問済にする
 
    // v から行ける各頂点 next_v について
    for (auto next_v : G[v]) { 
        if (seen[next_v]) continue; // next_v が探索済だったらスルー
        dfs(G, next_v); // 再帰的に探索
    }
}

//dpTable
int dp[100050];

int main(){
    int N;
    cin >> N;
    vector<ll> A(N);
    vector<ll> hoge(N,0);
    rep (i,N) {
        cin >> A.at(i);
        hoge.at(A.at(i) - 1)++;
    }
    ll a = 0;
    rep (i,N) {
        a += hoge.at(i) * (hoge.at(i) - 1) / 2;
    }
    rep (i,N) {
        ll b = a;
        b -= hoge.at(A.at(i) - 1) * (hoge.at(A.at(i) - 1) - 1) / 2;
        b += (hoge.at(A.at(i) - 1) - 1) * (hoge.at(A.at(i) - 1) - 2) / 2;
        cout << b << endl;
    }
    return 0;
}


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


int main(){
  int x ,a,b;
  cin >> x >> a >>b;
  int c = abs(x-a);
  int d =abs(x-b);
  int ans =min(c,d);
  if(ans == c){
    cout << "A" << endl;
  }
  else{
    cout <<"B" << endl;
  }
}


Measured memory_bytes: 256
import collections
N,M = map(int,input().split())
l=[]
for _ in range(N):
    a,b = map(int,input().split())
    l.append((a,b))
l.sort()
L = collections.deque(l)
ans = 0
while M>0:
    a,b = L.popleft()
    if M>=b:
        ans += a*b
    else:
        ans +=a*M
    M-=b
print(ans)

Measured memory_bytes: 78904
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1<<30;

int count(int num){
  int count = 0;
  while(num!=0){
    count += num%10;
    num /= 10;
  }
  return count;
}

int main(){
  int n, a, b;cin>>n>>a>>b;
  int ans=0;
  for(int i=1;i<=n;i++){
    int c = count(i);
    if(a<=c && c<=b){
      ans += i;
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
N = int(input())

def D(n):
  A = []
  for i in range(1,int(n**0.5)+1,1):
    if n % i == 0:
      A.append(i)
      if i != n//i:
        A.append(n//i)
  return A

a = 0

for i in D(N):
  M = N
  if i == 1:
    continue
  else:
    while M % i == 0:
      M = M//i
    if M % i == 1:
      a += 1

print(len(D(N-1))-1+a)


Measured memory_bytes: 3188
N = int(input())
S = input()

num = {}
for s in S:
    if s in num.keys():
        num[s] += 1
    else:
        num[s] = 1

ans = 1
for n in num.values():
    ans = int(ans * (n+1) % (1E9+7))

ans -= 1

print(ans)

Measured memory_bytes: 3316
for v in range(1000):
  s,t = input().split()
  s = int(s)
  t = int(t)
  if s == 0:
    break
  else:
    x=[]
    A = input().split()
    B = [int(l) for l in A]
    for i in range(s):
      for j in range(s):
        if i==j:
          pass
        else:
          w=B[i]+B[j]
          if w <= t:
             x.append(w)
          else:
            pass 
    lll=len(x)
    if lll == 0:
      print("NONE")
      continue
    else:  
      mx = max(x)
      print(mx)
      continue



Measured memory_bytes: 52580
#include <iostream>
#include <queue>
#include <utility>
using namespace std;

int a[100][100];
bool r[100];


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            cin>>a[i][j];
        }
    }
    priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > >q;
    q.push(make_pair(0,0));
    int cost=0;
    pair<int,int> e;
    while(!q.empty()) {
        e=q.top();
        q.pop();
        if(r[e.second]) continue;
        r[e.second]=true;
        cost+=e.first;
        for(int i=0; i<n; i++) {
            if(a[e.second][i]!=-1) q.push(make_pair(a[e.second][i], i));
        }
    }
    cout<<cost<<'\n';
    return 0;
}

Measured memory_bytes: 1368
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;
 
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define ROF(i,b,a) for (int i=(b);i>(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,n-1,-1)
 
typedef int i32;
typedef long long int i64;
typedef unsigned long long int ui64;
typedef float f32;
typedef double f64;
typedef long double f128;
typedef pair<int,int> Pi;
 
constexpr int INF = 1e9 + 7;
constexpr int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
 
int main() {
    int N; cin>>N;
    int x[N], y[N], h[N]; REP(i,N) cin>>x[i]>>y[i]>>h[i];
    REP(cx,101) {
        REP(cy,101) {
            bool flag = true;
            int h_tmp = -1;
            int h_tmp_max = 2*INF;
            REP(i,N) {
                int dist = abs(x[i]-cx)+abs(y[i]-cy);
                if (h[i]==0) {
                    h_tmp_max = min(h_tmp_max, dist);
                    if (h_tmp>dist) {
                        flag = false;
                        break;
                    }
                } else {
                    if (h_tmp!=-1 && h_tmp!=h[i]+dist) {
                        flag = false;
                        break;
                    }
                    h_tmp = h[i]+dist;
                }
            }
            if (flag && (h_tmp<=h_tmp_max)) {
                cout<<cx<<" "<<cy<<" "<<h_tmp<<endl;
                return 0;
            }
        }
    }
}


Measured memory_bytes: 256
class Factorial:
    def __init__(self, max_fact, mod):
        #mod should be prime number
        #using homogeneous_product(n,r), max_fact ≧ max(n+r-1)
        f = [1] * (max_fact + 1)
        for idx in range(2, max_fact + 1):
            f[idx] = f[idx - 1] * idx
            f[idx] %= mod
        fi = [pow(f[-1], mod - 2, mod)]
        for idx in range(max_fact, 0, -1):
            fi += [fi[-1] * idx % mod]
        fi = fi[::-1]
        self.mod = mod
        self.f = f
        self.fi = fi

    def factorial(self, n):
        return self.f[n]

    def factorial_inverse(self, n):
        return self.fi[n]

    def combination(self, n, r):
        f = self.f
        fi = self.fi
        return f[n] * fi[r] * fi[n - r] % self.mod

    def permutation(self, n, r):
        return self.f[n] * self.fi[n - r] % self.mod

    def homogeneous_product(self, n, r):
        f = self.f
        fi = self.fi
        return f[n + r - 1] * fi[r] * fi[n - 1] % self.mod


max_fact = 10**6
mod = 10**9 + 7
fact_instance = Factorial(max_fact, mod)
comb = fact_instance.combination
perm = fact_instance.permutation

N, M = [int(_) for _ in input().split()]
ans = 0
for p in range(N + 1):
    ans += (-1)**p * comb(N, p) * perm(M - p, N - p)
    ans %= mod
ans *= perm(M, N)
ans %= mod
print(ans)


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

using namespace std;

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

bool fill(vector<vector<int> > &v, int x, int y, int gx, int gy) {
    if(x == gx && y == gy) return true;

    int W = v.size();
    int H = v[0].size();
    int clr = v[x][y];
    v[x][y] = 0;

    for(int i = 0; i < 4; ++i) {
        int xx = x + dx[i];
        int yy = y + dy[i];

        if(0 <= xx && xx < W && 0 <= yy && yy < H) {
            if(v[xx][yy] == clr) {
                if(fill(v, xx, yy, gx, gy)) return true;
            }
        }
    }
    return false;
}

int main() {
    while(true) {
        int W, H;
        int sx, sy, gx, gy;
        int N;

        cin >> W >> H;
        if(!W && !H) break;
        cin >> sx >> sy >> gx >> gy >> N;
        --sx; --sy; --gx; --gy;

        vector<vector<int> > v(W, vector<int>(H, 0));
        for(int i = 0; i < N; ++i) {
            int c, d, x, y;
            cin >> c >> d >> x >> y;
            --x; --y;
            int w = d ? 2 : 4;
            int h = d ? 4 : 2;
            for(int dx = 0; dx < w; ++dx) 
                for(int dy = 0; dy < h; ++dy)
                    v[x+dx][y+dy] = c;
        }

        if(v[sx][sy] != 0 && fill(v, sx, sy, gx, gy)) cout << "OK" << endl;
        else cout << "NG" << endl;
    }
    return 0;
}

Measured memory_bytes: 0
import java.util.Scanner;

public class Main{

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		//n x
		String[] temp = scanner.nextLine().split(" ");
		int n = Integer.parseInt(temp[0]);
		int x = Integer.parseInt(temp[1]);

		//list 差を求める
		String[] temp2 = scanner.nextLine().split(" ");

		int ans = Math.abs(Integer.parseInt(temp2[0]) - x);
		int ne  = 0;
		int temp3 = 0;

		for(int i = 1; i < n; i++) {

			temp3 = Math.abs(Integer.parseInt(temp2[i]) - x);

			if(ans > temp3) {
				ne = ans;
				ans = temp3;
				ne = 0;
			}

			while((ne = temp3 % ans) != 0) {

				ans = temp3;
				temp3 = ne;
			}
			ne = 0;
		}
		System.out.println(ans);
	}
}


Measured memory_bytes: 46932
#include <malloc.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//ABC98C
int main(void) {
  int n, k, i, cou=1e6, tmp;
  scanf("%d", &n);
  char s[n];
  int x[n];
  scanf(" %s", s);
  x[0]=s[0]=='W' ? 1 : 0;
  for(i=1; i<n; i++)
    x[i]=(s[i]=='W') ? x[i-1]+1 : x[i-1];


  for(i=0; i<n; i++){
    //printf("x[%d]=%d\n", i, x[i]);
    tmp=0;
    if(i==0) tmp=(n-1-i-(x[n-1]-x[i]));
    else if(i==n-1) tmp=x[i-1];
    else tmp=x[i-1]+(n-1-i-(x[n-1]-x[i]));
    cou=cou>tmp ? tmp : cou;
  }
  printf("%d", cou);
}

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

const ll MOD = 1E+9 + 7;

int main() {
    ll N;
    cin >> N;

    string S;
    cin >> S;

    map<char, int> mp;
    rep(i, N) mp[S[i]]++;

    ll ans = 1;
    for (auto& x: mp) {
        ans *= x.second + 1;
        ans %= MOD;        
    }
    ans--;

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 512
// Created by martinezdiego on 27/5/19

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  int a[n];
  pair<int,int> bc[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < m; i++) {
    cin >> bc[i].first >> bc[i].second;
  }
  sort(bc, bc + m, [](pair<int,int> a, pair<int,int> b) { return a.second > b.second; });
  bool finished = false;
  for (int i = 0, p = 0; i < m and p < n and !finished; i++) {
    while (bc[i].first and p < n and !finished) {
      if (a[p] < bc[i].second) a[p] = bc[i].second;
      else finished = true;
      p++;
      bc[i].first--;
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i];
  }
  cout << ans << '\n';
  return 0;
}

Measured memory_bytes: 1408
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;
 
int main() {
  int n;
  cin >> n;
  
  vector<int> v(n);
  for (int i=0;i<n;i++){
    cin >> v[i];
  }
  
  bool flag = true;
  int count = 1;
  for(int i=1;i<n;i++){
    flag = true;
    for(int j=0;j<i;j++){
      if(v[j]>v[i]){
        flag = false;
      }
    }
    if(flag!=false){
      count+=1;
    }
  }

  std::cout << count << std::endl;

  return 0;
}

Measured memory_bytes: 256
n, x, y = map(int, input().split())
x -= 1
y -= 1
ll = y-x+1
ans = [0]*(n-1)
for i in range(n-1):
  for j in range(i+1,n):
    if j <= x or i >= y:
      l = j - i
    elif i < x and x <= j <= y:
      l = x-i+min(j-x,ll-j+x)
    elif j > y and x <= i <= y:
      l = min(y-i,ll-y+i)+j-y
    elif i < x and j > y:
      l = j - y + x - i + 1
    else:
      l = min(j-i,ll-j+i)
    ans[l-1] += 1
for item in ans:
  print(item)

Measured memory_bytes: 3444
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>

const int MAXL=105;

char s[MAXL],t[MAXL];

int main()
{
	scanf("%s%s",s,t);
	int sL=strlen(s),tL=strlen(t);
	std::sort(s,s+sL);std::sort(t,t+tL,std::greater<int>());
	puts(strcmp(s,t)<0?"Yes":"No");
	return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) (v).begin(),(v).end()
#define int long long
using namespace std;
typedef vector<int>   vint;
typedef pair<int,int> pint;

int f(int N){
    if(N<10) return N;
    return N%10+f(N/10);
}

signed main()
{
    int N; cin>>N;
    cout<<(N%f(N)==0? "Yes":"No")<<endl;
}


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

using namespace std;
int sx,sy,gx,gy,ans;
int main(){
    cin>>sx>>sy>>gx>>gy;
    for (int i=0; i<(gy-sy); i++) {
        cout<<'U';
    }
    for (int i=0; i<(gx-sx); i++) {
        cout<<'R';
    }
    for (int i=0; i<(gy-sy); i++) {
        cout<<'D';
    }
    for (int i=0; i<(gx-sx); i++) {
        cout<<'L';
    }
    cout<<'L';
    for (int i=0; i<(gy-sy)+1; i++) {
        cout<<'U';
    }
    for (int i=0; i<(gx-sx)+1; i++) {
        cout<<'R';
    }
    cout<<'D';
    cout<<'R';
    for (int i=0; i<(gy-sy)+1; i++) {
        cout<<'D';
    }
    for (int i=0; i<(gx-sx)+1; i++) {
        cout<<'L';
    }
    cout<<'U'<<endl;

}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(void){
  string s, t;
  cin >> s >> t;

  if(s.size()-t.size()==0){
  for(int i=0; i<s.size(); i++){
    cout << s[i] << t[i];
  }
  cout << endl;
  return 0;
  }

  else
  for(int i=0; i<s.size(); i++){
    if(i==s.size()-1){cout << s[i] <<endl;return 0;}
    cout << s[i] << t[i];
  }
  cout << endl;
  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
typedef struct{
    int *s;
    int cnt;
}score;
typedef struct{
    int *r;
    int cnt;
}rank;
void sort(rank *R){
    int i,tmp;
    for(i=R->cnt-1;i>0;i--){
        if(R->r[i]>R->r[i-1]){
            tmp=R->r[i];
            R->r[i]=R->r[i-1];
            R->r[i-1]=tmp;
        }else{
            break;
        }
    }
}
int main(){
    score S;
    S.cnt=0;
    rank R;
    R.cnt=0;
    
    int i;
    int n,s;
    while(1){
        scanf("%d,%d",&n,&s);
        if(n==0)break;
        if(S.cnt==0){
            S.cnt++;
            S.s=(int*)malloc(sizeof(int));
            S.s[0]=s;
        }else{
            S.cnt++;
            S.s=(int*)realloc(S.s,sizeof(int)*S.cnt);
            S.s[n-1]=s;
        }
        if(R.cnt==0){
            R.cnt++;
            R.r=(int*)malloc(sizeof(int));
            R.r[0]=s;
        }else{
            for(i=0;i<R.cnt;i++){
                if(R.r[i]==s)break;
            }
            if(i==R.cnt){
                R.cnt++;
                R.r=(int*)realloc(R.r,sizeof(int)*R.cnt);
                R.r[R.cnt-1]=s;
                sort(&R);
            }
        }
    }
    
    while(scanf("%d",&n)==1){
        for(i=0;i<R.cnt;i++){
            if(S.s[n-1]==R.r[i])printf("%d\n",i+1);
        }
    }    
    
    return 0;
}


Measured memory_bytes: 2032
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define MAX_N 1002
#define i197 1000000007

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P1;
typedef pair<int,int> Pi;
typedef pair<double,Pi> Pdi;
typedef pair<ll,int>Pli;
typedef pair<P1,ll> P2;
typedef pair<string,string> Ps;
const ll INF=10000000000000000001;
struct edge{int to,cost;};
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
struct Road{double cost;int a,b;};
struct pos{
	// 1 変数を入れる;
	ll x,cost;
};
int main() {

	int a,b;
	cin>>a>>b;
	for(int i=1; i<100001; i++){
		ll t1=i*0.08,t2=i*0.1;
		if(t1==a&&t2==b){
			cout<<i<<endl;
			return 0;
		}
	}
	cout<<"-1"<<endl;
	return 0;
}


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

#define int long long
#define endl "\n"

using namespace std;

int32_t main()
{
    int n;
    cin >> n;
    
    int cnt = 1;
    
    for(int i=0;i<n;i++)
    {
        int x;
        cin >> x;
        
        if(x == cnt)
            cnt++;
    }
    
    if(cnt == 1)
        cout << -1 << endl;
    else
        cout << n-cnt+1 << endl;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for (int i = 0; i < n; i++) {
			String train = sc.next();
			HashSet<String> trains = new HashSet<String>();
			for (int j = 1; j < train.length(); j++) {
				String sstr = train.substring(0, j);
				String estr = train.substring(j);
				String rsstr = reverse(sstr);
				String restr = reverse(estr);
				trains.add(sstr + estr);
				trains.add(estr + sstr);
				trains.add(rsstr + estr);
				trains.add(estr + rsstr);
				trains.add(sstr + restr);
				trains.add(restr + sstr);
				trains.add(rsstr + restr);
				trains.add(restr + rsstr);
			}
			System.out.println(trains.size());
		}
		sc.close();
	}

	static String reverse(String str) {
		char[] c = str.toCharArray();
		char[] rc = new char[c.length];
		for (int i = 0; i < c.length; i++)
			rc[i] = c[c.length - i - 1];
		String s = String.valueOf(rc);
		return s;
	}
}

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

using namespace std;

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


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

    int N;
    cin >> N;
    int count[13];
    for (int i = 0; i <= 12; i++) {
        count[i] = 0;
    }
    count[0]++;
    int d;
    for (int i = 0; i < N; i++) {
        cin >> d;
        count[d]++;
    }

    if (count[0] > 1) {
        cout << 0 << endl;
        return 0;
    }
    for (int i = 1; i <= 12; i++) {
        if (count[i] > 2) {
            cout << 0 << endl;
            return 0;
        }
    }

    int ans = 0;
    for (int i = 0; i < (1<<12); i++) {
        vector<int> time;
        time.push_back(0);
        for (int j = 1; j <= 12; j++) {
            if (count[j] == 1) {
                if (i & (1 << j))
                    time.push_back(j);
                else
                    time.push_back(24 - j);
            } else if (count[j] == 2) {
                time.push_back(j);
                time.push_back(24 - j);
            }
        }

        int mi = INT_MAX;
        for (int j = 0; j < time.size(); j++) {
            for (int k = j + 1; k < time.size(); k++) {
                int d = abs(time[j] - time[k]);
                mi = min(mi, min(d, 24 - d));
            }
        }

        ans = max(ans, mi);
    }

    cout << ans << endl;

    return 0;
}


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

int main(){
    int W, H, N; cin >> W >> H >> N;
    int a;
    int Wmin = 0, Wmax = W, Hmin = 0, Hmax = H;
    for (int i = 0; i < N; i++)
    {
        cin >> W >> H >> a;
        switch(a){
            case 1:
                Wmin = max(Wmin, W);
                break;
            case 2:
                Wmax = min(Wmax, W);
                break;
            case 3:
                Hmin = max(Hmin, H);
                break;
            case 4:
                Hmax = min(Hmax, H);
                break;
            default:
                break;
        }

    }

    cout << max(0, (Wmax-Wmin)) * max(0, (Hmax-Hmin)) << endl;
    
    

    return 0;
}

Measured memory_bytes: 256
#include<algorithm>     //sort,二分探索,など
#include<bitset>        //固定長bit集合
#include<cmath>         //pow,logなど
#include<complex>       //複素数
#include<deque>         //両端アクセスのキュー
#include<fstream>       //ファイルストリーム（標準入力変更用）
#include<functional>    //sortのgreater
#include<iomanip>       //setprecision(浮動小数点の出力の誤差)
#include<iostream>      //入出力
#include<iterator>      //集合演算(積集合,和集合,差集合など)
#include<map>           //map(辞書)
#include<numeric>       //iota(整数列の生成),gcdとlcm(c++17)
#include<queue>         //キュー
#include<set>           //集合
#include<stack>         //スタック
#include<string>        //文字列
#include<unordered_map> //イテレータあるけど順序保持しないmap
#include<unordered_set> //イテレータあるけど順序保持しないset
#include<utility>       //pair
#include<vector>        //可変長配列

//名前
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef map<string, int> msi;
typedef map<string, ll> msll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pllll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<vector<string>> vvs;
typedef vector<vector<bool>> vvb;

//定数
const ll MOD = 1000000007;
const ll INF = 1000000000000000000;

//マクロ
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,s,e) for(int i=s;i<e;i++)
#define repse(i,s,e) for(int i=s;i<=e;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define in(x1) cin >> x1
#define in2(x1, x2) cin >> x1 >> x2
#define in3(x1, x2, x3) cin >> x1 >> x2 >> x3
#define inN(x, N) rep(i, N) in(x[i])
#define outl(x) cout << x << endl
#define out2l(x, y) cout << x << " " << y << endl 

//よく使う関数
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()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    //標準入力をファイルに変更
    //std::ifstream in("input.txt");
    //std::cin.rdbuf(in.rdbuf());
    int A, B;
    in2(A, B);

    outl(A * B);

    return 0;
}

Measured memory_bytes: 3620
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    PrintWriter out = new PrintWriter(outputStream);
    TaskE solver = new TaskE();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskE {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
      int n = in.nextInt();
      int k = in.nextInt();
      int[] a = new int[n];
      for (int i = 0; i < n; i++) {
        a[i] = in.nextInt();
      }
      int[] dp = new int[n];
      Arrays.fill(dp, (int) 1e9 +  100);
      dp[0] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n && j <= i + k; j++) {
          dp[j] = Math.min(dp[j], dp[i] + Math.abs(a[j] - a[i]));
        }
      }
      out.println(dp[n - 1]);
    }

  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream), 32768);
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

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

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

  }
}


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

int main() {
  int N; 
  char S;
  int color[4] = {0, 0, 0, 0};
  int type = 0;
  
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S;
    if (S == 'P')
      color[0] += 1;
    else if (S == 'W')
      color[1] += 1;
    else if (S == 'G')
      color[2] += 1;
    else if (S == 'Y')
      color[3] += 1;
  }

  for (int i = 0; i < 4; i++) {
    if (color[i] > 0)
      type += 1;
  }
  
  if (type == 3)
    cout << "Three" << endl;
  else if (type ==4)
    cout << "Four" << endl;

}

Measured memory_bytes: 256
#![allow(non_snake_case)]

fn main() {
    loop {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let mut iter = buf.split_whitespace();
        let H = iter.next().unwrap().parse::<usize>().unwrap();
        let W = iter.next().unwrap().parse::<usize>().unwrap();

        if H == 0 && W == 0 {break;}

        for _ in 0..H {
            for _ in 0..W {
                print!("#");
            }
            println!();
        }
        println!();
    }
}



Measured memory_bytes: 5032
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
struct rollinghash{
  string S;
  ull B;
  vector<ull> hash,p;
  int len;
  rollinghash(){}
  rollinghash(string S,ull B):S(S),B(B){init();};
  void init(){
    len=S.length();
    hash.resize(len+1);
    p.resize(len+1);
    hash[0]=0;p[0]=1;
    for(int i=0;i<len;i++){
      hash[i+1]=hash[i]*B+S[i];
      p[i+1]=p[i]*B;
    }
  }
  ull find(int l,int r){
    return hash[r]-hash[l]*p[r-l];
  }
};
typedef pair<ull,ull> P;
int main(){
  string s,t;
  cin>>s>>t;
  ull B=1000000007LL,C=1777771LL;
  int sl=s.length(),tl=t.length();
  rollinghash r1(s,B),r2(s,C);
  vector<P> v(sl-tl+1);
  for(int i=0;i<=sl-tl;i++) v[i]=P(r1.find(i,i+tl),r2.find(i,i+tl));
  sort(v.begin(),v.end());
  //for(int i=0;i<=sl-tl;i++) cout<<v[i].first<<" "<<v[i].second<<endl;
  //cout<<endl;
  ull h1=0,p1=1,h2=0,p2=1,ans=0;
  for(int i=0;i<tl;i++) h1=h1*B+t[i],h2=h2*C+t[i];
  for(int i=tl-1;i>=0;i--){
    h1-=p1*t[i];
    h2-=p2*t[i];
    for(int j='a';j<='z';j++){
      if(j==t[i]) continue;
      h1+=p1*j;
      h2+=p2*j;
      //cout<<h1<<endl;
      ans+=upper_bound(v.begin(),v.end(),P(h1,h2))
	-lower_bound(v.begin(),v.end(),P(h1,h2));
      h1-=p1*j;
      h2-=p2*j;
    }
    for(int j='A';j<='Z';j++){
      if(j==t[i]) continue;
      h1+=p1*j;
      h2+=p2*j;
      //cout<<h1<<endl;
      ans+=upper_bound(v.begin(),v.end(),P(h1,h2))
	-lower_bound(v.begin(),v.end(),P(h1,h2));
      h1-=p1*j;
      h2-=p2*j;
    }
    h1+=p1*t[i];
    h2+=p2*t[i];
    p1*=B;
    p2*=C;
  }
  cout<<ans<<endl;
  return 0;
}

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

using namespace std;

const int N = 100010;

int p[N];

int find(int x) {
  if (p[x] != x) {
    p[x] = find(p[x]);
  }
  return p[x];
}

int a[N];
vector <int> g[N];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  if (m == n - 1) {
    puts("0");
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < n; ++i) {
    p[i] = i;
  }
  for (int i = 0; i < m; ++i) {
    int x, y;
    scanf("%d %d", &x, &y);
    p[find(x)] = find(y);
  }
  for (int i = 0; i < n; ++i) {
    g[i].clear();
  }
  for (int i = 0; i < n; ++i) {
    g[find(i)].push_back(a[i]);
  }
  vector <int> b;
  int rem = 0;
  long long ans = 0;
  for (int i = 0; i < n; ++i) {
    int sz = (int)g[i].size();
    if (sz == 0) {
      continue;
    }
    sort(g[i].begin(), g[i].end());
    ans += g[i][0];
    for (int j = 1; j < sz; ++j) {
      b.push_back(g[i][j]);
    }
    ++rem;
  }
  sort(b.begin(), b.end());
  reverse(b.begin(), b.end());
  for (int i = 0; i < n; ++i) {
    if (!g[i].empty()) {
      ans -= g[i][0];
      b.push_back(g[i][0]);
      break;
    }
  }
  reverse(b.begin(), b.end());
  --rem;
  for (int e : b) {
    ans += e;
    --rem;
    if (rem == 0) {
      break;
    }
  }
  if (rem > 0) {
    puts("Impossible");
  } else {
    printf("%lld\n", ans);
  }
}


Measured memory_bytes: 6528
#include <cstdio>
#include <bitset>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 100032;
inline void Min(int &a, int b) {if (a > b) a = b;}
typedef vector<pair<int,int>> vec;

struct sort_t {
	int w, i;
} A[N], B[N];
bitset<N> S;

int main() {
	int n;
	scanf("%d",&n);
	for (int i = 0; i < n; i++) {
		scanf("%d%d",&A[i].w,&B[i].w);
		A[i].i = B[i].i = i;
	}
	sort(A, A+n, [](sort_t x, sort_t y){return x.w < y.w;});
	sort(B, B+n, [](sort_t x, sort_t y){return x.w > y.w;});
	int i = 0;
	long long ans = 0;
	while (i < n && A[i].w < B[i].w) {
		S.flip(A[i].i);
		S.flip(B[i].i);
		ans += A[i++].w;
	}
	if (!i || i == n || S.any()) {
		while (i < n) ans += B[i++].w;
		printf("%lld\n",ans);
		return 0;
	}
	vec v1(i==1 || A[i-1].i != B[i-1].i ? vec{{A[i-1].w, B[i-1].w}}
			: vec{{A[i-1].w, B[i-2].w}, {A[i-2].w, B[i-1].w}}),
		v2(i==n-1 || A[i].i != B[i].i ? vec{{A[i].w, B[i].w}}
			: vec{{A[i].w, B[i+1].w}, {A[i+1].w, B[i].w}});
	int cur = 0x7fffffff;
	for (auto i : v1)
		for (auto j : v2)
			Min(cur, min(i.first, j.second) + min(j.first, i.second)
					-min(i.first, i.second) - min(j.first, j.second));
	ans += cur;
	while (i < n) ans += B[i++].w;
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 1792
from collections import deque
N=int(input())
As=[int(i) for i in input().split()]
neg_odd,neg_even=deque([]),deque([])
S_odd,S_even=0,0
for i,A in enumerate(As):
  if i%2==0:
    if A>=0:
      S_even+=A
    else:
      neg_even.append(i)
  if i%2==1:
    if A>=0:
      S_odd+=A
    else:
      neg_odd.append(i)
ans=[]
A_size=N
if S_odd==S_even==0:
  a=max(As)
  print(a)
  i=0
  while As[i]!=a:
    ans.append(1)
    i+=1
    A_size-=1
  while A_size>1:
    ans.append(A_size)
    A_size-=1
elif S_odd<=S_even:
  print(S_even)
  if N%2==0:
    ans.append(N)
    A_size-=1
  if neg_even:
    t=0
    while A_size-1==neg_even[-1]:
      neg_even.pop()
      ans.append(A_size)
      ans.append(A_size-1)
      A_size-=2
      if not neg_even:
        break
    if neg_even:
      while t==neg_even[0]:
        neg_even.popleft()
        ans.append(1)
        ans.append(1)
        t+=2
        A_size-=2
        if not neg_even:
          break
    for a in neg_even:
      ans.append(a+1-t)
      A_size-=2
      t+=2
  while A_size>1:
    ans.append(2)
    A_size-=2
else:
  print(S_odd)
  if N%2==1:
    ans.append(N)
    A_size-=1
  ans.append(1)
  A_size-=1
  if neg_odd:
    t=1
    while A_size==neg_odd[-1]:
      neg_odd.pop()
      ans.append(A_size)
      ans.append(A_size-1)
      A_size-=2
      if not neg_odd:
        break
    if neg_odd:
      while t==neg_odd[0]:
        neg_odd.popleft()
        ans.append(1)
        ans.append(1)
        t+=2
        A_size-=2
        if not neg_odd:
          break
    for a in neg_odd:
      ans.append(a-t+1)
      A_size-=2
      t+=2
  while A_size>1:
    ans.append(2)
    A_size-=2
print(len(ans))
for a in ans:
  print(a)

Measured memory_bytes: 40432
#include <iostream>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <cmath>
#define INF 100000000000
#define MOD 1000000007
#define ll long long int
//#define int long long int
using namespace std;

signed main(){
    int n,k;
    cin>>n>>k;
    int ans = n;
    ans -= k;
    if(k==1) {cout<<0<<endl;return 0;}

    cout<<n-k<<endl;

    return 0;
}

Measured memory_bytes: 256
N = int(input())
S = list(input())
cnt = (S[1:]).count("E")
ans = cnt
for i in range(1,N):
  if S[i] == 'E':
    cnt -= 1
  if S[i-1] == 'W':
    cnt += 1
  ans = min(ans,cnt)
print(ans)

Measured memory_bytes: 8140
<?php
$n = trim(fgets(STDIN));
for($i = 0; $i < $n; $i++) {
	echo "Case " . ($i + 1) . ":\n";
	$s = trim(fgets(STDIN));
	for ($j = 0; $j < 10; $j++) {
		$s = middleSquare($s);
		echo intval($s) . "\n";
	}
}

function middleSquare($m) {
	$square = sprintf("%08d", $m * $m);
	$middle = substr($square, 2, 4);
	return $middle;
}

Measured memory_bytes: 6592
use std::cmp;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::io::Read;
use std::usize::MAX;

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

fn solve() {
    input! {
        k: usize,
        n: usize,
        a: [usize;n]
    }

    let mut farest_start = 0;
    let mut farest_distance = 0;

    for i in (1..n).rev() {
        if a[i] - a[i - 1] > farest_distance {
            farest_distance = a[i] - a[i - 1];
            farest_start = i - 1;
        }
    }

    if (k - a[n - 1]) + a[0] > farest_distance {
        farest_distance = (k - a[n - 1]) + a[0];
        farest_start = n - 1;
    }

    println!("{}", k - farest_distance);
}

fn main() {
    let thd = std::thread::Builder::new().stack_size(104_857_600);
    thd.spawn(|| solve()).unwrap().join().unwrap();

    /*
    // 入力を一括で読み込む場合
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut input = buf.split_whitespace();

    // inputに対しnext()で読み込んでいく
    let q: usize = input.next().unwrap().parse().unwrap();
    */
}

const LARGE_PRIME: u64 = 1_000_000_007;

// @〜でsnippet


Measured memory_bytes: 10620
package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 10000000
)

var (
	sc = bufio.NewScanner(os.Stdin)
	wt = bufio.NewWriter(os.Stdout)
)

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

func nextInt() int {
	i, _ := strconv.Atoi(next())
	return i
}

func nextInt3() (int, int, int) {
	return nextInt(), nextInt(), nextInt()
}

func nextInts(n int) []int {
	slice := make([]int, n)
	for i := 0; i < n; i++ {
		slice[i] = nextInt()
	}
	return slice
}

func puts(a ...interface{}) {
	fmt.Fprintln(wt, a...)
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, initialBufSize), maxBufSize)
	defer wt.Flush()

	n, m, x := nextInt3()
	c, a := make([]int, n), make([][]int, n)
	for i := 0; i < n; i++ {
		c[i], a[i] = nextInt(), nextInts(m)
	}

	get := func(bit, i uint) bool {
		return ((bit)>>(i))&1 == 1
	}

	enough := func(aSum []int) bool {
		for i := range aSum {
			if aSum[i] < x {
				return false
			}
		}
		return true
	}

	ans := -1
	bitMax := (1 << uint(n)) - 1
	for bit := 1; bit <= bitMax; bit++ {
		cSum := 0
		aSum := make([]int, m)
		for i := 0; i < n; i++ {
			if get(uint(bit), uint(i)) {
				cSum += c[i]
				for j := range aSum {
					aSum[j] += a[i][j]
				}
			}
		}

		if enough(aSum) && (ans == -1 || ans > cSum) {
			ans = cSum
		}
	}

	puts(ans)
}


Measured memory_bytes: 2168
#include <cstdio>
#include <algorithm>
#include<cmath>
typedef long long ll;
using namespace std;
int main(){
    int t,b,c;
    ll s,i;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&b,&c);
        s=1ll*b*c;
        i=sqrt(s)+1;
        while(i*i>=s) i--;
		ll ans=i*2-2;
		if(i*(i+1)<s) ans++;
		if(b==c) ans++;
        printf("%lld\n",ans);
    }
    return 0;
}


Measured memory_bytes: 128
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

typedef long long LL;
const int N=200009;
int del[N],head[N],cnt,n,point[N],siz[N],tot,pos,fa[N];
LL ans,Min,dep[N];
struct Edge
{
	int nxt,to,w;
}g[N*2];
struct P
{
	int x,y;
	LL z;
	bool operator < (const P &A)const
	{
		return z<A.z;
	}
}a[N*60];

void add(int from,int to,int w)
{
	g[++cnt].nxt=head[from];
	g[cnt].to=to;
	g[cnt].w=w;
	head[from]=cnt;
}

void init()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
		scanf("%d",&point[i]);
	int x,y,z;
	for (int i=1;i<n;i++)
		scanf("%d %d %d",&x,&y,&z),add(x,y,z),add(y,x,z);
}

void dfs(int x,int fa)
{
	siz[x]=1;
	for (int i=head[x];i;i=g[i].nxt)
	{
		int v=g[i].to;
		if(v==fa||del[v])
			continue;
		dfs(v,x);
		siz[x]+=siz[v];
	}
}

int Get_Weight(int x)
{
	dfs(x,-1);
	int k=siz[x],fa=-1;
	while(1)
	{
		int tmp=0;
		for (int i=head[x];i;i=g[i].nxt)
		{
			int v=g[i].to;
			if(v==fa||del[v])
				continue;
			if(siz[tmp]<siz[v])
				tmp=v;
		}
		if(siz[tmp]<=k/2)
			return x;
		fa=x,x=tmp;
	}
}

void DFS(int x,int fa)
{
	if(dep[x]+point[x]<Min)
		Min=dep[x]+point[x],pos=x;
	for (int i=head[x];i;i=g[i].nxt)
	{
		int v=g[i].to;
		if(v==fa||del[v])
			continue;
		dep[v]=dep[x]+g[i].w;
		DFS(v,x);
	}
}

void DFS_1(int x,int fa)
{
	a[++tot]=(P){pos,x,Min+dep[x]+point[x]};
	for (int i=head[x];i;i=g[i].nxt)
	{
		int v=g[i].to;
		if(v==fa||del[v])
			continue;
		DFS_1(v,x);
	}
}

void Get_Ans(int x)
{
	dep[x]=0,Min=1LL<<60,pos=-1; 
	DFS(x,-1);
	DFS_1(x,-1);
}

void conquer(int x)
{
	int w=Get_Weight(x);
	Get_Ans(w);
	del[w]=1;
	for (int i=head[w];i;i=g[i].nxt)
	{
		int v=g[i].to;
		if(del[v])
			continue;
		conquer(v);
	}
}

int find(int x)
{
	if(x==fa[x])
		return x;
	return fa[x]=find(fa[x]);
}

void kruskal()
{
	sort(a+1,a+1+tot);
	for (int i=1;i<=n;i++)
		fa[i]=i;
	for (int i=1;i<=tot;i++)
	{
		int A=find(a[i].x),B=find(a[i].y);
		if(A!=B)
		{
			ans+=a[i].z;
			fa[A]=B;
		}
	}
}

void work()
{
	conquer(1);
	kruskal();
	printf("%lld\n",ans);
}

int main()
{
	init();
	work();
	return 0;
}


Measured memory_bytes: 74112
#include<stdio.h>
int main(void){
  int H,W,i,j;
  while(1){
    scanf("%d %d",&H,&W);
    if(H==0 && W==0){
        break;
    }
    for(i=0;i<H;i++){
      for(j=0;j<W;j++){
	    if(j%2==1){
	      if(i%2==1){
	          printf("#");
	      }else{
	          printf(".");
	      }
	    }else{
            if(i%2==1){
                printf(".");
            }else{
                printf("#");
            }
	        }
	    }
        printf("\n");
        }
    printf("\n");
	}
  return 0;
}


Measured memory_bytes: 2088
#include <bits/stdc++.h>
using namespace std;
int main() {
    #ifdef DEBUG
    std::ifstream in("/home/share/inputf.in");
    std::cin.rdbuf(in.rdbuf());
    #endif
    int N, M, X;
    int a = 0, b = 0;
    cin >> N >> M >> X;
    for(int i = 0; i < M; i++) {
        int tmp;
        cin >> tmp;
        if(tmp < X) {
            a++;
        } else {
            b++;
        }
    }
    cout << min(a, b) << endl;
    return 0;
}

Measured memory_bytes: 256
#ifndef ___CLASS_MODINT
#define ___CLASS_MODINT

#include <cstdint>

template<std::uint32_t mod>
class modint {
private:
	std::uint32_t n;
public:
	modint() : n(0) {};
	modint(std::uint64_t n_) : n(n_ % mod) {};
	bool operator==(const modint& m) const { return n == m.n; }
	bool operator!=(const modint& m) const { return n != m.n; }
	std::uint32_t get() const { return n; }
	modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; }
	modint operator+(const modint& m) const { return modint(*this) += m; }
	modint operator-(const modint& m) const { return modint(*this) -= m; }
	modint operator*(const modint& m) const { return modint(*this) *= m; }
	modint binpow(std::uint64_t b) const {
		modint ans = 1, m = modint(*this);
		while (b) {
			if (b & 1) ans *= m;
			m *= m;
			b >>= 1;
		}
		return ans;
	}
	modint inv() { return (*this).binpow(mod - 2); }
};

#endif // ___CLASS_MODINT

#include <vector>
#include <iostream>
using namespace std;
using modulo = modint<1000000007>;
long long tenpow(int X) {
	return X == 0 ? 1 : tenpow(X - 1) * 10;
}
const int first_lim = 7;
int main() {
	int N;
	cin >> N;
	int primary_lim = tenpow(first_lim);
	int second_lim = primary_lim + N / (first_lim + 1) + 2;
	vector<long long> SEQ(second_lim);
	long long mul = 1;
	for (int i = 1; i <= first_lim + 1; ++i) {
		for (int j = mul; j < mul * 10 && j < second_lim; ++j) {
			SEQ[j] = i;
		}
		mul *= 10;
	}
	modulo ans = 0;
	int r = 1, s = 0;
	for (int i = 1; i < primary_lim; ++i) {
		while (s < N) s += SEQ[r++];
		if (s == N) ans += 1;
		s -= SEQ[i];
	}
	for (int i = 2; i <= N; ++i) {
		if (N % i == 0) continue;
		if (N / i <= first_lim) break;
		ans += 1;
	}
	for (int i = first_lim + 1; i <= N / 2; ++i) {
		if (N % i == 0) {
			ans += modulo(10).binpow(i - 1) * 9 - modulo(N / i) + 1;
		}
	}
	if (N >= first_lim + 1) ans += modulo(10).binpow(N - 1) * 9;
	cout << ans.get() << endl;
	return 0;
}

Measured memory_bytes: 176000
#include <atcoder/convolution>
#include <iostream>
#include <vector>
#define rep(i, j, n) for (int i = 0; i < n; ++i)
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, m) cin >> b[i];
  const auto c = atcoder::convolution(a, b);
  rep(i, 0, n + m - 1) cout << c[i] << '\n';
  return 0;
}

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> ds(N, -1);
  bool isExist = true;
  rep(i, M) {
    int s, c;
    cin >> s >> c;
    s--;
    if (ds[s] == -1) {
      ds[s] = c;
    } else if (ds[s] != c) {
      isExist = false;
    }
  }
  if (!isExist) {
    cout << -1 << endl;
    return 0;
  }
  if (2 <= N) {
    int sum = 0;
    if (ds[0] == 0) {
      cout << -1 << endl;
      return 0;
    } else if (ds[0] == -1) {
      sum = 1;
    } else {
      sum = ds[0];
    }

    for (int i = 1; i < N; i++) {
      if (ds[i] == -1) {
        sum = sum * 10;
      } else {
        sum = sum * 10 + ds[i];
      }
    }
    cout << sum << endl;
    return 0;
  } else {
    if (ds[0] == -1) {
      cout << 0 << endl;
    } else {
      cout << ds[0] << endl;
    }
  }
  return 0;
}

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


void solve(std::string S){
    if (S == "Sunny") cout << "Cloudy" << endl;
    else if (S == "Cloudy") cout << "Rainy" << endl;
    else if (S == "Rainy") cout << "Sunny" << endl;
}

int main(){
    std::string S;
    std::cin >> S;
    solve(S);
    return 0;
}


Measured memory_bytes: 3564