akhauriyash/Code-Regression huggingface dataset:
import java.util.ArrayList;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    String s = sc.next();
    int K = Integer.parseInt(sc.next());

    ArrayList<String> subs = new ArrayList<>();
    for (int i = 0; i < s.length(); i++) {
      for (int j = 0; j < 5; j++) {
        if (i + j >= s.length()) {
          break;
        }
        subs.add(s.substring(i, i + j + 1));
      }
    }
    subs.sort(null);
    String ans = subs.get(0);
    int count = 1;
    if (count == K) {
      System.out.println(ans);
      return;
    }
    for (int i = 1; i < subs.size(); i++) {
      if (ans.equals(subs.get(i))) {
        continue;
      } else {
        ans = subs.get(i);
        count++;
      }
      if (count == K) {
        System.out.println(ans);
        break;
      }
    }
  }
}

Measured memory_bytes: 25180
#include <stdio.h>
#include <stdlib.h>

int main() {
    int k;
    int A[5];
    int f,l = 4;
    int flag = 0;

    for (int i = 0; i < 5; ++i) {
        scanf("%d",&A[i]);
    }
    scanf("%d",&k);

    while (l > 1) {
        if (A[l] - A[0] > k) {
            flag = 1;
            break;
        }
        else {
            l -= 1;
        }
    }

    if (flag == 0) {
        printf("Yay!\n");
    }
    else {
        printf(":(\n");
    }
    return 0;
}
  

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

const string YES = "Yes";
const string NO = "No";

void solve(std::string S){
    bool ans = true;
    int size = S.size();
    for(int i=0;i<size;i++){
        if(S[i] != S[size-1-i]){
            ans = false;
        }
    }
    int half = size/2;
    for(int i=0;i<half;i++){
        if(S[i] != S[half-1-i]){
            ans = false;
        }
    }
    if(size >3){
          for(int i = half+1;i<size;i++){
        if(S[i] != S[size -1-i]){
            ans = false;
        }
    }

    }
  
    if(ans){
        cout << YES << endl;
    }else{
        cout << NO << 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(){
    std::string S;
    std::cin >> S;
    solve(S);
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<set>
#include<unordered_set>
#include<list>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<algorithm>
#include<cmath>
#include<tuple>
#include<iomanip>
#include<climits>
#define ll long long int
#define mod 1000000007
#define pi acos(-1)
using namespace std;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int a,b,c;
  cin>>a>>b>>c;
  if(a+b+c >=22)
   cout<<"bust";
  else
  cout<<"win";
  return 0;
}

Measured memory_bytes: 384
#include "iostream"
#include "vector"
#include "string"
#include "algorithm"
#include "math.h"
#include "cstring"
#include "stack"
#include "queue"

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll> > mat;

const int max_n = 2010;

int main()
{
  int h, w;
  cin >> h >> w;
  string s[max_n];
  char c[max_n][max_n];
  for (int i = 0; i < h; i++)
  {
    cin >> s[i];
    for (int j = 0; j < w; j++)
    {
      c[i][j] = s[i][j];
    }
  }

  int row[max_n][max_n];
  int col[max_n][max_n];
  for (int i = 0; i < h; i++)
  {
    int check = 0;
    for (int j = 0; j < w; j++)
    {
      if (c[i][j] == '.')
      {
        check++;
        if (j == w - 1)
        {
          for (int k = j; k > j - check; k--)
          {
            row[i][k] = check;
          }
          check = 0;
        }
      }
      else
      {
        row[i][j] = -10000;
        for (int k = j - 1; k > j - 1 - check; k--)
        {
          row[i][k] = check;
        }
        check = 0;
      }
    }
  }

  for (int j = 0; j < w; j++)
  {
    int check = 0;
    for (int i = 0; i < h; i++)
    {
      if (c[i][j] == '.')
      {
        check++;
        if (i == h - 1)
        {
          for (int k = i; k > i - check; k--)
          {
            col[k][j] = check;
          }
          check = 0;
        }
      }
      else
      {
        col[i][j] = -10000;
        for (int k = i - 1; k > i - 1 - check; k--)
        {
          col[k][j] = check;
        }
        check = 0;
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < h; i++)
  {
    for (int j = 0; j < w; j++)
    {
      if (ans < row[i][j] + col[i][j])
      {
        ans = row[i][j] + col[i][j];
      }
    }
  }
  cout << ans - 1 << "\n";
}

Measured memory_bytes: 39808
n, m = gets.split.map(&:to_i)
h = [nil] + gets.split.map(&:to_i)
d = []
m.times do
  a, b = gets.split.map(&:to_i)
  if h[a] <= h[b]
    d << a
  end
  if h[a] >= h[b]
    d << b
  end
end
p n - d.uniq.size


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

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)

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

    int a, b, c, d, k; cin >> a >> b >> c >> d >> k;

    int x = a*60 + b, y = c*60 + d;
    cout << max(0,y-x-k) << '\n';
}



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

int main() {
  
  int N; cin >> N; int maxH = 0;
  
  vector<int> vec(N);
  
  for (int i = 0; i < N; i++) cin >> vec[i];
  
  for (int i = 0; i < N; i++) {
    
    if (vec[i] < maxH - 1) {
      cout << "No" << endl;
      return 0;
    }
    
    maxH = max(maxH, vec[i]);
    
  }
  
  cout << "Yes" << endl;
  
}

Measured memory_bytes: 640
height, width = map(int, input().split())
lis = [[0 for i in range(width)] for i in range(height)]
for i in range(height):
    lis[i] = input()

blank_rows = []
for row in range(height):
    if not "#" in lis[row]:
        blank_rows.append(row)

blank_cols = []
for col in range(width):
    flag = 1
    for row in range(height):
        if "#" == lis[row][col]:
            flag = 0
            break
    if flag:
        blank_cols.append(col)

for row in range(height):
    out = ""
    if not row in blank_rows:
        for col in range(width):
            if not col in blank_cols:
                out += lis[row][col]
    if not out == "":
	    print(out)

Measured memory_bytes: 3188
# import sys
import math #sqrt,gcd
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import permutations #順列生成
# import collections # deque
# from operator import itemgetter
# from fractions import Fraction

mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

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

def main():
    x,y,z = readInt()
    ans = 0
    while True:
        if y*(ans+1)+z*(ans+1)<=x-z:
            ans += 1
        else:
            break
    print(ans)
    return

if __name__=='__main__':
    main()

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

#define rep(i,n) for(int i = 0; i < (n); ++i)
#define ll long long
const ll mod = 1e9+7;
int inf = 1000000000; //10e9
 
int main() {

    int K, N;
    cin >> K >> N;
    vector<int> A(N);
    rep(i,N) cin >> A[i];

    int res = (K - A[N-1]) + A[0];
    rep(i,N){
        res = max(res, A[i+1] - A[i]);
    }

    res = K - res;

    cout << res << endl;

    return 0;

}

Measured memory_bytes: 4064
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<long long >
#define vpll vector< pair<long long, long long> >
#define mll map<long long, long long>
#define mlli map<long long, int>
#define msi map<string, int>
#define s string
#define mllvll map<long long, vector<long long>>
#define pll pair<long long, long long>
#define LLM  LONG_LONG_MAX
#define forn(i, n) for (ll i = 0; i < n; ++i)
#define firn(i, n) for (ll i = 1; i < n; ++i)
ll minll ( ll a, ll b){
    return a>b ? b : a;
}

ll maxll (ll a, ll b){
    return a>b ? a:b;
}

//////////////////////////Solution////////////////////////////////////////////////////////////////

int main()
{
 ios::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);
 ll testcases,n,k,minrep,prevp=0,prevc=0;
//  cin>>testcases;
 testcases=1;
 while(testcases-->0){
   cin>>n;
   ll moves=0,a,b;
   for(ll i = sqrt(n);i>0 ;i--){
    //    cout<<i<<":";
       if(n%i==0){
           a=i;
           b=n/i;
           break;
       }
   }
//    cout<<a<<b;
   moves = a-1 + b-1;
   cout<<moves<<"\n";
 }
return 0;
}


Measured memory_bytes: 256
import sys
from fractions import gcd
import bisect
 
stdin = sys.stdin
 
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
s = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n, T = na()
cost = []
for _ in range(n):
        c, t = na()
        if t <= T:
                cost.append(c)
if len(cost) != 0:
        print(min(cost))
else:
        print("TLE")


Measured memory_bytes: 5072
# トポロジカルソート->簡単なDP
import sys
sys.setrecursionlimit(300000) # ないとerr

N,M = map(int, input().split())
adj = [[] for _ in range(N)]
for _ in range(M):
    x,y = map(int, input().split())
    adj[x-1].append(y-1) 
# print("adj list: ", adj)

visited = [False] * N
topo_order = [] # sort後を格納

# topological sort by DFS
def dfs_visit(node):
    global visited, topo_order
    if visited[node]: return
    visited[node] = True
    for neighbor in adj[node]:
        if not visited[neighbor]:
            dfs_visit(neighbor)
    # 帰りがけに追加
    topo_order.append(node)
    # print(topo_order)

# topological sort main
for node in range(N):
    dfs_visit(node)
topo_order = topo_order[::-1]
# print("topo_order: ", topo_order)

dp = [0]*N

for node in topo_order:
    for neighbor in adj[node]:
        dp[neighbor] = max(dp[neighbor], dp[node]+1)
print(max(dp))
# print(dp)

Measured memory_bytes: 165708
n, q = [int(i) for i in input().split()]
st = input() + '-'
table = []
cnt = 0
for i, v in enumerate(st):
	if st[i: i+2] == 'AC':
		cnt += 1
	table.append(cnt)
table = [0] + table

for j in range(q):
	l, r = list(map(int, input().split()))
	print (table[r-1] - table[l-1])

Measured memory_bytes: 6376
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, char *> P;
const int L = 200;

vector<string> split(const string &s, char delim) {
  vector<string> res;
  stringstream ss(s);
  string item;
  while (getline(ss, item, delim)) res.push_back(item);
  return res;
}

bool check(string a, string b, string c, int &num){
  REP(i, a.length()) if(a[i] == 'X') a[i] = num + '0';
  REP(i, b.length()) if(b[i] == 'X') b[i] = num + '0';
  REP(i, c.length()) if(c[i] == 'X') c[i] = num + '0';
  REVERSE(a); REVERSE(b);
  int pls = 0;
  stringstream ss;
  REP(i, max(a.length(), b.length())){
    if(i < a.length()) pls += (a[i] - '0');
    if(i < b.length()) pls += (b[i] - '0');
    ss << (pls % 10);
    pls /= 10;
  }
  if(pls != 0) ss << pls;
  string r = ss.str();
  REVERSE(r);
  return (r == c);
}

int main() {
  char _s[L];
  while(scanf("%s", _s) != EOF){
    string a, b, c, s = string(_s);
    vector<string> v = split(s, '=');
    a = v[0]; c = v[1];
    v = split(a, '+');
    a = v[0]; b = v[1];
    int ans = -1;
    REP(num, 10){
      if(num == 0 && ((a.length() > 1 && a[0] == 'X') || (b.length() > 1 && b[0] == 'X') || (c.length() > 1 && c[0] == 'X'))) continue;
      if(check(a, b, c, num)) ans = num;
    }
    if(ans == -1) cout <<"NA" <<endl;
    else cout <<ans <<endl;
  }
  return 0;
}

Measured memory_bytes: 1248
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bitset>
using namespace std;

int main() {
	int ans = 0;
	int A, B;
	cin >> A >> B;
	for (int i = 0; i < 2; i++) {
		if (A > B) {
			ans += A;
			A--;
		}
		else {
			ans += B;
			B--;
		}
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 384
#include <iostream>
#include <set>

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

    std::set<int> s;
    int Q;
    std::cin >> Q;
    for (int q = 0; q < Q; ++q) {
        int t;
        std::cin >> t;
        if (t == 0) {
            int x;
            std::cin >> x;
            s.insert(x);
            std::cout << s.size() << "\n";
        } else if (t == 1) {
            int x;
            std::cin >> x;
            std::cout << s.count(x) << "\n";
        } else if (t == 2) {
            int x;
            std::cin >> x;
            s.erase(x);
        } else {
            int l, r;
            std::cin >> l >> r;
            auto e = s.upper_bound(r);
            for (auto itr = s.lower_bound(l); itr != e; ++itr) std::cout << *itr << "\n";
        }
    }
}


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

using namespace std;

using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;

using _loop_int = int;
#define REP(i,n) for(_loop_int i=0; i<(_loop_int)(n); i++)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a); i<(_loop_int)(b); i++)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1; i>=(_loop_int)(a); i--)

#define CHMIN(a,b) (a)=min((a),(b))
#define CHMAX(a,b) (a)=max((a),(b))
#define ALL(v) (v).begin(),(v).end()

#define DEBUG(x) cerr<<#x<<": "<<(x)<<endl
#define DEBUG_VEC(v) cerr<<#v<<": ";REP(__i,(v).size())cerr<<((v)[__i])<<", ";cerr<<endl

const ll MOD = 1000000007ll;

int n;
char s[252521];

int main(){
    scanf("%s",s);
    n = strlen(s);
    ll ans = 0;
    ll bcs = 0;
    int stat = 0;
    FORR(i,0,n){
        if(s[i]=='A'){
            if(stat==0){
                ans += bcs;
            }else{
                bcs = 0;
                stat = 0;
            }
        }else if(s[i]=='B'){
            if(stat==0){
                bcs = 0;
                stat = 0;
            }else if(stat==1){
                bcs += 1;
                stat = 0;
            }
        }else if(s[i]=='C'){
            if(stat==0){
                stat = 1;
            }else if(stat==1){
                bcs = 0;
                stat = 1;
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}


Measured memory_bytes: 384
class UnionFind:
    __slots__ = ["N", "root", "rank"]

    def __init__(self, N):
        """
        N:要素数
        root:各要素の親要素の番号を格納するリスト.
             ただし, root[x] < 0 ならその頂点が根で-root[x]が木の要素数.
        rank:ランク
        """
        self.N = N
        self.root = [-1] * N
        self.rank = [0] * N

    def __repr__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

    def find(self, x):
        """頂点xの根を見つける"""
        if self.root[x] < 0:
            return x
        while self.root[x] >= 0:
            x = self.root[x]
        return x

    def union(self, x, y):
        """x,yが属する木をunion"""
        # 根を比較する
        # すでに同じ木に属していた場合は何もしない.
        # 違う木に属していた場合はrankを見てくっつける方を決める.
        # rankが同じ時はrankを1増やす
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        elif self.rank[x] > self.rank[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def same(self, x, y):
        """xとyが同じグループに属するかどうか"""
        return self.find(x) == self.find(y)

    def count(self, x):
        """頂点xが属する木のサイズを返す"""
        return - self.root[self.find(x)]

    def members(self, x):
        """xが属する木の要素を列挙"""
        _root = self.find(x)
        return [i for i in range(self.N) if self.find == _root]

    def roots(self):
        """森の根を列挙"""
        return [i for i, x in enumerate(self.root) if x < 0]

    def group_count(self):
        """連結成分の数"""
        return len(self.roots())

    def all_group_members(self):
        """{ルート要素: [そのグループに含まれる要素のリスト], ...}の辞書を返す"""
        return {r: self.members(r) for r in self.roots()}


import sys
input = sys.stdin.readline
n, q = map(int, input().split())
uf = UnionFind(n)
for i in range(q):
    t, u, v = map(int, input().split())
    if t:
        print(int(uf.same(u, v)))
    else:
        uf.union(u, v)


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

int main(){
    double N;
    cin >> N;
    vector<int> X(N);
    vector<long long> P(N);
    int s;
    int i;
    int num = 0;
    for(i=0;i<N;i++){
        cin >> X[i];
        num += X[i];
    }
    int ave = round(num/N);
    int ans = 0;
    for(i=0;i<N;i++){
        s = (ave-X[i]) * (ave-X[i]);
        ans += s;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;

struct node1
{
    string s;
    int d;
    int t;
}node[20];

bool cmp(node1 p1,node1 p2)
{
    if(p1.d!=p2.d)
        return p1.d<p2.d;
    return p1.s<p2.s;
}

int main()
{
    string s1,s2,s3;
    cin>>s1>>s2>>s3;
    printf("%c%c%c",s1[0]-'a'+'A',s2[0]-'a'+'A',s3[0]-'a'+'A');
    return 0;
}


Measured memory_bytes: 256
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

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

N = int(input())
dd = defaultdict(int)
ans = 0
for i in range(N):
	a = int(input())
	if dd[a] == 0:
		dd[a] = 1
		ans += 1
	else:
		dd[a] = 0
		ans -= 1

print(ans)

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

bool sft(int num) {
    bool seven = false, five = false, three = false;
    while (num) {
        switch (num % 10) {
            case 7:
                seven = true;
                break;
            case 5:
                five = true;
                break;
            case 3:
                three = true;
                break;
            default:
                return false;
        }
        num /= 10;
    }
    if (seven == true && five == true && three == true) {
        return true;
    } else {
        return false;
    }
}

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

    int ans = 0;
    if (N > 100000000) {
        ans = 8334;
        if ((N / 100000000) % 10 >= 7) {
            for (int i = 733333335; i <= N; i++) {
                if (sft(i)) ans++;
            }
            ans += 12100;
        } else if ((N / 100000000) % 10 >= 5) {
            for (int i = 533333337; i <= N; i++) {
                if (sft(i)) ans++;
            }
            ans += 6050;
        } else if ((N / 100000000) % 10 >= 3) {
            for (int i = 333333357; i <= N; i++) {
                if (sft(i)) ans++;
            }
        }
    } else {
        for (int i = 1; i <= N; i++) {
            if (sft(i)) ans++;
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 384
import java.util.*;

class Main {
	Scanner sc = new Scanner(System.in);

	public void run() {
		int Case=sc.nextInt();sc.nextLine();
		while (Case--!=0){
			String str=sc.nextLine();
			int n=sc.nextInt();sc.nextLine();
			int cursor=0;
			for(int i=0;i<n;i++){
				String op=sc.nextLine();
				if("forward word".equals(op)){
					if(str.length()==cursor)continue;
					if(str.charAt(cursor)==' '){
						while(cursor<str.length() && str.charAt(cursor)==' ')cursor++;
					}
					while(cursor<str.length() && str.charAt(cursor)!=' ')cursor++;
				}else if("forward char".equals(op)){
					cursor=Math.min(str.length(),cursor+1);
				}else if("backward word".equals(op)){
					if(cursor==0)continue;
					if(str.charAt(cursor-1)==' '){
						while(cursor>0  && str.charAt(cursor-1)==' ')cursor--;
					}
					while(cursor>0  && str.charAt(cursor-1)!=' ')cursor--;
				}else if("backward char".equals(op)){
					cursor=Math.max(0, cursor-1);
				}else if("delete char".equals(op)){
					if(cursor==str.length())continue;
					str=str.substring(0,cursor)+str.substring(cursor+1);
				}else if("delete word".equals(op)){
					int e=cursor;
					while(e<str.length() && str.charAt(e)==' ')e++;
					if(e==str.length())continue;
					while(e<str.length() && str.charAt(e)!=' ')e++;
					str=str.substring(0,cursor)+str.substring(e);
				}else if(op.startsWith("insert")){
					int s=-1,e=-1;
					for(int index=0;index<op.length();index++){
						if(op.charAt(index)=='"'){
							if(s==-1)s=index;
							else e=index;
						}
					}
					String word=op.substring(s+1,e);
					str=str.substring(0,cursor)+word+str.substring(cursor);
					cursor+=word.length();
				}
			}
			ln(str.substring(0,cursor)+"^"+str.substring(cursor));
		}
	}
	public static void main(String[] args) {
		new Main().run();
	}
	public static void pr(Object o) {
		System.out.print(o);
	}
	public static void ln(Object o) {
		System.out.println(o);
	}
	public static void ln() {
		System.out.println();
	}
}

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

#define LL long long
#define RG register

using namespace std;
template<class T> T gi() {
	T x = 0; bool f = 0; char c = getchar();
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	if (c == '-') f = 1, c = getchar();
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return f ? -x : x;
}
const int N = 1e5 + 10;
int p[N], siz[N], X[N], Y[N], fa[N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
LL D[N];
map<LL, int> M;
vector<int> e[N];
bool cmp(int x, int y) { return D[x] > D[y]; }
LL res;
void dfs(int u, int ff) {
	siz[u] = 1;
	for (auto v : e[u])
		if (v != ff) {
			dfs(v, u);
			res += siz[v];
			siz[u] += siz[v];
		}
}
int main() {
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	int n = gi<int>();
	for (int i = 1; i <= n; i++)
		D[i] = gi<LL>(), M[D[i]] = i, p[i] = i;
	sort(p + 1, p + 1 + n, cmp);
	for (int i = 1; i <= n; i++) siz[i] = 1, fa[i] = i;
	int cnt = 0;
	for (int i = 1; i <= n; i++) {
		int x = p[i];		
		if (M.find(D[x] - n + 2 * siz[x]) == M.end()) continue;
		int y = M[D[x] - n + 2 * siz[x]];
		if (find(x) == find(y)) continue;
		fa[find(y)] = find(x);
		siz[y] += siz[x]; X[++cnt] = x; Y[cnt] = y;
		e[x].push_back(y); e[y].push_back(x);
	}
	dfs(1, 0);
	if (cnt < n - 1 || res != D[1])
		puts("-1");
	else
		for (int i = 1; i < n; i++)
			printf("%d %d\n", X[i], Y[i]);	
	return 0;
}


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

using namespace std;

typedef long long ll;
ll mod = 1000000007;

int main() {
    string s;
    cin >> s;
    int rnum,lnum;
    rnum = lnum = 0; 
    for (int i = 0; i < s.size(); i++) {
        if (s[i] == 'R' && s[i + 1] == 'R') rnum++,printf("%d ",0);
        else {
            while(i + 2 < s.size() && s[i + 2] == 'L') {
                lnum++;
                i++;
                if (i >= s.size()) break;
            }
            int Rnum,Lnum;
            Rnum = Lnum = 1;
            Rnum += rnum / 2 + (lnum + 1) / 2;
            Lnum += (rnum + 1) / 2 + lnum / 2;
            printf("%d %d ",Rnum,Lnum);
            for (int j = 0; j < lnum; j++) printf("%d ",0);
            lnum = rnum = 0;
            i++;
        }
    }
    printf("\n");
}

Measured memory_bytes: 640
while 1:
  n = int(input())
  if n == 0:
    break
    
  data = list(map(int, input().split()))
  
  if n == 1:
    print(2)
  elif data.count(0) + data.count(1) == n:
    print('NA')
  else:
    print(n-data.count(0)+1)


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

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

int main() {
    long long N, ans = 1000000007;
    cin >> N;
    vector<long long> W(N), W_sum(N+1,0);
    rep(i, N){
        cin >> W[i];
    }

    // 累積和の作成
    rep(i,N) {
        W_sum[i+1] = W_sum[i] + W[i];
        // cout << W_sum[i+1] << " ";
    }
    // cout << endl;

    for(int i = 1; i <= N+1; ++i) {
        ans = min(ans, abs((W_sum[N] - W_sum[i]) - W_sum[i])); 
        // cout << ans << " ";
    }
    // cout << endl;

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


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

#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 ddrep(i, n) for (int i = n; i > 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define ssrep(i, s, t) for (int i = s; i <= t; ++i)
#define chmax(x, y) (x = max(x, y))
#define chmin(x, y) (x = min(x, y))

using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using ld = long double;
using str = string;

#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second

long int M = 1e+9+7;
int inf = 1e+9;

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << std::setprecision(10);

    int n;
    cin >> n;
    int ans=0;
    rrep(i,50000){
        if(int(i*1.08)==n){
            ans = i;
            break;
        }
        // << i*1.08 << endl;
    }
    if(!ans){
        cout << ":(" << endl;
        return 0;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <bitset>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <regex>
using namespace std;
using ll = long long int;
using dd = long double;
const ll MOD = 1e9 + 7;

ll gcd(ll a, ll b)
{
    if (a % b == 0)
    {
        return b;
    }
    return gcd(b, a % b);
}

int main()
{
    ll A, B;
    cin >> A >> B;
    cout << (A - 1) * (B - 1) << endl;
}

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef map<int, int> mii;

#define fastIO \
ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl "\n"
#define MOD 1000000007
#define pb push_back
#define ff first
#define ss second
#define mp make_pair 

int main(){
	ll n;
	cin >> n;
	string s;
	cin >> s;
	ll ans = 0;
	for(int i=0;i<n;i++){
		++ans;
		int j = i+1;
		while(j<n && s[j]==s[i])	++j;
		i = j-1;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
priority_queue<ll> q;
int main(){
    ll n,m,x,sum=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>x;
        q.push(x);
    }
    while(!q.empty()&&m){
        ll y=q.top();
        q.pop();
        y/=2;m--;
        q.push(y);
    }
    while(!q.empty()){
        sum+=q.top();q.pop();
    }
    cout<<sum<<endl;
}

Measured memory_bytes: 1400
import numpy as np
bisect_left = np.searchsorted
input()
a = np.array(input().split(), 'int32')
b = np.array(input().split(), 'int32')
ans = 0
for k in range(28, -1, -1):
    low = pow(2, k)
    high = pow(2, k + 1)
    a %= high
    b %= high
    b.sort()
    y = np.hstack((b - high, b))
    left = bisect_left(y, low - a)
    right = bisect_left(y, high - a)
    if np.sum(right - left) % 2:
        ans += low
print(ans)

Measured memory_bytes: 35908
#include<iostream>
#include<stdio.h>
//#include <bits/stdc++.h>
#include<vector>
#include<float.h>
#include<iomanip>
#include<algorithm>
#include<string>
#include<cstring>
#include<math.h>
#include<cmath>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include <cassert>
#include <cmath>
#include<cstdint>

#define INF 1e9
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define vec(type,n) vector<type>(n)
#define vvec(m,n) vector<vector<int>> (int(m),vector<int>(n))
#define ALL(a)  (a).begin(),(a).end()

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int,int>;

vector<int>bitSearch(int bit,int n){
    vector<int>S;
    rep(i,n)if(bit&(1<<i))S.push_back(i);
    return S;
}



int main(){
   string s,t;cin>>s>>t;
   reverse(ALL(t));
   cout<<(t==s?"YES":"NO");
}



Measured memory_bytes: 256
H1, M1, H2, M2, K = map(int, input().split())

h1 = H1 * 60
m1 = M1
h2 = H2 * 60
m2 = M2

ans = (h2 + m2) - (h1 + m1) - K if (h2 + m2) > (h1 + m1) else (h2 + m2) - (h1 + m1) - K + 24 * 60

print(ans)

Measured memory_bytes: 9184
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author ilyakor
 */
public class Main {

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

  static class TaskA {

    public void solve(int testNumber, InputReader in, OutputWriter out) {
      int n = in.nextInt();
      int cnt0 = 0, cnt1 = 0;
      for (int i = 0; i < n; ++i) {
        int x = in.nextInt() % 2;
        if (x == 0) {
          ++cnt0;
        } else {
          ++cnt1;
        }
      }
      out.printLine(cnt1 % 2 == 0 ? "YES" : "NO");
    }

  }

  static class InputReader {

    private InputStream stream;
    private byte[] buffer = new byte[10000];
    private int cur;
    private int count;

    public InputReader(InputStream stream) {
      this.stream = stream;
    }

    public static boolean isSpace(int c) {
      return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public int read() {
      if (count == -1) {
        throw new InputMismatchException();
      }
      try {
        if (cur >= count) {
          cur = 0;
          count = stream.read(buffer);
          if (count <= 0) {
            return -1;
          }
        }
      } catch (IOException e) {
        throw new InputMismatchException();
      }
      return buffer[cur++];
    }

    public int readSkipSpace() {
      int c;
      do {
        c = read();
      } while (isSpace(c));
      return c;
    }

    public int nextInt() {
      int sgn = 1;
      int c = readSkipSpace();
      if (c == '-') {
        sgn = -1;
        c = read();
      }
      int res = 0;
      do {
        if (c < '0' || c > '9') {
          throw new InputMismatchException();
        }
        res = res * 10 + c - '0';
        c = read();
      } while (!isSpace(c));
      res *= sgn;
      return res;
    }

  }

  static class OutputWriter {

    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
      writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
      this.writer = new PrintWriter(writer);
    }

    public void print(Object... objects) {
      for (int i = 0; i < objects.length; i++) {
        if (i != 0) {
          writer.print(' ');
        }
        writer.print(objects[i]);
      }
    }

    public void printLine(Object... objects) {
      print(objects);
      writer.println();
    }

    public void close() {
      writer.close();
    }

  }
}



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


using namespace std;

using ll   = long long;
using ii   = pair   < int , int >;
using i3   = pair   < int , ii  >;
using li   = pair   < ll  , int >;
using lii  = pair   < ll  , ii  >;
using pll  = pair   < ll  , ll  >;
using vi   = vector < int       >;
using vl   = vector < ll        >;
using vii  = vector < ii        >;
using vli  = vector < li        >;
using vpll = vector < pll       >;
using vi3  = vector < i3        >;
using vlii = vector < lii       >;


const int N = 1e4 + 5;
const ll INF = 1e17 + 7;
const double eps = 1e-9 , PI = acos(-1);


int n , m;

int a[N] , b[N];

ll mem[1005][N];

ll dp(int i , int H){
    if(H <= 0)return 0;
    if(i == n)return INF;
    ll &ret = mem[i][H];
    if(ret != -1)return ret;

    return ret = min(1ll * dp(i + 1 , H) , 1ll * b[i] + 1ll * dp(i , H - a[i]));
}

void solve(int testCase){

    scanf("%d %d" , &m , &n);
    for(int i = 0 ; i < n ; i ++){
            scanf("%d %d" , &a[i] , &b[i]);
    }

    memset(mem , -1 , sizeof mem);
    printf("%lld\n" , dp(0 , m));

}

main(){

    int t = 1;
    // scanf("%d" , &t);

    for(int testCase = 1 ; testCase <= t ; testCase ++){
            solve(testCase);
    }

    return 0;
}


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

int main()
{
	for (int w = 0; w < 2; w++)
	{
		int a[10];
		for (int i = 0; i < 10; i++)
		{
			cin >> a[i];
		}
		sort(a, a + 10);
		int an = a[7] + a[9] + a[8];
		cout << an;
		if (w == 0)cout << " ";
	}
	cout << endl;
}

Measured memory_bytes: 1180
W, a, b = map(int,input().split())
# N = int(input())
# al = list(map(int,input().split()))


if a <= b:
    if b-(a+W) >= 0:
        print(b-(a+W))
    else:
        print(0)
else:
    if a - (b+W) >= 0:
        print(a - (b+W))
    else:
        print(0)

Measured memory_bytes: 9116
double*q,s[99],*p=s+2,v,atof();short t[9];main(c){for(;~scanf("%[^ \n]%c",t,&c);c-10||printf("%f\n",*--p))v=*--p,q=p-1,*q=*t-43?*t-45?*t-42?*t-47?(p++,*p++=atof(t),*q):*q/v:*q*v:*q-v:*q+v;}

Measured memory_bytes: 0
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define INF 999999999
#define rep(i,n) for(int i=0;i<n;i++)
const int MOD = 1000000007;
ll cnt =0,ans=0;

int main(){
    int n;  cin >> n;
    if(n>=30) cout << "Yes" <<endl;
    else cout << "No" << endl;

}

Measured memory_bytes: 3600
import sys
import collections
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    s = [input() for _ in range(n)]
    cnt = collections.Counter(s)
    values, counts = zip(*cnt.most_common())
    d = sorted(values[:counts.count(counts[0])])
    print(*d, sep='\n')

if __name__ == '__main__':
    main()

Measured memory_bytes: 59496
import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		String s = scn.next();
		if (s.equals("ABC")) {
			System.out.println("ARC");
		} else {
			System.out.println("ABC");
		}
	}
}

Measured memory_bytes: 35508
n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))

cnt = 0

def binary_search(S, n):
    high = len(S)-1
    low = 0
    
    while(high >= low):
        mid = (high+low)//2
        if(S[mid] == n):
            return mid
        elif(S[mid] < n):
            low = mid+1
        else:
            high = mid -1
    return False
    
for i in range(q):
    if(binary_search(S, T[i])):
        cnt += 1
print(cnt)


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

#define ll long long

const ll int lim = 1e9 + 7;
int H, N, tmp, a[1000], b[1000], out[10001];
 
void solve() {
    std::cin >> H >> N;
    for (int i=0; i<N; ++i)
        std::cin >> a[i] >> b[i];
    for (int i=0; i<10001; ++i)
        out[i] = lim*2;
    out[0] = 0;
    for (int i=0; i<N; ++i) {
        for (int j=0; j<H; ++j) {
            tmp = std::min(H, j + a[i]);
            if (out[tmp] > (out[j] + b[i]))
                out[tmp] = out[j] + b[i];
        }
    }
    std::cout << out[H] << std::endl;
}
 
int main() {
    std::ios_base::sync_with_stdio(0);
    solve();
    return 0;
}


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

using namespace std;

using int64 = long long;

int main() {
  int N;
  cin >> N;
  vector< int64 > X;
  for(int i = 0; i <= N; i++) {
    int v;
    cin >> v;
    X.emplace_back(v);
  }
  sort(begin(X), end(X));
  X.erase(unique(begin(X), end(X)), end(X));
  int64 small = 0;
  for(int i = 1; i < X.size(); i++) small = __gcd(small, X[i] - X[i - 1]);
  cout << small << endl;
}

Measured memory_bytes: 1400
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, s, n) for (int i = (s); i < (n); ++i)
#define rrep(i, n, g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define len(x) (int)(x).size()
#define dup(x, y) (((x) + (y)-1) / (y))
#define pb push_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int a, b;
  cin >> a >> b;
  cout << a * a - b << endl;
}

Measured memory_bytes: 3592
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
int n,k,A,B,x;
ll ans,C[60][60],sum[60];
vector<int> d,e;
int yc[1<<18];
map<int,int> M;
map<int,int>::iterator it;
bool in(int s,int t) {
	return (s&t)==s&&(s|t)==t;
}
int F(int x,int i) {
	if (x&(1<<i)) return 1;
	return 0;
}
int main() {
	//printf("MLE %d\n",sizeof(dp)/1024/1024);
	//freopen("1.txt","r",stdin);
	for (int i=1;i<(1<<18);i++)
		yc[i]=yc[i-(i&(-i))]+1;
	read(n); read(k);
	read(A); read(B);
	if (!in(A,B)) { printf("0\n"); return 0; }
	
	for (int i=0;i<=n;i++) {
		C[i][0]=C[i][i]=1;
		for (int j=0;j<i;j++) C[i][j]=C[i-1][j-1]+C[i-1][j];
		for (int j=1;j<=k;j++) sum[i]+=C[i][j];
		//printf("%d %lld\n",i,sum[i]);
	}
	
	for (int i=1;i<=n;i++) {
		read(x);
		if (in(A,x)&&in(x,B)) d.push_back(x-A);
	}
	
	
	for (int i=0;i<(1<<18);i++) if (in(i,B-A)) {
		int tmp=yc[i];
		if (tmp&1) tmp=-1;
		else tmp=1;
		
		M.clear();
		for (int j=0;j<d.size();j++)
			M[d[j]&i]++;
		
		for (it=M.begin();it!=M.end();it++)
			ans+=tmp*sum[it->second];
	}
	printf("%lld\n",ans);
	return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?
  
  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?
    
  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?
    
  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/

Measured memory_bytes: 4796
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define N 100000
typedef long long ll;
#define ALL(v) (v).begin(),(v).end()
#define SZ(x) int(x.size())
#define OUT(a) cout<<(a)<<endl

int main(){
	int n;
	cin>>n;
	OUT((n+1)/2);
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <utility>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <limits.h>
#include <bitset>
#define int long long
using namespace std;
using P = pair<int, int>;
using ll = long long;
using prique = priority_queue<int>;
const int MOD = 1000000007;
const int INF = LLONG_MAX/3;
const int MAX = 510000;
signed main(){
    int h,w,d;
    cin>>h>>w>>d;
    P p[100000];
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            int k;
            cin>>k;
            p[k-1]=P(i,j);
        }
    }
    vector<int>a[100000];
    for(int i=0;i<d;i++){
        int c=i;
        int j=0;
        while(1){
            if(c>h*w)break;
            if((j==0&&i>=1)||(i==0&&j<=1))a[i].push_back(0);
            else{
                a[i].push_back(a[i][j-1]+abs(p[c-1].first-p[c-d-1].first)+abs(p[c-1].second-p[c-d-1].second));
            }
            c+=d;
            j++;
        }
    }
    int q;
    cin>>q;
    for(int i=0;i<q;i++){
        int l,r;
        cin>>l>>r;
        int t=l%d;
        cout<<a[t][r/d]-a[t][l/d]<<endl;
    }
    return 0;
}


Measured memory_bytes: 7168
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e5+10;
struct node{
    int cnt,num;
    node(){}
    node(int _n,int _c){num = _n; cnt = _c;}
    bool operator<(const node &t)const {return num > t.num;}
}maz[maxn];
int a[maxn];
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for (int i=0;i<n;i++) scanf("%d",&a[i]);
    sort(a,a+n);
    for (int i=0;i<m;i++){
        scanf("%d%d",&maz[i].cnt, &maz[i].num);
    }
    sort(maz,maz+m);
    int pos = 0;
    for (int i=0;i<m;i++){
        int rest = maz[i].cnt, t = maz[i].num;
        while (pos < n && a[pos] < maz[i].num && rest > 0){
            a[pos] = t;rest--;
            pos++;
        }
    }
    LL ans = 0;
    for (int i=0;i<n;i++) ans += a[i];
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 1408
#include <bits/stdc++.h>
#define FOR(i,k,n)  for(int i = (k);i < (n);++i)
#define REP(i,n)    FOR(i,0,n)
#define ALL(x)      begin(x),end(x)

using namespace std;
using vecint = vector<int>;
using ll = int64_t;

int main()
{
  int d;
  cin>>d;
  cout<<"Christmas";
  REP(i,25-d) {
    cout<<" Eve";
  }
  cout<<endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define vsort(v) sort(v.begin(), v.end())
#define rvsort(v) sort(v.begin(), v.end(), greater<ll>())
#define prvsort(v) sort(v.begin(), v.end(), greater<pair<ll, ll>>())
#define ll long long
#define ld long double
#define fixset(val) fixed << setprecision(10) << val 
#define rep(i, s, g) for (ll i = (s); i < (g); i++)
#define mod 1000000007
#define vfind(v, target) find(v.begin(), v.end(), target)
#define notFound(v, target) (vfind(v, target) == v.end())

using namespace std;

int main()
{
  string S; cin >> S;
  ll Q, T, F; cin >> Q;
  bool rev = false;
  char C;
  string S1 = "", S2 = "";
  rep(i, 0, Q) {
  	cin >> T;
    if (T == 1) {
    	if (rev) rev = false;
      	else rev = true;
    } else if (T == 2) {
    	cin >> F >> C;
      	if (F == 1) {
        	if (rev) S2 += C;
          	else S1 += C;
        } else if (F == 2) {
        	if (rev) S1 += C;
          	else S2 += C;
        } 
    }
  }
  if (rev) {
    reverse(S2.begin(), S2.end());
    reverse(S.begin(), S.end());
    cout << S2 << S << S1 << endl;
  } else {
    reverse(S1.begin(), S1.end());
    cout << S1 << S << S2 << endl;
  }
}

Measured memory_bytes: 3948
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <utility>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>
#include <cstdio>
#include <map>

using namespace std;

using ll = long long;
using P = pair<ll,ll>;

#define rep(i,x) for(ll i = 0; i < (ll)(x); i++)
#define rrep(i,x) for(ll i = ((ll)(x) - 1); i >= 0; i--)

#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

//ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}

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

const ll mod = 1e9 + 7;
const ll IMF = 1LL<<29;
const double PI = 3.14159265358979323846;


// ******************************CODE**********************************//




int main()
{
        int N;
        cin >> N;
        vector<vector<int>> graph(N,vector<int>());
        for(int i = 0; i < N-1; i++){
                int a,b;
                cin >> a >> b;
                a--; b--;
                graph[a].push_back(b);
                graph[b].push_back(a);
        }

        vector<int> c(N);
        for(int i = 0; i < N; i++) cin >> c[i];
        sort(all(c),greater<int>());

        vector<int> d(N);
        int id = 0;
        queue<pair<int,int>> q;
        q.push({0,-1});

        while(!q.empty()){
                int now = q.front().first;
                int parent = q.front().second;
                q.pop();

                d[now] = c[id];
                id++;

                for(int i = 0; i < graph[now].size(); i++){
                        int nxt = graph[now][i];
                        if(nxt != parent){
                                q.push({nxt,now});
                        }
                }
        }

        int sum = 0;
        for(int i = 1; i < N; i++){
                sum += c[i];
        }

        cout << sum << endl;
        for(int i = 0; i < N; i++){
                cout << d[i] << " ";
        }
        cout << endl;
        return 0;
}



Measured memory_bytes: 1024
#include <stdio.h>
#include <math.h>

int n, K;
int H[100001];
long long DP[100001];

long long max(long long a, long long b) {
    return (a > b ? a : b);
}
long long min(long long a, long long b) {
    return (a < b ? a : b);
}

int main() {
    scanf("%d%d", &n, &K);
    for(int i = 0; i < n; i++) scanf("%d", &H[i]);
    
    for(int i = 1; i < n; i++) {
        DP[i] = (long long)(1e16);
        for(int j = max(0, i-K); j < i; j++)
            DP[i] = min( DP[i], DP[j] + abs(H[i] - H[j]));
    }

    printf("%lld", DP[n-1]);
}

Measured memory_bytes: 1280
#include <bits/stdc++.h>
#define ll long long
#define ld double
#define oo 2000000000
#define ull unsigned long long
#define ui unsigned int
#define uc unsigned char
#define zero 1E-6
#define vi vector<int>
#define vii vector<ii>
#define ii pair<int,int>
#define mii map<int,int>
#define msi map<string,int>
#define miii map<int,int>::iterator
#define fto(i,x,y) for(i=(x);i<=(y);i++)
#define fdto(i,x,y) for(i=(x);i>=(y);i--)
#define ftoi(i,x,y) for(int i=(x),__dest=(y);i<=__dest;i++)
#define fdtoi(i,x,y) for(int i=(x),__dest=(y);i>=t;i--)
#define upn(i,x) for(int i=1,__dest=(x);i<=__dest;i++)
#define cto(it,x) for(auto it=x.begin();it!=x.end();++it)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
#define upb upper_bound
#define lob lower_bound
#define eqr equal_range
#define ins insert
#define rm remove
#define ers erase

#define cppio cin.sync_with_stdio (0); cin.tie (0); cout.tie (0);
#define bug(x) cout<<#x<<':'<<x<<' ';
#define abug(a,x,y) {ftoi(i,x,y){cout<<a[i]<<' ';} cout<<'\n';}

using namespace std;

void swap (int& a, int& b) {
	int t = a;
	a = b;
	b = t;
}
int main () {
	int A, B, C;
	cin >> A >> B >> C;
	int M = A + B + C;
	int mx = A;
	if (mx < B) mx = B;
	if (mx < C) mx = C;
	int X = M;
	while ((X/3 < mx) || (X % 3) || (X % 2 != M % 2))
		X += 2;
	cout << (X - M) / 2;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
#define Sort(s) sort(s.begin(),s.end())
#define f0(i,n) for(int i=0;i<n;i++)
#define fs(i,s) for(int i=0;i<s.size();i++)
#define ll long long int
#define nl endl
int main()
{
	int n;
	cin>>n;
	cout<<n+n*n+n*n*n;
}

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

using namespace std;
int main(){
	int n;cin>>n;int arr[n+10];
	int s=0;
	for(int i=1;i<=n;i++){
		cin>>arr[i];
		if(arr[i]!=i)
			s++;
	}
	if(s==2||s==0){
		cout<<"YES";
	}else{
		printf("NO");
	}
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cctype>

using namespace std;

int main(){
	char ch;
	
	while(1){
		scanf("%c", &ch);
		
		if(islower(ch)) ch = toupper(ch);
		else if(isupper(ch)) ch = tolower(ch);
		printf("%c", ch);
		if( ch == '\n') break;
	}
	
	return 0;
}

Measured memory_bytes: 3052
import java.io.*;
public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(
				new InputStreamReader (System.in));
		String line=br.readLine();
		String[] str = line.split(" ");
		int a=Integer.parseInt(str[0]);
		int b=Integer.parseInt(str[1]);
		System.out.println(a*b+" "+(a+b)*2);
	}
}

Measured memory_bytes: 24284