akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
template <typename X, typename Y> istream& operator>>(istream& is, pair<X, Y>& p) { return is >> p.first >> p.second; }
int grundy(int n, int k) {
    return n % k == 0 ? n / k : (n / k == 0 ? 0 : grundy(n - n / k - 1, k));
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    int ans = 0;
    for(int i = 0; i < n; i++) {
        int ai, k;
        cin >> ai >> k;
        ans ^= grundy(ai, k);
    }
    cout << (ans ? "Takahashi" : "Aoki") << endl;
    return 0;
}


Measured memory_bytes: 256
use std::io::*;
use std::str::FromStr;
use std::cmp::{min, max};
use std::mem::swap;
use std::collections::HashMap;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin_lock = stdin.lock();
    let s = stdin_lock
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>();
    s.parse::<T>().ok().unwrap()
}

static DX: &'static [i32] = &[0, 0, 1, -1];
static DY: &'static [i32] = &[1, -1, 0, 0];

fn main() {
    let n: usize = read();

    let mut a: Vec<u32> = (0..n).map(|_| read()).collect();

    a.sort();

    let m = a[n - 1];

    let mut l = a[0];
    let mut idx = 0;

    for i in 0..n - 1 {
        let t = min(a[i], m - a[i]);

        if l < t {
            l = t;
            idx = i;
        }
    }

    println!("{} {}", m, a[idx]);
}

fn pow_mod(mut n: u64, mut exp: u64, m: u64) -> u64 {
    let mut t = 1;

    n %= m;

    while exp > 1 {
        if exp & 1 == 1 {
            t *= n;
            t %= m;
        }

        n *= n;
        n %= m;
        exp >>= 1;
    }

    (n * t) % m
}

Measured memory_bytes: 4352
#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 a, b, m;
    cin >> a >> b >> m;
    vector<int> rei(a);
    vector<int> ren(b);
    int minA = 200000;
    int minB = 200000;
    int wa0, wa1, wa2;
    rep(i, a){
      cin >> rei.at(i);
      if(minA > rei.at(i)) minA = rei.at(i);
    }
    rep(i, b){
      cin >> ren.at(i);
      if(minB > ren.at(i)) minB = ren.at(i);
    }
    int min = minA + minB;
    rep(i, m){
      cin >> wa0;
      cin >> wa1;
      cin >> wa2;
      if(rei.at(wa0-1) + ren.at(wa1-1) - wa2 < min) min = rei.at(wa0-1) + ren.at(wa1-1) - wa2;
    }
    cout << min << endl;
}

Measured memory_bytes: 1024
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    
int long long n,k=0,sum=0;
    cin>>n;
    long long arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<n-1;i++)
    {
        if(arr[i]>=arr[i+1])
        {
            k++;
           
          sum = max(sum,k);
            
        }
        else
        {
            k=0;
        }
    }
    cout<<sum;
 
 
    
    return 0;
}



Measured memory_bytes: 1024
import java.util.ArrayList;
import java.util.Scanner;

public class Main 
{
	static class Student
	{
		ArrayList<Record> recs = new ArrayList<>();
		int id;
		
		public Student(int i)
		{
			id = i;
		}
	}
	
	static class Record
	{
		int time;
		int pc;
		int type;
		
		public Record(int t, int p, int ty)
		{
			time = t;
			pc = p;
			type = ty;
		}
	}

	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		
		while(true)
		{
			int pcs = sc.nextInt(); //1000
			int studs = sc.nextInt(); //10000
			
			if(pcs == 0 && studs == 0)return;
					
			int recs = sc.nextInt(); //1000
			
			Student[] sts = new Student[studs];
			
			for(int i = 0; i < studs; i++)
			{
				sts[i] = new Student(i);
			}
			
			
			for(int i = 0; i < recs; i++)
			{
				int time = sc.nextInt();
				int pc = sc.nextInt()-1;
				int stud = sc.nextInt()-1;
				int s = sc.nextInt(); //1 = login
				
				sts[stud].recs.add(new Record(time, pc, s));
			}
			
			int que = sc.nextInt(); //50
			
			for(int i = 0; i < que; i++)
			{
				int ts = sc.nextInt();
				int te = sc.nextInt();
				int stud = sc.nextInt()-1;
				
				Student s = sts[stud];
				
				int comps = 0;
				
				long ans = 0;
				
				int lasttime = 0;
				for(Record r : s.recs)
				{
					if(r.time >= ts)
					{
						if(comps > 0)
						{
							ans += Math.min(r.time, te) - Math.min(Math.max(lasttime, ts), te);
						}
					}
					
					if(r.type == 1)
					{ //login
						comps++;
					}
					else
					{
						comps--;
					}
					
					lasttime = r.time;
				}
				
				if(lasttime < te && comps > 0)
				{
					ans += te - lasttime;
				}
				
				System.out.println(ans);
			}
		}

	}

}



Measured memory_bytes: 67720
#include "bits/stdc++.h"
#include <set>

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x)
{
	std::cout << x << " ";
}
template <typename T>

void println(T x)
{
	std::cout << x << "\n";
}

signed main(void) {
	int x, y;
	cin >> x >> y;
	if (y - 2 * x < 0 || 4 * x - y < 0 || y % 2 != 0) {
		print("No");
	}
	else {
		print("Yes");
	}
	
}

Measured memory_bytes: 3628
# -*- coding:utf-8 -*-

def main():
    while True:
        try:
            ax,ay,bx,by,cx,cy,px,py=map(float,input().split())
            A=(bx-ax)*(py-ay)-(by-ay)*(px-ax)
            B=(cx-bx)*(py-by)-(cy-by)*(px-bx)
            C=(ax-cx)*(py-cy)-(ay-cy)*(px-cx)

            if ((A<=0)and(B<=0)and(C<=0))or((A>0)and(B>0)and(C>0)):
                print("YES")
            else:
                print("NO")

            

        except:
            break
            
    
if __name__ == '__main__':
    main()

Measured memory_bytes: 7384
# ABC 50 B - contest with drink easy
n = int(input())
T = [int(el) for el in input().split(' ')]
m = int(input())
total_T = sum(T)
d = {}
for i in range(m):
    p, x = [int(el) for el in input().split(' ')]
    print(total_T - (T[p-1] - x))



Measured memory_bytes: 2940
#include <iostream>
//#include <iomanip>
//#include <algorithm>
//#include <numeric>
//#include <queue>
//#include <stack>
//#include <math.h>
//#include <vector>
#include <string>
typedef long long LL;
using namespace std;

int main(){
    LL a;
    LL b;
    LL k;
    cin >> a >> b >> k;
    if(a>k){
        a-=k;
        k=0;
    }else{
        k-=a;
        a=0;
    }
    if(b>k){
        b-=k;
    }else{
        b=0;
    }
    cout << a << " " << b << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <cstring>
#include <climits>
using namespace std;
#define ll long long
#define FOR(i,n) for(int i=0;i<n;i++)
#define sz(x) (int)(x.size())
#define all(x) (x).begin(),(x).end()
template<typename tp> void read(tp &x) {cin >> x;}
template<typename tp, typename... Args> void read(tp& x, Args&... args) {read(x);read(args...);}
const int mxN = (int)1e5,INF = (int)1e9,MOD = (int)1e9+7;

void solve()
{
	int n,k;
	read(n,k);
	int h[n];
	FOR(i,n) read(h[i]);
	int dp[n];
	dp[0] = 0;
	for(int i=1;i<n;i++)
	{
		dp[i] = INF;
		for(int j=i-1,c=0;j>=0 && c<k;j--,c++)
			dp[i] = min(dp[i],abs(h[i]-h[j])+dp[j]);
	}
	
	cout << dp[n-1] << '\n';
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int test = 1;
	//read(test);
	while(test--)
		solve();
	return 0;
}



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

int N;
vector<int> x[29][2];
ll cnt[29];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    for(int i = 0; i < N; i++){
        int A;
        cin >> A;
        for(int j = 0; j <= 28; j++){
            int mask = (2 << j) - 1;
            x[j][(A >> j) & 1].push_back(mask & A);
        }
    }
    for(int i = 0; i <= 28; i++){
        for(int j = 0; j < 2; j++){
            sort(x[i][j].begin(), x[i][j].end());
        }
    }
    for(int i = 0; i < N; i++){
        int B;
        cin >> B;
        for(int j = 28; j >= 0; j--){
            B = B & ((2 << j) - 1);
            if((1 << j) <= B){
                int ub = x[j][1].size();                
                int lb = (3 << j) - B;
                lb = lower_bound(x[j][1].begin(), x[j][1].end(), lb) - x[j][1].begin();
                cnt[j] += ub - lb;

                ub = (2 << j) - 1 - B;
                ub = upper_bound(x[j][0].begin(), x[j][0].end(), ub) - x[j][0].begin();
                lb = 0;
                cnt[j] += ub - lb;
            }else{
                int ub = (2 << j) - 1 - B;
                ub = upper_bound(x[j][1].begin(), x[j][1].end(), ub) - x[j][1].begin();
                int lb = 0;
                cnt[j] += ub - lb;

                ub = x[j][0].size();
                lb = (1 << j) - B;
                lb = lower_bound(x[j][0].begin(), x[j][0].end(), lb) - x[j][0].begin();
                cnt[j] += ub - lb;
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < 29; i++){
        if(cnt[i] & 1ll) ans |= 1 << i;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 29224
#include<iostream>
#include <string>
#include <cstdlib> //絶対値利用のため
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
#define fr(i,n) for(int i=0;i<n;i++)
#define ifr(i,n) for(int i=n-1;i>=0;i--)

//const int INF = INT_MAX / 2;

void trace(int a[], int n){
	for (int i = 0; i < n; i++) {
		if (i > 0) cout << " ";
		cout << a[i];
	}
	cout << endl;
}

void bubleSort(int a[], int n) {
	int counter = 0;
	for (int i = 1; i < n; i++) {
		for (int j = n - 1; j >= i; j--) {
			if (a[j] < a[j - 1]) {
				int t = a[j];
				a[j] = a[j - 1];
				a[j - 1] = t;
				counter++;
			}
		}
	}
	trace(a, n);
	cout << counter << endl;
}

int selectionSort(int a[], int n) {
	int sw = 0;
	for (int i = 0; i < n-1; i++) {
		int minj = i;
		for (int j = i + 1; j < n; j++) {
			if (a[minj] > a[j]) {
				minj = j;
			}
		}
		swap(a[minj], a[i]);
		if(minj != i) sw++;
	}
	return sw;
}


int main() {
	int n, a[100], sw;
	cin >> n;
	for (int i = 0; i < n; i++) cin >> a[i];
	sw = selectionSort(a, n);
	trace(a, n);
	cout << sw << endl;
	return 0;
}


Measured memory_bytes: 3100
#include <algorithm>
#include <iostream> //入出力
#include <stdio.h>
#include <map>
#include <set>
#include <string>
#include <math.h> //算術演算子
#include <vector>
#include <cstdlib>
#include <queue>
#include <stack>
#include <list>
#include <iomanip> //小数点以下を表示させる(setprecision())
//#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //64bit型

#define debug(x) cerr << x << " " \
                      << "(L:" << __LINE__ << ")" << '\n'
#define PRINT(V)                                   \
        {                                          \
                for (int i = 0; i < V.size(); i++) \
                {                                  \
                        cout << V[i] << " ";       \
                }                                  \
                cout << endl;                      \
        }

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

int main()
{
        //input->compute->output//
        int N, M, X;
        cin >> N >> M >> X;
        vector<int> vec(M);
        int r = 0, l = 0;
        for (int i = 0; i < M; i++)
        {
                cin >> vec[i];
                if (vec[i] > X)
                {
                        r++;
                }
                else
                {
                        l++;
                }
        }

        cout << min(l, r) << endl;

        return 0;
}


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

using namespace std;
typedef vector<int> vi;
int M, N;
int character[150][15];
map<vi, int> mp;

int dfs(vi S) {
	if (S.size() <= 1) return 0;
	int& ret = mp[S];
	if (ret > 0) return ret;
	ret = M;
	for (int i = 0; i < M; i++) {
		vi left, right;
		for (int v : S) {
			if (character[v][i]) left.push_back(v);
			else right.push_back(v);
		}
		if (left.empty() || right.empty()) continue;
		ret = min(ret, 1+max(dfs(left), dfs(right)));
	}
	return ret;
}

int main() {
	while (cin >> M >> N) {
		if (M == 0 && N == 0) break;
		for (int i = 0; i < N; i++) {
			string s;
			cin >> s;
			for (int j = 0; j < M; j++) {
				character[i][j] = s[j] - '0';
			}
		}
		mp.clear();
		vector<int> S(N);
		for (int i = 0; i < N; i++)
			S[i] = i;
		cout << dfs(S) << endl;
	}
}

Measured memory_bytes: 5296
#include <iostream>

using namespace std;

int main ()
{
  int N, sum, tmp;
  sum = 0;

  cin >> N;
  tmp = N;

  while ( tmp > 0 ) {
    sum += tmp%10;
    tmp /= 10;
  }

  if ( N%sum  ) cout << "No\n";
  else cout << "Yes\n";
}


Measured memory_bytes: 888
from collections import defaultdict
import sys
sys.setrecursionlimit(10**6)

def scc(N, g, rg):
    V = list(range(N))


    def dfs(u, done, o):
        done.add(u)
        for v in g[u]:
            if v in done:
                continue
            done.add(v)
            dfs(v, done, o)
        o.append(u)

    def dfs_rev(u, done, c):
        done.add(u)
        c.append(u)
        for v in rg[u]:
            if v in done:
                continue
            dfs_rev(v, done, c)

    done = set()
    o = []
    for v in V:
        if v in done:
            continue
        dfs(v, done, o)


    done = set()
    ans = []
    while o:
        u = o.pop()
        if u in done:
            continue
        vv = []
        dfs_rev(u, done, vv)
        ans.append(vv)


    return ans


def atcoder_practice2_g():
    N, M = map(int, input().split())

    g = defaultdict(list)
    rg = defaultdict(list)
    for _ in range(M):
        a, b = map(int, input().split())
        g[a].append(b)
        rg[b].append(a)
    vv = scc(N, g, rg)
    print(len(vv))
    for r in vv:
        print(len(r), *r)


def test():
    g = {
        1: [2],
        5: [2],
        2: [4, 3],
        3: [4, 7, 6],
        4: [5, 8],
        6: [9],
        7: [8],
        8: [9],
        9: [7, 10],
        10: []
    }

    vv = scc(g)
    for r in vv:
        print(r)


if __name__ == "__main__":
    # test()
    atcoder_practice2_g()


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

int main(){
    int t[4],n[4];
    cin >>t[0]>>n[0]>>t[1]>>n[1]>>t[2]>>n[2]>>t[3]>>n[3];
    int s,a,b,c;
    if(t[0]==1){
        s=n[0]*6000;
    }else if(t[0]==2){
        s=n[0]*4000;
    }else if(t[0]==3){
        s=n[0]*3000;
    }else{
        s=n[0]*2000;
    }
    if(t[1]==1){
        a=n[1]*6000;
    }else if(t[1]==2){
        a=n[1]*4000;
    }else if(t[1]==3){
        a=n[1]*3000;
    }else{
        a=n[1]*2000;
    }   
    if(t[2]==1){
        c=n[2]*6000;
    }else if(t[2]==2){
        b=n[2]*4000;
    }else if(t[2]==3){
        b=n[2]*3000;
    }else{
        b=n[2]*2000;
    }   
    if(t[3]==1){
        c=n[3]*6000;
    }else if(t[3]==2){
        c=n[3]*4000;
    }else if(t[3]==3){
        c=n[3]*3000;
    }else{
        c=n[3]*2000;
    }   
    cout <<s<<endl<<a<<endl<<b<<endl<<c<<endl;

return 0;
}

Measured memory_bytes: 3076
from collections import Counter

N = int(input())

A = list(map(int, input().split()))
c = Counter(A)
a = list(set(A))


if len(a) >= 4:
    ans = False

elif len(a) == 3:
    a, b, d = a
    if c[a] == c[b] and c[b] == c[d] and a^b == d and d^b == a and a^d == b:
        ans = True
    else:
        ans = False

elif len(a) == 2:
    a, b = sorted(a)
    if a == 0 and c[a] == N/3 and c[b] == (N/3)*2:
        ans = True
    else:
        ans = False

elif len(a) == 1:
    if sum(a) == 0:
        ans = True
    else:
        ans = False

print("Yes" if ans else "No")


Measured memory_bytes: 20824
#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define pr(...) cerr<< GET_MACRO(__VA_ARGS__,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__) <<endl
#define pr1(a) (#a)<<"="<<(a)<<" "
#define pr2(a,b) pr1(a)<<pr1(b)
#define pr3(a,b,c) pr1(a)<<pr2(b,c)
#define pr4(a,b,c,d) pr1(a)<<pr3(b,c,d)
#define pr5(a,b,c,d,e) pr1(a)<<pr4(b,c,d,e)
#define pr6(a,b,c,d,e,f) pr1(a)<<pr5(b,c,d,e,f)
#define pr7(a,b,c,d,e,f,g) pr1(a)<<pr6(b,c,d,e,f,g)
#define pr8(a,b,c,d,e,f,g,h) pr1(a)<<pr7(b,c,d,e,f,g,h)
using namespace std;
using Int = long long;
using ll = long long;
using Double = long double;
const Int INF = 1LL<<55;
const Int mod = (1e9)+7;
using P = pair<Int,Int>;
using T = tuple<Int,Int,Int>;
const Double EPS = 1e-8;
const Double PI = 6.0 * asin((Double)0.5);
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
ostream& operator<<(ostream& o,P p){return o<<"("<<p.first<<","<<p.second<<")";}
ostream& operator<<(ostream& o,T t){return o<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")";}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
ostream& operator<<(ostream& o,vector<auto> &a){Int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;}
istream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}
void prArr(auto a,string s=" "){Int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}


signed main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  map<string,int> M;
  
  int q;
  cin>>q;

  while(q--){
    int cmd;
    cin>>cmd;
    if(cmd == 0){
      string key;
      int x;
      cin>>key>>x;
      M[key] = x;
    }
    
    if(cmd == 1){
      string key;
      cin>>key;
      assert(M.count(key));
      cout<<M[key]<<endl;
    }
    

  }
  
  return 0;
}



Measured memory_bytes: 25764
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

satisfaction = 0

for i in range(N):
  satisfaction += B[A[i]-1]
  if i != 0:
    if (A[i-1] + 1) == A[i]:
      satisfaction += C[A[i-1]-1]

print(satisfaction)
  

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

int main()
{
	int n, m; cin >> n >> m;
	vector<vector<int>> g(n+1);
	for (int i = 0; i < m; ++i) {
		int u, v; cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	queue<int> q; q.push(1);
	vector<int> dist(n+1, INT_MAX); dist[1] = 0; dist[0] = -1;
	vector<int> ans(n+1);
	while (!q.empty()) {
		auto u = q.front(); q.pop();
		for (auto v : g[u]) {
			if (dist[v] < INT_MAX) continue;
			dist[v] = 1 + dist[u];
			ans[v] = u;
			q.push(v);
		}
	}
	if (*max_element(begin(dist), end(dist)) == INT_MAX) cout << "No\n";
	else {
		cout << "Yes\n";
		for (int i = 2; i <= n; ++i)
			cout << ans[i] << ' ';
	}
}

Measured memory_bytes: 10636
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<math.h>
#include<vector>
#include<iomanip>
using namespace std;

long long int gcd(long long int x, long long int y)
{
	if (y == 0) { return x; }
	return gcd(y, x % y);
}

int main(void)
{
	int a, b, h;
	cin >> a >> b >> h;
	cout << (a + b) * h / 2 << endl;
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define yesno(flg) if(flg){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define MAX_N 2002
using namespace std;

typedef long long ll;
typedef pair<int,int> P1;
typedef pair<P1,P1> P2;
const ll INF=100000000000000001;
int dp1[MAX_N][MAX_N]={};
int dp2[MAX_N][MAX_N]={};
bool d[MAX_N][MAX_N]={};
struct edge{int to,cost;};
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
queue<P1> que;
vector<string> v;

int a[100002]={};

int main() {
	int n;
	cin>>n;
	rep(i,n)cin>>a[i+1];
	int t=0;
	rep(i,n){
		if(a[a[i+1]]==i+1)t++;
	}
	t/=2;
	cout<<t<<endl;

	return 0;
}


Measured memory_bytes: 640
#include <iostream>

using namespace std;

int main()
{
	int n;
	int x = 0;
	int xmax = 0;
	string str;
	cin >> n >> str;
	for (int i = 0; i < n; i++) {
		if (str[i] == 'I') x++;
		if (str[i] == 'D') x--;
		if (xmax < x) xmax = x;
	}
	cout << xmax << endl;
}


Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int i,n,w,h,m,h2,m2,s[200],cnt=0;
	int h3,m3;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d %d",&h,&m);
		s[i]=(h*60)+m;
	}
	scanf("%d",&w);
	for(i=n;i<n+w;i++){
		scanf("%d %d",&h2,&m2);
		s[i]=(h2*60)+m2;
	}
	sort(s,s+(n+w));
	for(i=0;i<n+w;i++){
		if(i>0){
			printf(" ");
		}
		h3=s[i]/60;
		m3=s[i]%60;
		if(m3/10==0){
			printf("%d:0%d",h3,m3);
		}
		else{
			printf("%d:%d",h3,m3);
		}
		if(s[i]==s[i+1]){
			i++;
		}
	}
	printf("\n");
	return 0;
}


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

int main() {
  int a,b,c;
  cin >> a >> b >> c ;
  
  if ( a + b + c == 17 ){
    
    cout << "YES" << endl;
    
  }
  
  else {
    
    cout << "NO" << endl;
    
  }
  
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cctype>
#include<map>
#include<cstring>
#include<bitset>
#include<cassert>

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep2(i,a,b) for (int (i)=a;(i)<(b);(i)++)
#define all(x) (x).begin(),(x).end()

using namespace std;
using ll = long long int;

const int inf = 1001001000;
const long long int Inf = 1001001001001001000;



void print(vector<vector<int>> a){
	for (int i = 0; i < a.size(); i++)
	{
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<vector<long long int>> a){
	for (int i=0;i<a.size();i++){
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(vector<long long int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(set<int> a){
	for (auto x:a)
		cout << x << " ";
	cout << endl;
}

int main()
{
	string s;
	cin >> s;
	int n = s.size();
	int p = count(all(s),'p');
	cout << n / 2 - p << endl;

	return 0;
}

Measured memory_bytes: 3716
//Use valgrind once if the submission doesn't go through. It can detect uninitialized variables and see where they could be an issue.
#include <bits/stdc++.h>
using namespace std;

int main(){

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

	int a,b,c,k;
	cin>>a>>b>>c>>k;
	while(b<=a){
		k--;
		b<<=1;
	}
	while(c<=b){
		k--;
		c<<=1;
	}
	if(k<0){
		cout<<"No\n";
	}
	else{
		cout<<"Yes\n";
	}
}


Measured memory_bytes: 3652
//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
int A[1001],B[1001];
int N,M;
int D;

int main() {
	cin>>N>>M;
	for(int i=0;i<M;i++){
		cin>>A[i]>>B[i];
	}

	int T=0;
	int S[1001]={};
	for(int i=0;i<M;i++){
		if(A[i]>=N){
			S[i]=0;
		}
		if(A[i]<N){
			S[i]=N-A[i];
			T+=S[i];
		}
	}

	int R=0;
	for (int i=0;i<M;i++){
		if (R<S[i])R=S[i];
	}
	cout<<T-R<<endl;
	return 0;
}

Measured memory_bytes: 3060
#include <stdio.h>
int main(void) {
  while(1) {
    int m, f, r, score;
    scanf("%d %d %d\n", &m, &f, &r);
    if ((m == -1) && (f == -1) && (r == -1))
      break;
    if ((m == -1) || (f == -1)) printf("F\n");
    else {
      score = m + f;
      if (score >= 80)       printf("A\n");
      else if (score >= 65)       printf("B\n");
      else if (score >= 50)       printf("C\n");
      else if (score >= 30) {
        if (r >= 50)              printf("C\n");
        else                      printf("D\n");
      }
      else                        printf("F\n");
    }
  }
  return 0;
}

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

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;

template <typename T> T id(T b) {return b;};
template <class It> bool all(It f,It l){return std::all_of(f,l,id<bool>);}
template <class It> bool any(It f,It l){return std::any_of(f,l,id<bool>);}
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }

int d(int x) {
  int c = 0;
  while (x) {
    c++;
    x /= 10;
  }
  return c;
}

int n;

int main()
{
  cin >> n;
  int res = 0;
  forn1(i,n) if (d(i)%2) res++;
  cout << res << endl;
  return 0;
}


Measured memory_bytes: 256
from collections import defaultdict
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    atog={}
    aa=[[1,3,5,7,8,10,12],[4,6,9,11],[2]]
    for g,ga in enumerate(aa):
        for a in ga:
            atog[a]=g
    x,y=MI()
    if atog[x]==atog[y]:print("Yes")
    else:print("No")

main()

Measured memory_bytes: 3316
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 1e18;
const double INFD = 1e30;
const double EPS = 1e-9;
const double PI = acos(-1);
const int MOD = 998244353;
template <typename T>
inline T read() {
    T X = 0, w = 0;
    char ch = 0;
    while (!isdigit(ch)) {
        w |= ch == '-';
        ch = getchar();
    }
    while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
const int MAXN = 500005;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
char str[MAXN];
int pref[MAXN];
int tens[MAXN];
void extgcd(ll a, ll b, ll& d, ll& x, ll& y) {
    if (!b) {
        d = a;
        x = 1;
        y = 0;
    } else {
        extgcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}

inline ll getInv(ll x, ll mod) {
    ll g, a, b;
    extgcd(x, mod, g, a, b);
    return (a % mod + mod) % mod;
}
int main() {
#ifdef LOCALLL
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif
    scanf("%s", str + 1);
    n = strlen(str + 1);
    map<int, int> moddd;
    moddd[0] = 1;
    ll ans = 0;
    tens[0] = 1;
    for (int i = 1; i <= n; i++) tens[i] = tens[i - 1] * 10 % 2019;
    for (int i = 1; i <= n; i++) {
        pref[i] = (pref[i - 1] * 10 + str[i] - '0') % 2019;
    }
    for (int i = 1; i <= n; i++)
        pref[i] = pref[i] * getInv(tens[i], 2019) % 2019;
    for (int i = 1; i <= n; i++) {
        ans += moddd[pref[i]];
        moddd[pref[i]]++;
    }
    printf("%lld", ans);
    return 0;
}

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

using namespace std;

int main(){
	int A, B, C, X, ans = 0; cin >> A >> B >> C >> X;
	for(int i = 0; i <= A; ++i){
		for(int j = 0; j <= B; ++j){
			for(int k = 0; k <= C; ++k){
				if(500*i + 100*j + 50*k == X) ans++;
			}
		}
	}
	cout << ans << endl;
}

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

using namespace std;

using i64 = int64_t;

const i64 MOD = 1e9+7;

const i64 INF = 1e18+7;


signed main(){
    int n, m;
    cin >> n >> m;
    vector<int> a(n, 0);
    for(int i = 0; i < m; ++i){
        int x, y;
        cin >> x >> y;
        ++a[--x], ++a[--y];
    }
    for(auto& x : a)
        cout << x << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
	string n;
	int i,j=0;
	cin>>n;
	for(i=0;i<n.size();i++)
	{
		if(n[i]!=n[i+1])
		{
			j++;
		}
	}
	cout<<j-1<<endl;
	return 0;
}

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

namespace ABC126_1
{
    class D
    {

        static List<long>[] Nodes;
        static long[] Res;
        //static long[,] Dist;
        static List<long>[] Dist;
        static void Main(string[] args)
        {

            //StreamReader r = new StreamReader(@"./testcase");

            var n = long.Parse(Console.ReadLine());
            //var n = long.Parse(r.ReadLine());
            Nodes = new List<long>[n].Select(x => x = new List<long>()).ToArray();
            Dist = new List<long>[n].Select(x => x = new List<long>()).ToArray();
            Res = new long[n];

            for (int i = 0; i < n - 1; i++)
            {
                var a = Console.ReadLine().Split().Select(long.Parse).ToArray();
                //var a = r.ReadLine().Split().Select(long.Parse).ToArray();
                // ノード間の接続情報
                Nodes[a[0] - 1].Add(a[1]);
                Nodes[a[1] - 1].Add(a[0]);
                // 2点間の距離
                Dist[a[0] - 1].Add(a[2] % 2);
                Dist[a[1] - 1].Add(a[2] % 2);
            }

            /*
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write(string.Join(",", Dist[i, j]));
                }
                Console.WriteLine();
            }
            */

            dfs(0, 1, 0);
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine(Res[i]);
            }
        }


        static void dfs(long preNode, long nowNode, long distSum)
        {
            if (distSum % 2 == 0) Res[nowNode - 1] = 0;
            if (distSum % 2 == 1) Res[nowNode - 1] = 1;

            for (int i = 0; i < Nodes[nowNode - 1].Count; i++)
            {
                var nextNode = Nodes[nowNode - 1][i];
                if (preNode != nextNode) dfs(nowNode, nextNode, distSum + Dist[nowNode - 1][i]);
            }

            /*
            foreach (var nextNode in Nodes[nowNode - 1])
            {
                // 前のノードの接続情報は無視する
                if (preNode != nextNode) dfs(nowNode, nextNode, distSum + Dist[nowNode - 1, nextNode - 1]);
            }
            */
        }
    }
}


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

#define ll long long
#define ull unsigned long long
#define dbg(args...)    do { cerr << #args << ' ' ; print(args); } while(0); cerr << endl;
#define Max 100005
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define PI acos(-1)
#define all(a)           a.begin(),a.end()
long long limit = 4294967296;
int MOD = 1000000007;
template <typename T>
void print(const T& x)
{
    cerr<<x<<' ';
}

template <typename T1, typename... T2>
void print(const T1& first, const T2&... anst)
{
    print(first);
    print(anst ...) ;
}

/*direction array*/

/***4 moves***/
/*
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
*/
/***8 moves***/
/*
int dx[] = { 0,  0, -1, -1, -1, +1, +1, +1 } ;
int dy[] = {-1, +1, -1,  0, +1, -1,  0, +1 } ;
*/

/*direction array ends*/

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

ll lcm(ll a, ll b)
{
    return (a * (b / gcd(a, b)));
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    fastIO;

    ll a, b, c, d;
    cin>>a>>b>>c>>d;

    vector<ll> v;

    v.push_back(a * c);
    v.push_back(a * d);
    v.push_back(b * c);
    v.push_back(b * d);

    sort(all(v));
    cout<<v[3]<<endl;
}


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

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

int main()
{
  int h, w;
  cin >> h >> w;
  char grid[h][w];
  rep(i, h) rep(j, w) cin >> grid[i][j];
  rep(i, h)
  {
    rep(j, w)
    {
      if(grid[i][j] == '#') continue;
      char c = '0';
      rep(k, 8)
      {
        int ny = i + dy[k];
        int nx = j + dx[k];
        if(ny >= 0 && ny < h && nx >= 0 && nx < w && grid[ny][nx] == '#')
        {
          c++;
        }
      }
      grid[i][j] = c;
    }
  }
  
  rep(i, h)
  {
    rep(j, w)
    {
      cout << grid[i][j];
    }
    cout << "\n";
  }
  
  return 0;
}

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

int main(){
	string n;
	cin>>n;
	string answer="";
	int len=n.length();
	for(int i=0;i<len;i++){
		if(n.substr(i, 1)=="1"){
			answer += "9";
		}	else if(n.substr(i, 1)=="9"){
			answer += "1";
		}	else{
			answer += n[i];
		}
	}
	cout<<answer;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int A[500], sum = 0;
    int N;
    cin >> N;
    for(int i = 0; i < N; i++){
        cin >> A[i];
        sum += A[i];
    }
    sort(A, A+N);
    if(sum % 10 != 0) {
        cout << sum << "\n";
        return 0;
    }
    else{
        for(int i = 0; i < N; i++){
            if((sum - A[i]) % 10 != 0){
                cout << sum - A[i] << "\n";
                return 0;
            }

        }
    }
    cout << 0 << "\n";
    return 0;
}


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

int main() {
  int H, W;
  cin >> H >> W;
  
  vector<string> board(50);
  
  for(int i = 0; i < H; i++){
    cin >> board.at(i);
  }
  
  vector<int> dx = {1, 1, 0, -1, -1, -1, 0, 1};
  vector<int> dy = {0, 1, 1, 1, 0, -1, -1, -1};
  
  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      if(board.at(i).at(j) == '#') continue;
      
      int num = 0;
      for(int d = 0; d < 8; d++){
        int ni = i + dx.at(d);
        int nj = j + dy.at(d);
        
        if(ni < 0 || H <= ni) continue;
        if(nj < 0 || W <= nj) continue;
        if(board.at(ni).at(nj) == '#') num++;
      }
      
      board.at(i).at(j) = char(num + '0');
    }
  }
  for(int i = 0; i < H; i++) cout << board.at(i) << endl;
}

Measured memory_bytes: 256
--https://www.lua.org/pil/11.4.html
List = {}
function List.new ()
  return {first = 0, last = -1}
end

function List.pushleft (list, value)
  local first = list.first - 1
  list.first = first
  list[first] = value
end

function List.pushright (list, value)
  local last = list.last + 1
  list.last = last
  list[last] = value
end

function List.popleft (list)
  local first = list.first
  if first > list.last then return false end
  local value = list[first]
  list[first] = nil
  list.first = first + 1
  return value
end

function List.popright (list)
  local last = list.last
  if list.first > last then return false end
  local value = list[last]
  list[last] = nil
  list.last = last - 1
  return value
end

----------

local n,q=io.read("n","n")
local edge={}
local counter={}
for i=1,n do
    counter[i]=0
    edge[i]={}
end
for i=1,n-1 do
    local a,b=io.read("n","n")
    edge[a][b]=false
    edge[b][a]=false
end
for i=1,q do
    local p,x=io.read("n","n")
    counter[p]=counter[p]+x
end

local que=List.new()
List.pushright(que,1)

while true do
    local a=List.popleft(que)
    if a then
        for b,_ in pairs(edge[a]) do
            if not edge[a][b] and not edge[b][a] then
                counter[b]=counter[b]+counter[a]
                edge[a][b]=true
                edge[b][a]=true
                List.pushright(que,b)
            end
        end
    else
        break
    end
end

print(table.concat(counter," "))

Measured memory_bytes: 54500
import sys
N = int(input())

A = list(map(int, input().split()))
result = A[0]


if 0 in A:
  print(0)
  sys.exit()

for i in range(1,N):
  result = result * A[i]
  #print(result)

  if result >= 10**18+1:
    print(-1)
    sys.exit()

print(result)

Measured memory_bytes: 84720
function Main(arg) {
 
    var N = arg.split("\n")[0]-0;
    var d = arg.split("\n")[1].split(" ").map(v => v-0);
 
    var ans = 0;

    for (var i = 0; i < N - 1; i++){
        for (var j = i+1; j < N; j++){
            ans += d[i] * d[j];
        }
    }

    console.log(ans)

    return;
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));

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

int main() {
  int n;
  cin >> n;
  map<string, int> mp;
  for (int i=0; i<n; i++){
    string s;
    cin >> s;
    sort(s.begin(), s.end());
    mp[s]++;
  }
  
  long long ans = 0;
  
  for (auto& p : mp ){
    int s = p.second;
    ans += (long long)s*(s-1)/2;
  }
  
  cout << ans << endl;
  
  
}


Measured memory_bytes: 11136
n, m, x = [int(elem) for elem in input().split()]

cs = []
a_lsts = []
for i in range(n):
    elems = [int(elem) for elem in input().split()]
    c, a_lst = elems[0], elems[1:]
    cs.append(c)
    a_lsts.append(a_lst)


min_ = -1
for i in range(2 ** n):
    s = '0' * n + bin(i)[2:]
    s = s[-n:]
    res = 0
    costs = [0] * m
    for j in range(n):
        if s[j] == '0':
            pass
        else:
            res += cs[j]
            costs = [costs[k] + a_lsts[j][k] for k in range(m)]
    if all(elem >= x for elem in costs):
        if min_ == -1:
            min_ = res
        elif min_ > res:
            min_ = res
print(min_)


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

int a[300001];
bool solve1(int n){
	int p = 0;
	for(int i = 0;i < n;i++){
		if(i > p)return 0;
		p = max(p, i+(a[i]/10));
	}
	return 1;
}

bool solve2(int n){
	int p = n-1;
	for(int i = n-1;i >= 0;i--){
		if(i < p)return 0;
		p = min(p, i-(a[i]/10));
	}
	return 1;
}

int main(){
	int n;
	scanf("%d", &n);
	for(int i = 0;i < n;i++)scanf("%d", &a[i]);
	bool x = solve1(n);
	bool y = solve2(n);
	if(x && y)printf("yes\n");
	else printf("no\n");
	return 0;
}


Measured memory_bytes: 4300
#include <iostream>
#include <string>
#define foor for(int i=0;i<n;i++)
#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl;
#define ic int a;cin>>a;
#define ic2 ic int b;cin>>b;
#define ic3 ic2 int c;cin>>c;
int s = 0;
using namespace std;
int main() {
	string a, b; cin >> a >> b; cout <<  b << a;
}

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

const int mod = 1000000007;

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

    ll ans;
    ll cnt = 0;
    for (ll i = 0; i < s.size(); i++) {
        if (i % 2 == 0 && s[i] != '0') cnt++;
        if (i % 2 != 0 && s[i] != '1') cnt++;
    }
    ll size = s.size();
    ans = min(cnt, size-cnt);
    cout << ans << endl;
}


Measured memory_bytes: 512
#include<iostream>
#include <bits/stdc++.h>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int main() {
	
    int n ;
    cin >> n ;
    int x[n],y[n] ;
    for(int i=0;i<n;i++) cin >> x[i] ;

    for(int i=0; i<n; i++) y[i]=x[i];
	sort(y, y+n);
	for(int i=0; i<n; i++){
		if(x[i]<=y[(n/2)-1]) cout<<y[n/2]<<endl;
		else cout<<y[(n/2)-1]<<endl;
    }
	return 0;
}



Measured memory_bytes: 3712
#include <bits/stdc++.h>
#define fail {puts("-1");return 0;}
typedef unsigned ui;
constexpr ui N(1e5+1),M(N<<1);
ui head[N],e_cnt,to[M],nxt[M];
void addedge(ui u,ui v)
{
	to[++e_cnt]=v;
	nxt[e_cnt]=head[u];
	head[u]=e_cnt;
	return;
}
bool odd;
ui S,T;
int sz[N],k[N];
void DFS1(ui u,ui fa)
{
	for(ui e(head[u]),v(to[e]);e;v=to[e=nxt[e]])
		if(v^fa)
		{
			if(sz[v])
			{
				if(sz[u]==sz[v])
					odd=true;
				S=u,T=v;
			}
			else
			{
				sz[v]=-sz[u];
				DFS1(v,u);
			}
		}
	return;
}
void DFS2(ui u,ui fa)
{
	for(ui e(head[u]),v(to[e]);e;v=to[e=nxt[e]])
		if(v!=fa&&(u!=S||v!=T)&&(u!=T||v!=S))
		{
			DFS2(v,u);
			sz[u]+=sz[v];
			k[u]+=k[v];
		}
	return;
}
signed main(void)
{
	ui n,m;
	scanf("%u%u",&n,&m);
	for(ui i(0),u,v;i<m;++i)
	{
		scanf("%u%u",&u,&v);
		addedge(u,v);
		addedge(v,u);
	}
	sz[1]=1,DFS1(1,0);
	int sum(std::accumulate(sz+1,sz+n+1,0));
	ui ans(0);
	if(m<n&&sum) fail
	if(m==n)
	{
		if(odd)
		{
			if(sum&1) fail
			ans+=abs(sum/2);
			sz[S]-=sum/2,sz[T]-=sum/2;
		}
		else
		{
			if(sum) fail
			k[S]=1,k[T]=-1;
		}
	}
	DFS2(1,0);
	static int st[N];
	ui top(0);
	for(ui i(1);i<=n;++i)
		if(k[i])
			st[++top]=k[i]*sz[i];
		else
			ans+=abs(sz[i]);
	st[++top]=0,std::sort(st+1,st+top+1);
	int mid(st[(top+1)>>1]);
	for(ui i(1);i<=top;++i)
		ans+=abs(st[i]-mid);
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 8064
#include <iostream>

using namespace std;
typedef long long LL;
const int MAXN = 2e5 + 10;
LL du[MAXN], sum[MAXN];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    // scanf("%d", &n);
    cin >> n;
    LL jian = 0;
    for (int i = 1; i < n; i++)
    {
        int u, v;
        // scanf("%d %d", &u, &v);
        cin >> u >> v;
        if (u > v)
            swap(u, v);
        du[u] += (n - v + 1);
    }
    for (int i = 1; i <= n; i++)
        jian += du[i];
    LL allans = 0;
    for (int i = 1; i <= n; i++)
    {
        allans += (1ll * (n - i + 1) * (n - i + 1 + 1)) / 2;
        allans -= jian;
        jian -= du[i];
    }
    // printf("%lld\n", allans);
    cout << allans << '\n';
    // cin >> n;
    
    
}

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

#define FOR(i,n,j) for(int i=(j);i<(n);++i)
#define ssort(n) sort((n).begin(),(n).end())
#define rsort(n) sort((n).begin(),(n).end(),greater<int>())
using ll=long long;
using ld=long double;
typedef pair<double,int> P;

#define repl(i,n) for(ll i=0;i<(n);++i)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define INF 11711711

int main()
{
    char a,b;
    cin >> a >> b;
    if(a<=b){
        rep(i,b-'0'){
            cout << a;
        }
        cout << endl;
    }else{
        rep(i,a-'0'){
            cout << b;
        }
        cout << endl;
    }
}

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

using namespace std;

int main(){
    int N,l,r,s;
    s=0;
    scanf("%d",&N);
    for (int i=0;i<N;i++){
        scanf("%d %d",&l,&r);
        s +=  r-l+1;
    }
    printf("%d\n",s);
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
long solve(vector<vector<long>>& dp,vector<vector<int>>& arr,int n,int k){
    if(n==0){
        return 1;
    }
    if(dp[n][k]!=-1){
        return dp[n][k];
    }
    dp[n][k] = 0;
    for(int i=0;i<arr.size();i++){
        if(arr[n-1][i]==1&&(k&(1<<i))==0){
            dp[n][k] = (dp[n][k]%mod + solve(dp,arr,n-1,(k|(1<<i)))%mod)%mod;
        }
    }
    return dp[n][k];
}
int main() {
	int n;
	cin>>n;
	vector<vector<int>> arr(n,vector<int>(n));
	for(int i=0;i<n;i++){
	    for(int j=0;j<n;j++){
	        cin>>arr[i][j];
	    }
	}
	vector<vector<long>> dp(n+1,vector<long>(4194304,-1));
	cout<<solve(dp,arr,n,0);
	return 0;
}


Measured memory_bytes: 754048
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define per(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
#define INF 2e9
typedef long long ll;
typedef pair<int, int> P;

int main() {
    int n, m; cin >> n >> m;
    vector<bool> broken(n, 0);
    rep(i, m) {
        int a;
        cin >> a;
        broken[a] = 1;
    }
    int mod = 1e9+7;
    vector<int> dp(n+1);
    dp[0] = 1;
    if (broken[1]) dp[1] = 0;
    else dp[1] = 1;
    for (int i=0; i<n-1; i++) {
        if (broken[i+2]) {
            dp[i+2] = 0;
            continue;
        }
        dp[i+2] = dp[i] + dp[i+1];
        dp[i+2] %= mod;
    }

    cout << dp[n] << endl;

}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef double D;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
 
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d %d",&x,&y)
#define rl(x) scanf("%lld",&x)
#define rl2(x,y) scanf("%lld %lld",&x,&y)
#define wd(x) printf("%d" ,x)
#define wd2(x,y) printf("%d %d",x,y)
#define wl(x) printf("%lld",x)
#define wl2(x,y) printf("%lld %lld",x,y)
#define PC(x) putchar(x)
#define GC() getchar()
#define NL printf("\n")
#define SP printf(" ")
 
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a,value) memset(a, value, sizeof(a))

#define F first
#define S second
#define MP make_pair
#define PB push_back

#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)
#define fr(i,s,e) for(int i=s;i<e;i++)
#define frl(i,s,e) for(ll i=s;i<e;i++)
#define frr(i,s,e) for(int i=s-1;i>=e;i--)
#define frv(i,a) for(int i = 0;i<(int)a.size();i++)
#define frvr(i,a) for(int i = a.size()-1;i>=0;i--)
#define tr(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end();i++)

#define mod 1000000007
#define maxn 100009
#define maxr 100009

void solve()
{
	string s,t;
	cin>>s>>t;
	ll ans=0,ns,nt,x=0;
	ns=SZ(s);
	nt=SZ(t);
	map<char,VL> mp;
	fr(i,0,ns)
	{
		mp[s[i]].PB(i);
	}
	fr(i,0,nt)
	{
		if(mp.count(t[i]))
		{
			//VL tmp=mp[t[i]];
			VL::iterator lower;
			//ll x=ans%ns;
			// if(x==0)
			// {
			// 	if(s[ns-1]==t[i]&&ans!=0)
			// 		x=ns;
			// }
			// if(x==0&&ans!=0)
			// 	x=ns;
			lower=lower_bound(ALL(mp[t[i]]),x);
			ll pos=lower-mp[t[i]].begin();
			//cout<<x<<" : "<<tmp[pos]<<endl;
			if(pos==SZ(mp[t[i]])||(mp[t[i]][pos]+1<x))
			{
				ans+=(ns-x)+mp[t[i]][0]+1;
				x=mp[t[i]][0]+1;
			}
			else
			{
				ans+=(mp[t[i]][pos]-x+1);
				x=mp[t[i]][pos]+1;
			}
			if(x>=ns)x-=ns;
		}
		else
		{
			cout<<-1<<endl;
			return;
		}
	}
	cout<<ans<<endl;
	return;
}
int main(){
	//sstd::ios::sync_with_stdio(false); cin.tie(NULL);
	solve();
	return 0;
}


Measured memory_bytes: 1728
#include "bits/stdc++.h"
using namespace std;
const int MOD = 1e9 + 7;
typedef long long lint;
const int INF = 1e7;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, k, n) for (int i = (k); i < (int)(n); ++i)
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(x) (x).begin(), (x).end()
#define pb push_back
int main()
{
    int n, c;
    cin >> n >> c;
    vvi d(c, vi(c));
    rep(i, c)
    {
        rep(j, c)
        {
            cin >> d[i][j];
        }
    }
    vvi m(3, vi(c));
    rep(i, n)
    {
        rep(j, n)
        {
            int x;
            cin >> x;
            m[(i + j) % 3][x-1]++;
        }
    }
    lint ans = LLONG_MAX;
    rep(i, c)
    {
        rep(j, c)
        {
            rep(k, c)
            {
                if (i == j || j == k || k == i)
                {
                    continue;
                }
                lint cc = 0;
                rep(l, c)
                {
                    cc += m[0][l] * d[l][i];
                }
                rep(l, c)
                {
                    cc += m[1][l] * d[l][j];
                }
                rep(l, c)
                {
                    cc += m[2][l] * d[l][k];
                }
                ans = min(ans, cc);
            }
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
// Ruined Square

#include <iostream>

using namespace std;

int main(){
	int x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;

	cout << (y1-y2) + x2 << ' ' << (x2-x1) + y2 << ' ';
	cout << (y1-y2) + x1 << ' ' << (x2-x1) + y1 << endl;

	return 0;
}

Measured memory_bytes: 256