akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
/*
大体写経
けんちょんさんのわかりやすい!
http://drken1215.hatenablog.com/entry/2019/02/04/013700
*/
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

const int  MAX_DIGIT = 50;

vector <vector<long long>> dp(100 ,vector <long long>(2,-1 ));
int main(void){
    int n;
    long long k;
    cin>>n>>k;
    vector <long long> a(n);
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    dp[0][0] = 0;
    for(int d = 0; d < MAX_DIGIT; d++){
        long long mask = 1LL<<(MAX_DIGIT - d -1);
        
        int num = 0;
        for(int i=0;i<n;i++) if (a[i] & mask) num++;
        
        long long cost0 = mask * num;
        long long cost1 = mask * (n-num);
        //numは1の数でn-numは0の数
        
        if(dp[d][1] != -1){
            chmax(dp[d+1][1], dp[d][1]+max(cost0,cost1));
        }
        if(dp[d][0] != -1){
            if(k&mask) chmax(dp[d+1][1],dp[d][0] + cost0);
        }
        if(dp[d][0] != -1){
            if(k&mask) chmax(dp[d+1][0], dp[d][0] + cost1);
            else chmax(dp[d+1][0], dp[d][0] + cost0);
        }
    
    }
    cout<<max(dp[MAX_DIGIT][0], dp[MAX_DIGIT][1])<<endl;
}


Measured memory_bytes: 1024
INF = 2**60 # 無限大を表す値
H, N = map(int, input().split())
AB = [map(int, input().split()) for i in range(N)]
dp = [[INF] * (H + 1)] * (N + 1)
dp[0][0] = 0
for i, (A, B) in enumerate(AB):
    for j in range(H+1):
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        dp[i+1][j] = min(dp[i+1][j], dp[i+1][max(j-A, 0)] + B)
print(dp[N][H])

Measured memory_bytes: 74544
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <list>
#include <ctime>
#include <complex>
#include <bitset>
#include <tuple>
#include <functional>

#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define MOD 1000000007LL
#define rep(i,a,n) for (int i=a ; i<n ; i++)
#define per(i,a,n) for (int i=n-1 ; i>=a ; i--)
#define LLINF (llong)1e18+5
#define INF 1e9+1
#define endl '\n'

using namespace std;
using llong = long long;
using VI = vector<int>;
using VLL = vector<long long>;
using PII = pair<int, int>;

//struct cmp {
//	bool operator() (pair<PII, int> a, pair<PII, int> b)
//	{
//		return a.ss > b.ss;
//	}
//};

int main()
{
	IOS;
	int a, b, c, d, e, k;
	cin >> a >> b >> c >> d >> e >> k;
	if (e - a > k) cout << ":(";
	else cout << "Yay!";
	
	return 0;
}

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

using namespace std;

const int N = 200020;

map<long long, int> cnt;
int a[N];

int main()
{
	int n;
	long long ans = 0, sum = 0;
	scanf("%d", &n);
	cnt[0] = 1;
	for(int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		sum += a[i];
		ans += cnt[sum];
		cnt[sum]++;
	}
	printf("%lld\n", ans);
}

Measured memory_bytes: 13568
#include"bits/stdc++.h"
#include<boost/multi_array.hpp>
#include<boost/optional.hpp>
#include<boost/range/irange.hpp>
#include<boost/range/algorithm.hpp>
#include<boost/range/adaptors.hpp>
using namespace std;
#define req(i,n) for(int i = 0; i < (n); i++)

namespace adaptor = boost::adaptors;

void Main()
{
	int n;
    cin >> n;
    vector<int> p(n), q(n);
    req(i,n) cin >> p[i];
    req(i,n) cin >> q[i];
    vector<int> a(n);
    req(i,n) a[i] = i+1;

    map<vector<int>, int> mp;
    do {
        mp[a] = mp.size();
    } while(next_permutation(a.begin(), a.end()));

    int ans = abs(mp[p] - mp[q]);
    cout << ans << endl;
}

int main()
{
	std::cin.tie(nullptr);
	std::ios_base::sync_with_stdio(false);
	std::cout << std::fixed << std::setprecision(15);
	
	Main();
}

Measured memory_bytes: 5248
import java.util.Scanner;

public final class Main{
    public static void main(String[] args){
        Scanner stdIn = new Scanner(System.in);

        int n = stdIn.nextInt();
        String s = stdIn.next();
        int k = stdIn.nextInt();

        char[] ca = s.toCharArray();

        for(int i=0; i<n; i++){
            if(ca[i] != ca[k-1]){
                ca[i] = '*';
            }
        }

        String ans = "";

        for(int i=0; i<n; i++){
            ans = ans + ca[i];
        }

        System.out.println(ans);
    }
}


Measured memory_bytes: 22612
import sys
import itertools

# import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, m, l = map(int, readline().split())
A = [[int(x) for x in readline().split()] for _ in range(n)]
B = [[int(y) for y in readline().split()] for _ in range(m)]
lis = [[0] * l for _ in range(n)]
for i in range(n):
    for j in range(l):
        for k in range(m):
            lis[i][j] += A[i][k] * B[k][j]
for i in lis:
    print(*i)



Measured memory_bytes: 7056
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define srt(a) sort(a.begin(),a.end())
#define rsrt(a) sort(a.rbegin(),a.rend())
#define lb(a,b) lower_bound(a.begin(),a.end(),b)
#define ub(a,b) upper_bound(a.begin(),a.end(),b)
#define MOD ((int)(1000000007))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define float long double
#define sqrt sqrtl
#define cbrt cbrtl
typedef vector<int> vi;
typedef vector<float> vf;
typedef pair<float,int> fi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<fi> vfi;
typedef vector<vi> vvi;
typedef vector<vf> vvf;
typedef vector<vii> vvii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef priority_queue<fi> pqfi;
typedef priority_queue <int, vector<int>, greater<int> > rpqi; 
typedef priority_queue <ii, vector<ii>, greater<ii> > rpqii; 
typedef priority_queue <fi, vector<fi>, greater<fi> > rpqfi; 

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define srt(a) sort(a.begin(),a.end())
#define rsrt(a) sort(a.rbegin(),a.rend())
#define lb(a,b) lower_bound(a.begin(),a.end(),b)
#define ub(a,b) upper_bound(a.begin(),a.end(),b)
#define MOD ((int)(1000000007))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define float long double
#define sqrt sqrtl
#define cbrt cbrtl
typedef vector<int> vi;
typedef vector<float> vf;
typedef pair<float,int> fi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<fi> vfi;
typedef vector<vi> vvi;
typedef vector<vf> vvf;
typedef vector<vii> vvii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef priority_queue<fi> pqfi;
typedef priority_queue <int, vector<int>, greater<int> > rpqi; 
typedef priority_queue <ii, vector<ii>, greater<ii> > rpqii; 
typedef priority_queue <fi, vector<fi>, greater<fi> > rpqfi; 





int32_t main()
{
	IOS
	int n;cin>>n;
	vi a(n);
	multiset<int>s;
	for(int i=0;i<n;i++)
	{
		int x;cin>>x;
		if(s.empty())
		{
			s.insert(x);continue;
		}
		auto it=s.lower_bound(x);
		if(it!=s.begin())
		{
			it--;
			s.erase(it);
		}
		s.insert(x);
		
	
	
	}
	cout<<s.size()<<endl;
	
	
}


Measured memory_bytes: 5760
//ABC091B
#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <math.h>
#include <stdio.h>
#include <climits>
#include <cfloat>
#include <string>
#include <unordered_map>
using namespace std;

int main(){
	int n, m;
	cin >> n;
	vector<string> s(n);
	for(int i=0;i<n;i++){
		cin >> s.at(i);
	}
	cin >> m;
	vector<string> t(m);
	for(int i=0;i<m;i++){
		cin >> t.at(i);
	}
	sort(s.begin(),s.end());
	sort(t.begin(),t.end());
	int ans = 0;
	for(int i=0;i<n;i++){
		int count = 0;
		for(int j=i;j<n;j++){
			if(s.at(i) == s.at(j)){
				count++;
			}
		}
		for(int k=0;k<m;k++){
			if(s.at(i) == t.at(k)){
				count--;
			}
		}
		ans = max(ans,count);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <array>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <utility>
#include <functional>
#include <random>

using namespace std;
using lli = long long int;
template<class T, class U> void init_n(vector<T>& v, size_t n, U x) 
{ v = vector<T>(n, x); }
template<class T> void init_n(vector<T>& v, size_t n) { init_n(v, n, T()); }
template<class T> void read_n(vector<T>& v, size_t n, size_t o = 0)
{ v = vector<T>(n+o); for (lli i=o; i<n+o; ++i) cin >> v[i]; }
template<class T> void read_n(T a[], size_t n, size_t o = 0)
{ for (lli i=o; i<n+o; ++i) cin >> a[i]; }
template<class T> T gabs(const T& x) { return max(x, -x); }
#define abs gabs

lli gcd(lli x, lli y) {
  while (x%y!=0) {
    lli t=y;
    y=x%y;
    x=t;
  }
  return y;
}

lli n;
vector<lli> a;
int main() {
  cin >> n; read_n(a, n);
  lli ans = a[0];
  for (lli i=1; i<n; ++i) {
    ans = gcd(ans, a[i]);
  }
  cout << ans << endl;
  return 0;
}


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

long long power(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
    }
   
    
   int binomial(int n, int k)  
{  
    // Base Cases  
    if (k == 0 || k == n)  
        return 1;  
  
    // Recur  
    return binomial(n - 1, k - 1) + binomial(n - 1, k);  
}   

bool Sieve(ll int n) 
{ 
    
    bool prime[n+100000]; 
    memset(prime, true, sizeof(prime)); 
  
    for (int p=2; p*p<=n; p++) 
    { 
        if (prime[p] == true) 
        { 
            for (int i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
    return prime[n];
}
  
    
vector<long long> factor;  
vector<long long>prime(long long n) {
 for (long long d = 2; d * d <= n; d++) {
     while (n % d == 0) {
         factor.push_back(d);
            n /=d;
        }
    }
 if (n > 1)
    factor.push_back(n);
    return factor;
}




//It reurns Fn,Fn+1
pair<int, int> fib (int n) {
    if (n == 0)
        return {0, 1};

    auto p = fib(n >> 1);
    int c = p.first * (2 * p.second - p.first);
    int d = p.first * p.first + p.second * p.second;
    if (n & 1)
        return {d, c + d};
    else
        return {c, d};
}


  
int main() {
string s;
cin>>s;
for(int i=0;i<s.length();i++)
{
    if(s[i]=='?')
    {
        s[i]='D';
    }
}
cout<<s<<endl;
}



Measured memory_bytes: 3752
#include <bits/stdc++.h>
#define ll long long
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) // rep(i, 1, 5) --> {1,2,3,4} // rep(i, 5, 1) --> {4,3,2,1} // rep(it, end(v), begin(v)) --> *it

using namespace std;

int main()
{
    int n;
    cin >> n;
    ll sum = 0;
    rep(i, 1, n+1)
    {
        if (i%5!=0 && i%3!=0)
            sum+=i;
    }
    cout << sum;
    return 0;
}

Measured memory_bytes: 3628
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    s = input().rstrip()
    if s=='SUN':
        print(7)
    if s=='MON':
        print(6)
    if s=='TUE':
        print(5)
    if s=='WED':
        print(4)
    if s=='THU':
        print(3)
    if s=='FRI':
        print(2)
    if s=='SAT':
        print(1)

if __name__ == '__main__':
    main()

Measured memory_bytes: 3316
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int a,b;
    while(cin>>a>>b){
        int d[100]={0};
        --a;--b;
        ++d[a],++d[b];

        while(cin>>a>>b,a||b){
            --a;--b;
            ++d[a],++d[b];
        }
        bool Flag=false;
        if(d[0]%2==0||d[1]%2==0)Flag=true;

        for(int i=2;i<100;i++){
            if(d[i]%2)Flag=true;
        }

        cout<<(Flag?"NG":"OK")<<endl;
    }
}

Measured memory_bytes: 1152
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

#include <functional>
#include <algorithm>
#include <numeric>
#include <cmath>

using namespace std;
typedef long long ll;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
#define rep(i,n) REP(i,0,n)
#define REP(i,x,n) for(int i=x;i<n;++i)

///////////////////////////////////////////////////
// -------------------- I/O -------------------- //
///////////////////////////////////////////////////
// https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords
template<typename T> void outputVec(const std::vector<T>& vec, const int precision = 0) {
	if (precision > 0) std::cout << std::fixed << std::setprecision(precision);
	for (auto it = vec.begin(); it != vec.end() - 1; ++it) { std::cout << *it << " "; }
	std::cout << *(vec.end() - 1) << "\n";
}
template<typename T> void outputVal(const T& res, const int precision = 0) {
	if (precision > 0) std::cout << std::fixed << std::setprecision(precision);
	std::cout << res << "\n";
}
template<typename T> T inputVal() { T a; std::cin >> a; return a; }
template<typename T> std::vector<T> inputVec(int N) {
	std::vector<T> vec(N);
	for (auto it = vec.begin(); it != vec.end(); ++it) { std::cin >> *it; }
	return vec;
}
template<typename T> void inputVec(std::vector<T>& vec) {
	for (auto it = vec.begin(); it != vec.end(); ++it) { std::cin >> *it; }
}

///////////////////////////////////////////////////
// ------------------- utils ------------------- //
///////////////////////////////////////////////////
// change min/max
template<class T> inline bool chMin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chMax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

///////////////////////////////////////////////////
// ------------------- main -------------------- //
///////////////////////////////////////////////////
void Main() {
	const int N = inputVal<int>();
	vector<int> p = inputVec<int>(N);
	vector<int> sp = p;
	sort(sp.begin(), sp.end());

	int cnt = 0;
	rep(i, N) {
		if (p[i] != sp[i]) ++cnt;
	}

	if (cnt > 2) {
		cout << "NO" << endl;
	}
	else {
		cout << "YES" << endl;
	}
}

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

	double tmp;
	cin >> tmp;

	return 0;
}


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

#include <iostream>
//#include <algorithm>
// #include <iomanip>
#define ll long long
#define map unordered_map
#define set unordered_set
#define pll pair<ll, ll>
#define vll vector<ll>

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

ll dp[110][110];

int main() {
  fill(dp[0], dp[109], INF);

  // std::cout << std::fixed << std::setprecision(10);
  ll H, W;
  scanf("%lld %lld", &H, &W);

  vector<string> walls;
  for (ll h = 0; h < H; h++) {
    string s;
    cin >> s;
    walls.emplace_back(s);
  }

  dp[0][0] = (walls[0][0] == '.' ? 0 : 1);

  for (ll x = 0; x < W; x++) {
    for (ll y = 0; y < H; y++) {
      char c = walls[y][x];

      if (x < W - 1) {
        char right = walls[y][x + 1];
        ll next_value = dp[y][x];
        if (c == '.' && right == '#') {
          next_value += 1;
        }
        dp[y][x + 1] = min(dp[y][x + 1], next_value);
      }
      if (y < H - 1) {
        char down = walls[y + 1][x];
        ll next_value = dp[y][x];
        if (c == '.' && down == '#') {
          next_value += 1;
        }
        dp[y + 1][x] = min(dp[y + 1][x], next_value);
      }
    }
  }
  cout << dp[H - 1][W - 1] << endl;
  // cout << dp[0][1] << endl;
}


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

typedef long long int int64;

#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define ABS(a) ((a)>(0)?(a):-(a))

int elem(char *s,int n,char c){
  int i;
  for(i=0;i<n;i++) if(s[i]==c) return 1;
  return 0;
}

void run(void){
  char s[1001];
  scanf("%s",s);
  int n;
  for(n=0;s[n]!='\0';n++);
  if(elem(s,n,'N')==elem(s,n,'S') && elem(s,n,'W')==elem(s,n,'E')){
    printf("Yes\n");
  } else {
    printf("No\n");
  }
}

int main(void){
  run();
  return 0;
}


Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef string::const_iterator state;

int mod_pow(int n,int m,int mod){
    if(!m)return 1;
    int x=mod_pow(n,m/2,mod);
    x=x*x%mod;
    if(m&1)x=x*n%mod;
    return x;
}

int p;
bool flag;
int number(state &b){
    int ret=0;
    while(isdigit(*b)){
        ret=ret*10+(*b-'0');
        b++;
    }
    return ret;

}
int expression(state&);

int factor(state &b){
    if(*b=='('){
        b++;
        int ret=expression(b);
        b++;
        return ret;
    }
    else return number(b);
}

int term(state &b){
    int ret=factor(b);
    while(true){
        if(*b=='*'){
            b++;
            ret=(ret*factor(b))%p;
        }
        else if(*b=='/'){
            b++;
            int a=factor(b);
            if(a==0){
                flag=false;
                return 0;
            }
            ret=ret*mod_pow(a,p-2,p)%p;
        }
        else break;
    }
    return ret;
}


int expression(state &b){
    int ret=term(b);
    while(flag){

        if(*b=='+'){
            b++;
            ret=(ret+term(b))%p;
        }
        else if(*b=='-'){
            b++;
            ret=(ret-term(b)+p)%p;
        }
        else break;
    }
    return ret;
}

signed main(){
    string str;
    char ig;
    while(cin>>p>>ig,getline(cin,str),p){
        flag=true;
        string s;
        for(int i=0;i<str.size();i++)if(str[i]!=' ')s+=str[i];
        s+=" ";

        state start=s.begin();
        int v=expression(start);
        if(flag)cout<<s<<"= "<<v<<" (mod "<<p<<")"<<endl;
        else cout<<"NG"<<endl;
    }

    return 0;
}

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

int main() {
  int N, Alice = 0, Bob = 0;
  cin >> N;
  
  vector<int> vec(N);
  for (int i = 0; i < vec.size(); i++) {
    cin >> vec.at(i);
    // cout << vec.at(i) << endl;
  }
  
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  for (int i = 0; i < vec.size(); i += 2) {
    Alice += vec.at(i);
  }
  for (int i = 1; i < vec.size(); i += 2) {
    Bob += vec.at(i);
  }
  
  cout << Alice - Bob << endl;
  
}

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++)
    cin >> a[i] >> b[i];
  vector<int> c(m), d(m);
  for (int i = 0; i < m; i++)
    cin >> c[i] >> d[i];

  for (int i = 0; i < n; i++) {
    int mn      = INT_MAX;
    int mnIndex = 0;
    for (int j = 0; j < m; j++) {
      int tmp = abs(a[i] - c[j]) + abs(b[i] - d[j]);
      if (mn > tmp) {
        mn      = tmp;
        mnIndex = j + 1;
      }
    }
    cout << mnIndex << endl;
  }
}

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

int main() {
    int a, b;
    cin >> a >> b;
    int m1 = a + a - 1;
    int m2 = b + b - 1;
    int m3 = a + b;
    cout << max({m1, m2, m3}) << endl;
    return 0;
}


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

int main() {
  int N;
  cin >> N;
  while (N > 0) {
    int num = N % 10;
    if (num == 9) {
      cout << "Yes" << endl;
      return 0;
    }
    N /= 10;
  }
  cout << "No" << endl;
  return 0;
}


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

int main()
{
    int a,b,c;
    cin>>a>>b>>c;

    if(a+b+c>=22)
        cout<<"bust\n";
    else
        cout<<"win\n";

    return 0;
}


Measured memory_bytes: 256
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = []
for _ in range(n):
    a = ri()
    s0 = set()
    s1 = set()
    for _ in range(a):
        i, b = ril()
        if b == 0:
            s0.add(i - 1)
        else:
            s1.add(i - 1)
    ls.append((s0, s1))

res = 0
for b in range(1, 2**n):
    b = bin(b)[2:].rjust(n, '0')
    count = b.count('1')
    if count <= res:
        continue
    ss0 = set()
    ss1 = set()
    t0 = set()
    t1 = set()
    for i, c in enumerate(b):
        s0, s1 = ls[i]
        if c == '1':
            t1.add(i)
            ss0.update(s0)
            ss1.update(s1)
        else:
            t0.add(i)
    if ss0 & ss1 or ss0 & t1 or ss1 & t0:
        continue
    res = count
print(res)

Measured memory_bytes: 3188
#include<iostream>
#include<algorithm>
using namespace std;
int N_MAX = 100001;
typedef long long ll;
int main()
{
  int n,i;
  ll c,res;
  ll x[N_MAX],v[N_MAX],p[N_MAX],m[N_MAX],sp[N_MAX],sm[N_MAX];
  cin >> n >> c;
  x[0]=0;
  v[0]=0;
  p[0]=0;
  m[0]=0;
  sp[0]=0;
  sm[0]=0;
  for(i=1;i<=n;i++){
    cin >> x[i] >> v[i];
    sp[i]=sp[i-1]+v[i];
    p[i]=max(p[i-1],sp[i]-x[i]);
  }
  for(i=1;i<=n;i++){
    sm[i]=sm[i-1]+v[n+1-i];
    m[i]=max(m[i-1],sm[i]-c+x[n+1-i]);
  }
  res=max(p[n],m[n]);
  for(i=1;i<n;i++)
    res=max(res,sp[i]-2*x[i]+m[n-i]);
  for(i=1;i<n;i++)
    res=max(res,sm[i]-2*(c-x[n+1-i])+p[n-i]);
  cout << res << endl;
  return 0;
}

Measured memory_bytes: 8320
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
typedef  long long ll;
ll gcd(ll x,ll y){
   if(y==0) return x;
   else return gcd(y,x%y);
}
ll lcm(ll x,ll y){
   return x/gcd(x,y)*y;
}
using namespace std;
int main(){ 
   string s;
   cin>>s;
   int a[26];
   rep(i,26){
      a[i]=0;
   }
   rep(i,s.size()){
      a[s[i]-'a']++;
   }
   int flag=0;
   rep(i,26){
      if(a[i]==0){
         cout<<char(i+'a');
         flag=1;
         break;
      }
   }
   if(flag==0) cout<<"None";
   
   
   return 0;
}

Measured memory_bytes: 512
# frozen_string_literal: true

s = gets.chomp
t = gets.chomp

max = 0
(0..(s.size - t.size)).each do |i|
  count = 0
  a = s[i..(i + t.size - 1)]
  (0..(t.size - 1)).each do |j|
    count += 1 if a[j] == t[j]
  end
  max = count if max < count
end
puts t.size - max


Measured memory_bytes: 14380
#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>;

void solve() {
    int n;
    cin >> n;
    vector<ll> A(n);
    rep(i, n) cin >> A[i];
    sort(A.begin(), A.end());
    vector<ll> cumsum(n, 0);
    cumsum[0] = A[0];
    for (int i = 1; i < n; i++) {
        cumsum[i] = cumsum[i-1] + A[i];
    }

    int ans = 1;
    for (int i = n-2; i >= 0; i--) {
        if (2 * cumsum[i] >= A[i+1]) {
            ans++;
        } else {
            break;
        }
    }

    cout << ans << endl;

}

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


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

typedef long long ll;
typedef vector <ll> v_ll;
typedef vector <vector <ll> > vv_ll;
typedef vector < pair <ll,ll> > vp_ll;
typedef pair <ll,ll> p_ll;

bool TEST=false;
ll MOD = 1000000007;

void show(){if (TEST){cout << endl;}}
void show(string x){if (TEST){cout << x;}}
void show(string x,bool enter){
  if (TEST){if (enter) cout << "<#> " << x << endl;else cout << x;}
}
void show(ll x){if (TEST){cout << x;}}
void show(ll x,bool enter){
  if (TEST){if (enter) cout << "<#> " << x << endl;else cout << x;}
}
void show(p_ll p){
  if (TEST){cout << "{ ";show(p.first);cout << " ";show(p.second);cout << " }";}
}
void show(v_ll v){
  if (TEST){
    cout << "<begin> ";
    for (ll i=0;i<v.size();i++){show(v[i]); cout << " ";}cout <<"<end>"<< endl;
}}
void show(vv_ll v){
  if (TEST){
    cout << "//////////" << endl;
    for (ll i=0;i<v.size();i++){
      show(v[i]); cout << endl;
}}}
void show(vp_ll v){
  if (TEST){
    cout << "//////////" << endl;
    for (ll i=0;i<v.size();i++){
      show(v[i]); cout << endl;
}}}


ll mod(ll x){
  ll n=MOD;
  ll ret=x%n;
  if (ret<0)ret+=n;
  return ret;
}
void mod_self(ll &x){
  x=mod(x);
}
ll mod_inv(ll x){
  ll n=MOD;
  ll y = x,z = n-2, ret=1;
  y=mod(y);
  while (z>0){
    if(z%2==1) ret=mod(ret*y);
    y=mod(y*y);
    z/=2;
  }
  return ret;
}
ll mod_pow(ll x,ll a){
  ll y = x,z = a,ret=1;
  y=mod(y);
  if (z<0)z=-z;
  while (z>0){
    if(z%2==1) ret=mod(ret*y);
    y=mod(y*y);
    z/=2;
  }
  if (a<0) ret=mod_inv(ret);
  return ret;
}
v_ll mod_nck(ll n){
  v_ll nck(n+1,0);
  nck[0]=1;
  for (ll i=1;i<=n;i++){
    nck[i]=mod(nck[i-1]*(n+1-i));
    nck[i]=mod(nck[i]*mod_inv(i));
  }
  return nck;
}
v_ll mod_factorial(ll n){
  v_ll fct(n+1,0);
  fct[0]=1;
  for (ll i=1;i<=n;i++){
    fct[i]=mod(fct[i-1]*i);
  }
  return fct;
}

ll N,M;
v_ll A;
v_ll B;
int main(){
  //TEST=true;
  MOD = 1000000007;
  cin >> N>>M;
  A.assign(M+10,0);
  B.assign(N+10,0);
  for (ll i=0;i<M;i++){
    cin >> A[i];
  }
  A[M]=-1;
  ll idx_a=0;
  B[0]=1;
  for (ll i=1;i<=N;i++){
    if (A[idx_a]==i){
      B[i]=0;
      idx_a++;
    }else{
      if (i>=2) B[i]=B[i-1]+B[i-2];
      else B[i]=B[i-1];
    }
    B[i]=mod(B[i]);
  }
  cout << B[N] << endl;
}


Measured memory_bytes: 1792
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
#include <map>
#include <cmath>
#include <bitset>
#include <deque>
#define MAX_N 100
#define MAX_T 200
#define INF (1LL<<31)
#define fst first
#define snd second
typedef long long ll;
typedef std::pair<int,int> ii;
using namespace std;
 
int main(){
  int n;
  int t,ans;
  cin>>n;
  ans=0;
  for(int i=1; i<=n;i++){
    int p,q;
    cin>>p;
    if(p==i){
      q=p;
      while(q==i&&i<=n){
        if(i==n){
          i++;
          break;
        }
        cin>>q;
        i++;
      }
     ans+=(i-p+1)/2;
    }
  }
  cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 256
using System;
using System.Net.NetworkInformation;

public class Program
{
    public void Solve()
    {
        var sc = new Scanner();
        string s1 = sc.Next();
        string s2 = sc.Next();

        var dp = new int[s1.Length + 1, s2.Length + 1];
        for (int i = 1; i <= s1.Length; i++)
        {
            dp[i, 0] = i;
        }

        for (int j = 1; j <= s2.Length; j++)
        {
            dp[0, j] = j;
        }

        for (int i = 0; i < s1.Length; i++)
        {
            for (int j = 0; j < s2.Length; j++)
            {
                int a = dp[i + 1, j] + 1;
                int b = dp[i, j + 1] + 1;
                int c = dp[i, j] + (s1[i] == s2[j] ? 0 : 1);
                dp[i + 1, j + 1] = Math.Min(a, Math.Min(b, c));
            }
        }

        Console.WriteLine(dp[s1.Length, s2.Length]);
    }

    public static void Main(string[] args)
    {
        new Program().Solve();
    }
}

class Scanner
{
    public Scanner()
    {
        _pos = 0;
        _line = new string[0];
    }

    const char Separator = ' ';
    private int _pos;
    private string[] _line;

    #region スペース区切りで取得

    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = Console.ReadLine().Split(Separator);
            _pos = 0;
        }

        return _line[_pos++];
    }

    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

    public double NextDouble()
    {
        return double.Parse(Next());
    }

    #endregion

    #region 型変換

    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }

    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }

    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }

    #endregion

    #region 配列取得

    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = Console.ReadLine().Split(Separator);

        _pos = _line.Length;
        return _line;
    }

    public int[] IntArray()
    {
        return ToIntArray(Array());
    }

    public long[] LongArray()
    {
        return ToLongArray(Array());
    }

    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }

    #endregion
}


Measured memory_bytes: 11084
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

var (
	readString func() string
	readBytes  func() []byte
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Bytes()
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

func readInt64() int64 {
	i, err := strconv.ParseInt(readString(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(readString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func minInt64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func maxInt64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func absInt64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func sum(a []int) int {
	var ret int
	for i := range a {
		ret += a[i]
	}
	return ret
}

func sumInt64(a []int64) int64 {
	var ret int64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func sumFloat64(a []float64) float64 {
	var ret float64
	for i := range a {
		ret += a[i]
	}
	return ret
}

func gcdInt64(m, n int64) int64 {
	for m%n != 0 {
		m, n = n, m%n
	}
	return n
}

func lcmInt64(m, n int64) int64 {
	return m / gcdInt64(m, n) * n
}

// sort ------------------------------------------------------------

type int64Array []int64

func (s int64Array) Len() int           { return len(s) }
func (s int64Array) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s int64Array) Less(i, j int) bool { return s[i] < s[j] }

type xxx struct {
	x int
}

type sortArray []xxx

func (s sortArray) Len() int           { return len(s) }
func (s sortArray) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s sortArray) Less(i, j int) bool { return s[i].x < s[j].x }

// -----------------------------------------------------------------

func main() {
	n := readInt()
	a := readInt()
	b := readInt()
	fmt.Println(min(n*a, b))
}


Measured memory_bytes: 640
import sys
input = sys.stdin.readline

taka, aoki, K = list(map(int, input().split()))


if taka <= K:
    if aoki - K + taka < 0:
        print(0, 0)
    else:
        print(0, aoki - (K - taka))
else:
    print(taka - K, aoki)


Measured memory_bytes: 2940
#include<stdio.h>

int main(void){

    int a,n,i,min=1000000,max=-1000000;
    long int sum=0;
    scanf("%d",&n);

    for(i=0;i<n;i++){
        scanf("%d",&a);
        sum+=a;

        if(min>a)
            min=a;
        if(max<a)
            max=a;

    }

    printf("%d %d %ld\n",min,max,sum);
    return 0;
}






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

using namespace std;

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

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

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

// mod
const ll MOD = 1000000007ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

// floating
typedef double Real;
const Real EPS = 1e-11;
#define EQ0(x) (abs(x)<EPS)
#define EQ(a,b) (abs(a-b)<EPS)
typedef complex<Real> P;

int n,k;

Real cross(P a,P b){
  return imag(a*conj(b));
}
// http://www.deqnotes.net/acmicpc/2d_geometry/lines
P intersection_ls(P a1, P a2, P b1, P b2) {
  P b = b2-b1;
  Real d1 = abs(cross(b, a1-b1));
  Real d2 = abs(cross(b, a2-b1));
  Real t = d1 / (d1 + d2);

  return a1 + (a2-a1) * t;
}

int main(){
  scanf("%d%d",&n,&k);
  double a_ph = 0.0;
  double b_ph = (double)k/n;
  double c_ph = 1.0/n;
  double d_ph = (double)(n+1-k)/n;
  a_ph *= 2.0*M_PI;
  b_ph *= 2.0*M_PI;
  c_ph *= 2.0*M_PI;
  d_ph *= 2.0*M_PI;
  P a(cos(a_ph),sin(a_ph));
  P b(cos(b_ph),sin(b_ph));
  P c(cos(c_ph),sin(c_ph));
  P d(cos(d_ph),sin(d_ph));
  P o(0,0);
  P e = intersection_ls(a,o,c,d);
  P f = intersection_ls(c,o,a,b);
  P g = intersection_ls(a,b,c,d);
  double unit = (abs(cross(f-a,o-a))+abs(cross(f-c,g-c)))/2.0;
  double ans = unit*n;
  printf("%.10lf\n",ans);
  return 0;
}

Measured memory_bytes: 1228
import sys

input = sys.stdin.readline

k = int(input())
'''if k == 7 or k == 1:
    print(1)
elif k % 2 == 0 or k % 5 == 0: print(-1)
else:
    if k % 7 == 0: k = k // 7
    mod = 9*k
    i = 1;
    result = 10;
    while result != 1:
        result = (result * 10) % mod
        i += 1
    print(i)'''
num = 7%k
soln = False
for i in range(1, k+1):
    if num == 0:
        print(i)
        soln = True
        break
    num= (num * 10 + 7) % k
if not soln:
    print(-1)
    

        


Measured memory_bytes: 9044
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr<<" "<<(hoge)<<endl;
#define DBG cerr<<"!"<<endl;
#define BITLE(n) (1LL<<((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
vvi cnt(30);
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll ans=0;
  string s,t;cin>>s>>t;
  ll n=s.size(),m=t.size();
  ll kuri=0;
  for(int i=0;i<n;i++)
    cnt[s[i]-'a'].pb(i);
  for(int i=0;i<30;i++){
    cnt[i].pb(llINF);
  }
  ll now=-1;
  for(int i=0;i<m;i++){
    char c=t[i];
    if(cnt[c-'a'].size()==1){cout<<-1<<endl;return 0;}
    auto lb=upper_bound(ALL(cnt[c-'a']),now);
    //cout<<(*lb)<<endl;
    if(*lb==llINF){
      kuri++;
      now=-1;
      auto lb2=upper_bound(ALL(cnt[c-'a']),now);
      now=*lb2;
    }else{
      now=*lb;
    }
    // cout<<now<<endl;
  }
  cout<<now+n*kuri+1<<endl;
  return 0;
}


Measured memory_bytes: 1736
n = gets.to_i
as = 2.times.map{gets.chomp.split.map(&:to_i)}
memo = Array.new(n)
1.upto(n) do |i|
  memo[i] = as[0][0..(i-1)].inject(:+) + as[1][(i-1)..(n-1)].inject(:+)
end
p memo.compact.max


Measured memory_bytes: 1788
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;

class Main {

	 private static final int A = 0;
	    private static final int B = 1;
	    private static final int C = 2;
	    private static final int D = 3;
	    private static final int E = 4;
	    private static final int F = 5;


	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str ;
		String[] num1 = null;

		try {


		while ((str = br.readLine()) !=null) {


		 num1 = str.split(" ");

		BigDecimal[] array = new BigDecimal[num1.length] ;


		for(int i=0; i<num1.length; i++){
			array[i] = new BigDecimal(num1[i]);
		}


		BigDecimal x= array[C].multiply(array[E]).subtract(array[B].multiply(array[F])).divide(array[A].multiply(array[E]).subtract(array[B].multiply(array[D])));

		BigDecimal y= array[C].multiply(array[D]).subtract(array[A].multiply(array[F])).divide(array[B].multiply(array[D]).subtract(array[A].multiply(array[E])));

		System.out.println(x.setScale(3, BigDecimal.ROUND_HALF_UP)+" "+y.setScale(3,BigDecimal.ROUND_HALF_UP));

		}

		} catch (Exception e) {
			// TODO: handle exception
		}


	}

}

Measured memory_bytes: 24244
import java.lang.Math;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        int[] V = new int[N];
        int[] C = new int[N];
        for (int i = 0; i < N; i++) {
            V[i] = sc.nextInt();
        }
        for (int i = 0; i < N; i++) {
            C[i] = sc.nextInt();
        }

        int answer = 0;
        for (int i = 0; i < N; i++) {
            answer += Math.max((V[i] - C[i]), 0);
        }

        System.out.println(answer);
    }
}


Measured memory_bytes: 22868
import sys

def main():
    lines = sys.stdin.readlines()

    remaining_cards_num = 0
    S = [] # Spade
    H = [] # Heart
    C = [] # Club
    D = [] # Diamond
    for index, line in enumerate(lines):
        line = line.strip('\n')
        elems = line.split(" ")
        #print elems
        if index == 0:
            remaining_cards_num = int(elems[0])
        elif index <= remaining_cards_num:
            if elems[0] == "S":
                S.append(int(elems[1]))
            elif elems[0] == "H":
                H.append(int(elems[1]))
            elif elems[0] == "C":
                C.append(int(elems[1]))
            elif elems[0] == "D":
                D.append(int(elems[1]))
        else:
            break

    for i in range(1, 13+1):
        if i not in S:
            print("S %d" % (i))
    for i in range(1, 13+1):
        if i not in H:
            print("H %d" % (i))
    for i in range(1, 13+1):
        if i not in C:
            print("C %d" % (i))
    for i in range(1, 13+1):
        if i not in D:
            print("D %d" % (i))

if __name__ == "__main__":
    main()

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

using namespace std;

int main(){
    int r[26];
    
    rep(i, 26) r[i] = 0;
    string s;
    while(cin >> s) {
        for(int i = 0; i < s.length(); i++)
        {
            if(isalpha(s[i])) {   
                r[tolower(s[i]) - 'a'] += 1;
            }
        }
    }
    
    for(int i = 0; i < 26; i++) {
        cout << (char)(i + 'a') << " : " << r[i] << endl;
    }
    
}


Measured memory_bytes: 3072
#include<bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(),(obj).end()
#define RALL(obj) (obj).rbegin(),(obj).rend()
#define REP(i, n) for(int i=0;i<(n);++i)
#define RANGE(i, a, b) for(int i=(a);i<(b);++i)
#define RREP(i, n) for(int i=(n)-1;i>= 0;--i)

#define endl '\n'
#define pb push_back
#define eb emplace_back
#define pque priority_queue
#define umap unordered_map
#define uset unordered_set
#define BIG 2000000000
#define VERYBIG 1000000000000000ll
#define PI 3.141592653589793238
#define dcout cout<<fixed<<setprecision(50)
#define _popcnt __builtin_popcount

const long long dx[]={1,0,-1,0,1,1,-1,-1}, dy[]={0,-1,0,1,1,-1,1,-1};
const long long MOD = 1e9+7;

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

template<typename T> inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;}
template<typename T> inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;}
template<typename T> inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
template<typename T> inline T nHr(T a,T b){return nCr(a+b-1,b);}
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;}

typedef pair<int, int> INTP;
typedef vector<int> INTV;
struct edge2 {int to, cost;};
struct edge3 {int from, to, cost;};

int N, u, v;
INTV distT, distA;
vector<INTV> E;

void dfs1(int pos){
  for(auto nxt : E[pos]){
    if(distT[nxt] != BIG) continue;
    distT[nxt] = distT[pos] + 1;
    dfs1(nxt);
  }
}

void dfs2(int pos){
  for(auto nxt : E[pos]){
    if(distA[nxt] != BIG) continue;
    distA[nxt] = distA[pos] + 1;
    dfs2(nxt);
  }
}

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

  cin >> N >> u >> v;
  distT.resize(N, BIG); distA.resize(N, BIG);
  u--; v--;
  distT[u] = 0; distA[v] = 0;
  E.resize(N);
  REP(i, N-1){
    int a, b; cin >> a >> b;
    a--; b--;
    E[a].eb(b); E[b].eb(a);
  }
  dfs1(u); dfs2(v);
  int ans = 0;
  REP(i, N){
    if(distT[i] < distA[i]) chmax(ans, distA[i]-1);
  }
  cout << ans << endl;
}

Measured memory_bytes: 9984
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll n;
  cin >> n;
  vl s(4, 0);
  rep(i, n) {
    string st;
    cin >> st;
    if(st == "AC")
      s[0]++;
    else if(st == "WA")
      s[1]++;
    else if(st == "TLE")
      s[2]++;
    else
      s[3]++;
  }
  printf("AC x %d\n", s[0]);
  printf("WA x %d\n", s[1]);
  printf("TLE x %d\n", s[2]);
  printf("RE x %d\n", s[3]);
}

Measured memory_bytes: 3716
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author toshibablack
 */

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define ll long long int
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define REP(i, n) for(int i=0;i<n;++i)
#define FOREACH(x, a) for(auto& (x) : (a))
#define ALL(a) (a).begin(),(a).end()
#define COUT(x) out << (x) << endl;

const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

using namespace std;


class AFrog1 {
public:
    int N;
    int h[100001];
    int dp[100001]{0};

    // dp: 残りiでの最小コスト
    void solve(std::istream& in, std::ostream& out) {
        in >> N; REP(i, N) in >> h[i];
        reverse(h, h+N);
        dp[0] = 0; dp[1] = abs(h[1] - h[0]);
        FOR(i, 2, N) dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2]));
        COUT(dp[N-1]);
    }
};


int main() {
    AFrog1 solver;
    std::istream& in(std::cin);
    std::ostream& out(std::cout);
    solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define int         long long
#define mp          make_pair
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vb          vector<bool>
// #define vc          vector<char>
#define vii         vector<pii>
//#define mi          map<int,int>
//#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (int)x.size()
// #define ub(x)       upper_bound(x)
// #define lb(x)       lower_bound(x)
#define endl        '\n'
#define file        freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define fastio      ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// #define N            1e5+5
#define PRIME           998244353
#define MAX         1000000
#define mem0(a)     memset(a,0,sizeof(a))
#define prec        cout<<std::setprecision(9)<<std::fixed
#define forn(i,a,b)  for(int i=a;i<b;i++)
#define pi          3.14159265358979323
int i,j,k;

void Anon_mouS(){
    int n;
    cin>>n;
    vi a(n);
    for(int &x:a)cin>>x;
    int dp[n];
    dp[0]=0;
    dp[1]=abs(a[1]-a[0]);
    for(i=2;i<n;i++){
        dp[i]=min(dp[i-1]+abs(a[i-1]-a[i]),abs(a[i]-a[i-2])+dp[i-2]);
    }
    cout<<dp[n-1];
}
 
int32_t main(){
    fastio;
    int q=1;
    // cin>>q;
    // SieveOfEratosthenes();
    while(q--){
        Anon_mouS();
    }
    return 0;   
}

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

void insertion_sort(int* A, int N);
void print_array(int* A, int N);

int main() {
    int n;
    int* A;
    int i;
    scanf("%d", &n);
    A = (int*)malloc(sizeof(int)*n);
    for ( i = 0; i < n; i++ ) {
        scanf("%d", &A[i]);
    }
    
    insertion_sort(A, n);
    
    free(A);
    return 0;
}

void insertion_sort(int* A, int N) {
    int i,j;
    int v;
    for ( i = 1; i < N; i++ ) {
        print_array(A, N);
        
        v = A[i];
        j = i-1;
        while ( j >= 0 && A[j] > v ) {
            A[j+1] = A[j];
            j--;
        }
        A[j+1] = v;
    }
    print_array(A, N);
}

void print_array(int* A, int N ) {
    int i;
    for ( i = 0; i < N; i++ ) 
        if ( i != N-1 )printf("%d ", A[i]);
        else printf("%d", A[i]);
    printf("\n");
}

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

using namespace std;

int main(){
        map<vector<int>, int>MVII;
        vector<int>D(8);
        for(int i = 0;i < 8; i++){
                D[i]=i;
        }

        queue<vector<int>>que;
        que.push(D);
        while(que.size()){
                vector<int> now = que.front(); que.pop();
                int zero;
                for(int i = 0; i < 8; i++) {
                        if(now[i] == 0) zero = i;
                }
                if(zero >=4) {
                        auto E = now;
                        swap(E[zero], E[zero - 4]);
                        if(MVII[E]==0){
                                MVII[E]=MVII[now]+1;
                                que.push(E);
                        }
                } else{
                        auto E = now;
                        swap(E[zero], E[zero + 4]);
                        if(MVII[E]==0){
                                MVII[E]=MVII[now]+1;
                                que.push(E);
                        }
                }
                if(zero != 0 && zero != 4) {
                        auto E = now;
                        swap(E[zero], E[zero -1]);
                        if(MVII[E]==0){
                                MVII[E]=MVII[now]+1;
                                que.push(E);
                        }
                }
                if(zero != 3 && zero != 7) {
                        auto E = now;
                        swap(E[zero], E[zero +1]);
                        if(MVII[E]==0){
                                MVII[E]=MVII[now]+1;
                                que.push(E);
                        }
                }
        }
        for(int i = 0;i < 8; i++){
                D[i]=i;
        }
        MVII[D] = 0;
        while(cin>>D[0]){
                for(int i = 1;i<8;i++){
                        cin>>D[i];
                }
                cout<<MVII[D]<<endl;
        }

}

Measured memory_bytes: 5696