akhauriyash/Code-Regression huggingface dataset:
from bisect import bisect_left, bisect_right

N, K = map(int, input().split())
As = sorted(map(int, input().split()))
        
l = - (10 ** 18 + 10)
r = 10 ** 18 + 10
while True:
    mid = (l + r) // 2
    count = 0
    for i, A in enumerate(As):
        if A > 0:
            count += bisect_right(As, mid//A, lo = i+1) - (i+1)
        elif A < 0:
            count += N - bisect_left(As, -((-mid)//A), lo = i+1)
        else:
            if mid >= 0:
                count += N - i - 1
    if count < K:
        l = mid
    else:
        r = mid
    if r - l == 1:
        break

print(r)


Measured memory_bytes: 104800
#include <bits/stdc++.h>
using namespace std;
int main() {
    int h,n;
    cin>>h>>n;
    int dp[h+1];
    dp[0]=0;
    for (int i=1;i<=h;i++)
        dp[i]=1000000000;
    int a[n+1],b[n+1];
    for (int i=1;i<=n;i++)
        cin>>a[i]>>b[i];
    for (int i=0;i<h;i++)
        for (int j=1;j<=n;j++)
            dp[min(h,a[j]+i)]=min(dp[min(h,a[j]+i)],dp[i]+b[j]);
    cout<<dp[h]<<endl;
}


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

using namespace std;

#define int long long
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for(ll i = (a); i > (b); --i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CINT(...)                                                              \
    int __VA_ARGS__;                                                           \
    CIN(__VA_ARGS__)
#define DCIN(...)                                                              \
    double __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    ll __VA_ARGS__;                                                            \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v)                                                              \
    {                                                                          \
        FOREACH(x, v) { cout << x << " "; }                                    \
        cout << "\n";                                                          \
    }
template <typename T = string> inline void eputs(T s) {
    cout << s << "\n";
    exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;

typedef long long ll;
typedef unsigned long long ul;
typedef vector<ll> VL;
typedef pair<ll, ll> PL;

const int INF = 1e9;
const int MOD = 1e9 + 7;
const double PI = atan(1.0) * 4.0;
// const int MOD = 998244353;
const ll LINF = 1e18;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};

void cinfast() {
    cin.tie(0);
    ios::sync_with_stdio(false);
}

ll rev[200000], in[200000], out[200000], ptr, ans[200001], val[200001];
vector<VL> tree(200000);

void dfs(ll idx, ll par = -1) {
    rev[ptr] = idx;
    in[idx] = ptr++;
    FOREACH(next, tree[idx]) if(next != par) dfs(next, idx);
    out[idx] = ptr;
}

signed main() {
    LCIN(N);
    double ans = 0;
    vector<double> v(N);
    VECCIN(v);
    sort(All(v));
    REP(i, N) {
        ans += v[i];
        if(i) ans /= 2.0;
    }
    printf("%.9f\n", ans);
}


Measured memory_bytes: 4992
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BinaryLiterals #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE NumDecimals #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}

import Control.Applicative
import Control.Arrow
import Control.Monad
import Control.Monad.ST

import Data.Char
import Data.List
import Data.Maybe

import qualified Data.ByteString.Char8 as B

import Data.Array.ST.Safe
import Data.STRef

import Debug.Trace
import Text.Printf

readInt = readLn :: IO Int
readInteger = readLn :: IO Integer
readInts = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine
readIntegers = map ( fst . fromJust . B.readInteger ) . B.words <$> B.getLine

which a b f = if f then a else b
mp [ a, b ] = ( a, b )

modifyArray a i f = writeArray a i =<< f <$> readArray a i

main = do
	[ n, m ] <- readInts
	as <- readInts
	let
		s = sum as
	print $ if s <= n
		then n - s
		else -1

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

r = 0
g = 0
b = 0
cnt_S = []
for i, s in enumerate(S):
    if s == 'R':
        r += 1
    elif s == 'G':
        g += 1
    else:
        b += 1
    cnt_S.append([r, g, b])

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        if S[i] == 'R':
            if S[j] == 'G':
                ans += cnt_S[-1][2] - cnt_S[j][2]
                if 2 * j - i < N and S[j + j - i] == 'B':
                    ans -= 1
            if S[j] == 'B':
                ans += cnt_S[-1][1] - cnt_S[j][1]
                if 2 * j - i < N and S[j + j - i] == 'G':
                    ans -= 1
        elif S[i] == 'G':
            if S[j] == 'R':
                ans += cnt_S[-1][2] - cnt_S[j][2]
                if 2 * j - i < N and S[j + j - i] == 'B':
                    ans -= 1
            if S[j] == 'B':
                ans += cnt_S[-1][0] - cnt_S[j][0]
                if 2 * j - i < N and S[j + j - i] == 'R':
                    ans -= 1
        else:
            if S[j] == 'R':
                ans += cnt_S[-1][1] - cnt_S[j][1]
                if 2 * j - i < N and S[j + j - i] == 'G':
                    ans -= 1
            if S[j] == 'G':
                ans += cnt_S[-1][0] - cnt_S[j][0]
                if 2 * j - i < N and S[j + j - i] == 'R':
                    ans -= 1
print(ans)





Measured memory_bytes: 69220
n=int(input())
cnt=n
ans=0
cnt25=divmod(cnt,25)
ans+=cnt25[0]
cnt=cnt25[1]
cnt10=divmod(cnt,10)
ans+=cnt10[0]
cnt=cnt10[1]
cnt5=divmod(cnt,5)
ans+=cnt5[0]
cnt=cnt5[1]
cnt1=divmod(cnt,1)
ans+=cnt1[0]
print(ans)


Measured memory_bytes: 5592
#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define Mod 1000000007

int main() {
    ll n,k;
    cin>>n>>k;
    int ok[n]={0};
    for (int i = 0; i < k; ++i) {
        int num;
        cin>>num;
        for (int j = 0; j < num; ++j) {
            int hito;
            cin>>hito;
            hito--;
            if(ok[hito]==0)ok[hito]=1;
        }
    }
    int ans=0;
    for (int l = 0; l < n; ++l) {
        if(ok[l]==0)ans++;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rp(i,n) for (int i=0;i<n;i++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define all_acc(x) (x).begin(), (x).end(), 0LL
#define int long long
#define absSort(v) sort(a.begin(), a.end(), [](int i, int j) -> bool { return abs(i) < abs(j); });
using namespace std;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using ll = long long;
double pi=3.14159265359;


void output_yoko(vector<int> s){
    int n = s.size();
    for (int i = 0; i < n; i++){
        //if (i != 0)
            cout << " ";
        cout << s[i];//<<endl;
    }
    cout << endl;
}

//出力形式を返す関数
string out(vector<int> v){
    string ans="[";
    for(int i=0;i<v.size();i++){
        if(i!=0) ans+=", ";
        ans+=to_string(v[i]);
    }
    ans+="]";
    return ans;
}

struct Node{
    //自分の親、左右の子、深さ、高さ、次数を保持
    int parent=-1,sibling=-1, left, right, depth=0 , height=0 ,degree;
    string status="internal node";
};


struct bstNode{
    int key;
    bstNode *parent, *left, *right;
};

bstNode *root, *NIL;

void insert(int k){
    bstNode *y=NIL;
    bstNode *x=root;
    bstNode *z;

    z = (bstNode*)malloc(sizeof(bstNode));
    z->key = k;
    z->left = NIL;
    z->right = NIL;

    //親を探す
    while(x != NIL){
        y = x;
        if( z->key < x->key){
            x = x->left;
        }else{
            x = x->right;
        }
    }

    //親を設定。NILのときはｚは根
    z->parent = y;

    if(y==NIL){
        root=z;
    }else{
        if(z->key < y->key){
            y->left = z;
        }else{
            y->right = z;
        }
    }
}

bool find(bstNode *x,int k){
    if(k < x->key){
        if(x->left != NIL) return find(x->left,k);
        else return false;
    }else if(k > x->key){
        if(x->right != NIL) return find(x->right,k);
        else return false;
    }else{
        return true;
    }
}

void inorder(bstNode *u){
    if(u==NIL) return;
    inorder(u->left);
    cout<<" "<<u->key;
    inorder(u->right);
}

void preorder(bstNode *u){
    if(u==NIL) return;
    cout<<" "<<u->key;
    preorder(u->left);
    preorder(u->right);
}


signed main(){
    int m,x; cin>>m;
    string s;

    for(int i=0;i<m;i++){
        cin>>s;
        if(s=="insert"){
            cin>>x;
            insert(x);
        }else if(s=="find"){
            cin>>x;
            if(find(root,x)) cout<<"yes"<<endl;
            else cout<<"no"<<endl;
        }
        else{
            inorder(root); cout<<endl;
            preorder(root); cout<<endl;
        }
    } 

    //setDepthAll(tree,root_index);

    //setHeightAll(tree,root_index);

    //output_tree(tree);
}


Measured memory_bytes: 21784
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    const int size = 5;
    vector<int> antennas(size);
    for (int i = 0; i < size; ++i)
        cin >> antennas[i];

    int k;
    cin >> k;

    for (int i = 0; i < size - 1; ++i) {
        for (int j = i + 1; j < size; ++j) {
            if (abs(antennas[i] - antennas[j]) > k) {
                cout << ":(\n";
                return 0;
            }
        }
    }
    cout << "Yay!\n";
}


Measured memory_bytes: 764
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("3 1 1");
            WillReturn.Add("1 2 1");
            WillReturn.Add("2 1 2");
            WillReturn.Add("3 3 10");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("1 1 10");
            WillReturn.Add("10 10 10");
            //-1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct YakuhinInfoDef
    {
        internal int A;
        internal int B;
        internal int C;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(X => int.Parse(X)).ToArray();

        SplitAct(InputList[0]);
        int MA = wkArr[1];
        int MB = wkArr[2];

        var YakuhinInfoList = new List<YakuhinInfoDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            YakuhinInfoDef WillAdd;
            WillAdd.A = wkArr[0];
            WillAdd.B = wkArr[1];
            WillAdd.C = wkArr[2];
            YakuhinInfoList.Add(WillAdd);
        }

        //最小金額[タイプAの合計,タイプBの合計]なDP表
        var PrevDP = new Dictionary<string, int>();
        PrevDP["0,0"] = 0;

        foreach (YakuhinInfoDef EachYakuhin in YakuhinInfoList) {
            var CurrDP = new Dictionary<string, int>(PrevDP);
            foreach (var EachPair in PrevDP) {
                string[] wkStr = EachPair.Key.Split(',');
                int CurrA = int.Parse(wkStr[0]);
                int CurrB = int.Parse(wkStr[1]);

                int NewA = CurrA + EachYakuhin.A;
                int NewB = CurrB + EachYakuhin.B;
                int NewC = EachPair.Value + EachYakuhin.C;

                string NewKey = string.Format("{0},{1}", NewA, NewB);
                if (CurrDP.ContainsKey(NewKey)) {
                    if (CurrDP[NewKey] > NewC)
                        CurrDP[NewKey] = NewC;
                }
                else CurrDP[NewKey] = NewC;
            }
            PrevDP = CurrDP;
        }

        int Answer = int.MaxValue;
        bool HasAnswer = false;

        foreach (var EachPair in PrevDP) {
            string[] wkStr = EachPair.Key.Split(',');
            int CurrA = int.Parse(wkStr[0]);
            int CurrB = int.Parse(wkStr[1]);

            for (int I = 1; I < int.MaxValue; I++) {
                if (MA * I > CurrA) break;
                if (MB * I > CurrB) break;

                if (MA * I != CurrA) continue;
                if (MB * I != CurrB) continue;

                HasAnswer = true;
                if (Answer > EachPair.Value) {
                    Answer = EachPair.Value;
                }
            }
        }
        Console.WriteLine(HasAnswer ? Answer : -1);
    }
}


Measured memory_bytes: 23040
using System;
public class Program{
    public static void Main(){
        //????????????????????????????¨???°
        int x = 0;
        int y = 0;
        int kai = 0;

        for(x=1 ;x<10;x++){
           for(y=1 ;y<10;y++){
            kai = x * y;
            Console.WriteLine(x + "x" + y + "=" + kai);
           }
        }
    }
}

Measured memory_bytes: 9060
import std.stdio, std.conv, std.string, std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
void main(string[] args){ if(args.length > 1) if(args[1] == "-debug") DEBUG = 1; solve(); }
void log()(){ writeln(""); } void log(T, A ...)(T t, lazy A a){ if(DEBUG) write(t, " "), log(a); } bool DEBUG = 0; 
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //
void solve(){
	int n = read.to!int;
	
	long[] as;
	foreach(i; 0 .. n) as ~= read.to!long;
	
	int[long] ac;
	foreach(a; as){
		if(a !in ac) ac[a] = 0;
		ac[a] += 1;
	}
	
	string ans;
	if(ac.keys.length > 3) ans = "No";
	else if(ac.keys.length == 3){
		long[] ks = ac.keys;
		if(ac[ks[0]] == ac[ks[1]] && ac[ks[1]] == ac[ks[2]]
			&& (ks[0] ^ ks[1]) == ks[2]) ans = "Yes";
		else ans = "No";
	}
	else if(ac.keys.length == 2){
		long[] ks = ac.keys;
		if(ac[ks[0]] == ac[ks[1]] * 2 && (ks[0] ^ ks[0]) == ks[1]) ans = "Yes";
		else if(ac[ks[0]] * 2 == ac[ks[1]] && (ks[0] ^ ks[1]) == ks[1]) ans = "Yes";
		else ans = "No";
	}
	else if(ac.keys.length == 1){
		long[] ks = ac.keys;
		if(ks[0] == 0) ans = "Yes";
		else ans = "No";
	}
	
	ans.writeln;
}


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

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

#define REP(i, n, m) for(ll i=n; i<(ll)m; ++i)
#define IREP(i, n, m) for(ll i=n-1; i>=m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()
#define vprint(v) for(auto e:v){cout<<e<<" ";};cout<<endl;
#define vvprint(vv) for(auto v:vv)vprint(v);


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

    ll N;
    cin >> N;
    vll A(N-1);
    rep(i, N-1) cin >> A[i];

    vvll subs(N);
    rep(i, N-1) subs[A[i]-1].push_back(i);
    rep(i, N){
        cout << subs[i].size() << endl;
    }
}


Measured memory_bytes: 15872
s = input()
q = int(input())

answer = s
s1 = ""
s2 = ""
state = 0


for _ in range(q):
    Q = input()
    if Q == "1":
        if state == 0:
            state = 1
        else:
            state = 0
        continue

    _,F,C = Q.split()
    if F == "1":
        if state == 0:
            s2 = s2 + C
        else:
            s1 = s1 + C
    else:
        if state == 0:
            s1 = s1 + C
        else:
            s2 = s2 + C





if state == 0:
    answer = s2[::-1] + s + s1
else:
    answer = s1[::-1] + s[::-1] + s2



print(answer)


Measured memory_bytes: 4340
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+2;
int lef[N],rig[N];
signed main(){
	int n,i,j,k,l,cnt=0;
	long long ans=0;
	string s;
	cin>>s;
	for(i=0;i<s.size();i++){
		if(s[i]=='<'){
			cnt++;
		}
		else{
			cnt=0;
		}
		lef[i+1]=cnt;
	}
	cnt=0;
	for(i=s.size()-1;i>-1;i--){
		if(s[i]=='>'){
			cnt++;
		}
		else{
			cnt=0;
		}
		rig[i]=cnt;
	}
	for(i=0;i<=s.size();i++){
		ans+=max(lef[i],rig[i]);
	}
	cout<<ans;
}

Measured memory_bytes: 4740
#include <iostream>
#include <vector>
#include <map>
int main(){
  int n;
  std::map<int,long long> m;
  std::vector<int> v;
  //mlÛ¶vÛ¶
  long long a,b,c;
  //aÔbP¿cÊ
  bool f;
  while(std::cin>>n && n){
     m.clear();
     v.clear();
     f=true;
    for(;n;n--){
      std::cin>>a>>b>>c;
      if(!m[a])v.push_back(a);
      m[a] += b*c;
    }
    
    for(int i=0;i<v.size();i++){
      if(m[v[i]]>=1000000){
	f=false;
	std::cout<<v[i]<<std::endl;}
    }
    if(f)std::cout<<"NA"<<std::endl;
  }
  return 0;
}

Measured memory_bytes: 0
using System;

class Program
{
    static void Main(string[] args)
    {
        var input = Console.ReadLine().Split(' ');
        var start = Convert.ToInt32(input[0]);
        var time = Convert.ToInt32(input[1]);

        var result = start - time >= 0 ? start - time : 0;
        Console.WriteLine(result);
    }
}

Measured memory_bytes: 11220
X = int(input())
if(X<600):
     print(8);
elif(X<800):
     print(7);
elif(X<1000):
     print(6);
elif(X<1200):
     print(5);
elif(X<1400):
     print(4);
elif(X<1600):
     print(3);
elif(X<1800):
     print(2);
else:
     print(1);


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

int main() {
 int N,K;
  cin >> N >> K;
  int sum;
  for(int i = 0; i < N; i++){
    int x;
    cin >> x;
    if(K>x){
      if(x < (K-x))
        sum += x;
      else
        sum += K-x;
      }
    else{
      if(x < (x-K))
        sum += x;
      else
        sum += x-K;
      }
    }
  cout << 2 * sum << endl;
}


Measured memory_bytes: 256
object Main {
  def main(args: Array[String]): Unit = {
    val N = Console.in.readLine().toInt
    val S = Console.in.readLine()
    if(N % 2 == 0) {
      val s1 = S.substring(0, N / 2)
      val s2 = S.substring(N / 2)
      print(if(s1 == s2) "Yes" else "No")
    } else {
      print("No")
    }
  }
}

Measured memory_bytes: 25536
#include <bits/stdc++.h>
using namespace std;
int main(){
  long long x;
  cin>>x;
  long long res = 0;
  res += (x / 500) * 1000;
  x %= 500;
  res += (x / 5) * 5;
  cout<<res<<endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i,l,r) for(i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) x.begin(),x.end()
#define P pair<ll,ll>
#define F first
#define S second
signed main(){
  ll N,i,ans;cin>>N;ll A[N];
  REP(i,N)cin>>A[i];
  REP(i,N)if(A[i]==i+1){ans++;i++;}
  cout<<ans<<endl;
  return 0;
}

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

#define llong long long
#define fr(i,l,r) for(i=(l);i<(r);i++)
#define min(p,q) ((p)<(q)?(p):(q))
#define max(p,q) ((p)>(q)?(p):(q))
#define INF 1000000000000//10^12



int main(void)
{
	//変数の宣言
	int n;
	int a[100010];
	
	//よく使う変数
	int i,j,k,l;
	int flag=0;
	llong int ans=0;
	int count=0;
	int temp,temp1,temp2;
	int max,min;
	int len;
	int sum=0;
	int under,upper,middle;

	//データの読み込み
	scanf("%d",&n);
	
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	
//	printf("nは%dです\n", n);
//	printf("データの読み込み終了\n");
	//実際の処理
	
	flag=0;
	for(i=0;i<n;i++){
		if(a[i]%2==1){
			flag=1;
		}
	}
	
	
//	printf("計算部分終了\n");
	//出力
	
	if(flag==0){
		printf("second");
	}else{
		printf("first");
	}
	
	
	
//	printf("結果の出力終了\n");
	
	return 0;
	
}



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

using namespace std;
typedef long long ll;
typedef double dd;
#define all(v) v.begin()  ;v.end()
#define endl "\n"

void fast() {

    cin.tie(0);
    cin.sync_with_stdio(0);
}
int main(){
    fast();
    int n,k;cin>>n>>k;
    int freq[n+1]={0};
    for(int i=1;i<=k;i++){
        int d;cin>>d;
        for(int i=1;i<=d;i++){
            int a;cin>>a;
            freq[a]++;
        }
    }
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(!freq[i])cnt++;
    }
    cout<<cnt;
}

Measured memory_bytes: 3696
from collections import deque

n, m = list(map(int, input().split()))
edges = [[] for _ in range(n+1)]
landmarks = [-1] * (n+1)
for i in range(m):
  a, b = list(map(int, input().split()))
  edges[a].append(b)
  edges[b].append(a)

queue = deque()
landmarks[1] = 0
queue.append(1)
while len(queue) != 0:
  v = queue.popleft()
  for i in edges[v]:
    if landmarks[i] != -1:
      continue
    landmarks[i] = v
    queue.append(i)
flag = 1
for i in range(2,n+1):
  if landmarks[i] == -1:
    flag = 0
if flag:
  print('Yes')
  for i in range(2,n+1):
    print(landmarks[i])
else:
  print('No')


Measured memory_bytes: 33980
import copy
class Node:
    def __init__(self):
        self.adjacentnode=[]

def search(lista,nodet):
    global count
    global n
    if len(lista)==n:
            count+=1
            return True
    for a in nodet[lista[-1]].adjacentnode:
        if a not in lista:
                newlist=copy.copy(lista)
                newlist.append(a)
                search(newlist,nodet)

n,m=map(int,input().split()) #n is node,m is edge 
count=0
listsrch=[0]
nodelist=[Node() for a in range(n)]
for t in range(m):
    a,b=map(int,input().split())
    nodelist[a-1].adjacentnode.append(b-1)
    nodelist[b-1].adjacentnode.append(a-1)
search(listsrch,nodelist)
print(count)

Measured memory_bytes: 3572
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;

struct DSU
{
	int par[N], sz[N];

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

	int getPar(int k)
	{
		while(k!=par[k])
		{
			par[k]=par[par[k]];
			k=par[k];
		}
		return k;
	}

	int getSize(int k)
	{
		return sz[getPar(k)];
	}

	void unite(int u, int v)
	{
		int par1=getPar(u), par2=getPar(v);

		if(par1==par2)
			return;


		if(sz[par1]>sz[par2])
			swap(par1, par2);

		sz[par2]+=sz[par1];
		sz[par1]=0;
		par[par1]=par[par2];
	}
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    DSU dsu;
    int n,m;
    cin >> n >> m;
    dsu.init(n);
    while(m--)
    {
        int x,y,z;
        cin >> x >> y >> z;

            dsu.unite(x,y);
        
    }
    int cnt = 1;
    int now;
    for(int i=2;i<=n;++i)
    {
        now = dsu.getPar(i);
        if(now!=dsu.getPar(1))
        {
            dsu.unite(1,i);
            cnt++;
        }
    }
    cout << cnt;
}

Measured memory_bytes: 1024
#include <cstdio>
#include <set>
#include <queue>
#include <cstdlib>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<b; ++i)
#define QCLR(q) while(!q.empty()) q.pop();

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

int N,K,rx[100],ry[100],gx,gy;

class Hex
{
public:
	int px[8], py[8];
	Hex(int tpx[8], int tpy[8])
	{
		REP(i,N) 
		{
			px[i]=tpx[i];
			py[i]=tpy[i];
		}
	}

	bool operator<(const Hex& h) const
	{
		REP(i,N)
		{
			if(px[i]!=h.px[i]) return px[i]<h.px[i];
			if(py[i]!=h.py[i]) return py[i]<h.py[i];
		}
		return false;
	}
};

class State
{
public:
	Hex h;
	int c;
	State(Hex h, int c)
	:h(h),c(c)
	{}
};

set<Hex> s;
queue<State> q;

int dist60(int x1, int y1, int x2, int y2)
{
	return (abs(x1-x2)+abs(y1-y2)+abs((x1+y1)-(x2+y2)))/2;
}

bool crash(Hex& h, int p)
{
	REP(i,K) if(h.px[p]==rx[i]&&h.py[p]==ry[i]) return true;
	
	return false;
}

bool connect(Hex& h, int j)
{
	REP(i,j)
	{
		if(h.px[i]==h.px[j]&&h.py[i]==h.py[j]) return false;
		
		int cnt=0;
		REP(k,6)
		{
			int tx=h.px[i]+dx[k], ty=h.py[i]+dy[k];
			if(h.px[j]==tx&&h.py[j]==ty) 
			{
				cnt++;
				break;
			}
		}
		
		if(abs(i-j)==1&&cnt!=1) return false;
		if(abs(i-j)!=1&&cnt==1) return false;
	}
	
	return true;
}

bool create(int p, Hex& h, int c, bool sk)
{
	if(p==N) 
	{
		if(!s.count(h)) 
		{
			s.insert(h);
			q.push(State(h,c+1));
			return true;
		}
		return false;
	}
	
	int cnt=0;
	if(connect(h,p)) create(p+1,h,c,false);
	if(!sk)
	{
		REP(i,6)
		{	
			h.px[p]+=dx[i], h.py[p]+=dy[i];
			if(crash(h,p) || !connect(h,p))
			{
				h.px[p]-=dx[i], h.py[p]-=dy[i];
				continue;
			}
			
			if(create(p+1,h,c,true)) cnt++;
			h.px[p]-=dx[i], h.py[p]-=dy[i];
			
			if(p==0||p==N-1) { if(cnt==2) break; }
			else { if(cnt==1) break; }
		}
	}
	
	return false;
}

int bfs(int ix[8], int iy[8])
{
	q.push(State(Hex(ix,iy), 0));

	while(1)
	{
		State s=q.front(); q.pop();
		if(s.h.px[0]==gx&&s.h.py[0]==gy) return s.c;
		int dist=dist60(gx,gy,s.h.px[0],s.h.py[0]);
		if(dist>1) dist=(dist-1)*2;
		if(dist > 20-s.c) continue;
		create(0,s.h,s.c,false);
	}
	
	return 0;
}
int main()
{
	while(scanf("%d", &N),N)
	{
		QCLR(q);
		s.clear();
		int px[8], py[8];
		REP(i,N) scanf("%d %d", &px[i], &py[i]);
		
		scanf("%d", &K);
		REP(i,K) scanf("%d %d", &rx[i], &ry[i]);

		scanf("%d%d", &gx,&gy);
		printf("%d\n", bfs(px,py));
	}
}

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

using namespace std;

#define int long long
#define ll long long
typedef pair<int, int> P;
#define mod 1000000007
#define INF (1LL<<60)

#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl



signed main(){

	int A, B;
	cin >> A >> B;
	int ans = 0;
	if (A>B){
		ans += A;
		A--;
	}
	else{
		ans += B;
		B--;
	}
	if (A>B){
		ans += A;
		A--;
	}
	else{
		ans += B;
		B--;
	}
	cout << ans << endl;


	return 0;
}

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

int main(){
  int n,a,b;
  cin>>n>>a>>b;
  int p1=n*a;
  if(p1<b)
    cout<<p1;
  else
  {
    cout<<b;
  }
  
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int n ;
  int alice = 0 ;
  int bob = 0 ;
  cin >> n;
  vector<int> a(n);
  for(int i = 0 ; i < n; i++){
    cin >> a.at(i);
  }
  sort(a.begin(),a.end());
  reverse(a.begin(), a.end());

  for(int i = 0 ; i < n; i += 2){
    alice += a.at(i);
  }
  for(int i = 1 ; i < n ; i += 2){
    bob += a.at(i);
  }
  cout << alice - bob << endl;
}

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

int main(){
    int X,Y;
    cin >> X >> Y;
    if(Y%2 == 0&& 4*X-Y>=0 && Y-2*X >=0){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
}

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

int main(){
    ll n;
    cin >> n;
    ll x1,x2,x3,x4;
    ll y1,y2,y3,y4;

    ll x,y;
    rep(i,n){
        cin >> x >> y;
        if(i==0){
            x1=x;
            x2=x;
            x3=x;
            x4=x;
            y1=y;
            y2=y;
            y3=y;
            y4=y;
            
        }
        else{
            if((x+y)>(x1+y1)){
                x1=x;
                y1=y;
            }
            if((-x+y)>(-x4+y4)){
                x4=x;
                y4=y;
            }
            if((-x-y)>(-x3-y3)){
                x3=x;
                y3=y;
            }
            if((x-y)>(x2-y2)){
                x2=x;
                y2=y;
            }

        }
    }

    ll ans;

    ans=x1+y1-x3-y3;
    ans=max(ans,-x4+y4+x2-y2);

    cout << ans << endl;
    
    

    return 0;
}

Measured memory_bytes: 3592
import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

int calc(int[][] g) {
    int n = cast(int)g.length;

    auto ind = new int[n];

    auto gg = new int[][](n, n);
    int[][] q;
    for (int i = 0; i < n; i++) {
        // i 番目の対戦相手
        int index = ind[i]++;
        if (index == n) continue;
        int t = g[i][index]; // 相手

        int a = min(i, t);
        int b = max(i, t);
        gg[a][b]++;
        if (gg[a][b] == 2) {
            q ~= [a, b];
        }
    }

    int match = 0;
    bool update = true;
    int date = 0;
    auto used = new bool[n];
    while (update) {
        used[] = false;
        update = false;
        int[][] qq;
        while (q.length > 0) {
            auto x = q[$-1]; q.popBack();
            int a = x[0];
            int b = x[1];
            if (!used[a] && !used[b]) {
                used[a] = used[b] = true;
                match++;
                // writeln("match ", a, " ", b);
                update = true;

                int aIndex = ind[a]++;
                if (aIndex < n-1) { // 次の対戦相手
                    int t = g[a][aIndex];
                    int mi = min(a, t);
                    int ma = max(a, t);
                    gg[mi][ma]++;
                    if (gg[mi][ma] == 2) {
                        qq ~= [mi, ma];
                    }
                }

                int bIndex = ind[b]++;
                if (bIndex < n-1) {
                    int t = g[b][bIndex];
                    int mi = min(b, t);
                    int ma = max(b, t);
                    gg[mi][ma]++;
                    if (gg[mi][ma] == 2) {
                        qq ~= [mi, ma];
                    }
                }
            }
        }

        for (int i = 0; i < qq.length; i++) q ~= qq[i];



        if (update) date++;
    }

    if (match == n * (n - 1) / 2) return date;
    return -1;
}

void main() {
    int n; scan(n);
    int[][] g;
    for (int i = 0; i < n; i++) {
        g ~= readints.map!(e => e - 1).array;
    }
    writeln(calc(g));
}


Measured memory_bytes: 12668
#include <bits/stdc++.h>
#include <numeric>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define rep1(i,n) for (int i = 1; i <= n; ++i)
template<typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
const long long MOD = 1e9+7;
template<typename T> inline void add(T& a, T b) { a += b; if (a >= MOD) a -= MOD; }

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

  int N; cin >> N;
  string S; cin >> S;

  vector<vector<int>> T(3);
  rep(i, N) {
    int t;
    if(S[i] == 'R') {
      t = 0;
    } else if(S[i] == 'G') {
      t = 1;
    } else {
      t = 2;
    }
    T[t].push_back(i);
  }

  if(T[0].size() == 0 || T[1].size() == 0 || T[2].size() == 0) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = 0;

  rep(one, 3) {
    rep(two, 3) {
      if(one == two) continue;
      vector<int>& r = T[one];
      vector<int>& g = T[two];
      vector<int>& b = T[(one + two) == 1 ? 2 : (one + two) == 2 ? 1 : 0];

      int rn = r.size();
      int gn = g.size();
      int bn = b.size();

      rep(i, rn) {
        rep(j, gn) {
          if(r[i] > g[j]) continue;
          auto it = upper_bound(b.begin(), b.end(), g[j]);
          if(it == b.end()) continue;
          ans += b.end() - it;
          if(binary_search(it, b.end(), g[j] + g[j] - r[i])) --ans;
        }
      }
    }
  }

  cout << ans << endl;
}


Measured memory_bytes: 3684
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = float("inf")


def main():
    N, A, B = map(int, input().split())
    print(max(((N - 1) * B + A) - ((N - 1) * A + B) + 1, 0))


if __name__ == '__main__':
    main()


Measured memory_bytes: 3444
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;

int dp[1 << 21];
int a[100][100];
int n;

void add(int &a, int b)
{
    a += b;
    if (a >= mod) a -= mod;
}

int getbit(int x, int i)
{
    return (x >> i) & 1;
}

int solve(int i, int cur)
{
    int &res = dp[cur];
    if (i == n) return 1;
    if (res != -1) return res;
    res = 0;
    for (int j = 0; j < n; j++)
    {
        if (!getbit(cur, j) && a[i][j])
        {
            add(res, solve(i + 1, cur | (1 << j)));
        }
    }
    return res;
}

signed main()
{
    //freopen("file.inp","r",stdin);
    cin >> n;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
    {
        cin >> a[i][j];
    }
    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0);
    return 0;
}


Measured memory_bytes: 20032
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //これつけるとA[N]でもいいらしい
//for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define big 1000000007
#define all(a) sort((a).begin(),(a).end()) //ソートのマクロ
#define Re(a) reverse((a).begin(),(a).end())
#define YN(a) if(a){cout<<"Yes"<<endl;}else cout<<"No"<<endl;//条件によってYes、Noを出力する
int main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    vector<int> w(n+1,0),b(n+1,0);
    for(int i=1;i<=n;i++){
        if(s[i-1]=='.'){
            w[i] = w[i-1] + 1;
            b[i] = b[i-1];
        }
        else{
            w[i] = w[i-1];
            b[i] = b[i-1] + 1;
        }
    } 
    /*
    rep(i,n+1){
        cout<<w[i]<<" ";
    }
    cout<<endl;
    rep(i,n+1){
        cout<<b[i]<<" ";
    }
    */
    int Min=10000000;
    rep(i,n+1){
        int change = b[i] + w[n]-w[i];
        if(change<Min){
            Min = change;
        }
    }
    cout<<Min<<endl;
}

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

int main() {
	int h, w, n;
	cin >> h >> w >> n;

	string s[h];
	for ( int i = 0; i < h; i++ ) cin >> s[i];

	bool flag = false;
	for ( int i = 0; i < h; i++ ) {
		for ( int j = 0; j < w-1; j++ ) {
			if ( s[i][j] == s[i][j+1] ) continue;

			string t[h];
			for ( int k = 0; k < h; k++ ) t[k] = s[k];
			swap(t[i][j], t[i][j+1]);			
			
			bool aaa = true;
			while ( aaa ) {
				aaa = false;
				for ( int y = h-2; y >= 0; y-- ) {
					for ( int x = 0; x < w; x++ ) {
						if ( t[y+1][x] == '.' && t[y][x] != '.' ) {
							swap(t[y+1][x], t[y][x]);
							aaa = true;
						}
					}
				}				
			}
			
			bool ch = true;
			
			while ( ch ) {
				bool used[h][w];
				memset(used, 0, sizeof(used));
				ch = false;
				for ( int y = 0; y < h; y++ ) {
					for ( int x = 0; x < w; x++ ) {
						bool ch2 = true;
						if ( y+n-1 < h ) {
							for ( int k = 1; k < n; k++ ) {
								if ( t[y][x] != t[y+k][x] || t[y+k][x] == '.' ) {
									ch2 = false;
									break;
								}
							}
							if ( ch2 ) {
								// if ( i == 3 && j == 2 ) cout << y << " " << x << endl;
								ch = true;
								for ( int k = 0; k < n; k++ ) {
									used[y+k][x] = true;
								}
							}
						}
						if ( x+n-1 < w ) {
							ch2 = true;
							for ( int k = 1; k < n; k++ ) {
								if ( t[y][x] != t[y][x+k] || t[y][x+k] == '.' ) {
									ch2 = false;
									break;
								}
							}
							if ( ch2 ) {
								// if ( i == 3 && j == 2 ) cout << y << " " << x << endl;
								ch = true;
								for ( int k = 0; k < n; k++ ) {
									used[y][x+k] = true;
								}
							}
						}
					}
				}

				for ( int y = 0; y < h; y++ ) {
					for ( int x = 0; x < w; x++ ) {
						if ( used[y][x] ) t[y][x] = '.';
					}
				}

				bool aaa = true;
				while ( aaa ) {
					aaa = false;
					for ( int y = h-2; y >= 0; y-- ) {
						for ( int x = 0; x < w; x++ ) {
							if ( t[y+1][x] == '.' && t[y][x] != '.' ) {
								swap(t[y+1][x], t[y][x]);
								aaa = true;
							}
						}
					}				
				}
			}
			
			bool flag2 = true;
			for ( int y = 0; y < h; y++ ) {
				for ( int x = 0; x < w; x++ ) {
					if ( t[y][x] != '.' ) {
						flag2 = false;
						break;
					}
				}
			}

			if ( flag2 ) {
				flag = true;
				break;
			}			
		}
	}

	if ( flag ) cout << "YES" << endl;
	else cout << "NO" << endl;
	
	return 0;
}



Measured memory_bytes: 3228
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww
{
    cww()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
    }
} star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
int main()
{
    string S, T;
    cin >> S >> T;
    int A, B;
    cin >> A >> B;
    string U;
    cin >> U;
    if (S == U)
    {
        A--;
    }
    else if (T == U)
    {
        B--;
    }
    cout << A << " " << B;
    return 0;
}

Measured memory_bytes: 256
#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++)
typedef long long ll;
const long long INF = 1LL << 60;
const int MOD = 1000000007;

ll mul( ll a, ll b ) {
	return a * b % MOD;
}

int main() {
	int n;
	cin >> n;
	vector <ll> a( n );
	rep( i, n ) cin >> a[ i ];
	ll res = 0;
	for (int bit = 0; bit < 64; bit++){
		ll cnt = 0;
		ll t = 1LL << bit;
		for (int i = 0; i < n; i++){
			if (a[ i ] & t) cnt++;
		}
		res += mul( mul( cnt, n - cnt ), t % MOD );
		res %= MOD;
	}
	cout << res << endl;
}


Measured memory_bytes: 2560
#include <bits/stdc++.h>
using namespace std;
 
#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;


signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin>>n;
    vector<int>ans(n+1,0);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= n; j+=i)
        {
            ans[j]++;
        }
    }
    int cnt=0;
    //show(ans);
    for (int i = 1; i <=n; i++)
    {
        cnt+=i*ans[i];
    }
    cout<<cnt<<endl;
    
    return 0;
}

Measured memory_bytes: 81300
#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()
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;

template <typename T>
vector<T> Dijkstra(int s, vector<vector<pair<int, T>>> & G, /*vector<int> & prev,*/ const T INF = 1e9){
    using P = pair<T, int>;
    int V = G.size();
    vector<T> dist(V, INF);
    priority_queue<P, vector<P>, greater<P>> que;
    dist[s] = 0;
    que.emplace(0, s);
    /*prev.assign(V, -1);*/
    while(!que.empty()){
        P p = que.top();
        que.pop();
        int v = p.second;
        if(dist[v] < p.first) continue;
        for(int i = 0; i < G[v].size(); i++){
            int to = G[v][i].first;
            T cost = G[v][i].second;
            if(dist[to] > dist[v] + cost){
                dist[to] = dist[v] + cost;
                /*prev[to] = v;*/
                que.emplace(dist[to], to);
            }
        }
    }
    return dist;
}

signed main(){
    int n;
    cin >> n;
    if(n == 1){
        cout << "First" << endl;
        return 0;
    }
    vector<vector<pair<int, int>>> G(n);
    int a,b;
    rep(i,n - 1){
        cin >> a >> b;
        a--;
        b--;
        G[a].emplace_back(b, 1);
        G[b].emplace_back(a, 1);
    }
    vector<int> dist = Dijkstra(0, G);
    int v;
    int mx = 0;
    rep(i,n){
        if(dist[i] > mx){
            mx = dist[i];
            v = i;
        }
    }
    vector<int> distv = Dijkstra(v, G);
    int d = 0;
    rep(i,n){
        d = max(d, distv[i]);
    }
    cout << (d % 3 == 1 ? "Second" : "First") << endl;
    return 0;
}

Measured memory_bytes: 17876
#include <stdio.h>

int main( void ) {
	int n, sum, elmNum[ 9 ] = { 5, 3, 3, 3, 3, 3, 4, 3, 4 };
	char ch[ 9 ][ 6 ] = { ".,!? ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }, key, tmp;

	scanf( "%d%*c", &n );

	while ( n-- ) {
		for ( ; scanf( "%c", &key ), key != '\n';  ) {
			if ( key == '0' )
				continue;

			for ( sum = 0; scanf( "%c", &tmp ), tmp != '0'; sum++ ) ;
			putchar( ch[ key - '1' ][ sum % elmNum[ key - '1' ] ] );
		}

		putchar( '\n' );
	}

	return 0;
}

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

int main(){
    string s;
    cin >>s;
    int count=1;
    int ans=0;
    for(int i=0;i<s.size();i++){
        for(int j=0;j<s.size();j++){
            if(s.at(i)==s.at(j))
            count++;
        }
        if(count%2!=1)
        ans++;
        count=1;
    }
    if(ans==0)
    puts("Yes");
    else
    puts("No");
}

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

using namespace std;

const int inf = 1 << 29;
char c[20][21];
int was[20][20];
int di[] = {0, 0, 1, -1};
int dj[] = {1, -1, 0, 0};

int main() {
  int n, m; cin >> n >> m;
  for(int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  int ans = 0;
  for(int i = 0; i < n; ++i) {
    for(int j = 0; j < m; ++j) {
      if(c[i][j] == '#') continue;
      memset(was, -1, sizeof was);
      was[i][j] = 0;
      queue<pair<int, int> > Q;
      Q.emplace(i, j);
      while(Q.size()) {
        auto top = Q.front();
        Q.pop();
        for(int k = 0; k < 4; ++k) {
          int ii = top.first + di[k];
          int jj = top.second + dj[k];
          if(ii < 0 || ii >= n || jj < 0 || jj >= m || c[ii][jj] == '#' || was[ii][jj] != -1) continue;
          was[ii][jj] = was[top.first][top.second] + 1;
          ans = max(ans, was[ii][jj]);
          Q.emplace(ii, jj);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
const int INF=1e9;
vector<int> dijkstra(vector<vector<ii>>&adj, int& n, int& src) {
	vector<int> dist(n, INF);
	dist[src]=0;
	priority_queue<ii> pq;
	pq.push(make_pair(0,src));
	while(!pq.empty()) {
		ll cost=-pq.top().first, cur_node=pq.top().second;
		pq.pop();
		if(dist[cur_node]<cost) continue;
		for (ll i=0; i<adj[cur_node].size(); i++) {
			ll nxt_node=adj[cur_node][i].first, nextDist=cost+adj[cur_node][i].second;
			if(dist[nxt_node]>nextDist) {
				dist[nxt_node]=nextDist;
				pq.push(make_pair(-nextDist, nxt_node));
			}
		}
	}
	return dist;
}
int main(){
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	int n, m;
	cin >> n >> m;
	int sz = n*m;
	int xi, yi, xf, yf;
	cin >> xi >> yi >> xf >> yf;
	xi--, xf--, yi--, yf--;
	vector<string> mat(n);
	for(int i=0; i<n; i++)
		cin >> mat[i];
	vector<vector<ii>> adj(sz);
	int pt1, pt2, w;

	for(int x=0; x<n; x++){
		for(int y=0; y<m; y++){
			if(mat[x][y] == '#') 
				continue;
			pt1 = m * x + y;
			for(int i=-2; i<=2; i++){
				for(int j=-2; j<=2; j++){
					int xp = x+i, yp = y+j;
					if((i == 0 && j == 0) || xp < 0 || yp < 0 || xp >= n || yp >= m || mat[xp][yp] == '#')
						continue;
					pt2 = m * xp + yp; 
					w = ((abs(i) + abs(j) == 1) ? 0 : 1);
					adj[pt1].push_back({pt2,w});
					adj[pt2].push_back({pt1,w}); 
				}
			}
		}
	}
	pt1 = m * xi + yi;
	pt2 = m * xf + yf;
	vector<int> dist = dijkstra(adj, sz, pt1);
	if(dist[pt2] == INF){
		cout << -1 << endl;
	}
	else{
		cout << dist[pt2] << endl;
	}
	return 0;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define size1 30005

int dp[3000+5][3000+5];
string s, t;

int main() {
//    int dp[100][100];
    cin >> s >> t;
    string str2;

    int m = s.size(), n=t.size();

    for(int i=0; i<=m ;i++) {
        for(int j=0; j<=n; j++) {
            if(i == 0 || j==0) {
                dp[i][j] = 0;
            }
            else if(s[i-1] == t[j-1]) {
                dp[i][j] = dp[i-1][j-1]+1;
            }
            else 
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
    }  

    int i=m, j=n;
    while(i>0 && j>0) {
        if(s[i-1] == t[j-1]) {
            str2 += s[i-1];
            i--; j--;
        }
        else if(dp[i][j-1] > dp[i-1][j])
            j--;
        else 
            i--;    
    }

    reverse(str2.begin(), str2.end());
    cout << str2<< endl;  
}

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

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

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

int N;
int A[310][310];
int d[310][310];
bool u[310][310];

int main() {
	cin >> N;

	rep(i, N) {
		rep(j, N) {
			cin >> A[i][j];
			d[i][j] = A[i][j];
		}
	}

	rep(k, N) {
		rep(i, N) {
			rep(j, N) {
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
			}
		}
	}

	rep(i, N) {
		rep(j, N) if (d[i][j] != A[i][j]) {
			puts("-1");
			return 0;
		}
	}

	rep(i, N) {
		rep(j, N) if (i != j) {
			rep(k, N) if (k != i && k != j && d[i][j] == d[i][k] + d[k][j]) {
				u[i][j] = 1;
			}
		}
	}

	ll ret = 0;

	rep(i, N) {
		rep(j, N) {
			if (!u[i][j]) {
				ret += d[i][j];
			}
		}
	}

	cout << ret / 2 << endl;

	return 0;
}

Measured memory_bytes: 1024