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

const int MAX_N = 1e5;
int par[MAX_N + 10];
int size[MAX_N + 10];
int rankrank[MAX_N + 10];

void init(int n){
    for (int i = 0; i < n; i++){
        par[i] = i;
        size[i] = 1;
        rankrank[i] = 0;
    }
}

int getRoot(int x){
    if (par[x] == x){
        return x;
    }else{
        return par[x] = getRoot(par[x]);
    }
}

void unite(int x, int y){
    x = getRoot(x);
    y = getRoot(y);
    if (x == y) return;
    if (rankrank[x] == rankrank[y]) {
        par[y] = x;
        size[x] = size[x] + size[y];
        rankrank[x]++;  
    }else if (rankrank[x] < rankrank[y]) {
        par[x] = y;
        size[y] = size[x] + size[y];
    }else if (rankrank[x] > rankrank[y]) {
        par[y] = x;
        size[x] = size[x] + size[y];
    }
}

int getSize(int x){
    return size[getRoot(x)];
}

bool isSame(int x, int y){
    return getRoot(x) == getRoot(y);
}

int main(){
	int N, M;
	cin >> N >> M;
	init(N);
	for (int i = 0; i < M; ++i){
		int x, y, z;
		cin >> x >> y >> z;
		--x; --y;
		unite(x,y);
	}

	set<int> s;
	int ans = 0;
	for (int i = 0; i < N; ++i){
		s.insert(getRoot(i));
	}
    for (auto ite = s.begin(); ite != s.end(); ++ite){
    	ans++;
    }
    cout << ans <<endl;

}

Measured memory_bytes: 6144
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <cstring>

using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using PP = pair<ll, pair<ll,ll>>;
#define rep(i,n) for (ll i = 0; i < (n); ++i)

ll x, k, d, ans;

int main() {
    cin >> x >> k >> d;

    x = abs(x);

    if ( (ll)x/d < k ){
        if( (k-(ll)x/d)%2 ){
            ans = d-(x%d);
        }
        else {
            ans = x%d;
        }
    }
    else {
        if ( (ll)x/d == k){
            ans = x%d;
        }
        else {
            ans = x-(k*d);
        }
    }

    ans = abs(ans);

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3624
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;

namespace atcorder2
{
    class Program
    {
        public static long Read()
        {
            return long.Parse(Console.ReadLine());
        }

        public static long[] Reads()
        {
            return Console.ReadLine().Split().Select(long.Parse).ToArray();
        }


        public static long gcd(long x,long y)
        {
            if (y < x) gcd(y, x);
            while (y != 0)
            {
                var z = x % y;
                x = y;
                y = z;
            }
            return x;
        }
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);

            var n = Read();
            var s = Console.ReadLine();
            long r = 0;
            long g = 0;
            long b = 0;
            long ans = 0;
            for (int i = 0; i < n; i++)
            {
                if (s[i] == 'R') r++;
                if (s[i] == 'G') g++;
                if (s[i] == 'B') b++;
            }
            for (int i = 0; i < n; i++)
            {
                if (s[i] == 'R') { r--; ans += b * g; }
                if (s[i] == 'G') { g--; ans += r * b; }
                if (s[i] == 'B') { b--; ans += r * g; }
            }
            for (int i = 0; i <n; i++)
            {
                for (int j = 1; j <n/2.0; j++)
                {
                    if (i + 2 * j < n && ((s[i] == 'R' && s[i + j] == 'G' && s[i + 2 * j] == 'B') || (s[i] == 'R' && s[i + j] == 'B' && s[i + 2 * j] == 'G')))
                        ans--;
                    if (i + 2 * j < n&&((s[i] == 'B' && s[i + j] == 'R' && s[i + 2 * j] == 'G') || (s[i] == 'B' && s[i + j] == 'G' && s[i + 2 * j] == 'R')))
                        ans--;
                    if (i + 2 * j < n&&((s[i] == 'G' && s[i + j] == 'R' && s[i + 2 * j] == 'B') || (s[i] == 'G' && s[i + j] == 'B' && s[i + 2 * j] == 'R')))
                        ans--;
                }
            }
            
            Console.WriteLine(ans);





            Console.Out.Flush();
        }
    }
}


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

using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	int n, d;
	cin >> n >> d;
	int cnt = 0;
	for (int i = 0; i < n; ++i)
	{
		long long int a, b;
		cin >> a >> b;
		long double dist = sqrt(a * a * 1.0 + b * b * 1.0);
		if ((long double)d >= dist)
			cnt++;
	}
	cout << cnt;
}

Measured memory_bytes: 3672
#include<iostream>
#include<algorithm>
#include<sstream>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)

using namespace std;

string line;

string itos(int i){ stringstream ss; ss << i; return ss.str();}

void compute()
{
  int cnt = 0;
  string nline;
  while(line.size() > 1)
    {
      line = itos(line[0]-'0'+line[1]-'0') + line.substr(2,line.size()-2);
      cnt++;
    }
  cout << (cnt%2?"Fabre wins.":"Audrey wins.") << endl;
}

int main()
{
  int T;
  while(cin >>T)
    rep(_,T)
      {
	cin >> line;
	compute();
      }
  return 0;
}

Measured memory_bytes: 1228
#include <iostream>
using namespace std;
int a,b,c,p;
void prim(int x,int f){
    if(f*f==x)cout<<"Yes";
    else if(f*f>x)cout<<"No";
          else prim(x,f+1);}
int main()
{
    cin>>a>>b;c=b,p=1;while(b!=0)p*=10,b/=10;
    prim(a*p+c,1);
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <tuple>
#include <algorithm>
#include <numeric>
#include <climits>
#include <map>
#include <functional>
#include <cmath>
#include <cassert>

using namespace std;

int main()
{
    int N;
    long K;
    cin >> N >> K;
    map<int,long> m;
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        m[a] += b;
    }
    int ans;
    for (auto it = m.begin(); it != m.end(); it++) {
        if (it->second >= K) {
            ans = it->first;
            break;
        }
        K -= it->second;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 4224
N,M,C = list(map(int,input().split()))
B = list(map(int,input().split()))
A = [list(map(int,input().split())) for i in range(N)]
ans = 0
c = 0
for i in range(N):
    ans = 0
    for j in range(M):
        ans += A[i][j]*B[j]
    if 0 < ans + C:
        c += 1
print(c)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(long long i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;
const ll INF = 1E+18;
const ll MAX_S = 2505;
int x[12][12];

int main()
{
  int n, d;
  cin >> n >> d;
  rep(i, n) rep(j, d) {
    cin >> x[i][j];
  }
  int ans = 0;
  rep(i, n-1) {
    for (int j=i+1; j<n; j++) {
      int dist2 = 0;
      rep(k, d) {
        dist2 += (x[i][k]-x[j][k])*(x[i][k]-x[j][k]);
      }
      int r = 1;
      bool ok = false;
      while (r*r<=dist2) {
        if (r*r==dist2) ok = true;
        r++;
      }
      if (ok) ans ++;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
    string s;
    cin >> s;
    int ans=0, cnt=0;
    for ( char c:s ) {
        if ( c=='T' ) {
            if ( cnt>0 ) cnt--;
            else ans++;
        } else {
            cnt++;
        }
    }
    cout << ans+cnt << '\n';
    return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 998244353;

vector<ll>to[100005];
vector<ll>point;
vector<ll>check;
vector<ll>res(100005);
ll x,y;

void dfs(ll v, ll p=-1){
  
  res[x]++;
  check[v]=x;
  //cout << check[v] << endl;
  //cout << x << " " << point[v] << endl;
  for(int i=0; i<to[v].size(); i++){
    ll u=to[v][i];
    if(u==p) continue;
    if(check[u]!=-1) continue;
    dfs(u,v);
  }
  
}


int main(){
  ll n,m,k;
  cin >> n >> m >> k;
  
  point.resize(n);
  check.resize(n,-1);
  
  for(int i=0; i<m; i++){
    ll a,b;
    cin >> a >> b;
    a--; b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  
  vector<ll>noto[n];
  for(int i=0; i<k; i++){
    ll a,b;
    cin >> a >> b;
    a--; b--;
    noto[a].push_back(b);
    noto[b].push_back(a);
  }
  
  for(int i=0; i<n; i++){
    if(check[i]!=-1){
      point[i]=res[check[i]];
      continue;
    }
    x=i;
    dfs(i);
    point[i]=res[i];
  }
  
  for(int i=0; i<n; i++){
    point[i]-=to[i].size();
    for(int j=0; j<noto[i].size(); j++){
     if(check[i]==check[noto[i][j]])point[i]--;
    }
    cout << point[i]-1 << " " ;
  }
  
  
}
  
  
  
  


Measured memory_bytes: 15872
#include <bits/stdc++.h>
typedef long long int ll;
# define pi 3.14159265358979323846 

using namespace std;


int main() {
	// your code goes here
	ll n,k;
	cin>>n>>k;
	ll M=1e9+7;
	ll ans=0;
	for(ll i=k;i<=n+1;i++)
	{
	    ans+=(i*(n-i+1))+1;
	    ans=ans%M;
	}
	cout<<ans;
	
	
	
	
	return 0;
}


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

int main(){
    string s, t;
    cin >> s;
    cin >> t;

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

Measured memory_bytes: 256
# input
s = input()

ans = ''

for i in range(len(s)):
    if s[i] == '0':
        ans += '0'
    if s[i] == '1':
        ans += '1'
    if s[i] == 'B' and ans != '':
        ans = ans[:len(ans) - 1]

print(ans)

Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,j,k,x,y,ans=0,curr;
    scanf("%d",&n);
    scanf("%d",&k);
    ans = -1000002;
    curr = n*k;
    ans = max(ans,curr);
    curr = n+k;
    ans = max(ans,curr);
    curr = n-k;
    ans = max(ans,curr);
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 256
from sys import exit, stderr, stdin
input = stdin.readline
# setrecursionlimit(10**7)

def debug(var, name="hoge"):
    print(name +":" + str(type(var)) + " = " + repr(var), file=stderr)
    return


def main():
    N = int(input())
    A = [int(n) for n in input().split()]
    B = [int(n) for n in input().split()]
    ans = 0
    for i in range(N, -1, -1):
        # print(A)
        # print(B)
        if A[i] > 0:
            if i != N:
                beat = min(B[i],A[i])
                ans += beat
                A[i]-=beat
                B[i]-=beat
                if A[i] == 0:
                    continue
            beat = min(B[max(0,i-1)],A[i])
            ans += beat
            A[i]-=beat
            B[max(0,i-1)]-=beat
    # print(A)
    # print(B)
    print(ans)







if __name__ == "__main__":
    main()


Measured memory_bytes: 19376
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define PI (acos(-1))
#define ct cout<<
#define cn cin>>
#define spc <<" "<<
#define nl puts("")
#define _fl(x) puts("FLAG " #x);
#define  _(x)  cout<< #x <<" is "<< x <<endl;
#define fs first
#define sc second
#define pb push_back
#define all(v)  (v).begin(), (v).end()
#define _109 (1000000000)
#define _0(arr) memset(arr,0,sizeof ( arr ) )
#define _1(arr) memset(arr,-1,sizeof ( arr ) )
#define _ninp(n,arr) for(int i=0; i<n;i++)cin>>arr[i];
#define _nout(n,arr) for(int i=0; i<n;i++)cout<<arr[i]<<" \n"[i==n-1];
double _eps=1e-6;
long long gcd(long long a, long long b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}


int arr[500000+10];
string s;

int main() {

cn s;

_1(arr);

for (int i = 0; i < s.size(); ++i)
{
    while(s[i]=='<'){
        if(arr[i]==-1)arr[i]=0,arr[i+1]=1;
        else arr[i+1]=arr[i]+1;
        i++;
    }
}


for (int i = s.size()-1; i >=0; i--)
{
    while(s[i]=='>'){
        if(arr[i+1]==-1)arr[i]=1;
        else arr[i]=max(arr[i],arr[i+1]+1);
        i--;
    }
}
ull sum=0;

for (int i = 0; i <s.size()+1; ++i)
{
    if(arr[i]!=-1)sum+=arr[i];
}

ct sum;nl;
    return 0;
}


Measured memory_bytes: 2820
#include <stdio.h>          // printf(), fgets()
#include <stdlib.h>         // qsort()

#define N 5

int
cmp_func(const void *l_, const void *r_)
{
	const int* l = (const int*) l_;
	const int* r = (const int*) r_;

	if (*l > *r)
		return -1;

	if (*l < *r)
		return 1;

	return 0;
}

int
main(int argc, char** argv)
{
	int a[N];
	int i;

	for (i = 0; i < N; ++i)
		scanf("%d", &a[i]);

	qsort(a, N, sizeof(int), cmp_func);
	for (i = 0; i < N; ++i)
		printf("%d%c", a[i], (i != N - 1) ? ' ' : '\n');

	return 0;
}

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

struct sieve {
  long long n;
  vector<long long> f, primes;
  sieve(long long n) : n(n), f(n+1, 0) {
    /* O(n log log n) */
    f[0] = f[1] = -1;
    for(long long i=2; i<=n; i++) {
      if(f[i]) continue;
      primes.push_back(i); f[i] = i;
      for(long long k=(long long)i*i; k<=n; k+=i) if(!f[k]) f[k] = i;
    }
  }
  bool is_prime(long long x) {return f[x]==x;}
  vector<long long> factor_list(long long x) {
    assert((long long)n*n >= x);
    vector<long long> factors;
    if( x>n ) {
      for(auto i : primes) { while (x%i==0) { factors.push_back(i); x /= i; } }
      if(x!=1) factors.push_back(x);
    } else { while (x != 1) { factors.push_back(f[x]); x /= f[x]; } }
    return factors;
  }
  vector<pair<long long,long long>> factor_freq(long long x) {
    assert((long long)n*n >= x);
    vector<long long> fl = factor_list(x);
    if (fl.size() == 0) return {};
    vector<pair<long long,long long>> fc(1, pair<long long,long long>(fl[0], 0));
    for (long long p : fl) {
      if (fc.back().first == p) { fc.back().second++; }
      else { fc.emplace_back(p, 1); }
    }
    return fc;
  }
};

long long N;

signed main() {
  cin >> N;
  sieve s(ceil(sqrt(N)));
  auto factors = s.factor_freq(N);

  int ans = 0;
  for(auto f : factors) {
    int count = f.second;
    int now = 1;
    while(count >= now) {
      ans++;
      count -= now;
      now++;
    }
  }
  cout << ans << endl;

}


Measured memory_bytes: 12256
#include <stdio.h>

int main (void)
{
  int a = 0, b = 0, c = 0;
  int tmp = 0;

  scanf("%d %d %d", &a, &b, &c);

  tmp = a;
  a = b;
  b = tmp;

  tmp = a;
  a = c;
  c = tmp;

  printf("%d %d %d", a, b, c);

  return 0;
}

Measured memory_bytes: 128
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <unordered_map>
#define rep(i,n) for (int i=0; i<n;++i)
#define rep_down(i,n) for (int i=n-1; i>=0;--i)
#define ALL(a)  (a).begin(),(a).end()
typedef long long ll;
using namespace std;
typedef pair<ll, int> P;
struct edge {int to; ll cost;};
const ll INF = 1e17;
int N, M, s,t;
vector<edge> Yen[100005], Snuuk[100005];
ll CostYen[100005], CostSnuuk[100005];

void dijkstra(int s_yen, int s_snuuk) {
  priority_queue<P, vector<P>, greater<P> >  que;
  fill(CostYen, CostYen +  N, INF);
  CostYen[s_yen] = 0ll;
  que.push(P(0ll, s_yen));

  while(!que.empty()){
    P p = que.top(); que.pop();
    int v = p.second;
    if (CostYen[v] < p.first) continue;
    for (int i = 0; i < Yen[v].size(); i++) {
      edge e = Yen[v][i];
      if (CostYen[e.to] > CostYen[v] + e.cost) {
        CostYen[e.to] = CostYen[v] + e.cost;
        que.push(P(CostYen[e.to], e.to));
      }
    }
  }

  fill(CostSnuuk, CostSnuuk +  N, INF);
  CostSnuuk[s_snuuk] = 0ll;
  que.push(P(0ll, s_snuuk));
  while(!que.empty()){
    P p = que.top(); que.pop();
    int v = p.second;
    if (CostSnuuk[v] < p.first) continue;
    for (int i = 0; i < Snuuk[v].size(); i++) {
      edge e = Snuuk[v][i];
      if (CostSnuuk[e.to] > CostSnuuk[v] + e.cost) {
        CostSnuuk[e.to] = CostSnuuk[v] + e.cost;
        que.push(P(CostSnuuk[e.to], e.to));
      }
    }
  }
}

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

  cin >> N >> M >> s >> t;
  s--;t--;
  int u,v;
  ll a,b;
  rep(i, M) {
    cin >> u >> v >> a >> b;
    u--;v--;
    Yen[u].push_back({v,a});
    Yen[v].push_back({u,a});
    Snuuk[u].push_back({v,b});
    Snuuk[v].push_back({u,b});
  }

  dijkstra(s,t);
  vector<P> Cost;
  priority_queue<P, vector<P>, greater<P> >  que;
  rep(i, N) que.push(P(CostYen[i] + CostSnuuk[i], i));
  
  int year=0;
  while (year < N) {
    int ec = que.top().second;
    if (ec < year) {
      que.pop();
      continue;
    }
    ll cs = 1e15 - que.top().first;
    year++;
    cout << cs << "\n";
  }

  return 0;
}

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

int main(){
  string s;
  cin>>s;
  int start,end;
  for(int i=0;i<s.length()-1;i++){
    if(s[i]=='A'){
      start =i;
      break;
    }
  }
  for(int i=s.length()-1;i>0;i--){
    if(s[i]=='Z'){
      end =i;
      break;
    }
  }



  cout<<end-start+1<<endl;
  
  return 0;

}


Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
typedef long long int uli;
string b[55];
int d[55][55];
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};

int main(){
  int n,m;
  scanf("%d %d",&n,&m);
  int black=0;
  for(int i=0;i<n;i++){
    cin>>b[i];
    for(int j=0;j<m;j++)black+=(b[i][j]=='#');
  }
  if(b[0][0]=='#' || b[n-1][m-1]=='#'){
    puts("-1");
    return 0;
  }
  queue<pair<int,int> >q;
  memset(d,-1,sizeof d);
  q.push({0,0});
  d[0][0]=0;
  while(!q.empty()){
    int r=q.front().first;
    int c=q.front().second;
    q.pop();
    for(int i=0;i<4;i++){
      int nr=r+dir[i][0];
      int nc=c+dir[i][1];
      if(0<=nr && nr<n && 0<=nc && nc<m && d[nr][nc]==-1 && b[nr][nc]=='.'){
        d[nr][nc]=1+d[r][c];
        q.push({nr,nc});
      }
    }    
  }
  if(d[n-1][m-1]==-1){
    puts("-1");
    return 0;
  }
  printf("%d\n",n*m-black-d[n-1][m-1]-1);
  return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
#include<math.h>
int main()
{
	int a,b,h,m;
	long double rad,length;

	scanf("%d%d%d%d",&a,&b,&h,&m);

	rad=2.0*M_PI*((long double)h/12.0+(long double)m/60.0/12.0-(long double)m/60.0);
	length=sqrtl((long double)(a*a+b*b)-(long double)(2.0*a*b)*cosl(rad));
	printf("%20.20Lf",length);
	return 0;
}

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

int main() {
  char x;
  cin >> x;
  
  if(x== 'a' || x== 'i' || x== 'u' || x== 'e' || x== 'o') {
    cout << "vowel" << endl;
  }

  else {
    cout << "consonant" << endl;
  }
}


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

typedef long long int ll;
#define f(a, b) for(auto i = a;i < b;i++)
#define ent cout << "\n"

bool sortbysec(const pair<ll, ll> &a, const pair<ll,ll> &b) 
{ 
    return (a.second < b.second); 
} 

void solve()
{
	return;
}
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);	
	ll n, sum = 0;
	cin >> n;

	for(ll i = 1;i <= n;i++)
	{
		for(ll j = 1;j <= n ;j++)
		{
			for(ll k = 1;k <= n;k++)
			{
				ll x = __gcd(i, j);
				x = __gcd(x, k);
				sum += x;
			}
		}
	}

	cout << sum << "\n";
}

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

int main(){
    int h,w;
    vector<string> g;
    cin>>h>>w;
    for (int i = 0; i < h; ++i) {
        string s;
        cin>>s;
        g.emplace_back(s);
    }
    for (int i = 0; i < h; ++i) {
        bool f=true;
        for (auto j:g[i]){
            if(j=='#')f=false;
        }
        if(f){
            for (int j = 0; j < w; ++j) {
                g[i][j]='!';
            }
        }
    }
    for (int i = 0; i < w; ++i) {
        bool f=true;
        for (int j = 0; j < h; ++j) {
            if(g[j][i]=='#')f=false;
        }
        if(f){
            for (int j = 0; j < h; ++j) {
                g[j][i]='!';
            }
        }
    }
    for (int i = 0; i < h; ++i) {
        bool f=false;
        for (int j = 0; j < w; ++j) {
            if(g[i][j]=='!')continue;
            cout<<g[i][j];
            f=true;
        }
        if(f)cout<<endl;
    }
    return 0;
}

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

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

constexpr ll MOD = 1'000'000'007;
int N;
int C[200100];
int stone[200100], nxt[200100];
ll dp[200100];

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

    std::cin >> N;

    for(int i=0;i<N;++i){
        std::cin >> C[i];
    }
    
    memset(stone, -1, sizeof(stone));

    for(int i=N-1;i>=0;--i){
        nxt[i] = stone[C[i]];
        stone[C[i]] = i;
    }

    dp[0] = 1;

    for(int i=0;i<N;++i){
        dp[i+1] = (dp[i+1] + dp[i]) % MOD;

        if(nxt[i] != -1 && nxt[i] > i + 1){
            dp[nxt[i]] = (dp[nxt[i]] + dp[i]) % MOD;
        }
    }

    ll c = dp[N];
    std::cout << c << std::endl;
}


Measured memory_bytes: 4224
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>

using namespace std;

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

	string s = "Balanced";
	if (a + b > c + d) {
		s = "Left";
	} else if (a + b < c + d) {
		s = "Right";
	}
	cout << s << endl;
	return 0;
}


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

int main(){
 int K,S,ans=0;
 cin>>K>>S;
 for(int i=0;i<K+1;i++){
  for(int j=0;j<K+1;j++){
   if(S-j-i>=0&&S-j-i<=K)ans+=1;
   }
  }cout<<ans<<endl;
 return 0;
 }

Measured memory_bytes: 256
#include<iostream>
#include <bits/stdc++.h>
#include<string>
#include<algorithm>
using ll = long long;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
static const int INFTY = (1<<21);
int nums[10],numsize;
ll sum;


void search(int index,ll value,ll tmpsum){
    if(index+1 == numsize){
        sum = sum + tmpsum + value;
        return;
    }
    search(index + 1, value * 10 + nums[index+1], tmpsum);
    search(index + 1, nums[index + 1], tmpsum+value);
}

int main(){
    string str;
    cin >> str;
    rep(i,10)nums[i]=(int)(str[i] - '0');
    sum =0;
    numsize = str.size();
    search(0,nums[0],0);
    cout << sum << endl;
    return 0;

}

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

int main() {
    int A,B; cin >> A >> B;
    int a,b,c; 
    a=A+B; b=A-B; c=A*B;
    a=max(a,b);
    a=max(a,c);
    cout << a << endl;

    return 0;
}

Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    public static void main(String[] args)throws Exception {
    	InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);

        int[] s = new int[2];
        while(true){
        	String[] input = br.readLine().split(" ");
            int x = Integer.parseInt(input[0]);
            int y = Integer.parseInt(input[1]);
            if(x == 0 && y == 0){
            	break;
            }
            s[0] = x;
            s[1] = y;
            Arrays.sort(s);
            System.out.println(s[0] + " " + s[1] );
        }
    }
}

Measured memory_bytes: 27348
#include<stdio.h>
#include<stdlib.h>
struct node{
  int key;
  struct node *parent;
  struct node *right;
  struct node *left;
};
typedef struct node *Node;
Node T;
#define NIL NULL;
void Insert(int);
int Find(int);
void Preorder(Node);
void Inorder(Node);
int main()
{
  int n,i,key;
  char S[7];
  
  scanf("%d",&n);

  for(i=0;i<n;i++){
    scanf("%s",S);
    if(S[0]=='i'){
      scanf("%d",&key);
      Insert(key);
    }
    else if(S[0]=='f'){
      scanf("%d",&key);
      if(Find(key))printf("yes\n");
      else printf("no\n");
    }
    else{
      Inorder(T);
      printf("\n");
      Preorder(T);
      printf("\n");

    }
  }
  return 0;
}

void Insert(int key)
{
  Node y,x,z;
  y=NIL;
  x=T;
  z=malloc(sizeof(struct node));
  z->key=key;
  z->left=NIL;
  z->right=NIL;
  while(x!=NULL){
    y=x;
    if( z->key < x->key )x=x->left;
    else x=x->right;
  }
  z->parent=y;
  if(y==NULL){
    T=z;
  }
 else if(z->key<y->key){
      y->left=z;
  }
  else{
    y->right=z;
  }
}

int Find(int key)
{
  Node TT;
  TT=T;
  while(TT!=NULL && TT->key!=key){
    if(key<TT->key)TT=TT->left;
    else TT=TT->right;
  }
  if(TT==NULL)return 0;
  else return 1;
}
void Preorder(Node T)
{
  printf(" %d",T->key);
  if(T->left!=NULL){
    Preorder(T->left);
  }
  if(T->right!=NULL){
    Preorder(T->right);
  }

}

void Inorder(Node T)
{
  if(T->left!=NULL && T->right!=NULL){
    Inorder(T->left);
    printf(" %d",T->key);
    Inorder(T->right);

  }
  if(T->left!=NULL && T->right==NULL){
   Inorder(T->left);
     printf(" %d",T->key);
  }

  if(T->right!=NULL && T->left==NULL){
  printf(" %d",T->key);
 Inorder(T->right);

  }
  if(T->left==NULL && T->right==NULL)printf(" %d",T->key);

}

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

int main() {
	long long n, m, l, a[100][100], b[100][100], c[100][100]={ 0 };
	cin >> n >> m >> l;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++)
			cin >> a[i][j];
	for (int i = 0; i < m; i++)
		for (int j = 0; j < l; j++)
			cin >> b[i][j];

	for (int i = 0; i < n; i++) 
	{
		for (int j = 0; j < l; j++) 
		{
			for (int k = 0; k < m; k++)
				c[i][j] += a[i][k] * b[k][j];
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < l; j++) {
			if (j == 0)
				cout << c[i][j];
			else 
				cout << " " << c[i][j];
		}
		cout << endl;
	}
	return 0;
}


Measured memory_bytes: 3312
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define pi 3.14159265359
#define all(i) i.begin(),i.end()
using namespace std;
const long long INF=1e9+7;
const string alp="abcdefghijklmnopqrstuvwxyz"; //26
const string ALP="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> T;
//const ll MOD=998244353LL;
//const ll MOD=1777777777LL;


int main() {
  ll N;
  cin>>N;
  ll a[N];
  rep(i,N) cin>>a[i];
  ll ans=0;
  bool judge=true;
  for(int i=0;i<N;i++){
    if(i+1-ans==a[i]) continue;
    else ans++;
  }
  if(ans==N) judge=false;
  if(judge) cout<<ans<<endl;
  else cout<<-1<<endl;
}

Measured memory_bytes: 5152
#include <iostream>
#include <queue>

using namespace std;

typedef long long ll;

priority_queue<pair<ll,ll>> q;

int main() {
	int t;
	cin >> t;
	while (t--) {
		ll n, a, b, c, d;
		cin >> n >> a >> b >> c >> d;
		while (q.size()) q.pop();
		q.push({0,n});
		ll p = 1e18;
		while (q.size()) {
			auto u = q.top();
			while (q.size() && q.top() == u) q.pop();
			if (-u.first >= p) continue;
			if (1e18/u.second >= d) q.push({u.first-d*u.second,0LL});
			for (int i = -4; i <= 4; i++) {
				ll f = -u.first+abs(i)*d;
				ll x = u.second+i;
				if (x < 0) continue;
				if (x == 0) {
					p = min(p,f);
					continue;
				}
				if (abs(i) < 2 && x%2 == 0) q.push({-f-a,x/2});
				if (abs(i) < 3 && x%3 == 0) q.push({-f-b,x/3});
				if (x%5 == 0) q.push({-f-c,x/5});
			}
		}
		cout << p << "\n";
	}
}


Measured memory_bytes: 7320
n = int(input())
p = [float(x) for x in input().split()]

dp = [[0]*(i+2) for i in range(n)]
dp[0][0] = 1-p[0]
dp[0][1] = p[0]

for i in range(1, n):
  for j in range(i+2):
    if j == 0:
      dp[i][j] = dp[i-1][j]*(1-p[i])
      continue
    if j == i+1:
      dp[i][j] = dp[i-1][j-1]*p[i]
      continue
    dp[i][j] = dp[i-1][j-1]*p[i]+dp[i-1][j]*(1-p[i])
ans = 0
for i in range(n//2+1, n+1):
  ans += dp[n-1][i]
print(ans)

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

//using namespace boost::multiprecision;
using namespace std;
typedef long long ll;
//typedef unsigned long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define sz(x) ll(x.size())
typedef pair<int, int> P;
//typedef pair<ll, int> P;
//typedef pair<ll, ll> P;
//const double INF = 1e10;
const ll INF = LONG_LONG_MAX / 100;
//const ll INF = (1ll << 31) - 1;
//const ll INF = 1e15;
const ll MINF = LONG_LONG_MIN;
//const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
//typedef pair<int, int> P;
//typedef pair<double, double> P;

bool contain(set<string> &s, string a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);
const int mod = 1000000007;
typedef priority_queue<long long, vector<long long>, greater<long long> > PQ_ASK;

int main() {
    int n;
    cin >> n;
    vector<ll> q(n);
    rep(i, n) cin >> q[i];
    q.erase(unique(q.begin(), q.end()), q.end());
    vector<char> transition;
    for (int i = 1; i < q.size(); i++) {
        if (q[i] > q[i - 1]) {
            transition.push_back('u');
        } else {
            transition.push_back('d');
        }
    }
//    transition.erase(unique(transition.begin(), transition.end()), transition.end());

    stack<char> s;
    s.push('n');

    for (char t : transition) {
        if (s.top() == 'n') s.top() = t;
        else if (s.top() == t) continue;
        else if (s.top() != t) s.push('n');
    }
    cout << s.size() << endl;


}




Measured memory_bytes: 1280
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const ll mod=1e18+7;
const ll inf = (ll)1e17;
const ld eps = 1e-12;
const ld PI = 3.14159265358979323846;
ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;}
ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}




int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    map<string,int>v;
    for(int i=0;i<n;i++){
        string s;
        cin>>s;
        v[s]++;
    }
    vector<string>res;
    int maxi=0;
    for(auto it=v.begin();it!=v.end();it++){
        maxi=max(maxi,it->second);
    }
    for(auto it=v.begin();it!=v.end();it++){
        if(it->second==maxi){
            res.push_back(it->first);
        }
    }
    sort(res.begin(),res.end());
    for(i:res){
        cout<<i<<endl;
    }
    return 0;
    }


Measured memory_bytes: 25584
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 2147483647;
const ll LINF = 9223372036854775807;
#define REP(i, n) for(int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

struct rl {
	int r;
	int l;
	bool operator<(const rl &another) const
	{
		return r < another.r;
	};
};

int main() {
	int n; cin >> n;
	VI x(n), l(n); REP(i, n)cin >> x[i] >> l[i];
	vector<rl> a(n);
	REP(i, n) {
		a[i].r = x[i] + l[i];
		a[i].l = x[i] - l[i];
	}
	sort(ALL(a)); ll m = -LINF, ans = 0;
	REP(i, n) {
		if (m <= a[i].l) {
			ans++;
			m = a[i].r;
		}
	}
	cout << ans;
}

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

int main() {
  int N;
  cin >> N;
  
  int total = 0;
  for (int i = 0; i < N; i++) {
    int r, l;
    cin >> r >> l;
    int part = l - r + 1;
    total += part;
  }
  cout << total << endl;
}

Measured memory_bytes: 3548
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int main(){

	int N;
	scanf("%d",&N);

	int num = 1,ans = 0;

	while(true){
		if(N >= 2*num-1){
			ans++;
		}else{
			break;
		}
		num *= 2;
	}

	printf("%d\n",ans);

	return 0;
}



Measured memory_bytes: 3220
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e7 + 5;
const ll MOD = 1e9 + 7;
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pis pair<ll,string>
#define F first
#define S second
#define LCM(a,b) ((a*b)/__gcd(a,b))
#define inf 1e10
#define test ll cse;cin>>cse;for(ll _i=1;_i<=cse;_i++)
#define PI 3.14159265
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const double EPS = 1E-9;
typedef vector< vector<double> > matrix;
typedef vector<int> vi;



ll ar[105];
int main() {
    fast;

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

    int cnt = 0;
    for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++) for(int k = j + 1; k < n; k++) {

        if((ar[i] + ar[j] > ar[k]) && (ar[j] + ar[k] > ar[i]) && (ar[i] + ar[k] > ar[j])
            && ar[i] != ar[j] && ar[j] != ar[k] && ar[i] != ar[k]) {
            cnt++;
        }
    }

    cout << cnt << "\n";

    return 0;
}

Measured memory_bytes: 3640
def cin():  return list(map(int,input().split()))

N, M = cin()
A = [cin() for _ in range(M)]

class UnionFind():
    
    # 要素数を指定して作成 はじめは全ての要素が別グループに属し、親はいない
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    
    # xの根を返す
    def find(self, x):
        if self.parents[x] < 0:  return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    
    # xとyが属するグループを併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:  return
        if self.parents[x] > self.parents[y]:  x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    
    # xが属するグループの要素数
    def size(self, x):  return -self.parents[self.find(x)]
    
    # ｘとyが同じグループか否か
    def same(self, x, y):  return self.find(x) == self.find(y)
    
    # xと同じメンバーの要素
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
    
    # 根の一覧
    def roots(self):  return [i for i, x in enumerate(self.parents) if x < 0]
    
    # グループ数
    def group_count(self):  return len(self.roots())
      
uf = UnionFind(N)
for i in range(M):
    uf.union(A[i][0] - 1, A[i][1] - 1)
ans = 0
for i in range(N):
	ans = max(ans, uf.size(i))
print(ans)

Measured memory_bytes: 111464
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		sc.useDelimiter("\\D");

		int w = sc.nextInt();
		int n = sc.nextInt();

		int[] amida;
		amida = new int[w];

		for (int i = 0; i < w; i++) {
			amida[i] = i + 1;
		}

		for (int i = 0; i < n; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();

			int t = amida[a-1];
			amida[a-1] = amida[b-1];
			amida[b-1] = t;
		}

		for (int i = 0; i < w; i++)
			System.out.printf("%d\n", amida[i]);

	}
}

Measured memory_bytes: 26268
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <iomanip>
#include <limits>
#include <list>
#include <string>
#include <queue>
#include <deque>
#include <tuple>
#include <map>
#include <sstream>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define FOR(i,a,b) for(int i=a; i<(int)(b); i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort(ALL(x))
#define CLR(a) memset((a), 0 ,sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
const double EPS = 1e-10;
const long double PI  = acos(-1.0L);
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
#define chmax(a,b) a = (((a)<(b))?(b):(a))
#define chmin(a,b) a = (((a)>(b))?(b):(a))

__attribute__((constructor))
void initial(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}

const int MAX_V = 200;
ll d[MAX_V][MAX_V];
int V;

void warshall_floyd(){
	rep(k,V){
		rep(i,V){
			rep(j,V){
				chmin(d[i][j],d[i][k]+d[k][j]);
			}
		}
	}
}

signed main(){
  int n,m,r; cin>>n>>m>>r;
	V = n;
	VI R(r); rep(i,r)cin>>R[i], R[i]--; sort(ALL(R));
	memset(d,INF,sizeof(d));
	rep(i,m){
		int a,b; ll c; cin>>a>>b>>c; a--; b--;
		d[a][b] = c; d[b][a] = c;
	}
	warshall_floyd();
	ll ans = LINF;
	do{
		ll tmp = 0;
		rep(i,r-1) tmp += d[R[i]][R[i+1]];
		chmin(ans,tmp);
	}while(next_permutation(ALL(R)));
	cout << ans << endl;
  return 0;
}


Measured memory_bytes: 640
a = [int(i) for i in input().split()]

def loss(a):
    maxItem = max(a)
    s = 0
    for i in a:
        s+=abs(i-maxItem)
    return s

def inctwo(a):
    maxidx = a.index(max(a))
    copy = list(a)
    for i in range(3):
        if i!=maxidx:
            copy[i]+=1
    return [copy,loss(copy)]

def incone(a):
    minItem = a.index(min(a))
    copy = list(a)
    copy[minItem]+=2
    return [copy,loss(copy)]

def solve(a,ans):
    if loss(a)==0:
        return ans
    one = inctwo(a)
    two = incone(a)
    if one[1]==0 or two[1]==0:
        return ans+1
    if one[1]>two[1]:
        return solve(two[0],ans+1)
    else:
        return solve(one[0],ans+1)

print(solve(a,0))


Measured memory_bytes: 3064
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n,mi=10000000,ma=-10000000;
    long long sum=0;
  cin >> n;
  int i,b;
  for(i=0;i<n;i++){
    cin >> b;
    mi=min(mi,b);
    ma=max(ma,b);
    sum+=b;
  }
  cout << mi <<  " " << ma << " " << sum << endl;
  return 0;
}


Measured memory_bytes: 3116
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;

int x,k,a,b,c,r[maxn];
int q,t[maxn],g[maxn];

int main()
{
	scanf("%d%d",&x,&k);
	for(int i=1;i<=k;i++) scanf("%d",&r[i]);
	r[0]=0;r[k+1]=1e9+10;
	scanf("%d",&q);
	for(int i=1;i<=q;i++) scanf("%d%d",&t[i],&g[i]);
	int pos=1;
	
	a=0;b=x;c=0;
	for(int i=0;i<=k;i++)
	{
		while(pos<=q && t[pos]<=r[i+1])
		{
			g[pos]=max(g[pos],a);
			g[pos]=min(g[pos],b);
			g[pos]+=c;
			if (i%2) g[pos]+=t[pos]-r[i];
			else g[pos]-=t[pos]-r[i];
			g[pos]=max(g[pos],0);
			g[pos]=min(g[pos],x);
			
			printf("%d\n",g[pos]);
			pos++;
		}
		
		int tmp=r[i+1]-r[i];
		if (i%2)
		{
			c+=tmp;
			if (b+c>x)
			{
				if (a+c<=x) b=x-c;
				else
				{
					a=0;b=0;
					c=x;
				}
			}
		}
		else
		{
			c-=tmp;
			if (a+c<0) 
			{
				if (b+c>=0) a=-c;
				else 
				{
					a=0;b=0;
					c=0;
				}	 
			}
		}
	}
	
	return 0;
}

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

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        Long k = Long.parseLong(sc.nextLine());

        long ans = solve(s, k);
        System.out.println(ans);
    }

    static long solve(String s, long k) {

        // 全部同じ文字の場合は置き換える数 * K回
        if (isAllSameChar(s)) {
            return s.length() * k / 2;
        }

        // 異なるが1つでもある場合
        List<Long> ranlength = new ArrayList<>();
        long ranl = 1;

        // 元文字列で何回置き換える必要があるか計算する
        // ex) issii -> 122 = 2回
        for (int i = 1; i <= s.length(); i++) {
            if (i == s.length()) {
                ranlength.add(ranl);
                break;
            }
            if (s.charAt(i) == s.charAt(i - 1)) {
                ranl++;
            } else {
                ranlength.add(ranl);
                ranl = 1;
            }
        }

        // 先頭と最後が同じ文字の場合、置き換える数が変わる
        long ans = 0;
        if (isSameFirstAndLastChar(s)) {
            if (k == 1) {
                for (int i = 0; i < ranlength.size(); i++) {
                    ans += ranlength.get(i) / 2;
                }
                return ans;
            }

            // 最初と最後が繋がる場合、最後の値以外をまず加算する
            for (int i = 0; i < ranlength.size() - 1; i++) {
                ans += ranlength.get(i) / 2;
            }
            // 一番最後のK回目の最終要素だけ先に加算する
            ans += ranlength.get(ranlength.size() - 1) / 2;

            // 最初の値を最後の値との合算値に置き換える
            ranlength.set(0, ranlength.get(0) + ranlength.get(ranlength.size() - 1));

            // 間の部分の計算
            long tmp = 0;
            for (int i = 0; i < ranlength.size() - 1; i++) {
                tmp += ranlength.get(i) / 2;
            }
            ans += tmp * (k - 1);

        } else {

            // 元の文字列を何回置き換えるか計算し、それをK倍したのが答え
            // ex) abbc 3 ->(121 = 1回) * 3 = 3回
            for (int i = 0; i < ranlength.size(); i++) {
                ans += ranlength.get(i) / 2;
            }
            ans *= k;
        }

        return ans;
    }

    private static boolean isAllSameChar(String s) {
        char bef = s.charAt(0);
        for (int i = 0; i < s.length(); i++) {
            if (bef != s.charAt(i)) {
                return false;
            }
        }
        return true;
    }

    private static boolean isSameFirstAndLastChar(String s) {
        return s.charAt(0) == s.charAt(s.length() - 1);
    }
}


Measured memory_bytes: 21844
#include<bits/stdc++.h>
using namespace std;
#define SORT(x) sort(x.begin(),x.end())
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
const ll LINF = 1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int gcd(int a,int b){return b?gcd(b,a%b):a;}

int main(){
    int n;
    cin>>n;
    ll a[n];
    rep(i,n)    cin>>a[i];
    ll b[n]={};
    rep(i,n){
        if(i%2==0){
            b[0]+=a[n-i-1];
        }
        else{
            b[0]-=a[n-i-1];
        }
    }
    for(int i=1;i<n;i++){
        b[i]=2*a[i-1]-b[i-1];
    }

    rep(i,n)    cout<<b[i]<<' ';
    cout<<endl;
    return 0;
}


Measured memory_bytes: 2816
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sstream>
#include <unordered_map>
#include <vector>


#define INF 4000000000000000000LL
#define MOD 1000000007
#define ALL(x) std::begin(x), std::end(x)


int main(int argc, char** argv)
{
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(0);

  std::cout << std::fixed << std::setprecision(6);
  std::cerr << std::fixed << std::setprecision(6);

  int D, N;

  std::cin >> D >> N;

  if (D == 0) {
    for (int i = 1, C = 0; i < 1111111; i ++)
      if (i % 100) {
        C ++;

        if (C == N) {
          std::cout << i << std::endl;

          return 0;
        }
      }
  }
  else {
    int b = D == 1 ? 100 : 10000;
    
    for (int i = 1, C = 0; i < 1111111; i ++) {
      int c = 0;

      for (int ii = i; ii % 100 == 0; ii /= 100)
        c ++;
      
      if (c == D) {
        C ++;

        if (C == N) {
          std::cout << i << std::endl;

          return 0;
        }
      }
    }
    
  }
  
  return 0;
}


Measured memory_bytes: 256
result = 0

while True:
  line = input()
  line = line.split()
  a = int(line[0])
  op = line[1]
  b = int(line[2])

  if op == '+':
    output = a + b
  elif op == '-':
    output = a-b
  elif op == '*':
    output = a * b
  elif op == '/':
    output = a // b

  if op == '?':
    break

  print(output)

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

int main()
{
	int d;
	cin >> d;

	vector<int> c(26+1);
	for (int i=1;i<=26;++i)
	{
		cin >> c[i];
	}

	vector<vector<int>> s(d+1,vector<int>(26+1));
	for (int i=1;i<=d;++i)
	{
		for (int j=1;j<=26;++j)
		{
			cin >> s[i][j];
		}
	}
	vector<int> t(d+1);
	for (int i=1;i<=d;++i)
	{
		cin >> t[i];
	}

	vector<int> last(26+1);
	for (int i=1;i<=26;++i)
	{
		last[i]=0;
	}

	int64_t score=0;
	for (int i=1;i<=d;++i)
	{
		score+=s[i][t[i]];
		last[t[i]]=i;
		for (int j=1;j<=26;++j)
		{
			score-=c[j]*(i-last[j]);
		}
		cout << score << endl;
	}
}






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

int main( void ) {
    int n;
    cin >> n;
    int h = n / 3600;
    int m = n % 3600 / 60;
    int s = n % 60;
    cout << h << ':' << m << ':' << s << '\n';
    return 0;
}


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

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

const long long MOD = 1000000007;

int main(){
  int n,a,b;
  int ck=0;
  bool ans = false;
  cin >> n;
  
  rep(i,n){
    cin >> a >> b;
    if(a==b) ck++;
    else ck=0;
    
    if(ck==3){
      ans=true;
      break;
    }
  }
  
  if(ans) cout << "Yes" << endl;
  else cout << "No" << endl;
}


Measured memory_bytes: 3628
#include<bits/stdc++.h>
using namespace std;
#define db(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<'\n'
const int N = 1e5;
int A[N+7], dp[N+7], n,k;
const int inf=1e9;

int solve(int i) {
	if(i==n) return 0;
	if(i>n) return inf;
	if(dp[i]!=-1) return dp[i];
	int mn = 2*inf;
	for(int j=1;j<=k;++j) {
		int a = solve(i+j) + abs(A[i]-A[i+j]);
		mn = min(a, mn);
	}
	return dp[i] = mn;
}
int main(){
	ios_base::sync_with_stdio(0);
	int a,b,T,cas=0;
	cin>>n>>k;
	for(int i=1;i<=n;++i) cin>>A[i];
	fill(dp, dp+n+1, -1);
	int res = solve(1);
	cout<<res<<endl;
	return 0;
}

Measured memory_bytes: 5760
import java.util.Scanner;

public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int q = scan.nextInt();
		String s = scan.next();
		int[] sum = new int[n + 1];

		int i;
		for(i = 1; i < n; i++) {
			if(s.substring(i - 1, i + 1).equals("AC")) {
				sum[i] = sum[i - 1] + 1;
			}else {
				sum[i] = sum[i - 1];
			}
		}

		int start,end;
		for(i = 0; i < q; i++) {
			start = scan.nextInt() - 1;
			end = scan.nextInt() - 1;

			System.out.println(sum[end] - sum[start]);
		}
	}
}


Measured memory_bytes: 65520
#include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
 
typedef long long ll;
 
const int MAX_N = 100010;
int par[MAX_N];
int rnk[MAX_N];

void init(int n) {
    rep(i,n) {
        par[i] = i;
        rnk[i] = 0;
    }
}

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

void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rnk[x] < rnk[y]) {
        par[x] = y;
    } else {
        par[y] = x;
        if (rnk[x] == rnk[y]) rnk[x]++;
    }
}

bool same(int x, int y) {
    return find(x) == find(y);
}

int main() {
    int N, M;
    cin >> N >> M;
    init(N);
    vector<int> a(N), x(M), y(M);
    rep(i, N) cin >> a[i];
    rep(i, M) {
        cin >> x[i] >> y[i];
        unite(x[i], y[i]);
    }
    vector<int> mina(N, 1e9 + 1);
    rep(i, N) {
        mina[find(i)] = min(mina[find(i)], a[i]);
    }
    vector<bool> f(N, true);
    vector<int> b(0);
    ll ans = 0;
    rep(i, N) {
        if (f[find(i)] && a[i] == mina[find(i)]) {
            f[find(i)] = false;
            ans += a[i];
            //cout << "a[" << i << "]=" << a[i] << endl;
        } else {
            b.push_back(a[i]);
        }
    }
    sort(b.begin(), b.end());
    int nt = 0;
    rep(i, N) {
        if (find(i) == i) nt++;
    }
    //cout << "nt=" << nt << endl;
    if (nt == 1) {
        cout << "0\n";
        return 0;
    }
    if (b.size() < (nt - 1) * 2 - nt) {
        cout <<  "Impossible\n";
        return 0;
    }
    rep(i, (nt - 1) * 2 - nt) ans += b[i];
    cout << ans << endl;
}

Measured memory_bytes: 3200