akhauriyash/Code-Regression huggingface dataset:
nm=input().split()
NM_list = list(map(int,nm))

A_list=[]
B_list=[]
for m in range(NM_list[1]):
	ab=input().split()
	AB_list = list(map(int,ab))
	A_list.append(AB_list[0])
	B_list.append(AB_list[1])

n_list=[]
for n in range(1,NM_list[0]+1):
	n_list.append(0)


for i in range(1,len(A_list)+1):
	for n_len in range(1,len(n_list)+1):
		if(n_len == A_list[i-1]):
			n_list[n_len-1] += 1
			break

for i in range(1,len(B_list)+1):
	for n_len in range(1,len(n_list)+1):
		if(n_len == B_list[i-1]):
			n_list[n_len-1] += 1
			break

for p in range(1,NM_list[0]+1):
	print(n_list[p-1])

Measured memory_bytes: 3064
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
#include <math.h>
#include <queue>
#include <deque>
#include <map>
const int MOD = 1e9+7;
typedef long long ll;
using namespace std;


int main(){
	int k, n;
	cin >> k >> n;
	vector<int> vec(n);
	for (int i = 0; i < n; i++){
		cin >> vec[i];
	}
	int ans = MOD;
	for (int i = 1; i < n-1; i++){
		ans = min(min(vec[i] + k - vec[i+1], k - vec[i] + vec[i-1]), ans);
	}
	ans = min(min(vec[0] + k - vec[1], vec[n-1] - vec[0]), ans);
	ans = min(k - vec[n-1] + vec[n-2], ans);
	cout << ans << endl;
 	return 0;
 }


Measured memory_bytes: 1152
#include<stdio.h>
main(){
int a=0;
int W,H,x,y,r=0;
scanf("%d %d %d %d %d",&W,&H,&x,&y,&r);
if(x-r>=0&&x+r<=W){
if(y-r>=0&&y+r<=H){
a=1;
}
}
else{
a=0;
}
if(a==1){
printf("Yes\n");
}
else if(a==0){
printf("No\n");
}
return 0;
}

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

    ll depth = 0;
    while (H > 0) {
        H /= 2;
        depth++;
    }

    ll res = 1;
    rep(i, depth) {
        res *= 2;
    }

    put(res - 1);
}
signed main(){ Main();return 0;}

Measured memory_bytes: 3592
#include  <iostream>

using namespace std;
int main() {
  int a,b=0;
  cin>>a>>b;
  int c=a+b;
  if(c<a-b){
    c=a-b;
  }
  if(c<a*b){
    c=a*b;
  }
  cout << c;
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
int main(){
  int n;
  cin>>n;
	string arr[n];
	int a=0,w=0,t=0,r=0;
	for(int i=0;i<n;i++){
		cin>>arr[i];
		if(arr[i][0]=='A'){
			a++;
		}
		else if(arr[i][0]=='W'){
			w++;
		}
		else if(arr[i][0]=='R'){
			r++;
		}
		else{
			t++;
		}
	}
	cout<<"AC x "<<a<<endl;
	cout<<"WA x "<<w<<endl;
	cout<<"TLE x "<<t<<endl;
	cout<<"RE x "<<r<<endl;
	return 0;
}

Measured memory_bytes: 6716
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
using P = pair<int,int>;

int main()
{
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N){
    cin >> A[i];
    A[i]--;
  }

  vector<int> B(N);
  REP(i,N){
    B[A[i]]=i;
  }

  int cnt = 0;
  int MAX = 0;
  B.push_back(-1);
  REP(i,N){
    if(B[i]<B[i+1]){
      cnt++;
    }else{
      cnt++;
      MAX=max(MAX,cnt);
      cnt=0;
    }
  }

  cout << N-MAX << endl;
}

Measured memory_bytes: 5636
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>

using namespace std;

int main(void){
    int a,b,c;
    vector<int> vec;
    cin >> a >> b >> c;
    vec.push_back(a);
    vec.push_back(b);
    vec.push_back(c);

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

    if(vec[2] == 7){
        if(vec[1] == vec[0] && vec[0] == 5){
            cout << "YES" << endl;
            return 0;
        }else{
            cout << "NO" << endl;
            return 0;
        }
    }else{
        cout <<  "NO" << endl;
        return 0;
    }
}

Measured memory_bytes: 256
#include<stdio.h>

//int strlen(char *a){for(int i=0;;i++)if(a[i]=='\0')return i;}


int odd[] = {105,135,165,189,195};

int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<5;i++){
		if(odd[i]>n){
			printf("%d",i);
			return 0;
		}
	}
	printf("5");
}


Measured memory_bytes: 128
//56
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>

using namespace std;

struct R{
  int s,x,y;
  bool operator<(R a)const{
    return s>a.s;
  }
};

int main(){
  for(int r;cin>>r,r;){
    static int w[2],h[2],x[2],y[2],g[2][500][500];
    vector<int> nr[2],sl[2];
    for(int i=0;i<2;i++){
      cin>>w[i]>>h[i]>>x[i]>>y[i];
      for(int j=0;j<h[i];j++){
	for(int k=0;k<w[i];k++){
	  cin>>g[i][j][k];
	}
      }
      int p=0,cn=0;
      priority_queue<R> que;
      R is={1,x[i]-1,y[i]-1};
      que.push(is);
      while(!que.empty()){
	R c=que.top();
	que.pop();
	if(g[i][c.y][c.x]==0)continue;
	if(c.s>p){
	  nr[i].push_back(cn);
	  sl[i].push_back(p);
	  p=c.s;
	}
	cn++;
	g[i][c.y][c.x]=0;
	for(int j=0;j<4;j++){
	  int d[]={0,1,0,-1,0};
	  int nx=c.x+d[j];
	  int ny=c.y+d[j+1];
	  if(0<=nx&&nx<w[i]&&0<=ny&&ny<h[i]){
	    R n={g[i][ny][nx],nx,ny};
	    que.push(n);
	  }
	}
      }
      nr[i].push_back(cn);
      sl[i].push_back(p);
    }
    int m=1<<30;
    for(int i=0;i<nr[0].size();i++){
      int x=lower_bound(nr[1].begin(),nr[1].end(),r-nr[0][i])-nr[1].begin();
      if(x!=nr[1].size()){
	m=min(m,sl[0][i]+sl[1][x]);
      }
    }
    cout<<m<<endl;
  }
  return 0;
}

Measured memory_bytes: 6076
//{{{
#include<algorithm>
#include<cmath>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<sys/time.h>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(printde) FUNC(printdbe) FUNC(printdwe) FUNC(printdu);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream& operator >> (istream& in, V<E>& v){ for(auto& e : v){ in >> e; } return in; }
//}}}

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    int t; cin >> t;
    for(int _ = 0; _ < t; _++){
        ll n, a, b, c, d;
        cin >> n >> a >> b >> c >> d;
        // unordered_map<ll, ll> mp;
        // mp[n] = 0;
        priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
        unordered_set<ll> appeared;
        pq.emplace(0, n);
        ll ans = MLL;
        int maxi = 0;
        while(pq.size()){
            // printde(pq.size());
            maxi = max(maxi, (int)pq.size());
            auto e = pq.top(); pq.pop();
            ll cost = e.first;
            if(cost >= ans){
                break;
            }
            ll num = e.second;

            if(appeared.count(num)){ continue; }
            appeared.insert(num);
            // printde(e);
            // if(pq.size() > 1000){
            //     break;
            // }

            int twoRem = 2 - num % 2, threeRem = 3 - num % 3, fiveRem = 5 - num % 5;
            ll c2 = cost, n2 = num;
            for(int i = 0; i < 5 && n2 && c2 < ans; i++){
                if(twoRem == i){ pq.emplace(c2 + a, n2 / 2); }
                if(threeRem == i){ pq.emplace(c2 + b, n2 / 3); }
                if(fiveRem == i){ pq.emplace(c2 + c, n2 / 5); }
                if(c2 + 1. * n2 * d < ans){
                    ans = min(ans, c2 + n2 * d);
                }
                n2++;
                c2 += d;
            }
            if(c2 + 1. * n2 * d < ans){
                ans = min(ans, c2 + n2 * d);
            }

            twoRem = num % 2; threeRem = num % 3; fiveRem = num % 5;
            c2 = cost; n2 = num;
            for(int i = 0; i < 5 && n2 && c2 < ans; i++){
                if(twoRem == i){ pq.emplace(c2 + a, n2 / 2); }
                if(threeRem == i){ pq.emplace(c2 + b, n2 / 3); }
                if(fiveRem == i){ pq.emplace(c2 + c, n2 / 5); }
                if(c2 + 1. * n2 * d < ans){
                    ans = min(ans, c2 + n2 * d);
                }
                n2--;
                c2 += d;
            }
            if(c2 + 1. * n2 * d < ans){
                ans = min(ans, c2 + n2 * d);
            }
        }
        printde(maxi);
        cout << ans << '\n';
    }
}


Measured memory_bytes: 3920
class Main {
	public static void main(String[] a) {
		execute_ququ();
	}

	private static void execute_ququ() {
		for (int i = 1; i < 10; i++) {
			for (int j = 1; j < 10; j++) {
				System.out.println(i + "x" + j + "="+ kakezan(i,j));
			}
		}
	}

	private static int kakezan(int x, int y) {
		return x * y;
	}
}

Measured memory_bytes: 24072
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9+7;
const int INF = 1e9;
const ll INFll = 1e18;
#define put(n) cout<<(n)<<endl
#define Cout(n) cout<<(n)
#define rep(i,num,N) for(int(i)=(num);(i)<(N);++(i))
#define rrep(i,num,N) for(int (i)=(num);(i)>(N);--(i))
#define all(v) (v).begin() , (v).end()
#define rall(v) (v).rbegin() , (v).rend()
#define MP make_pair
#define pb(q) push_back(q)
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int K,ans=0;
    cin>>K;
    for(int i=1;i<=K;++i){
        for(int j=i+1;j<=K;j+=2){
            ++ans;
        }
    }
    put(ans);
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(void){
    long long A,B,C,K,ans=0;
    
    cin >> A >> B >> C >> K;

    if((A+B+C)<K) return -1;

    if(K<=A){
        ans = K;
    }else if(K<=(A+B)){
        ans = A;
    }else{
        ans = A - (K-(A+B));
    }
    
    cout << ans << endl;
    
    return 0;
}

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

int main() {
    int N;
    cin >> N;
    int ans = 100;
    for (int A = 1; A < N; A++) {
        int B = N - A;
        int sa = 0;
        int sb = 0;
        int a = A;
        int b = B;
        while (a) {
            sa += a % 10;
            a /= 10;
        }
        while (b) {
            sb += b % 10;
            b /= 10;
        }
        ans = min(ans, sa + sb);
    }
    cout << ans << endl;
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

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

	int64 N, M;
	cin >> N >> M;
	vector<int64> A(N);
	REP(i, N) cin >> A[i];
	int64 sum = accumulate(all(A), 0LL);
	int64 res = 0;
	REP(i, N) {
	  if (A[i] * 4* M >= sum) {
	    res++;
	  }
	}
	if (res >= M) {
	  cout << "Yes" << endl;
	} else {
	  cout << "No" << endl;
	}
}


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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
#define REP(i,n) for(ll i = 0;i < (ll)n;i++)
#define REPD(i,n) for(ll i = (ll)n - 1;i >= 0;i--)
#define ALL(x) (x).begin(),(x).end()
#define FILLl(n,first_dimension_size,value) fill((ll*)n,(ll*)(n+first_dimension_size),value)
#define FILL(n,first_dimension_size,value) fill((int*)n,(int*)(n+first_dimension_size),value)
#define INF (1 << 30)
#define MOD 1000000007
//int dy[4] = {-1,0,0,1};
//int dx[4] = {0,1,-1,0};



int main(){

  int n,k;
  cin >> n >> k;
  vector<ll> sum(n+2,0);
  REP(i,n)sum[i+1] = (sum[i] + i+1)%MOD;
  ll res = 0;
  for(ll i = k;i < n+1;i++){
    res = (res + 2ll*MOD + sum[n]-sum[n-i] -(sum[i-1])+1)%MOD;
  }
  res++;
  
  cout << res << endl;

  


  

  return 0;
}


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

int main()
{
    int n;
    cin >> n;
    int adj[n][n];
    int V[n];
    for (int i = 0; i < n; i++)
    {
        int u, k;
        cin >> u >> k;
        V[i] = k;
        for (int j = 0; j < k; j++)
        {
            int v;
            cin >> v;
            adj[u - 1][j] = v - 1;
        }
    }
    queue<int> q;
    int d[n];
    for (int i = 0; i < n; i++)
        d[i] = -1;
    d[0] = 0;
    q.push(0);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        for (int i = 0; i < V[u]; i++)
        {
            int v = adj[u][i];
            if (d[v] == -1)
            {
                d[v] = d[u] + 1;
                q.push(v);
            }
        }
    }

    for (int i = 0; i < n; i++)
        cout << i + 1 << ' ' << d[i] << endl;
}


Measured memory_bytes: 3136
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
const int MOD = 1e9 + 7;
using namespace std;
typedef long long ll;

map<ll, int> mp;
int main() {
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	const int dx = c - a, dy = d - b;
	cout << string(dy, 'U') << string(dx, 'R');
	cout << string(dy, 'D') << string(dx, 'L');
	cout << 'L' << string(dy + 1, 'U') << string(dx + 1, 'R') << 'D';
	cout << 'R' << string(dy + 1, 'D') << string(dx + 1, 'L') << 'U';
	cout << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cmath>

using namespace std;

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

	bool ans = str[0] == str[1] & str[2] == str[1];
	ans |= str[1] == str[2] & str[2] == str[3];

	if(ans)cout << "Yes" << endl;
	else cout << "No" << endl;

	return 0;
}

Measured memory_bytes: 256
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
n = int(input())
x = list(map(int, input().split()))
sum = res = 0

for i in range(n):
    sum += x[i]
    
p = sum / n
p = Decimal(str(p)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
for i in range(n):
    res += (x[i] - Decimal(p)) ** 2
    
print(int(res))

Measured memory_bytes: 5460
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
using namespace std;

#ifdef DEBUG
#define dump(s) cerr << #s << " = " << s << endl
#else
#define dump(...)
#endif

char s[200];
char *p;
int n;

bool ok(int n){
    return 0<=n && n<(1<<10);
}

int E();
int F();
int T();
int N();

int E(){
    int res = F();
    while(*p=='+' || *p=='-'){
        if(*p=='+') p++, res += F();
        else if(*p=='-') p++, res -= F();
        if(!ok(res)) throw "";
    }
    return res;
}

int F(){
    int res = T();
    while(*p=='*' || *p=='/'){
        if(!ok(res)) throw "";
        if(*p=='*') p++, res *= T();
        else if(*p=='/') p++, res /= T();
        if(!ok(res)) throw "";
    }
    return res;
}

int T(){
    int res;
    if(*p=='(') p++, res = E(), p++;
    else res = N();
    if(!ok(res)) throw "";
    return res;
}

int N(){
    int res = 0;
    if(!isdigit(*p)) throw "";
    while(isdigit(*p)){
        res = res*2 + *p - '0';
        p++;
    }
    if(!ok(res)) throw "";
    return res;
}

bool chk(){
    for(int i=0;i<n;i++){
        if(s[i]!='(') continue;
        int j = i;
        int d = 0;
        int cnt = 0;
        while(s[j]){
            if(s[j]=='(') d++;
            else if(s[j]==')') d--;
            if(d==1) if(s[j]=='+' || s[j]=='-' || s[j]=='*' || s[j]=='/') cnt++;
            if(d==0) break;
            j++;
        }
        dump(cnt);
        dump(d);
        if(d!=0 || cnt==0) return false;
    }
    return true;
}

int main(){
    cin >> s;
    n = strlen(s);
    int x[6], k = 0;
    char cand[] = "01+-*())";
    for(int i=0;i<n;i++){
        if(s[i]=='.') x[k++] = i;
    }
    int ans = -1;
    for(int mask=0;mask<1<<(3*k);mask++){
        p = s;
        int t = mask;
        for(int i=0;i<k;i++) s[x[i]] = cand[t>>(3*i)&7];
        dump(s);
        if(!chk()) continue;
        try {
            int t = E();
            if(p-s!=n) continue;
            dump(t);
            ans = max(ans,t);
        } catch(...){}
    }
    cout << ans << endl;
}

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

using namespace std;
typedef long long ll;

ll n, a, b, h[100010], maxh = 0;

bool solve(ll m) {
    ll cnt = 0;
    for (ll i = 0; i < n; i++) {
        if (h[i] - m * b <= 0) continue;
        cnt += (h[i] - m * b + a - b - 1) / (a - b);
    }
    if (cnt <= m) return true;
    return false;
}

int main(void) {
    cin >> n >> a >> b;
    for (ll i = 0; i < n; i++) {
        cin >> h[i];
        maxh = max(maxh, h[i]);
    }
    
    ll l = 1, r = (maxh + b - 1) / b;
    while (l < r) {
        ll m = (l + r) / 2;
        if (solve(m)) r = m;
        else l = m + 1;
    }

    cout << l << endl;

    return 0;
}

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

#define rep(a,b) for(int a = 0;a < b;a++)
#define REP(i, x, n) for(int i = x; i < n; i++)
#define P(a) cout << a << endl
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ssort(v) sort(v.begin(),v.end())
#define rev(v) reverse(v.begin(),v.end())
#define vP(v) for(int i = 0;i < v.size();i++){cout << v[i];if(i != v.size()-1)cout << endl;}
#define vcin(v,n) rep(i,n){int a;cin >> a;v.pb(a);}

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int dx[] = { 1, -1 , 0 , 0 };
int dy[] = { 0,  0,  1, -1 };
ll MOD = 1000000007;

void solve(){
	int n,k;
	cin >> n >> k;
	int sum = 0;
	rep(i,n){
		int a;
		cin >> a;
		sum += min(a,abs(k-a));
	}
	P(sum*2);
}

int main() {
	solve();
	return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;


#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010

void calc(int n){
  char s[100][60];
  int s_len[100],max_len = 0;
  
  for(int i=0;i<n;i++){
    scanf("%s",s[i]);
    s_len[i] = strlen(s[i]);
    max_len = max(max_len, s_len[i]);
  }

  int l=1,r=max_len+1;

  while(l<r){
    int mid = (l+r)/2;
    bool ans = true;
    set<string> ss;
  
    for(int i=0;i<n;i++){
      bool f = true;
      int t = 0;
      string str;
      
      for(int j=0;j<s_len[i];j++){
        if(f){
          str += s[i][j];
          t++;
        }
        f = false;
        for(int k=0;k<5;k++)
          f |= "aiueo"[k] == s[i][j];

        if(t == mid) break;
      }

      if(ss.find(str) != ss.end()){
        ans = false;
      }else{
        ss.insert(str);
      }
    }

    if(ans){
      r = mid;
    }else{
      l = mid+1;
    }
  }

  if(max_len+1 == l){
    puts("-1");
  }else{
    printf("%d\n",l);
  }
}

int main(){
  int n;

  while(1){
    scanf("%d",&n);
    if(!n) break;
    calc(n);
  }
  
  
  return 0;
}

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

#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define repp(i, n, m) for(int (i) = (n); (i) < (m); (i)++)
#define repn(i, n) for(int (i) = 1; (i) <= (n); (i)++)
#define repr(i, n) for(int (i) = (n-1); (i) >= 0; (i)--)
#define all(x) (x).begin(), (x).end()
#define lint long long
#define ulint unsigned long long
#define ldou long double
#define fi first
#define se second
#define setpre(x) std::cout << fixed << setprecision(x)
#define ii(x) int x; cin >> (x)
#define ii2(x, y) int x, y; cin >> (x) >> (y)
#define ii3(x, y, z) int x, y, z; cin >> (x) >> (y) >> (z)
#define out(x) cout << (x) << endl
#define outs(x) cout << (x) << " "
#define yn(x) cout << ((x)?("Yes"):("No")) << endl
#define YN(x) cout << ((x)?("YES"):("NO")) << endl
#define bit_c(x) __builtin_popcountll(x)

inline void logger(){ std::cout << " [LOGGER] " << endl; }
template<typename A, typename... B>
void logger(const A& a, const B&... b){
    cout << a << " , ";
    logger(b...);
}

typedef pair<lint, lint> P;
const lint MOD = 1000000007;
const lint MOD9 = 998244353;
const lint INF = MOD * MOD;
const int MAX = 200005;

/* ...o(^-^)o... */

int main(){
    int n; cin >> n;
    vector<lint> h(n); rep(i, n) cin >> h[i];
    lint dp[MAX];
    rep(i, n) dp[i] = INF; dp[0] = 0;
    rep(i, n-1){
        dp[i+1] = min(dp[i+1], dp[i]+abs(h[i]-h[i+1]));
        if(i < n-2) dp[i+2] = min(dp[i+2], dp[i]+abs(h[i]-h[i+2]));
    }
    out(dp[n-1]);
}

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

using pii = pair<int,int>;
using ll = long long;
#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)
#define all(v) v.begin(),v.end()
#define debug(x) cerr << #x << " : " << x << endl

template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> bool set_max(T &a, const T &b) { return a < b  ? a = b, true : false; }
// vector
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
template<class T> ostream& operator << (ostream &os , const vector<T> &v) { for(const T &t : v) os << "\t" << t; return os << endl; }
// pair
template<class T, class U> ostream& operator << (ostream &os , const pair<T, U> &v) { return os << "<" << v.first << ", " << v.second << ">"; }

const int INF = 1 << 30;
const ll INFL = 1LL << 60;


class Solver {
  public:
    bool solve() {
        ll Q, H, S, D; cin >> Q >> H >> S >> D;
        ll N; cin >> N;
        // 1 2 4 8
        ll ans = 0;
        D = min({D, S * 2, H * 4, Q * 8});
        S = min({S, H * 2, Q * 4});
        H = min({H, Q * 2});
        N *= 4;
        ans += N / 8 * D;
        N = N % 8;
        ans += N / 4 * S;
        N = N % 4;
        ans += N / 2 * H;
        N = N % 2;
        ans += N * Q;
        cout << ans << endl;
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<endl;ashow(a...);}

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<string> vs;

const int MAX_N = 100005;

inline int add(int x,int y)
{
    return (x + y)%MOD;
}

inline int sub(int x,int y)
{
    return (x+MOD-y)%MOD;
}

inline int mul(int x,int y)
{
    return (ll)x*y%MOD;
}

int cnt[26];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    string s;
    cin >> s;
    rep(i,n){
        cnt[(int)(s[i]-'a')]++;
    }
    int ans = 1;
    rep(i,26){
        ans = mul(ans, cnt[i]+1);
    }
    cout << sub(ans,1) << "\n";
    return 0;
}


Measured memory_bytes: 512
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

const int MAXN=100005;

int n,k;
int a[MAXN];

int main(){
    scanf("%d%d",&n,&k);
    int st;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(a[i]==1) st=i;
    }
    int ans=0x3fffffff;
    for(int i=max(1,st-k);i<=st;i++){
        int t=1*ceil(1.0*(i-1)/(k-1))+1*ceil(1.0*(n-i-k+1)/(k-1));
        ans=min(ans,t+1);
        //printf("%d:%d\n",i,t);
    }
    printf("%d\n",ans);
}


Measured memory_bytes: 512
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;

public class Main {

	int INF = 1 << 28;

	void run() {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		
		int dp[][] = new int[h+1][w+1];
		for(int[] a: dp) fill(a, INF);
		dp[1][0] = dp[0][1] = 0;
		for(int i=1;i<=h;i++) {
			String str = sc.next();
			for(int j=1;j<=w;j++) {
				dp[i][j] = str.charAt(j-1) - '0';
			}
		}
		
		for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) {
			dp[i][j] += min(dp[i][j-1], dp[i-1][j]);
		}
		
		System.out.println(dp[h][w]);
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}
}

Measured memory_bytes: 12000
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef pair <int,int> P;
vector <P> G[500];
int mem[500],used[500];
 
int saiki(int ima,int cost){
   
  if(used[ima]) return cost+mem[ima];
  used[ima]=1;
 
  int res=cost;
  for(int i=0;i<G[ima].size();i++)
    res=max(res,saiki(G[ima][i].f,cost+G[ima][i].s));
 
  mem[ima]=res-cost;
  return res;
}
 
 
int main(){
  int n,m;
  cin>>n>>m;
  int flg[501]={};
 
  for(int i=0,a,b,c;i<m;i++){
    cin>>a>>b>>c;
    G[a].push_back(P(b,c));
    flg[b]=1;
  }
  int start=-1;
  for(int i=0;i<n;i++)if(flg[i]==0) start=i;
  cout << saiki(start,0)<<endl;
 
  return 0;
}

Measured memory_bytes: 1220
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define debug(x) cerr << #x " = " << (x) << endl;
#define P(x) cerr << #x << endl;    
#define int long long
typedef pair<int, int> pii;
const int mod = 1000000007;
const double pi =  3.141592653589793;
 
int n, k;
const int maxn = 2e5 + 10;
vector<int> a(maxn), b(maxn);

bool check(int mid)
{
    int req = 0;
    for(int i = 0; i < n; ++ i)
    {
        int is = mid / b[i];
        //debug(is);
        int that = max(0LL, a[i] - is);
        req += that;
    }
   // debug(req);
    return req <= k;
}

int binsearch(int lo, int hi)
{
    int ans = 2e18;
    while(lo <= hi)
    {
        int mid = (lo + hi) / 2;
        if(check(mid))
        {
            //debug(mid);
            ans = min(ans, mid);
            hi = mid - 1;
        }
        else
        {
            lo = mid + 1;
        }
    }
    return ans;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout << fixed << setprecision(9);
    
    
	cin >> n >> k;
	
	for(int i = 0; i < n; ++ i)
	{
	    cin >> a[i];
	}
	sort(a.begin(), a.begin() + n);
	reverse(a.begin(), a.begin() + n);
	
	for(int i = 0; i < n; ++ i)
	{
	    cin >> b[i];
	}
	sort(b.begin(), b.begin() + n);
	cout << binsearch(0, 1e18) << '\n';
    
    return 0;
}
 
 







Measured memory_bytes: 3328
#include<stdio.h>
int main(void)
{
	int N[3];
	int sum = 0;
	for (int i = 0; i < 4; i++) {
		scanf("%d", &N[i]);
		if (N[i] != 1 && N[i] != 9 && N[i] != 7 && N[i] != 4) {
			printf("NO"); 
			return 0;
		}
		sum = sum + N[i];

	}

	if (sum == 21) {
		printf("YES");
	}
	else
		printf("NO");


	return 0;
}

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

int main()
{
    string x;
    int s = 0 , t = 0;
    cin>>x;
    
    for ( int i=0; i < x.length(); i++)
    {
        if ( x[i] == 'S' )
        {
            s++;
        }
        else 
        {
            if ( s > 0 )
            {
                s--;
            }
            else 
            {
                t++;
            }
        }
    }
    cout<<s+t<<endl;

    return 0;
}

Measured memory_bytes: 640
MAX_A = 10**6 + 1
is_prim = [True] * MAX_A
is_prim[0] = is_prim[1] = False
for i in range(2, MAX_A):
    if not is_prim[i]: continue
    for j in range(i+i, MAX_A, i): is_prim[j] = False
prim = [i for i in range(MAX_A) if is_prim[i]]

def solve():
    C = [0] * MAX_A
    for a in A: C[a] += 1
    pairwise = True
    for p in prim:
        if sum(C[p::p]) > 1: pairwise = False
    if pairwise: return 'pairwise'
    from math import gcd
    g = 0
    for a in A: g = gcd(g, a)
    if g == 1: return 'setwise'
    return 'not'


n = int(input())
A = [*map(int, input().split())]
print(solve(), 'coprime')


Measured memory_bytes: 137668
/*
@author: sharrad99
*/
#include<bits/stdc++.h>
using namespace std;
int main(){

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int n, k;
	cin >> n >> k;

	vector<int> h(n + 1, 0);
	for(int i = 1; i <= n; i++) cin >> h[i];

	vector<int> dp(n + 1, INT_MAX);
	dp[0] = dp[1] = 0;
	for(int i = 2; i <= n; i++) {
		for(int j = max(1, i - k); j < i; j++) {
			dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
		}
	}
	cout << dp[n];
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<(int)n; i++)
#define rep2(i, m, n) for(int i=(int)m; i<(int)n; i++)
#define ALL(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;

int main(){
    int N, K;
    cin >> N >> K;
    vector<ll> p(N);
    rep(i, N) cin >> p[i];

    ll sum = 0;
    rep(i, K) sum += p[i];

    ll cmax = sum;
    rep(i, N-K){
        sum += p[i+K] - p[i];
        cmax = max(cmax, sum);
    }
    
    ld ans = (cmax + K) / 2.0;
    cout << setprecision(10) << ans << endl;
}

Measured memory_bytes: 1792
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<bitset>
#include<string>
#include<cstdio>
#include<cctype>
#include<cassert>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>

#define For(i,x,y) for (int i=x;i<y;i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lf else if

#define dprintf(...) fprintf(stderr,__VA_ARGS__)
using namespace std;

typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> Vi;

int IN(){
	int c,f,x;
	while (!isdigit(c=getchar())&&c!='-');c=='-'?(f=1,x=0):(f=0,x=c-'0');
	while (isdigit(c=getchar())) x=(x<<1)+(x<<3)+c-'0';return !f?x:-x;
}

const int N=1000+19;
const int M=5000+19;
const int oo=(1<<30)-1;

struct Edge{
	int y,nxt;
} E[N*2];
int dp[2][M];
int las[N],A[N],F[N];
int n,cnt;

void Add_Edge(int x,int y){
	E[cnt]=(Edge){y,las[x]};las[x]=cnt++;
}
bool dfs(int x){
	for (int i=las[x],y;~i;i=E[i].nxt){
		if (!dfs(y=E[i].y)) return 0;
	}
	int c=0;
	For(i,0,2) For(j,0,M) dp[i][j]=oo;
	dp[c][0]=0;
	for (int i=las[x],y,w,v;~i;i=E[i].nxt){
		c^=1;
		For(j,0,M) dp[c][j]=oo;
		y=E[i].y;
		w=A[y],v=F[y];
		For(j,w,A[x]+1) if (dp[c^1][j-w]!=oo) dp[c][j]=min(dp[c][j],dp[c^1][j-w]+v);
		w=F[y],v=A[y];
		For(j,w,A[x]+1) if (dp[c^1][j-w]!=oo) dp[c][j]=min(dp[c][j],dp[c^1][j-w]+v);
	}
	F[x]=oo;
	For(j,0,A[x]+1) F[x]=min(F[x],dp[c][j]);
	return F[x]!=oo;
}

int main(){
	memset(las,-1,sizeof(las));
	n=IN();
	For(i,2,n+1) Add_Edge(IN(),i);
	For(i,1,n+1) A[i]=IN();
	puts(dfs(1)?"POSSIBLE":"IMPOSSIBLE");
}

Measured memory_bytes: 384
#include "bits/stdc++.h"
#define ll long long
#define ALL(x) x.begin(),x.end()
#define MOD 1000000007
using namespace std;

int main(){
 ll N,K;
 cin>>N>>K;
 string s;
 cin>>s;

 ll happy=0;
 ll unhap=0;
char tmp='z';
 for(auto c:s){
   if(c==tmp){
     happy++;
   }
   else{
     unhap++;
     tmp = c;
   }
 }

 for(ll i=0;i<K;i++){
   if(unhap>2){
     unhap-=2;
   }
   else if(unhap==2){
     unhap--;
   }
   
 }

cout<<N-unhap<<endl;

}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> piii;
const int MAXN=1005,INF=1e9;
char a[MAXN][MAXN];
int dist[MAXN][MAXN];
int n,m;
int dr[]={0,0,-1,1};
int dc[]={-1,1,0,0};

bool border(int x,int y){
	return x>=1 && x<=n && y>=1 && y<=m;
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	int x1,y1,x2,y2;
	cin >> n >> m >> x1 >> y1 >> x2 >> y2;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin >> a[i][j];
			dist[i][j]=INF;
		}
	}
	priority_queue<piii,vector<piii>,greater<piii> > PQ;
	PQ.push({0,{x1,y1}});
	dist[x1][y1]=0;
	while(!PQ.empty()){
		piii now=PQ.top();
		PQ.pop();
		if(dist[now.second.first][now.second.second]<now.first)continue;
		for(int i=0;i<4;i++){
			if(!border(now.second.first+dr[i],now.second.second+dc[i]))continue;
			if(dist[now.second.first+dr[i]][now.second.second+dc[i]]<=now.first || a[now.second.first+dr[i]][now.second.second+dc[i]]=='#')continue;
			dist[now.second.first+dr[i]][now.second.second+dc[i]]=now.first;
			PQ.push({now.first,{now.second.first+dr[i],now.second.second+dc[i]}});
		}
		for(int i=-2;i<=2;i++){
			for(int j=-2;j<=2;j++){
				if(!border(now.second.first+i,now.second.second+j))continue;
				if(dist[now.second.first+i][now.second.second+j]<=now.first+1 || a[now.second.first+i][now.second.second+j]=='#')continue;
				dist[now.second.first+i][now.second.second+j]=now.first+1;
				PQ.push({now.first+1,{now.second.first+i,now.second.second+j}});
			}
		}
	}
	if(dist[x2][y2]==INF)dist[x2][y2]=-1;
	cout << dist[x2][y2] << '\n';
	return 0;
}



Measured memory_bytes: 8636
n = gets.to_i
list = []

n.times do
  cmd, num = gets.split
  case cmd
  when "insert"
    list.unshift(num.to_i)
  when "delete"
    index = list.find_index { |i| i == num.to_i }
    list.delete_at(index) unless index.nil?
  when "deleteLast"
    list.pop
  when "deleteFirst"
    list.shift
  end 
end

puts list.join(" ")

Measured memory_bytes: 23064
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <map>

#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;
const int MOD = 1000000007;

int main() {
  int X; cin >> X;
  string result = (X==3||X==5||X==7) ? "YES" : "NO";
  cout << result << endl;
  return 0;
}

Measured memory_bytes: 256
s = int(input())
a = [0] * 1000000
a[0] = s
m = 0
i = 0
while i < 1000000:
	if a[i] % 2 == 0:
		a[i + 1] = a[i] // 2
	else:
		a[i + 1] = a[i] * 3 + 1
	for j in range(i + 1):
		if a[j] == a[i + 1]:
			m = i + 2
			break
	if m > 0:
		break
	i += 1
print(m)


Measured memory_bytes: 10868
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <iomanip>
#include <sys/time.h>
#include <random>
using namespace std;

#define endl '\n'
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define UNIQ(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef complex<double> comp;
typedef vector< vector<ld> > matrix;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};
const int inf = 1e9 + 9;
const ll mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1);

string s[3];
int l[3], idx[3];

char solve() {
    for (int i = 0; i < 3; i++) {
        idx[i] = 0;
        l[i] = s[i].size();
    }

    int t = 0;
    int res;
    while (true) {
        if (idx[t] == l[t]) return 'A' + t;
        idx[t]++;
        t = s[t][idx[t]-1] - 'a';
    }
    return 'A';
}

void input() {
    for (int i = 0; i < 3; i++) cin >> s[i];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);

    input();
    cout << solve() << endl;
}

Measured memory_bytes: 256
//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\

#include <bits/stdc++.h>

#define F first
#define S second
#define _sz(x) (int)x.size()
#define pb push_back

#define error(x) cout << #x << " = " << x << endl

using namespace std ;
using ll = long long ;
using ld = long double ;
using pii = pair <int , int> ;

const int N = 5000 + 20 ;
int n , k ;
string s ;

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

	cin >> n >> k ;
	cin >> s ;

	string t = s , m = s ;
	reverse(t.begin() , t.end()) ;

	s += t ;

	for (int i = 0 ; i < n ; i ++)
		m = min(m , s.substr(i , n)) ;

	int a = 0 , b = 0 ;
	while (a < n && m[0] == m[a]) a ++ ;

	k -- ;

	if (k > 20 || (a << k) > n) b = n ;
	else b = a << k ;

	for (int i = 0 ; i < b ; i ++) cout << m[0] ;
	cout << m.substr(a , n - b) ;
	cout << '\n' ;
}


Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;

const int N = 100010;
int n, m, cnt;
int head[N], color[N];
bool flag = true;

struct Edge{
	int to, nex;
}e[2*N];

inline int read()
{
	char ch = getchar(); int x = 0, flag = 0;
	while (!isdigit(ch)){ if (ch == '-') flag = 1; ch = getchar(); }
	while (isdigit(ch)){ x = x * 10 + ch - '0'; ch = getchar(); }
	return flag ? -x : x;
}

inline void add(int x, int y)
{
	e[++ cnt].to = y; e[cnt].nex = head[x]; head[x] = cnt;
}

inline void dfs(int u, int c)
{
	color[u] = c;
	for (int i = head[u]; i; i = e[i].nex){
		int v = e[i].to;
		if (color[v] == -1) dfs(v, c^1);
		else if (color[v] == c) flag = false;
	}
}

int main()
{
	n = read(), m = read();
	for (int i = 1; i <= m; i ++){
		int x = read(), y = read();
		add(x, y); add(y, x);
	}
	memset(color, -1, sizeof color);
	for (int i = 1; i <= n; i ++)
		if (color[i] == -1) dfs(i, 0);
	long long ans;
	//printf("%d\n", flag);
	if (!flag){
		ans = 1LL*n*(n-1)/2;
		ans -= m; printf("%lld\n", ans);
	} else{
		int sum = 0;
		for (int i = 1; i <= n; i ++)
			if (color[i] == 0) sum ++;
		ans = 1LL*sum*(n-sum);
		ans -= m; printf("%lld\n", ans);
	}
	return 0;
}

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

int main() {
  int a;
  cin >> a;
  if((a/10)%111==0 || (a%1000)%111==0)
  {
    cout << "Yes" << endl;
  }
  else
  {
    cout << "No" << endl;
  }
  return 0;
}

Measured memory_bytes: 256
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
#define int long long


signed main(void){
  int x,y;
  cin >> x >> y;

  int ret = 0;
  if(abs(x)>abs(y)){
    if(x>0){
      x = -x;
      ret++;
    }
    ret += abs(x+abs(y));
    x += abs(x+abs(y));
    if(x!=y){
      ret++;
      x = -x;
    }
  }
  else{
    if(x<0){
      x = -x;
      ret++;
    }
    ret += abs(abs(y)-x);
    x += abs(abs(y)-x);
    if(x!=y){
      ret++;
      x = -x;
    }
  }
  cout << ret << "\r\n";

}


Measured memory_bytes: 256
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
struct Result{
  bool value;
  int p;
  Result(bool b, int p) : value(b), p(p) {}
};
int S;
Result expr(const string& s, int p);
Result factor(const string& s, int p);
bool is_ope(char c){
  string s = "*+-";
  REP(i, s.size()) if(c == s[i]) return true;
  return false;
}
Result expr(const string& s, int p){
  Result r = factor(s, p);
  while(is_ope(s[r.p])){
    if(s[r.p] == '-') assert(s[r.p + 1] == '>');
    Result r_ = factor(s, (s[r.p] == '-') ? r.p + 2 : r.p + 1);
    if(s[r.p] == '*'){
      r.value = r.value && r_.value;
    }else if(s[r.p] == '+'){
      r.value = r.value || r_.value;
    }else {
      r.value = !r.value || r_.value;
    }
    r.p = r_.p;
  }
  return r;
}
Result factor(const string& s, int p){
  if(s[p] == '('){
    Result r = expr(s, p + 1);
    assert(s[r.p] == ')');
    r.p ++;
    return r;
  }else if(islower(s[p])){
    return Result((S >> (s[p] - 'a')) & 1, p + 1);
  }else if(s[p] == 'T'){
    return Result(true, p + 1);
  }else if(s[p] == 'F'){
    return Result(false, p + 1);
  }else if(s[p] == '-'){
    Result r = factor(s, p + 1);
    r.value ^= 1;
    return r;
  }else {
    assert(false);
  }
}

int main(){
  string s;
  while(cin >> s && s != "#"){
    string a, b;
    REP(i, s.size()) if(s[i] == '='){
      a = s.substr(0, i);
      b = s.substr(i + 1);
    }
    bool equal = true;
    for(S = 0; S < 1<<10; S++){
      if(expr(a, 0).value != expr(b, 0).value){
        equal = false;
        break;
      }
    }
    if(equal) cout << "YES" << endl;
    else cout << "NO" << endl;
  }
  return 0;
}

Measured memory_bytes: 1236
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define repi(i,x) for(auto i=(x).begin(),i##_fin=(x).end();i!=i##_fin;i++)
#define all(x) (x).begin(), (x).end()
#define input(array) repi(array##_itr, array) { cin >> *(array##_itr); }
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
string solve(bool a) { return ((a) ? "Yes" : "No"); }
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int , int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1;} return 0;}
const long long INFLL = 1LL<<60;
const int INF = 1<<30;
const double PI=acos(-1);
int main(){
	int n;
	int ans=0;
	cin >>n;
	cout << ((n+999) / 1000* 1000 - n) << endl;
}

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

int f(int x)
{
    int y;
    while(x > 0){
        y = x % 10;
        x /= 10;
    }
    return y;
}

void Main()
{
    int n;
    cin >> n;
    auto m = vector<vector<int>>(10, vector<int>(10));
    for (int i = 1; i <= n; i++)
    {
        m[f(i)][i%10]++;
    }

    long ans = 0;
    for (int i = 1; i < 10; i++)
    {
        for (int j = 1; j < 10; j++)
        {
            ans += (long)m[i][j] * m[j][i];
        }
    }
    
    cout << ans << endl;
}

int main(int argc, char **argv)
{
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    Main();
    return 0;
}


Measured memory_bytes: 3652
# -*- coding: utf-8 -*-
"""
https://beta.atcoder.jp/contests/abc087/tasks/arc090_b

"""
import sys
from sys import stdin
from heapq import heappush, heappop
input = stdin.readline


def solve(data, N):
    scores = [-1] * (N+1)       #  各人の位置
    graphs = [[] for _ in range(N+1)] #  各人が持っている l, r dの情報
    in_degs = [0] * (N+1)       #  トポロジカルソート用の入力次数

    #  入力されたデータから入力次数とグラフ情報を組み立てる
    for l, r, d in data:
        in_degs[r] += 1
        graphs[l].append((r, d))

    pq = []                     #  このキューが空になるまで処理を続ける
    for pos in range(1, N+1):
        if in_degs[pos] == 0:
            heappush(pq, pos)   #  最初は入力次数が0の人を全てキューに登録しておく

    cs = 1                      #  ダミーのスコア
    while pq:
        pos = heappop(pq)
        if scores[pos] < 0:     #  自身に点数が付いていなければ、任意の点数を設定しておく
            scores[pos] = cs
        for r, d in graphs[pos]: #  自身が持っている情報に基づいて、他のノードを点数付けする
            if scores[r] < 0:
                scores[r] = scores[pos] + d
            else:
                if scores[r] != scores[pos] + d: #  他の人によりrが既に点数付けされており、自身の情報と矛盾していれば最終的な答えは'No'になる
                    return 'No'
            in_degs[r] -= 1     #  現在の人からの入力を消去するので、次数を-1する。結果として0になれば次の探索開始ノードとして登録
            if in_degs[r] == 0:
                heappush(pq, r)

    for d in in_degs[1:]:       #  最後まで情報を処理しても入力次数が残っているということは、どこかでループしている。ループは L < R にならないので、'No'を返す
        if d > 0:
            return 'No'
    return 'Yes'


def main(args):
    N, M = map(int, input().split())
    data = []
    for _ in range(M):
        l, r, d = map(int, input().split())
        data.append((l, r, d))
    ans = solve(data, N)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])


Measured memory_bytes: 103644
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
 
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
 
const int oo = 1e9 + 7;
const int mod = 1e9 + 7, maxn = (1 * 1e5) + 10;
const long double PI = acos(-1);

int solve (ll k){
    ll val = 7%k;

    for (int i=1; i<=k+1; i++){
        if (val == 0) return i;
        val = (val*10 + 7) % k;
    }
    return -1;
}

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

    int ans = solve(k);

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3652
#include <stdio.h>

int main(void)
{
	int i, j, n, s;
	
	scanf("%d", &n);
	
	for (i = 0; i < n; i++){
		scanf("%d", &s);
		
		printf("Case %d:\n", i + 1);
		
		for (j = 0; j < 10; j++){
			s = s * s / 100 % 10000;
			
			printf("%d\n", s);
		}
	}
	
	return (0);
}

Measured memory_bytes: 592
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (Scanner sc = new Scanner(System.in)) {
            while (sc.hasNextInt()) {
                int a = sc.nextInt();
                int b = sc.nextInt();
                int c = sc.nextInt();
                int x = sc.nextInt();
                int y = sc.nextInt();
                System.out.println(Math.min(
                    a*x+b*y,
                    Math.min( (x < y)?2*c*x+b*(y-x):2*c*y+a*(x-y),
                    Math.max(x,y)*2*c)));
            }
        }
    }
}

Measured memory_bytes: 22484