akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  for(int i = 0;i < n;i++){
    cin >> a[i];
    a[i] -= i+1;
  }
  sort(a.begin(),a.end());
  long long answer = 0;
  for(int i = 0;i < n/2;i++){
    answer += a[n-1-i]-a[i];
  }
  cout << answer << endl;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;

#define ll long long

const int N = 2e5 + 5;

int n;
ll K;
int a[N], h[N];
int fa[N], c[N];
boolean vis[N];

int main() {
	scanf("%d%lld", &n, &K);
	for (int i = 0; i < n; i++) {
		scanf("%d", a + i);
	}
	for (int i = n - 1; ~i; i--)
		h[a[i]] = i + n;
	for (int i = n - 1; ~i; i--) {
		int suf = h[a[i]];
		fa[i] = (suf + 1) % n;
		c[i] = (suf + 1) / n;
		h[a[i]] = i;
	}
	int p = 0, q;
	vector<int> stk;
	do {
		vis[p] = true;
		stk.push_back(p);
		p = fa[p];
	} while (!vis[p]);
	int sumcost = 0;
	do {
		q = stk.back();
		stk.pop_back();
		sumcost += c[q];
	} while (q ^ p);
	stk.clear();
	int x = 0;
	while (K - c[x] >= 1 && (x ^ p))
		K -= c[x], x = fa[x];
	if (x == p)
		K = (K - 1) % sumcost + 1;
	while (K - c[x] >= 1)
		K -= c[x], x = fa[x];
	memset(vis, false, sizeof(vis));
	for (int i = x; i < n + (K - 1) * n; i++) {
		int z = a[i % n];
		if (!vis[z]) {
			stk.push_back(z);
			vis[z] = true;
		} else {
			int y = 0;
			do {
				y = stk.back();
				stk.pop_back();
				vis[y] = false;
			} while (y ^ z);
		}
	}
	for (auto x : stk) {
		printf("%d ", x);
	}
	return 0;
}


Measured memory_bytes: 5748
#include <cstdio>
#include <algorithm>
using namespace std;
int K, N, A[200005];
int main() {
  scanf("%d %d", &K, &N);
  for(int i = 1; i <= N; ++i) scanf("%d", &A[i]);
  A[N+1] = A[1]+K;
  int diff = 0;
  for(int i = 1; i <= N; ++i) {
    diff = max(diff, A[i+1]-A[i]);
  }
  printf("%d\n", K-diff);
}

Measured memory_bytes: 896
#include <bits/stdc++.h> //キャンディー配分
using namespace std;


int main(){

long long N,x;
cin >> N >> x;

vector<int> good(N);

for(int i=0; i<N ;i++){
    cin >> good.at(i);
}

sort(good.begin(),good.end());
int count =0;

    for(int i=0;i<N; i++){
        if(x >= good.at(i)) {
            count++;
            x -= good.at(i);
            }
        else break;
    }

if (x > 0 && N == count) count--;

cout << count << endl;
}

Measured memory_bytes: 256
/*
　　　　　　 ＿＿
　　　　　／＞　　 フ
　　　　　|   _　 _l  
　 　　　／` ミ＿x ノ   Zzz..
　　 　 /　 　 　 |   コード見ちゃだめ！！
　　　 /　 ヽ　　  ﾉ
　 　 │　　|　|  |   
　／￣|　　 |　|  |
　| (￣ヽ＿_ヽ_)_ _)
　＼二つ
*/

#include <bits/stdc++.h>
using namespace std;
#define SORT(a) sort((a).begin(),(a).end())
#define RSORT(a) reverse((a).begin(),(a).end())
#define REP(i, n) for(int i = 0; i < n; i++)
const int MOD = 1e9+7;
typedef long long ll;

int main(){
  int k,s;
  cin>>k>>s;
  int count=0;
  for(int i=0;i<=k;i++){
    for(int j=0;j<=k;j++){
      if(s-j-i<=k && s-j-i>=0) count++;
    }
  }
  cout<<count<<endl;


}

Measured memory_bytes: 256
#pragma GCC optimize(2)
// Happy TLE and WA every day!
// by: zxb the vegetable chicken
#include<bits/stdc++.h>
#define mp				make_pair
#define rep(i,n)		for(int i = 0; i < n; i++)
#define foreach(i,c)	for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define BINF			0x7fffffff
#define INF				0x3f3f3f3f
#define LINF			3223372036854775807
#define END(s)			{cout<<s; return 0;}
#define CON(s)			{cout<<s; continue;}
#define BRE(s)			{cout<<s; break;}
#define pb				push_back
//#define int				long long
#define All(a)			a.begin(), a.end()  //with A in CAPITAL!!!
#define sz(a)			(int)a.size()
#define F				first
#define S				second
//#define usingFiles
using namespace std;

const int rp = 666666;
const bool debug = 1;
const bool I_good_vegetable_a = 1;

signed main(){
	ios::sync_with_stdio(false);
	#ifdef usingFiles
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
	int a, b;
	cin >> a >> b;
	cout << a + b/2 << endl;
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n, m;
    cin >> n >> m;
    vector<long long>v;
    vector<pair<long long, int> >cards;
    for(int i = 0; i < n; i++){
        long long x;
        cin >> x;
        v.push_back(x);
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < m; i++){
        int x;
        long long y;
        cin >> x >> y;
        cards.push_back({y, x});
    }
    sort(cards.rbegin(), cards.rend());
    int p = 0;
    for(int i = 0; i < m; i++){
        int indx = upper_bound(v.begin() + p, v.end(), cards[i].first) - v.begin();
        if(indx <= p){
            break;
        }
        else{
            for(int q = p; q < min(indx, p + cards[i].second); q++){
                v[q] = cards[i].first;
            }
            p = min(indx, p + cards[i].second);
        }
    }
    long long ans = 0;
    for(int i = 0; i < n; i++){
        ans = ans + v[i];
    }
    cout << ans;
    return 0;
}

Measured memory_bytes: 3188
n = gets.chomp.to_i

h = gets.chomp.split.map(&:to_i)

(n-1).times do |i|
  # 前後を見て、減らせるなら減らす
  if h[i] -1 <= h[i+1] && (h[i-1] || 0) <= h[i] - 1
    h[i] = h[i] - 1
    next
  end
  # 減らせない場合、そのままで満たしているかチェック
  if h[i] <= h[i+1]
    next
  end
  puts "No"
  exit
end
puts "Yes"


Measured memory_bytes: 9604
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <cmath>

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

constexpr ll LLINF {1001002003004005006};//ll = 9*LLINF
constexpr int INTINF {1000000000};//int = 2*INTINF

#define  rep(i,n)   for(int i = 0; i < (n); ++i)
#define rrep(i,n)   for(int i = 1; i <= (n); ++i)
#define drep(i,n)   for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for(int i = s; i < t;  ++i)

template<typename T>
void maxs(T& x, T&& y) {
  x=std::max(x,y);
}
template<typename T>
void maxs(T& x, T& y) {
  x=std::max(x,y);
}

template<typename T>
void mins(T& x, T&& y) {
  x=std::min(x,y);
}
template<typename T>
void mins(T& x, T& y) {
  x=std::min(x,y);
}
const int mod {998244353};
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};
struct combination {
  vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
};

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

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

  combination c(200005);

  mint ans {};
  mint col = m;
  for (int x = n-1;x >= 0;--x) {
    mint now {col};
    now *= c(n-1, x);
    if (x <= k) ans += now;
    col *= m-1;
  }
  cout << ans.x << '\n';
}


Measured memory_bytes: 6364
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	string s;
	cin>>s;
	char zh='0';
	int sum=0;
	for(int i=0;i<n;)
	{
		sum++;
		zh=s[i];
		i++;
		while(zh==s[i])
		{
			i++;
		}
		
	}
	cout<<sum;
}

Measured memory_bytes: 512
#include <stdio.h>

int main(){
    
    int dishes[5] = {};
    int ten[5] = {};
    int ans[5] = {};
    int temp;
    int min;
    
    for(int i = 0;i < 5;i++){
        
        do{
            
            scanf("%d",&dishes[i]);
            
        }while(dishes[i] < 1 || dishes[i] > 123);
    }
    
    for(int i = 0;i < 5;i++){
        
        temp = 10;
        
        while(temp < dishes[i]){
            
            temp+=10;
        }
        
        ten[i] = temp;
    }
    
    ans[0] = ten[0] + ten[1] + ten[2] + ten[3] + dishes[4];
    ans[1] = ten[0] + ten[1] + ten[2] + dishes[3] + ten[4];
    ans[2] = ten[0] + ten[1] + dishes[2] + ten[3] + ten[4];
    ans[3] = ten[0] + dishes[1] + ten[2] + ten[3] + ten[4];
    ans[4] = dishes[0] + ten[1] + ten[2] + ten[3] + ten[4];
    
    min = ans[0];
    
    for(int i = 1;i < 5;i++){
        
        if(min > ans[i]){
            
            min = ans[i];
        }
    }
    
    printf("%d",min);
    
    return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
#define PI 3.14159265358979323846
#define MAXINF (1e18L)
#define INF (1e9L)
#define EPS (1e-9)
#define MOD ((ll)(1e9+7))
#define REP(i, n) for(int i=0;i<int(n);++i)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define ALL(v) v.begin(),v.end()
#define FIND(v,x) (binary_search(ALL(v),(x)))
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v));reverse(ALL(v))
#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define Yes(n) cout<<((n)?"Yes":"No")<<endl
#define YES(n) cout<<((n)?"YES":"NO")<<endl
#define pb push_back
#define fi first
#define se second
using namespace std;
template<class A>void pr(A a){cout << (a) << endl;}
template<class A,class B>void pr(A a,B b){cout << a << " "  ;pr(b);}
template<class A,class B,class C>void pr(A a,B b,C c){cout << a << " " ;pr(b,c);}
template<class A,class B,class C,class D>void pr(A a,B b,C c,D d){cout << a << " " ;pr(b,c,d);}
template<class T> void pr_vec(vector<T> &v, char ep=' '){int n=v.size();REP(ni,n){cout<<v[ni];if(ni!=n-1)cout<<ep;}cout<<endl;};
template<class T> inline bool chmin(T& a, T b){return a>b ? a=b, true : false;}
template<class T> inline bool chmax(T& a, T b){return a<b ? a=b, true : false;}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;

int main(void)
{
    int N;
    cin >> N;
    vector<ll> A(N),B(N);
    REP(i,N) cin >> A[i] >> B[i];
    SORT(A);
    SORT(B);
    if(N&1){
        pr(B[N/2]-A[N/2]+1);
    }else{
        pr((B[N/2-1]+B[N/2])-(A[N/2-1]+A[N/2])+1);
    }
}

Measured memory_bytes: 6360
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define REP(i,x,n) for(int i = (x); i < (n); ++i)
#define ll long long
#define P pair<ll,ll>
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()

const ll mod = 1e9+7;
const ll INF = 1e18;
const double pi = acos(-1);

int main(void)
{
    ll a,b,c,d,ans;
    cin>>a>>b>>c>>d;
    cout<<max({a*c,a*d,b*c,b*d})<<endl;
    return 0;
}

Measured memory_bytes: 3636
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    x,n = LI()
    if x < n:
        print(0)
    else:
        print(10)
    return

#B
def B():
    n,x = LI()
    l = LI()
    d = 0
    ans = 1
    for i in range(n):
        d += l[i]
        if d <= x:
            ans += 1
    print(ans)
    return

#C
def C():
    w,h,x,y = LI()
    ans = w*h/2
    k = 0
    if x == w/2 and y == h/2:
        k = 1
    print(ans,k)
    return

#D
def D():
    n,k = LI()
    a = LI()
    b = [a[i] for i in range(n)]
    for i in range(n-1):
        b[i+1] += b[i]
    b.insert(0,0)
    ans = 0
    for l in range(n):
        r = bisect.bisect_left(b,b[l]+k)
        ans += n-r+1
    print(ans)
    return

#E
def E():
    n,m = LI()
    s = LI()
    t = LI()
    dp = [[0 for i in range(m+1)] for j in range(n+1)]
    for i in range(n+1):
        dp[i][0] = 1
    for j in range(m+1):
        dp[0][j] = 1
    for i in range(1,n+1):
        for j in range(1,m+1):
            if s[i-1] == t[j-1]:
                dp[i][j] = dp[i-1][j]+dp[i][j-1]
            else:
                dp[i][j] = dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+mod
            if dp[i][j] >= mod:
                dp[i][j] %= mod
    print(dp[n][m])
    return

#F
def F():
    return

#Solve
if __name__ == "__main__":
    E()


Measured memory_bytes: 77532
/*PM1 問題１８*/
#include<stdio.h>
int main()
{
	int i, j, n;
	int count[100];
	int temp[100];

	scanf("%d\n", &n);

	for (i = 0; i <n; i++)
	{
		scanf("%d\n", &count[i]);
	}

	for (i = 0; i < n; i++)
	{
		temp[n - (i+1)] = count[i];
	}

	for (i = 0; i < n-1; i++)
	{
		printf("%d ", temp[i]);
	}
	printf("%d\n", temp[n-1]);

	return 0;
}


Measured memory_bytes: 2148
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    edges[a - 1].append(b - 1)

def find_loop(index, visited, cycle):
    visited[index] = True
    cycle.append(index)
    for i in edges[index]:
        if visited.get(i):
            return cycle[cycle.index(i):]
        ret = find_loop(i, visited, cycle)
        if ret:
            return ret
    visited[index] = False
    cycle.pop(-1)
    return None

def find_local_minimum(cycle):
    nxt = {i: j for (i, j) in zip(cycle, cycle[1:] + cycle[:1])}
    processed = set()
    i = cycle[0]
    while i not in processed:
        processed.add(i)
        for j in edges[i]:
            if j in nxt:
                k = nxt[i]
                while k != j:
                    k = nxt.pop(k)
                nxt[i] = j
        i = nxt[i]
    start = i
    ret = [i]
    i = nxt[i]
    while i != start:
        ret.append(i)
        i = nxt[i]
    return ret

cycle = None
visited = {}
for start in range(n):
    if start not in visited:
        cycle = find_loop(start, visited, [])
        if cycle:
            break

if cycle:
    ret = find_local_minimum(cycle)
    print(len(ret))
    for i in ret:
        print(i + 1)
else:
    print(-1)



Measured memory_bytes: 48232
#include <bits/stdc++.h>
using namespace std;
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
    return os << "(" << P.first << "," << P.second << ")";}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...) 1
#endif
typedef long long ll;
typedef double ld;
#define pll pair<ll,ll>
#define pii pair<int, int>
#define pb push_back
#define mp(x, y) make_pair((x), (y))
#define F first
#define S second
#define I insert
#define vi vector<int>
#define vll vector<ll>
#define vpll vector<pll>
#define vpii vector<pii>
#define all(x) (x).begin(), (x).end()
#define sz(x) (ll)(x).size()

const ll Mod = 1e9 + 7;
const int N = 2e5 + 100;
int n;
ll A[N], X[N];

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

    int n; cin >> n;
    ll zz = 0;
    for(int i = 1;i <= n ; ++i){
	cin >> A[i];
	if(i != 1)
	zz ^= A[i];
    }
    cout << zz << ' ';
    A[1] ^= zz;
    for(int i = 2 ;i <= n ; ++i){
	ll p = (A[1]^A[i]);
	cout << p << ' '; 
    }
    cout << endl;

    return 0;
}


Measured memory_bytes: 5164
class UnionFind:
    def __init__(self, N):
        self.rank = [0] * N
        self.parent = [i for i in range(N)]
        self.n = N
        self.size = [1 for i in range(N)]
 
    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]
 
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if (x == y):
            return
 
        if (self.rank[x] < self.rank[y]):
            self.parent[x] = y
            self.size[y] += self.size[x]
        else:
            self.parent[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
 
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    def get_parents(self):
        count = 0
        for i in range(self.n):
            if self.find(i) == i:
                count += 1
 
        return count
 
    def get_size(self, x):
        root = self.find(x)
        return self.size[root]


N,M = map(int,input().split())
A = []
B = []
for i in range(M):
    a,b = map(int,input().split())
    A.append(a-1)
    B.append(b-1)
U = UnionFind(N)

ans = [0 for _ in range(M)]
i = M-1
ans[i] = (N*(N-1))//2

while i > 0:
    if not U.same(A[i],B[i]):
        ans[i-1] = ans[i] - (U.get_size(A[i])*U.get_size(B[i]))
        U.unite(A[i],B[i])
    else:
        ans[i-1] = ans[i]
    i -= 1

print(*ans,sep='\n')





Measured memory_bytes: 20100
# include <iostream>
# include <string>
# include <sstream>
# include <cctype>
using namespace std;
class Stack
{
	public:
		int a[105];
		int cnt = 0;
		void push(int x){ a[cnt++] = x;}
		void pop() {cnt--;}
		int top(){return a[cnt-1];}
};
int x,y;
bool is_digit(string s)
{
	for(int i = 0;i < s.length();i++)
		if(!isdigit(s[i])) return false;
	stringstream ss;
	ss.clear(); ss<<s;
	ss>>x; return true;
}
int main(void)
{
	Stack w;
	string s;
	while(cin>>s)
	{
		if(is_digit(s)) w.push(x);
		else
		{
			x = w.top(); w.pop();
			y = w.top(); w.pop();
			if(s == "+") w.push(x+y);
			if(s == "-") w.push(y-x);
			if(s == "*") w.push(y*x);
			if(s == "/") w.push(y/x);
		}
	}
	printf("%d\n",w.top());
	return 0;
}


Measured memory_bytes: 3320
#!/usr/bin/env python3
n = int(input())

# imos[k]: わたしがちょうどk人誘うときに来れる最大人数
imos = [0] * (10**5 + 2)
for _ in range(n):
    a, b = map(int, input().split())
    imos[a - 1] += 1
    imos[b] -= 1
for i in range(n):
    imos[i + 1] += imos[i]

ans = 0
for i in range(n + 1):
    if imos[i] >= i:
        ans = max(ans, i)
print(ans)



Measured memory_bytes: 9464
import java.util.Scanner;
//atcoderはScannerクラスのsc.next()で行ける
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt();
        long b = sc.nextInt();
        long c = sc.nextInt();
        long d = sc.nextInt();

        long[] ab = new long[]{a,b};
        long[] cd = new long[]{c,d};
        long ans = Long.MIN_VALUE;
        for (long i : ab){
            for (long j : cd){
                if (ans<i*j){
                    ans=i*j;
                }
            }
        }
        System.out.println(ans);
    }
}

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

int main(){
  string sen;
  char ans=97;
  char r = 'a' - 'A';//r=32
  int vec[26] = {};
  int x = 0, y = 0;
  while(getline(cin,sen)){
    for(int i = 0;i<= sen.size();i++){
      if(sen[i] >= 'A' && sen[i] <= 'Z'){
        x = (sen[i]-'A');
        vec[sen[i]-'A']++;
      }
      else if(sen[i] >= 'a' && sen[i] <='z'){
        x = (sen[i]-'a');
        vec[sen[i]-'a']++;
      }
    }
  }
  for(int i = 0;i < 26;i++){
    cout << (char)('a' + i) << " : " << vec[i] << endl;
  }
}


Measured memory_bytes: 3200
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

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

  int s_size = s.size();
  for (int i=0; i<s_size; ++i) {
    for (int j=0; j<s_size; ++j) {
      if (i!=j && s[j]==s[i]) {
        cout << "no" << endl;
        return 0;
      }
    }
  }

  cout << "yes" << endl;
  return 0;
}

Measured memory_bytes: 256
import itertools

N, M, X = map(int, input().split())
B = [tuple(map(int, input().split())) for _ in range(N)]

MAX = 10 ** 7
ans = MAX
for t in itertools.product((0, 1), repeat=N):
    A = [sum(t[j] * B[j][i + 1] for j in range(N)) for i in range(M)]
    if all(a >= X for a in A):
        ans = min(ans, sum(t[j] * B[j][0] for j in range(N)))
print(ans if ans != MAX else -1)


Measured memory_bytes: 9212
#include<bits/stdc++.h>
using namespace std;
char a[105];
int main()
{
	scanf("%s",a);
	if(a[0]=='S'&&a[1]=='U')
	{
		printf("7");
	}
	if(a[0]=='S'&&a[1]=='A')
	{
		printf("1");
	}
	if(a[0]=='M')
	{
		printf("6");
	}
	if(a[0]=='T'&&a[1]=='U')
	{
		printf("5");
	}
	if(a[0]=='W')
	{
		printf("4");
	}
	if(a[0]=='T'&&a[1]=='H')
	{
		printf("3");
	}
	if(a[0]=='F')
	{
		printf("2");
	}
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <queue>
#include <tuple>
#include <cstring>
#include <cctype>
#include <ctype.h>

#define INF 200000000
#define MOD 1000000007

using namespace std;

int main()
{
    int a[5];
    int tmpmin = 1000, tmp, lastdish, sum = 0;
    for (int i = 0; i < 5; i++)
    {
        cin >> a[i];
        tmp = a[i] % 10;
        if (tmp == min(tmpmin, tmp) && tmp != 0)
        {
            lastdish = i;
            tmpmin = tmp;
        }
    }
    //cout << lastdish << endl;
    for (int i = 0; i < 5; i++)
    {

        tmp = a[i] % 10;
        if (lastdish == i)
        {
            sum += a[lastdish];
            continue;
        }
        else if (tmp != 0)
        {
            a[i] += (10 - tmp);
        }
        sum += a[i];
    }
    cout << sum << endl;
    return 0;
}


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

using namespace std;

#define intt long long
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

const int N = 10005;

int n, m;
int a[N];

int main() {

	FAST;

	cin >> n >> m;
	int sum = 0;
	for (int i = 1; i <= m; i ++) {
		cin >> a[i];
		sum += a[i];
	}
	cout << ((n - sum < 0) ? -1 : (n - sum)) << '\n';
	return 0;
}

Measured memory_bytes: 3660
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define All(V) V.begin(), V.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9+7, INF = 1e9;

int main()
{
    char a; cin >> a;
    if(a >= 'a' && a <= 'z'){
        cout << 'a' << endl;
    }
    else cout << 'A' << endl;
    system("pause");
}


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

#define stirng string
#define vvi vector<vector<int>>
#define vi vector<int>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

const int inf = 1e9 + 7;

int main() {

  string s;
  cin >> s;
  if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
    cout << "Yes" << endl;
  } else if (s[0] != s[1] && s[1] == s[2] && s[2] == s[3]) {
    cout << "Yes" << endl;
  } else if (s[3] != s[1] && s[0] == s[1] && s[1] == s[2]) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define full(a) a.begin(),a.end()
#define rfull(a) a.rbegin(),a.rend()

using namespace std;
const double pi=acos(-1.0);
const double pii=2*pi;
const double eps=1e-6;
const long long MOD=1e9+7;

long long f(int n,int m, int x,bitset<12>& bs, vector<long long>& c, vector<vector<long long>>& a){
    vector<long long> ans(m,0);
    long long cst=0;
    for(int i=0;i<n;i++){
        if(bs[i]==1){
            for(int j=0;j<m;j++){
                ans[j]+=a[i][j];
            }
            cst+=c[i];
        }
    }
    for(int i=0;i<m;i++){
        if(ans[i]<x){
            return -1;
        }
    }
    return cst;
}

void solve(){
    int n,m,x;cin>>n>>m>>x;
    vector<long long> c(n,0ll);
    vector<vector<long long>> a(n,vector<long long>(m,0ll));
    for(int i=0;i<n;i++){
        cin>>c[i];
        for(int j=0;j<m;j++) cin>>a[i][j];
    }
    long long ans=-1;
    for(int i=1;i<=(1<<12);i++){
        bitset<12> bs(i);
        long long ret=f(n,m,x,bs,c,a);
        if(ans==-1 || (ret>=0 && ret<ans)) ans=ret;
    }
    cout<<ans<<"\n";
}

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

    solve();
    return 0;
}

Measured memory_bytes: 3644
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) for (int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
#define MOD 1000000007
int main(){
    int m1,m2,d1,d2;
    cin>>m1>>d1>>m2>>d2;
    if(d2==1) cout<<1<<endl;
    else cout<<0<<endl;
}

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

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

//YYMM
int YYMM;
YYMM = (S.at(2) - '0') * 10 + (S.at(3) - '0');
bool YYMM_flag = false;
if(YYMM <= 12 && YYMM > 0) YYMM_flag = true;

//MMYY
int MMYY;
MMYY = (S.at(0) - '0') * 10 + (S.at(1) - '0');
bool MMYY_flag = false;
if(MMYY <= 12 && MMYY > 0) MMYY_flag = true;

if(YYMM_flag && MMYY_flag) cout << "AMBIGUOUS" << endl;
else if(YYMM_flag) cout << "YYMM" << endl;  
else if(MMYY_flag) cout << "MMYY" << endl;
else cout << "NA" << endl;   


}

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

int main() {
  string S;
  cin >> S;
  
  if (S.at(2)==S.at(3) && S.at(4)==S.at(5)){
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }
}

Measured memory_bytes: 3576
#include<iostream>
#include<cmath>
using namespace std;
struct Circle { double x, y, r; };
int main() {
    Circle p, q;
    cin >> p.x >> p.y >> p.r;
    cin >> q.x >> q.y >> q.r;
    double dist = sqrt((p.x - q.x)*(p.x - q.x) + (p.y - q.y)*(p.y - q.y));
    if (dist > p.r + q.r)cout << "4" << endl;
    else if (dist == p.r + q.r) {
        cout << "3" << endl;
    }
    else if (p.r <= q.r) {
        if (p.r + dist == q.r)cout << "1" << endl;
        if (p.r + dist < q.r)cout << "0" << endl;
        if (p.r + dist > q.r)cout << "2" << endl;
    }
    else {
        if (q.r + dist == p.r)cout << "1" << endl;
        if (q.r + dist < p.r)cout << "0" << endl;
        if (q.r + dist > p.r)cout << "2" << endl;
    }
    return 0;
}

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

int main()
{
	int n,i,j,k=0;
	cin>>n;
	int temp=n,s=0,s1=0;
	while(n!=0)
	{
		s1=n%10;
		//cout<<s1<<endl;
		s=s+s1;
		n=n/10;
	}
	//cout<<s<<endl;
	//k=k+temp%s;
	//cout<<k<<endl;
	if (temp%s==0)
		cout<<"Yes";
	else
		cout<<"No";
	return 0;
}

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

long long int A[200000 + 5];
vector<long long int> visited;
bool beenBefore[200000 + 5];
	
int main()
{
	long long int N;
	long long K;
	scanf("%lld%lld", &N, &K);
	
	
	for(int i = 1; i <= N; i++)
		scanf("%lld", &A[i]);
	
	long long int src = 1;
	
	visited.push_back(1);
	beenBefore[1] = true;
	
	for(int i = 0; i <= N + 5; i++)
	{
		src = A[src];
		visited.push_back(src);
		
		if(beenBefore[src])
		{
			break;
		}
		
		beenBefore[src] = true;
	}
	
	int firstFound = -1, secondFound = -1;
	for(int i = 0; i < (int)(visited.size()); i++)
	{
		if(visited[i] == src)
		{
			if(firstFound == -1)
				firstFound = i;
				
			else
				secondFound = i;
		}
	}
	
	if(K <= (long long)(firstFound))
	{
		printf("%lld", visited[K]);
		return 0;
	}
	
	long long int cycleLength = secondFound - firstFound;
	long long diff = K - firstFound;
	
	printf("%lld", visited[(diff % cycleLength) + firstFound]);
}

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

using namespace std;

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

        switch (n % 10){
                case 2:
                case 4:
                case 5:
                case 7:
                case 9:
                        cout << "hon" << endl;
                        break;
                case 0:
                case 1:
                case 6:
                case 8:
                        cout << "pon" << endl;
                        break;
                case 3:
                        cout << "bon" << endl;
                        break;
        }
        return 0;
}

Measured memory_bytes: 3580
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;

int main()
{
  int n,i,vec1[100],vec2[100];
  double sum1=0,sum2=0,sum3=0,max=0;

  cin>>n;
  for(i=0;i<n;i++)
    cin>>vec1[i];
  for(i=0;i<n;i++)
    cin>>vec2[i];
  for(i=0;i<n;i++){
    sum1+=abs(vec1[i]-vec2[i]);
    if(abs(vec1[i]-vec2[i])>max) max=abs(vec1[i]-vec2[i]);
    sum2+=abs(vec1[i]-vec2[i])*abs(vec1[i]-vec2[i]);
    sum3+=abs(vec1[i]-vec2[i])*abs(vec1[i]-vec2[i])*abs(vec1[i]-vec2[i]);
  }
  printf("%f\n", sum1);
  printf("%f\n", sqrt(sum2));
  printf("%f\n", pow(sum3,1.0/3));
  printf("%f\n", max);
}


Measured memory_bytes: 1224
#include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#define ll long long
#define BUF 50
using namespace std;
ll MOD = 1e9 + 7;
ll A, B, C, D, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S;
ll ans = 0;
int main() {
	cin >>N;
	vector<char> march={ 'M','A','R','C','H' };
	vector<ll> marchN(5, 0);
	for (int i = 0; i < N; i++) {
		cin >> S;
		for (int j = 0; j < 5; j++) {
			if (S[0] == march[j])marchN[j]++;
		}
	}
	for (int bit = 0; bit < (1 << 5); bit++) {
		ll flag = 0;
		ll tmp=1;
		for (int i = 0; i < 5; i++) {
			if (bit&(1 << i)) {
				flag++;
				tmp *= marchN[i];
			}
		}
		if (flag == 3)ans += tmp;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
N,M,C =map(int,input().split())
B=list(map(int,input().split()))
A=[None]*N
for i in range(N):
    A[i]=list(map(int,input().split()))

ans=0
for i in range(N):
    S=0
    for j in range(M):
        S+=A[i][j]*B[j]
    if S+C>0:
        ans+=1
print(ans)


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

int main() {
  ll N,A,B; cin >> N >> A >> B;
  ll ans = 0;
  for(ll i = 1; i <= N; i++){
    ll check = 0;
    check += i % 10;
    check += (i / 10) % 10;
    check += (i / 100) % 10;
    check += (i / 1000) % 10;
    check += (i / 10000) % 10;
    if(check >= A && check <= B){
      ans += i;
    }
  }
  cout << ans;
}
  

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

int main() {
  ll n,k;
  cin >> n >> k;
  ll temp=1;
  ll ans=0;
  while(n/temp) {
    temp*=k;
    ans++;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int arr[100100];
int main()
{
    int n,i,j,k,x,y,ans,curr,m;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    sort(arr,arr+n);
    int a = arr[0];
    bool can = 0;
    ans = 1e9+7;
    for(i=1;i<n;i++)
    {
        ans = min(ans,__gcd(a,arr[i]));
    }
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 640
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>
#include<random>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXN = 22;
const int MAX = 202000;
double dp[MAXN][MAX];
vi dd[MAX];

vi divs(int n) {
	vi ret;
	for (int i = 1; i*i <= n; i++) {
		if (n%i == 0) {
			ret.push_back(i);
			if (i*i < n) ret.push_back(n / i);
		}
	}
	return ret;
}

int main() {
	for (int i = 1; i < MAX; i++)
		dd[i] = divs(i);
	int N;
	cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; i++)
		cin >> A[i];
	reverse(A.begin(), A.end());
	for (int i = 0; i < N; i++) for (int j = 0; j < MAX; j++)
		dp[i][j] = 1;
	for (int i = 1; i < 2 * A[0]; i++) {
		dp[0][i] = 1.*abs(i - A[0]) / A[0];
	}
	for (int i = 0; i < N-1; i++) {
		for (int j = 1; j < 2 * A[i]; j++) if (dp[i][j] < 1) {
			for (int d : dd[j]) {
				double tmp = max(dp[i][j], 1.*abs(A[i + 1] - d) / A[i + 1]);
				dp[i + 1][d] = min(dp[i + 1][d], tmp);
			}
		}
	}
	double ans = 1;
	for (int i = 0; i < MAX; i++)
		ans = min(ans, dp[N - 1][i]);
	printf("%.10lf\n", ans);
    return 0;
}

Measured memory_bytes: 54752
from sys import stdin
n = int(stdin.readline().rstrip())
s = str(stdin.readline().rstrip())

if n % 2 == 1:
    print("No")
else:
    e = n/2
    if s[0:int(e)] == s[int(e):n]:
        print("Yes")
    else:
        print("No")


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

int main() {
	int n;
	cin>>n;
	int o=n%10;
	if(o==3)
	cout<<"bon";
	else if(o==0||o==1||o==6||o==8)
	cout<<"pon";
	else
	cout<<"hon";
 	 
	// your code goes here
	return 0;
}

Measured memory_bytes: 3028
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();

		int[] p = new int[n];
		int[] sei = new int[n];
		int count = 0;

		for (int i = 0; i < n; i++) {
			p[i] = sc.nextInt();
			sei[i] = p[i];
		}

		Arrays.sort(sei);

		for (int i = 0; i < n; i++) {
			if (p[i] != sei[i]) {
				count++;
			}
		}

		if (count <= 2) {
			System.out.println("YES");
		} else {
			System.out.println("NO");
		}

		sc.close();

	}
}

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

using namespace std;

#define endl '\n'

typedef long long int64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const double eps = 1e-9;
const int oo = 0x3f3f3f3f;
const int mod = 1000000007;

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

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

    vector<int> p(n);

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

    deque<pii> I, D;

    for (int i = 0; i < k; ++i){
        while (!I.empty() && I.back().first > p[i])
            I.pop_back();
        I.push_back({p[i], i});

        while (!D.empty() && D.back().first < p[i])
            D.pop_back();
        D.push_back({p[i], i});
    }

    int eq = I.size() == k;
    int answer = 1 - eq;

    // cout << 0 << " " << k - 1 << endl;

    for (int i = k; i < n; ++i){
        bool diff = false;

        if (I.front().second == i - k && p[i] > D.front().first){

        } else {
            diff = true;
            // cout << i - k + 1 << " " << i << endl;
        }

        if (I.front().second == i - k) I.pop_front();
        if (D.front().second == i - k) D.pop_front();

        while (!I.empty() && I.back().first > p[i])
            I.pop_back();
        I.push_back({p[i], i});

        while (!D.empty() && D.back().first < p[i])
            D.pop_back();
        D.push_back({p[i], i});

        if (I.size() == k) {
            eq = 1;
        } else {
            answer += diff;
        }
    }

    // cout << answer << " " << eq << endl;

    cout << answer + eq << endl;

    return 0;
}

Measured memory_bytes: 2688
// 6/13
// リアルタイム参加
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;

// ll dp[1030][100010];

// // const int MAX = 256;
// const int LMAX = 100001;

vector<int> V, W;

int dfs(int i, int sum_w, int sum_v, vector<int> &idx, vector<int> &dp, int L) {
    int n = idx.size();
    if (i == n) {
        // ...
        // printf("sum_w, L = %d, %d\n", sum_w, L);
        if (sum_w > L) {
            // 重さが規定を超えている。
            return 0;
        } else {
            return dp[L - sum_w] + sum_v;
        }
    }

    // i番目を使わない場合
    int res1 = dfs(i + 1, sum_w, sum_v, idx, dp, L);

    // i番目を使う場合
    int res2 = dfs(i + 1, sum_w + W[idx[i]], sum_v + V[idx[i]], idx, dp, L);

    // printf("re1, res2 = %d, %d\n", res1, res2);
    return max(res1, res2);
}

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

    V.resize(N);
    W.resize(N);
    rep(i, N) cin >> V[i] >> W[i];

    int Q;
    cin >> Q;
    vector<int> v(Q), L(Q);
    rep(i, Q) cin >> v[i] >> L[i];

    // ll MAX = min(N, (ll)256);
    const int MAX = 100001;
    int limit = min(512, N);
    // int limit = min(8, N);

    // memset(dp, -1, sizeof(dp));
    // dp[0][0] = 0;

    vector<vector<int>> dp(limit, vector<int>(MAX));
    for (int j = 0; j < MAX; j++) {
        dp[0][j] = (j >= W[0] ? V[0] : 0);
    }

    for (int i = 1; i < limit; i++) {
        int p = (i - 1) / 2;

        for (int j = 0; j < MAX; j++) {
            dp[i][j] = dp[p][j];
            if (j >= W[i]) {
                dp[i][j] = max(dp[i][j], dp[p][j - W[i]] + V[i]);
            }
        }
    }

    // for (int i = 1; i < MAX; i++) {
    //     for (int j = 0; j <= 100000; j++) {
    //         dp[i + 1][j] = dp[(i + 1)/2][j];
    //         if (W[i] <= j) {
    //             if (dp[i + 1][j] < dp[(i + 1)/2][j - W[i]] + V[i]) {
    //                 dp[i + 1][j] = dp[(i + 1)/2][j - W[i]] + V[i];
    //             }
    //         }
    //     }
    // }

    rep(i, Q) {
        v[i]--;

        // if (v[i] < limit) {
        //     cout << dp[v[i]][L[i]] << endl;
        //     continue;
        // }

        // else {
        //     int x = v[i];
        //     while (x < 1024) {
        //         x /= 2;
        //     }

        // }

        int ver = v[i];
        int l = L[i];

        // v[i] > limitの場合。
        vector<int> idx;
        while (ver >= limit) {
            idx.push_back(ver);
            ver = (ver - 1) / 2;
        }

        // printf("v, L, ver = %d, %d, %d\n", v[i], l, ver);
        // cout << "idx = ";
        // for (auto id : idx) cout << id << " ";
        // cout << endl;

        // printf("dp[ver][l] = %d\n", dp[ver][l]);

        // if(v[i] == 13) {
        //     for (int j = 0; j <= l; j++) {
        //         printf("dp[%d][%d] = %d\n", ver, j, dp[ver][j]);
        //     }            
        // }

        // printf("V[13], W[13] = %d, %d\n", V[13], W[13]);

        int ans = dfs(0, 0, 0, idx, dp[ver], l);
        // int dfs(int i, int sum_w, int sum_v, vector<int> &idx, vector<int> &dp, int L)

        cout << ans << endl;
    }

}

Measured memory_bytes: 207268
#include <iostream>
#include <vector>
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define REP(i, n) for(int i = 1; i < (int)(n); ++i)
using namespace std;

int digit(int n) {
    int ans = 0;
    while (n > 0) {
        n /= 10;
        ++ans;
    }
    return ans;
}

int main() {
    int N;
    cin >> N;
    int ans = 0;
    REP(i, N + 1) {
        if (digit(i) % 2 == 1) ++ans;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#define _DEBUG 1

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#ifdef _DEBUG
#define dump(x) cerr << #x << "=" << x << endl
#define dump2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define dump3(x, y, z)                                                         \
  cerr << #x << "=" << x << "," << #y << "=" << y << #z << "=" << z << endl
#define check(s) cerr << s << endl
#else
#define dump(x)
#define dump2(x, y)
#define check(s)
#endif

#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());

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

typedef struct tuple {
  ll t;
  ll f;
  char c;
} record;

void solve(string S, vector<record> Q) {
  bool direction = true;

  for (record q : Q) {
    switch (q.t) {
    case 1:
      direction = !direction;
      break;
    case 2:
      switch (q.f) {
      case 1:
        if (direction) {
          S.insert(0, {q.c}); // heavy
        } else {
          S += q.c;
        }
        break;
      case 2:
        if (!direction) {
          S.insert(0, {q.c}); // heavy
        } else {
          S += q.c;
        }
        break;
      }
      break;
    }
  }

  if (!direction) {
    reverse(all(S));
  }

  cout << S << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  string S;
  cin >> S;
  ll N;
  cin >> N;
  vector<record> Q(N);
  rep(i, N) {
    ll t, f;
    char c;
    cin >> t;
    if (t == 1) {
      record r = {1, 0, '\0'};
      Q.at(i) = r;
    } else { // t == 2
      cin >> f >> c;
      record r = {t, f, c};
      Q.at(i) = r;
    }
  }

  solve(S, Q);
  return 0;
}


Measured memory_bytes: 10428
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
//const ll mod = 1000000087;
//const ll INF = mod * mod;
typedef pair<int, int>P;
typedef pair<int, bool> sP;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef vector<vector<ll>> mat;
typedef vector<ll> vec;

bool used[1 << 17];
int col[1 << 17];
vector<int> G[1 << 17];
vector<int> rG[1 << 17];

void normalize(int &x) {
	x = (x % 3 + 3) % 3;
}
void solve() {
	int n, m; cin >> n >> m;
	//assert(n <= 40000);
	rep(i, m) {
		int a, b; cin >> a >> b; a--; b--;
		G[a].push_back(b);
		rG[b].push_back(a);
	}
	ll ans = 0;
	rep(i, n) {
		if (used[i])continue;
		queue<int> q; q.push(i);
		used[i] = true;
		int tmp = 1;
		bool valid = true;
		int c[3] = {}; c[0] = 1;
		bool exi = false;

		int cnt = 0;
		while (!q.empty()) {
			int id = q.front(); q.pop();
			if (G[id].size() && rG[id].size())exi = true;
			rep(j, G[id].size()) {
				cnt++;
				int to = G[id][j];
				if (used[to]) {
					int dif = col[to] - col[id];
					normalize(dif);
					if (dif != 1)valid = false;
				}
				else {
					tmp++;
					used[to] = true; q.push(to);
					col[to] = col[id] + 1;
					c[col[to] % 3]++;
				}
			}
			rep(j, rG[id].size()) {
				int to = rG[id][j];
				if (used[to]) {
					int dif = col[to] - col[id];
					normalize(dif);
					if (dif != 2)valid = false;
				}
				else {
					tmp++;
					used[to] = true; q.push(to);
					col[to] = col[id] + 2;
					c[col[to] % 3]++;
				}
			}
		}
		if (!valid) {
			ans += (ll)tmp*tmp;
		}
		else {
			if (!exi) {
				ans += cnt;
			}
			else {
				ll num = (ll)tmp*tmp;
				num -= tmp; num /= 2;
				rep(j, 3) {
					num -= (ll)c[j] * (c[j] - 1) / 2;
				}
				ans += num;
			}
		}
	}
	cout << ans << endl;
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(7);
	//init();
	solve();
	//stop
	return 0;
}


Measured memory_bytes: 13184
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

const float m500[] = {
  35.5f, 37.5f, 40.0f, 43.0f, 50.0f, 55.0f, 70.0f, (float)(1<<30)
};
const float m1k[] = {
  71.0f, 77.0f, 83.0f, 89.0f, 105.f, 116.f, 148.0f, (float)(1<<30)
};
const string rank[] = {
  "AAA", "AA", "A", "B", "C", "D", "E", "NA"
};

int main() {
  const int sz = 8;
  float l, r;
  while (cin >> l >> r) {
    int ll = upper_bound(m500, m500 + sz, l) - m500;
    int rl = upper_bound(m1k, m1k + sz, r) - m1k;
    cout << rank[max(rl,ll)] << endl;
  }
  return 0;
}

Measured memory_bytes: 0
#include <cstdio>
#include <iostream>
#include <vector>

#define lson (idx << 1)
#define rson (idx << 1 | 1)
#define INF 1000000000000LL
#define MAXN 200010

using namespace std;
typedef long long ll;

template <typename T> void read(T &x) {
  x = 0;
  char c = getchar();
  T sig = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      sig = -1;
  for (; isdigit(c); c = getchar())
    x = (x << 3) + (x << 1) + c - '0';
  x *= sig;
}

// `lo` is the current minimum of the segment.
// `lh` is the current value of the left endpoint.
// `lazy` is the lazy tag for addition.
struct Node {
  int l, r;
  ll lo, lh;
  bool flag = false;
} s[MAXN << 2];
int h, w;

// Push up
void calc(int idx) {
  s[idx].lo = min(s[lson].lo, s[rson].lo);
  s[idx].lh = s[lson].lh;
}

void build(int idx, int l, int r) {
  s[idx].l = l, s[idx].r = r;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  build(lson, l, mid);
  build(rson, mid + 1, r);
}

// Lazy propagation
void pushdown(int idx) {
  if (s[idx].flag) {
    ll L = s[idx].lh;
    int l = s[idx].l;
    for (int i = lson; i <= rson; ++i) {
      s[i].lo = s[i].lh = L + s[i].l - l;
      s[i].flag = true;
    }
  }
  s[idx].flag = false;
}
// Assign segment [l, r] according to f[l-1] = L
void update(int idx, int l, int r, ll L) {
  if (s[idx].l >= l && s[idx].r <= r) {
    s[idx].lo = s[idx].lh = L + s[idx].l - l + 1;
    s[idx].flag = true;
    return;
  }
  pushdown(idx);
  int mid = (s[idx].l + s[idx].r) >> 1;
  if (l <= mid)
    update(lson, l, r, L);
  if (mid + 1 <= r)
    update(rson, l, r, L);
  calc(idx);
}
// Range minimum query
ll query(int idx, int l, int r) {
  if (r < 1 || l > w)
    return INF;
  if (s[idx].l >= l && s[idx].r <= r)
    return s[idx].lo;
  pushdown(idx);
  ll ans = INF;
  int mid = (s[idx].l + s[idx].r) >> 1;
  if (l <= mid)
    ans = min(ans, query(lson, l, r));
  if (mid + 1 <= r)
    ans = min(ans, query(rson, l, r));
  return ans;
}

class Solution {
public:
  void solve() {
    read(h), read(w);
    vector<int> l(h), r(h);
    for (int i = 0; i < h; ++i)
      read(l[i]), read(r[i]);
    vector<ll> ans(h, INF);
    build(1, 1, w);
    for (int i = 0; i < h; ++i) {
      ll L = query(1, l[i] - 1, l[i] - 1);
      update(1, l[i], r[i], L);
      ans[i] = min(ans[i], query(1, 1, w))+i+1;
      if (ans[i] == INF)
        break;
    }
    for (ll i : ans)
      cout << (i >= INF ? -1 : i) << endl;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  Solution solution = Solution();
  solution.solve();
}

Measured memory_bytes: 31456