akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
using namespace std;

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

Node *NIL, *root;

void insert(int key)
{
    Node *x, *y, *z;

    z = new Node;
    z->key = key;
    z->left = NIL;
    z->right = NIL;
    y = NIL;
    x = root;
    while (x != NIL)
    {
        y = x;
        if (z->key < x->key)
        {
            x = x->left;
        }
        else
        {
            x = x->right;
        }
    }

    z->parent = y;

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

Node *find(int key)
{
    Node *x;
    x = root;

    while (x != NIL)
    {
        if (key < x->key)
        {
            x = x->left;
        }
        else if (key > x->key)
        {
            x = x->right;
        }
        else
        {
            break;
        }
    }

    return x;
}

Node *next_node(Node *node)
{
    Node *x;

    if (node->right != NIL)
    {
        x = node->right;
        while (x->left != NIL)
        {
            x = x->left;
        }
    }
    else
    {
        x = node;
        while (x->parent != NIL && x == x->parent->right)
        {
            x = x->parent;
        }
        x = x->parent;
    }

    return x;
}

void erase(int key)
{
    Node *x, *y, *z;

    z = find(key);

    if (z->left == NIL || z->right == NIL)
    {
        y = z;
    }
    else
    {
        y = next_node(z);
    }

    if (y->left != NIL)
        x = y->left;
    else
        x = y->right;

    if (x != NIL)
        x->parent = y->parent;
    if (y->parent == NIL)
        root = x;
    else if (y == y->parent->left)
        y->parent->left = x;
    else
        y->parent->right = x;

    if (z != y)
        z->key = y->key;

    delete y;
}

// 先行順巡回
void preorder(Node *node)
{
    if (node == NIL)
        return;
    cout << " " << node->key;
    if (node->left != NIL)
        preorder(node->left);
    if (node->right != NIL)
        preorder(node->right);
}

// 中間順巡回
void inorder(Node *node)
{
    if (node == NIL)
        return;
    if (node->left != NIL)
        inorder(node->left);
    cout << " " << node->key;
    if (node->right != NIL)
        inorder(node->right);
}

void print()
{
    inorder(root);
    cout << "\n";
    preorder(root);
    cout << "\n";
}

int main()
{
    int i, m, key;
    string command;

    cin >> m;
    for (i = 0; i < m; i++)
    {
        cin >> command;
        if (command[0] == 'i')
        {
            cin >> key;
            insert(key);
        }
        else if (command[0] == 'f')
        {
            cin >> key;
            if (find(key) != NIL)
            {
                cout << "yes\n";
            }
            else
            {
                cout << "no\n";
            }
        }
        else if (command[0] == 'd')
        {
            cin >> key;
            erase(key);
        }
        else
        {
            print();
        }
    }

    return 0;
}


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

// #include <boost/algorithm/string/join.hpp>
// using boost::algorithm::join;
//
// #include "boost/multi_array.hpp"
// boost::multi_array<int, 3> dp;
// dp.resize(boost::extents[10][9][8]);
//
// #include <boost/multiprecision/cpp_dec_float.hpp>  // quite slowly
// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision; // mp::log10
// using bigint = boost::multiprecision::cpp_int;
// using rational = mp::cpp_rational;  // double d = static_cast<double>(r);

typedef long long ll;
typedef unsigned long long ull;
//typedef long double ld;

typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double,double> pdd;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<int> vi;
typedef vector<pair<int, int > > vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;

//constexpr ll MOD = 1e9+7;
template<typename T>constexpr auto inf=numeric_limits<T>::max()/4;
constexpr int INF=inf<int>;
constexpr ll LINF=inf<ll>;

#define Sort(x) sort(begin(x),end(x))
#define Reverse(x) reverse(begin(x),end(x))
#define ABS(a,b) ((a)<(b)?(b)-(a):(a)-(b))
#define nbit(n) (1ull<<(n))
#define _ol3(_1,_2,_3,name,...)name
#define _rep(i,n)_repi(i,0,n)
#define _repi(i,a,b)for(int i=a,i##_l=(b);i<i##_l;++i)
#define REP(...) _ol3(__VA_ARGS__,_repi,_rep,)(__VA_ARGS__)
#define REPR(i,n) for(int i=n-1;i>=0;--i)
#define REPA(i,v) REP(i,(v).size())

// sort(begin(x), end(x), greater<ll>());
// lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置
// upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置

// Returns minimum of a and b.
// If a is less b, a is set to b.
template<typename T>
T chmin(T& a, T b) {
  if (a > b) {
    a = b;
  }
  return a;
}

// Returns maximum of a and b.
// If a is less b, a is set to b.
template<typename T>
T chmax(T& a, T b) {
  if (a < b) {
    a = b;
  }
  return a;
}

template <typename T>
void dumpContents(const T& v, const string& msg)
{
  cerr << "### " << msg << " ###\n";
  for (const auto& x : v) {
    cerr << x << " ";
  }
  cerr << endl;
}

struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<setprecision(20)<<fixed;
#define endl "\n"
  }
} before_main_function;

// ========== end of template ==========

int main(int argc, char** argv) {
  int N, M;
  cin >> N >> M;

  ll ret = N*(N-1)/2 + M*(M-1)/2;
  cout << ret << endl;

  return 0;
}

/* vim: set expandtab shiftwidth=2 softtabstop=2 : */


Measured memory_bytes: 256
using System;

class AOJ{
  static void Main(string[] args)
  {
    int[] a = new int[4];
    int[] b = new int[4];
    int hit, blow;

    string[] s1, s2;
    string str;

    while ((str = Console.ReadLine()) != null){

      s1 = str.Split(' ');
      s2 = Console.ReadLine().Split(' ');

      for (int i = 0; i < 4; i++){
        a[i] = int.Parse(s1[i]);
        b[i] = int.Parse(s2[i]);
      }

      hit = 0;
      blow = 0;

      for (int i = 0; i < 4; i++){
        if (a[i] == b[i])
          hit++;
        for (int j = 0; j < 4; j++){
          if (a[i] == b[j])
            blow++;
        }
      }

      blow -= hit;

      Console.WriteLine("{0} {1}", hit, blow);
    }
  }
}

Measured memory_bytes: 9176
H, N = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N)]

dp = [float("INF")] * 10 ** 5
dp[0] = 0

for h in range(H):
    for a, b in AB:
        dp[h+a] = min(dp[h] + b, dp[h+a])

print(min(dp[H:]))

Measured memory_bytes: 75820
#include <stdio.h>

int main() {
	int in[100][2], i;

	for (i = 0; i < 100; i++) {
		scanf("%d %d", &in[i][0], &in[i][1]);
		if (in[i][0] == 0 && in[i][1] == 0)break;
	}

	for (int j = 0; j < i; j++) {
		for (int k = 0; k < in[j][0]; k++) {
			for (int l = 0; l < in[j][1]; l++) {
				if (k == 0 || k + 1 == in[j][0] || l == 0 || l + 1 == in[j][1]) printf("#");
				else printf(".");
			}printf("\n");
		}printf("\n");
	}

	return 0;
}


Measured memory_bytes: 2104
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
typedef long long ll;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define FOR(i,n) for(int i=0;i<n;i++)
#define all(s) s.begin(),s.end()
#define lla(s) s.rbegin(),s.rend()
const int MOD = 1e9 + 7;
//const int MOD = 998244353;
const ll INF = 1e18;



signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,ans = 0;
    cin>>n;
    vector<int>a(n),app(n+1,0);
    rep(i,0,n){
        cin>>a[i];
    }
    int st = 1,count = 0;
    rep(i,0,n){
        if(a[i]==st){count++;st++;}
    }
    if(count==0){cout<<-1<<"\n";}
    else{
        cout<<(n-count)<<endl;
    }
}

Measured memory_bytes: 3456
#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
#include<math.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef long double ld;
const long double pi = 3.14159265358979323846264338327950288;
bool a[1000001];
int main() {
	ll a, b, c;
	cin >> a >> b >> c;
	if (a % 2 == 0 || b % 2 == 0 || c % 2 == 0) {
		cout << 0 << endl;
	}
	else
		cout << min(a*b, min(a*c, b*c)) << endl;
}

Measured memory_bytes: 256
fn main(){
    let raw = input();
    let len = raw.len();
    let s = raw.as_bytes();
    
    let raw = input();
    let t = raw.as_bytes();
    
    let mut ans = 0;
    
    for i in 0..len {
        if s[i] != t[i] {
            ans += 1;
        }
    }
    
    println!("{}", ans);
    
    
}

fn input() -> String{
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    buf.trim_end().to_string()
}

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

int main() {
  int n;
  cin>>n;
  while(1){
    string s=to_string(n);
    if(s[0]==s[1] && s[1]==s[2]){
      cout<<n<<endl;
      break;
    }
    n++;
  }
}


Measured memory_bytes: 3684
{-# LANGUAGE TupleSections #-}
import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad.State (State, evalState, gets, modify)
import Data.Map.Strict (Map, lookup, insert, empty)
import Data.Maybe (maybe)
import Control.Monad
main :: IO ()
main = breads >>= main'
    where
        main' [d, g] = replicateM d breads >>= print . solve g

-- |
-- >>> solve 700 [[3,500],[5,800]]
-- 3
--
-- >>> solve 2000 [[3,500],[5,800]]
-- 7
--
-- >>> solve 400 [[3,500],[5,800]]
-- 2
--
-- >>> solve 25000 [[20,1000],[40,1000],[50,1000],[30,1000],[1,1000]]
-- 66
solve :: Int -> [[Int]] -> Int
solve g pcs = run calc . (g, [],) . map build $ zip pcs [1..]
    where
        build ([p, c], i) = (100 * p * i + c, p, 100 * i)

        calc f (r, ps, (w, c, p) : xs)
            | r > 0 = minimum <$> sequence [(+ c) <$> f (r - w, ps, xs), f (r, (c - 1, p) : ps, xs)]
            | otherwise = return 0
        calc _ (r, ps, _)
            | r > 0 = return . calc2 r . sortBy cmpf $ filter (\(c, _) -> c > 0) ps
            | otherwise = return 0
            where
                cmpf (_, lp) (_, rp) = compare rp lp
        
        calc2 r ((c, p) : xs)
            | r >= cp = c + calc2 (r - cp) xs
            | r > 0 = v
            | otherwise = 0
            where
                cp = c * p
                (a, m) = r `divMod` p
                v
                    | m > 0 = a + 1
                    | otherwise = a
        calc2 r _
            | r > 0 = 100000
            | otherwise = 0


type Memoizer a b = a -> State (Map a b) b

run :: (Ord a) => (Memoizer a b -> Memoizer a b) -> a -> b
run f = flip evalState empty . memoizeFix f

memoizeFix :: (Ord a) => (Memoizer a b -> Memoizer a b) -> Memoizer a b
memoizeFix f x = gets (Data.Map.Strict.lookup x) >>= maybe (f (memoizeFix f) x >>= ((<$) <*> (modify . Data.Map.Strict.insert x))) return

breads :: IO [Int]
breads = unfoldr uff <$> B.getLine
    where
        uff b = check <$> B.readInt b
        check (a, b)
            | B.null b = (a, b)
            | otherwise = (a, B.tail b)

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

int main(){
    int n, k;
    scanf("%d %d", &n, &k);
    double divided_k = k;
    int divided_count = 0;
    int pow2_divided_count = 1;
    double probability = 0;
    int top_uncount = n;
    while(divided_k > 0.5){
        int range_min = ceil(divided_k);
        int range_size = top_uncount - range_min + 1;
        if(range_size < 0){
            range_size = 0;
        }
        probability += (double)range_size / (double)pow2_divided_count;

        //printf("range_min: %d, range_size: %d, top_uncount: %d, probability: %lf, pow2_divided_count: %d\n", range_min, range_size, top_uncount, probability, pow2_divided_count);

        top_uncount -= range_size;
        divided_k /= 2;
        pow2_divided_count *= 2;
    }
    probability /= n;
    printf("%.16f\n", probability);
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

int main(){
    ll A, B;
    cin >> A >> B;

    ll fa, fb;
    if(A % 2 == 0) fa = (A/2)%2;
    else fa = (((A-1)/2)%2) ^ (A-1);
    if((B+1) % 2 == 0) fb = ((B+1)/2)%2;
    else fb = ((B/2)%2) ^ B;
    cout << (fa ^ fb) << endl;
    return 0;
}

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

int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> table(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> table.at(i).at(j);
    }
  }
  for (int i = 0; i < H; i++) {
    bool flag = true;
    for (int j = 0; j < W; j++) {
      if (table.at(i).at(j) == '#') {
        flag = false;
        break;
      }
    }
    if (flag) {
      H--;
      for (int k = i; k < H; k++) {
        for (int l = 0; l < W; l++) {
          table.at(k).at(l) = table.at(k+1).at(l);
        }
      }
      i--;
    }
  } 
  for (int j = 0; j < W; j++) {
    bool flag = true;
    for (int i = 0; i < H; i++) {
      if (table.at(i).at(j) == '#') {
        flag = false;
        break;
      }
    }
    if (flag) {
      W--;
      for (int k = j; k < W; k++) {
        for (int l = 0; l < H; l++) {
          table.at(l).at(k) = table.at(l).at(k+1);
        }
      }
      j--;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << table.at(i).at(j);
      if (j == W - 1) {
        cout <<endl;
      }
    }
  }
}

Measured memory_bytes: 256
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math

n,k,q = getList()
life = [0 for i in range(n)]
aswer = []
for i in range(q):
    a = getN()
    life[a-1] += 1

for li in life:
    if li <= q - k:
        print("No")
    else:
        print("Yes")

Measured memory_bytes: 48160
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>

using namespace std;
typedef pair<int, int> pii;
const int INF = 1000000000;

int main(){
        ios_base::sync_with_stdio(false);
        int N;
        cin >> N;
        vector<int> p(N);
        for(int i = 0; i < N; ++i){ cin >> p[i]; }
        vector<bool> loop(N);
        vector<int> passed(N, -1), jumpto(N);
        for(int i = 0; i < N; ++i){ jumpto[i] = i; }
        for(int i = 0; i < N; ++i){
                if(passed[i] >= 0){ continue; }
                int c = i;
                while(p[c] != 0 && !loop[c] && passed[c] < 0){
                        passed[c] = i;
                        c += p[c];
                }
                if(p[c] != 0 && (loop[c] || passed[c] == i)){
                        c = i;
                        while(!loop[c]){
                                loop[c] = true;
                                c += p[c];
                        }
                }else{
                        int to = jumpto[c];
                        c = i;
                        while(p[c] != 0 && passed[c] == i){
                                jumpto[c] = to;
                                c += p[c];
                        }
                }
        }
        vector<bool> bfs_passed(N);
        bfs_passed[0] = true;
        queue<pii> q;
        q.push(pii(0, 0));
        int answer = INF;
        while(!q.empty()){
                pii e = q.front();
                q.pop();
                if(e.second == N - 1){
                        answer = e.first;
                        break;
                }
                for(int i = 1; i <= 6; ++i){
                        int c = jumpto[min(e.second + i, N - 1)];
                        if(loop[c] || bfs_passed[c]){ continue; }
                        bfs_passed[c] = true;
                        q.push(pii(e.first + 1, c));
                }
        }
        cout << answer << endl;
        return 0;
}

Measured memory_bytes: 2424
d = int(input())
c = list(map(int, input().split()))
score = []
for i in range(d):
    score.append(list(map(int, input().split())))
last_day = [0 for i in range(26)]
k = 0
for i in range(d):
    ans = 0
    max_score = -10**9
    for j in range(1, 27):
        s = score[i][j-1]
        for k in range(1,27):
            if j == k:
                continue
            else:
                s -= c[k-1] * (d - i+1 - last_day[k-1])
        if s > max_score:
            ans = j
            max_score = s
    last_day[ans-1] = i+1
    print(ans)
    k += max_score

Measured memory_bytes: 68564
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
//    freopen("input.txt", "r", stdin);
    string s;
    cin >> s;
    int mvs = s.size() - 2;
    if (s[0] == s.back()) mvs--;
    if (mvs % 2) cout << "First";
    else cout << "Second";
    return 0;
}

Measured memory_bytes: 512
#include <iostream>
#include <vector>
#include <limits>
#include <cmath>

using namespace std;


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

	vector<long long> x(N), y(N);

	for (int i = 0; i < N; i++)
		cin >> x[i] >> y[i];

	long long ans = numeric_limits<long long>::max();

	for (int left = 0; left < N; left++) {
		for (int right = 0; right < N; right++) {
			for (int top = 0; top < N; top++) {
				for (int bottom = 0; bottom < N; bottom++) {

					int cnt = 0;
					for (int i = 0; i < N; i++) {
						if (x[left] <= x[i] && x[i] <= x[right]
							&& y[i] <= y[top] && y[bottom] <= y[i])
							cnt++;
					}
					if (cnt >= K) ans = min(ans, (x[right] - x[left])*(y[top] - y[bottom]));
				}
			}
		}
	}

	cout << ans << endl;

	return 0;
}

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

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

int main(){
  int n, x[100], y[100], h[100];

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

  int mind = 1e9;
  for(int cx=0; cx<101; ++cx){
    for(int cy=0; cy<101; ++cy){

      int H = 1;
      for(int i=0; i<n; ++i){
	if(h[i] != 0){
	  int d = abs(x[i] - cx) + abs(y[i] - cy);
	  H = h[i] + d;
	  break;
	}
      }
      for(int i=0; i<n; ++i){
	int d = abs(x[i] - cx) + abs(y[i] - cy);
	if(h[i] != 0){
	  if(H != h[i] + d) break;
	}else{
	  if(H - d > 0) break;
	}
	if(i == n-1){
	  cout << cx << " " << cy << " " << H << endl;
	  return 0;
	}
      }

    }
  }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define sync ios_base::sync_with_stdio(false), cin.tie(NULL)
#define F first
#define S second
#define eb emplace_back
#define pb emplace_back
#define mt make_tuple
#define gcd __gcd
#define PI 3.141592653589
// Input
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define llin(a) cin >> a
#define inl(a) scanf("%lld",&a)
#define read(v,i,n) for(i=0;i<n;i++)in(v[i])
#define twod(mat,i,j,n,m) rep(i,n){rep(j,m)in(mat[i][j]);}
#define sc(ch) scanf("%c",&ch)
#define sstr(str) scanf("%s",str)
// Output
#define pr(a) printf("%d ",a)
#define pr2(a,b) printf("%d %d\n",a,b)
#define pr3(a,b,c) printf("%d %d %d\n",a,b,c)
#define out(a) printf("%d\n",a)
#define outl(a) printf("%lld\n",a)
#define llpr(a) cout << a << " "
#define llout(a) cout << a << "\n"
#define yes printf("YES\n")
#define no printf("NO\n")
#define lin printf("\n")
// Iterator
#define lp(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define all(vec) vec.begin(),vec.end()
#define lower(v,k) lower_bound(v.begin(),v.end(),k)-v.begin()
#define upper(v,k) upper_bound(v.begin(),v.end(),k)-v.begin()
#define tf(mytuple) get<0>(mytuple)
#define ts(mytuple) get<1>(mytuple)
#define tt(mytuple) get<2>(mytuple)
// Debug
#define dbg(v,i,n) for(i=0;i<n;i++)pr(v[i]); lin
#define what(x) cerr << #x << " : " << x << "\n"
#define ck printf("continue\n")
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
	cerr << *it << " = " << a << "\n";
	err(++it, args...);
}
// Data Type
#define ll long long int
#define ii pair<int,int>
#define pli pair<ll,int>
#define triple tuple<int,int,int>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<pair<int,int>,int> >
#define vvii vector<vector<pair<int,int> > >
#define M 1000000007
#define P 3000  //check the limits, man
#define N 55
const ll INF = 1e18;
ll dp[N][2*P];
int main()
{
    int i,n,a,j;
    in2(n,a);
    vi v(n);
    read(v,i,n);
    rep(i,n)
        v[i]-=a;
    dp[0][P]=1;
    rep(i,n)
    {
        a=v[i];
        for(j=-P;j<P;j++)
        {
            dp[i+1][j+P]=dp[i][j+P];
            if(j-a+P>=0 && j-a+P<2*P)
                dp[i+1][j+P]+=dp[i][j-a+P];
        }
    }
    llout(dp[n][P]-1);
}


Measured memory_bytes: 2560
N = int(input())
AC = 0
WA = 0
TLE = 0
RE = 0

for i in range(N):
    s = str(input())
    if s == 'AC':
        AC += 1
    elif s == 'WA':
        WA += 1
    elif s == 'TLE':
        TLE += 1
    else:
        RE += 1

print("AC x " + str(AC))
print("WA x " + str(WA))
print("TLE x " + str(TLE))
print("RE x " + str(RE))

Measured memory_bytes: 9212
MOD = 10 ** 9 + 7

N, M = map(int, input().split())
S = list(map(int, input().split()))
T = list(map(int, input().split()))

dp = [[1] * (M+1) for _ in range(N+1)]

for i, s in enumerate(S):
    for j, t in enumerate(T):
        dp[i+1][j+1] = dp[i+1][j] + dp[i][j+1]
        if s != t:
            dp[i+1][j+1] -= dp[i][j]
        dp[i+1][j+1] %= MOD

print(dp[N][M])

Measured memory_bytes: 98708
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;

vector<int> neighbors[100005];
int root[100005];

void dfs(int i, int start) {
  root[i] = start;
  // visit all i neighbors
  for (int j : neighbors[i]) {
    if (root[j] == -1) {
      dfs(j, start);
    }
  }
}

int main() {
  int n, m, x, y, q;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &x, &y);
    neighbors[x].push_back(y);
    neighbors[y].push_back(x);
  }
  memset(root, -1, sizeof(root));
  for (int i = 0; i < n; i++) {
    if (root[i] == -1)
      dfs(i, i);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &x, &y);
    if (root[x] == root[y])
      printf("yes\n");
    else
      printf("no\n");
  }
}



Measured memory_bytes: 11604
#include <bits/stdc++.h>
using namespace std;
// clang-format off

using LL = long long;
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
 
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
// clang-format on

void solve(long long N, std::vector<long long> h) {
  // solver code

  LL c[N];

  c[0] = 0;
  c[1] = abs(h[1] - h[0]);

  for (int i = 2; i < N; i++) {
    c[i] = min(c[i - 2] + abs(h[i] - h[i - 2]), c[i - 1] + abs(h[i] - h[i - 1]));
  }

  cout << c[N - 1] << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> h(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &h[i]);
  }
  solve(N, std::move(h));
  return 0;
}


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

int main() {
  int N, K;
  std::cin >> N >> K;
  if (N * (N - 1) / 2 - (N - 1) < K) {
    std::cout << -1 << std::endl;
  } else {
    bool connected[100][100];
    for (int i = 0; i < N; ++i)
      for (int j = 0; j < N; ++j)
	connected[i][j] = false;
    int m = 0;
    for (int i = 1; i < N; ++i) {
      connected[0][i] = true;
      connected[i][0] = true;
      ++m;
    }
    K -= (N - 1) * (N - 2) / 2;
    for (int i = 1; i < N && K < 0; ++i) {
      for (int j = i + 1; j < N && K < 0; ++j) {
	connected[i][j] = true;
	connected[i][j] = true;
	++K;
	++m;
      }
    }
    std::cout << m << std::endl;
    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
	if (!connected[i][j]) continue;
	std::cout << (i + 1) << " " << (j + 1) << std::endl;
      }
    }
  }
  return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
const double PI = acos(-1);
const double EPS = 1e-9;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

// O( ( |V| + |E| ) * log|V| )
const int W = 0, B = 1;
//重み付き有効グラフの隣接リスト表現
vector<int> dijkstra(const vector<vector<pair<int, int>>> &g, int s) {
	int n = g.size();
	priority_queue<pair<int, int>>pq;// cost, node
	vector<int>color(n, W), d(n, INF);
	d[s] = 0;
	pq.push(make_pair(0, s));

	while (!pq.empty()) {
		pair<int, int>f(pq.top()); pq.pop();// d[v], v
		color[f.second] = B;

		if (d[f.second] < (-1)*f.first)continue; // 最小値を取り出し、それが最短でなければ無視

		rep(i, 0, g[f.second].size()) {
			pair<int, int>v(g[f.second][i]);
			if (color[v.first] != B&&d[f.second] + v.second < d[v.first]) {
				d[v.first] = d[f.second] + v.second;
				pq.push(make_pair(d[v.first] * (-1), v.first)); // priority_queueはデフォルトで大きい値を優先するため-1を掛ける
			}
		}
	}
	return d;
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N; cin >> N;
	vector<vector<pair<int, int>>> adj(N, vector<pair<int, int>>());
	rep(i, 0, N-1) {
		int a, b, c; cin >> a >> b >> c;
		a--, b--;
		adj[a].emplace_back(make_pair(b, c));
		adj[b].emplace_back(make_pair(a, c));
	}

	int Q, K; cin >> Q >> K;
	K--;
	auto d = dijkstra(adj, K);
	rep(i, 0, Q) {
		int x, y; cin >> x >> y;

		x--, y--;
		cout << d[x] + d[y] << endl;
	}
	return 0;
}

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

int main(){
    int a, b, c;
    int k;
    cin >> a >> b >> c;
    cin >> k;
    vector<int>x ={a,b,c};
    sort(x.begin(), x.end());
    for(int i=0; i<k; i++){
        x[2]*=2;
    }
    cout << x[1]+x[2]+x[0] << endl;
}

Measured memory_bytes: 256
# ABC146 D - Coloring Edges on Tree
from collections import deque

n = int(input())
E = [[] for _ in range(n + 1)]
edge_order = []

for i in range(n-1):
    a,b=map(int,input().split())
    E[a].append(b)
    edge_order.append(b)

# 頂点1からBFS
q = deque([1])
color = [0] * (n+1) 
while q:
    V = q.popleft()
    c = 1
    for nv in E[V]:
        if c == color[V]:
            c += 1
        color[nv] = c
        c += 1
        q.append(nv)

print(max(color))
for i in edge_order:
    print(color[i])



Measured memory_bytes: 18548
#include <iostream>
#include <string>
#include <vector>

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

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main() {
  long long a, b, x;
  cin >> a >> b >> x;

  if (a == 0) cout << b / x + 1 << endl;
  else cout << (b / x) - ((a - 1) / x) << endl;

  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;

using namespace std;


void solve()
{
    int n;
    scanf("%d", &n);
    int a[10][10] = { 0 };
    int i;
    for (i = 1; i <= n; i++) {
        int curr = i;
        int tmp = 0;
        while (curr) {
            tmp = curr % 10;
            curr /= 10;
        }
        a[tmp][i % 10]++;
    }
    ll ans = 0;
    int j;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            if (i == j) {
                ans+=(ll)a[i][i] * a[i][i];
            }
            else {
                ans += a[i][j] * a[j][i];            
            }
        }
    }
    printf("%lld\n", ans);

    return;
}

int main(int argc, char* argv[])
{
#if 1
    solve();
#else
    int T; scanf("%d", &T);
    while(T--) {
        solve();
    }
#endif
    return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int , int> i_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<string>> ve_ves;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<(int)(n);i++)
#define rep2(i,n) for(int i=2;i<(int)(n);i++)
#define repk(i,k,n) for(int i=k;i<(int)(n);i++)
#define fs first
#define sc second
#define pub push_back
#define pob pop_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define maxel(a) max_element(all(a))
#define acc accumulate
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)

char maze[51][51];
int H, W;
int sx, sy;//start
int gx, gy;//goal

int d[51][51];//各pointまでの最短距離

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

int bfs(){
    queue<i_i> que;
    rep(i,H) rep(j,W) d[i][j] = INF;//初期化
    que.push(i_i(sx,sy));  //que->start
    d[sx][sy] = 0; //startは距離0

    //queが空になるまでloop
    while(que.size()){
        //queの先頭を取りだす
        i_i p = que.front(); que.pop();
        //getがgoalならend
        if(p.fs == gx and p.sc == gy) break;

        rep(i,4) {
            int nx = p.fs + dx[i];
            int ny = p.sc + dy[i];
            if(0 <= nx and nx < H and 0 <= ny and ny < W){
                if(maze[nx][ny] == '#') continue;
                if(d[nx][ny] != INF) continue;

                //それ以外なら移動可能
                que.push(i_i(nx,ny));
                d[nx][ny] = d[p.fs][p.sc] + 1;
            }
        } 
    }
    return d[gx][gy];
}



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

    cin >> H >> W;
    int cnt = 0;
    rep(i,H) rep(j,W) {
        cin >> maze[i][j];
        if(maze[i][j] == '.') cnt++;
    }
    sx = 0, sy = 0, gx = H - 1, gy = W - 1;
    int minw = bfs();
    if(minw == INF) cout << -1 << endl;
    else cout << cnt - minw - 1 << endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<functional>
#include<vector>
#include<set>
#include<queue>
#include<list>
#include<stdio.h>
#include<stdlib.h>
#include<map>
#include<cstdio>
#include <iomanip>

#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()

typedef long long ll;

using namespace std;

int count(string s, char c) {
	
	int cnt = 0;
	for (char x : s) {
		if (x == c)cnt++;
	}
	return cnt;
}

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

	int ans = 10000;

	for (char c : s) {
		string buf = s;
		int cnt = 0;

		while (count(buf, c) < buf.length()) {
			string temp = "";

			for (int i = 0; i<buf.length() - 1; i++) {
				if (buf[i] == c || buf[i + 1] == c) {
					temp += c;
				}
				else {
					temp += '@';
				}
			}

			cnt++;

			buf = temp;
		}
		ans = min(ans, cnt);
	}

	cout << ans << endl;
}


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

Measured memory_bytes: 256
import sys

if __name__ == '__main__':
    data = []
    for line in sys.stdin:
        data.append([int(x) for x in line.strip().split(' ')])
        if len(data) == 2:
            choice_a = data[0]
            choice_b = data[1]
            data = []

            #print(choice_a)
            #print(choice_b)

            # hit&blow?????????
            hit = 0
            blow = 0
#            for i, d in enumerate(choice_a):
#                if d in choice_b:
#                    if choice_a[i] == choice_b[i]:
#                        hit += 1
#                    else:
#                        blow += 1


            for i, d in enumerate(choice_a):
                if d in choice_b:
                    if d == choice_b[i]:
                        hit += 1
                    else:
                        blow += 1

            # ???????????¨???
            print('{0} {1}'.format(hit, blow))

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

using namespace std;
typedef long long ll;

// 整数nを素因数分解し, 分解結果のmapを返す;
// key = 素因数, value = 素因数の指数
map<ll, int> trial_division(ll n) {
    map<ll, int> res;
    // 2の場合のみ先行して処理 (次以降の更新効率を上げるため)
    while(n % 2 == 0) {
        res[2]++;
        n /= 2;
    }

    // 以降, 奇数のみ試し割りを行う
    for(ll f = 3; f * f <= n; f += 2) {
        while(n % f == 0) {
            res[f]++;
            n /= f;
        }
    }

    // 5以上の素数の場合, 上記までに一切割り切れないため, ここでresへ追加する.
    if(n != 1) res[n] = 1;

    return res;
}

#define FOR(i,a,n) for(ll i = ((ll) a); i < ((ll) n); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
    int n;
    cin >> n;
    cout << n << ":";
    for (auto i : trial_division(n)) {
        while(i.second > 0) {
            cout << " " << i.first;
            i.second--;
        }
    }
    cout << "\n";
    return 0;
}



Measured memory_bytes: 3112
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef pair<double,int> P;
P t[8],u[8],v[8],w[6];

void p(P x){
  cout<<x.second<<' ';
  printf("%.2f\n",x.first);
}


int main(){
  for(int i=0;i<8;i++){
    cin>>t[i].second;
    cin>>t[i].first;
  }
  sort(t,t+8);
  w[0]=t[2];w[1]=t[3];
  p(t[0]);
  p(t[1]);


  for(int i=0;i<8;i++){
    cin>>u[i].second;
    cin>>u[i].first;
  }
  sort(u,u+8);
  w[2]=u[2];w[3]=u[3];
  p(u[0]);
  p(u[1]);

  for(int i=0;i<8;i++){
    cin>>v[i].second;
    cin>>v[i].first;
  }
  sort(v,v+8);
  w[4]=v[2];w[5]=v[3];
  p(v[0]);
  p(v[1]);

  sort(w,w+6);
  p(w[0]);
  p(w[1]);
  return 0;
}

Measured memory_bytes: 1292
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int maxn = 1e3+3;
queue<int> a[maxn];
int g[maxn];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;++i)
	{
		for(int j=1;j<n;++j)
		{
			int X;
			cin>>X;
			a[i].push(X);
		}
	}
	for(int i=1;i<=n;++i)
	{
		g[i] = a[i].front();
	}
	int day = 0;
	int ecnt = 0;
	while(ecnt < n)
	{
		day++;
		set<pair<int,int>> this_day_pairs;
		for(int x=1;x<=n;++x)
		{
			int y = g[x];
			if(y == 0) continue;
			if(g[y] == x)
			{
				int xx = x,yy = y;
				if(xx>yy)swap(xx,yy);
				this_day_pairs.insert(make_pair(xx,yy));
			}
		}
		if(this_day_pairs.empty())
		{
			cout<<-1<<endl;
			return 0;
		}
		for(auto p:this_day_pairs)
		{
			int x = p.first;
			int y = p.second;
			a[x].pop();
			a[y].pop();
			g[x] = 0;
			g[y] = 0;
			if(!a[x].empty())
			{
				g[x] = a[x].front();
			}else
				ecnt++;
			if(!a[y].empty())
			{
				g[y] = a[y].front();
			}else
			{
				ecnt++;
			}
		}
	}
	cout<<day<<endl;
}

Measured memory_bytes: 4736
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for(int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
#define INF (int) 1e9 + 100
#define LLINF (ll) 1e18
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

int a;

int main() {
	fastio; cin >> a;
  cout << a + a*a + a*a*a;
}


Measured memory_bytes: 3612
import java.util.*;

// ABC 62-D
// http://abc062.contest.atcoder.jp/tasks/arc074_b

public class Main {
	
	public static void main (String[] args) {
		Scanner in = new Scanner(System.in);
		
		int n = in.nextInt();
		
		int answer = 0;
		for (int i = 0; i < n; i++) {
			int s = in.nextInt();
			int e = in.nextInt();
			answer += e + 1 - s;
		}
		
		System.out.println(answer);
	}
}

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

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

#define INF 1 << 30
#define REP(i,n) for(ll i=0; i<(int)(n); i++)
#define FOR(i,k,n) for(ll i=(k);i<(int)(n);i++)



vector<bool> gen_sosuu(vector<bool> sosuu){
  int size = sosuu.size();
  REP(i,size){
    sosuu[i] = true;
  }
  sosuu[0] = false;
  sosuu[1] = false;
  sosuu[2] = true;
  FOR(i,2,sqrt(size)+1){
    if(sosuu[i] == false) continue;
    for(int j = 2; i*j<size;j++){
      sosuu[i*j] =false;
    }
  }
  return sosuu;
}

int d,n;

int main(){
  cin >> d >> n;
  int a = 1;
  REP(i,d) a *= 100;
  cout << a*(n+(n == 100)) << endl;
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int w = Integer.parseInt(sa[0]);
		int h = Integer.parseInt(sa[1]);
		int[] p = new int[w + 1];
		for (int i = 0; i < w; i++) {
			p[i] = Integer.parseInt(br.readLine());
		}
		int[] q = new int[h + 1];
		for (int i = 0; i < h; i++) {
			q[i] = Integer.parseInt(br.readLine());
		}
		br.close();

		p[w] = 1000000000;
		q[h] = 1000000000;
		Arrays.parallelSort(p);
		Arrays.parallelSort(q);

		int i = 0;
		int j = 0;
		long ans = 0;
		while (i < w || j < h) {
			if (p[i] < q[j]) {
				ans += (long) p[i] * (h - j + 1);
				i++;
			} else {
				ans += (long) q[j] * (w - i + 1);
				j++;
			}
		}
		System.out.println(ans);
	}
}


Measured memory_bytes: 38932
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  int d[N];
  for(int i=0;i<N;i++) cin >> d[i];

  sort(d,d+N);
  int count=1;
  for(int i=0;i<N-1;i++){
    if(d[i]<d[i+1])count += 1;
  }

  cout << count << endl;
  return 0;
}

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

using namespace std;

int main(){
    int N,Q,K;



    cin>>N>>K>>Q;

    int Points[N];

    for(int i=0;i<N ;++i)
        Points[i]=K;

    for(int i=0;i<Q ;++i){
        int p;
        cin>>p;
        Points[p-1]++;
    }

    for(int i=0;i<N ;++i){
        if(Points[i]-Q>0)
            cout<<"Yes"<<"\n";
        else
            cout<<"No"<<"\n";
    }

    return 0;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll)x.size()
#define f first
#define s second
const ll MOD = 1e9+7;
const ll INF = 1e9;
const ll MAXN = 100100;

int H,W,h,w;
int G[MAXN];

int main(){
  cin>>H>>W>>h>>w;
  if (H%h==0&&W%w==0){
    cout<<"No\n";
    return 0;
  }
  int extras = (H%h*W) + (W%w*H) - (H%h)*(W%w);
  // cout<<extras<<'\n';

  int losses = (H/h)*(W/w);
  // cout<<losses<<' '<<extras<<'\n';
  int m = (losses + extras)/extras;
  cout<<"Yes\n";
  for (int i=1;i<=H;++i){
    for (int j=1;j<=W;++j){
      if (i%h==0&&j%w==0){
        cout<<-(h*w-1)*m - 1<<' ';
      }else{
        cout<<m<<' ';
      }
    }
    cout<<'\n';
  }

}


Measured memory_bytes: 1280
from collections import deque

n = int(input())
a = list(map(int,input().split()))
b_deque = deque()

for i in range(len(a)):
    if (n-i)%2 == 1:
        b_deque.appendleft(a[i])
    else:
        b_deque.append(a[i])
print(*b_deque)

Measured memory_bytes: 25412
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
using namespace std;
const int N=200005;
struct node
{
	int id,num,ty;
}g[N];
bool operator <(node a,node b)
{
	if(a.num!=b.num)
		return a.num<b.num;
	return a.id<b.id;
}
multiset<int> st;
int n,i,j,k,x,y,a[N],b[N],cola[N],colb[N];
long long ans,w;
bool cmp(node a,node b)
{
	return a.num<b.num;
}
int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)
	{
		scanf("%d%d",a+i,b+i);
		g[2*i-1]=(node){i,a[i],0};
		g[2*i]=(node){i,b[i],1};
	}
	sort(g+1,g+1+2*n,cmp);
	for(i=n+1;i<=2*n;++i)
		if(g[i].ty==0)
			cola[g[i].id]=1;
		else
			colb[g[i].id]=1;
	for(i=1;i<=n;++i)
	{
		ans+=g[i].num;
		st.insert(g[i].num);
	}
	for(i=1;i<=n;++i)
		if(cola[i]==colb[i])
		{
			printf("%lld",ans);
			return 0;
		}
	for(i=1,x=y=0;i<=n;++i)
		if(cola[i]==0)
			++x;
		else
			++y;
	if(!x||!y)
	{
		printf("%lld",ans);
		return 0;
	}
	w=1ll<<60;
	for(i=1;i<=n;++i)
		if(cola[i]==1)
		{
			if(y==1)
				j=*(--st.end());
			else
			{
				st.erase(st.find(b[i]));
				j=*(--st.end());
				st.insert(b[i]);
			}
			w=min(w,ans+a[i]-j);
		}
		else
		{
			if(x==1)
				j=*(--st.end());
			else
			{
				st.erase(st.find(a[i]));
				j=*(--st.end());
				st.insert(a[i]);
			}
			w=min(w,ans+b[i]-j);
		}
	printf("%lld",w);
	return 0;
}


Measured memory_bytes: 9600
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
using namespace std;

const int INF = 1e+9;
typedef pair<string, int> P;

int ans = 0;
int s_size;

string change(string s, int a, int b){
	if((s[a] == 'r' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'r'))
		s[b] = s[a] = 'b';
	else if((s[a] == 'b' && s[b] == 'g') || (s[a] == 'g' && s[b] == 'b'))
		s[b] = s[a] = 'r';
	else
		s[b] = s[a] = 'g';
	
	return s;
}

int main() {
	std::ios::sync_with_stdio(false);
    std::cin.tie(0);
	string str;
	while(cin >> str){
		if(str == "0")
			break;
		s_size = str.size();
		ans = INF;
		queue<P> que;
		map<string, bool> visited;
		que.push(P(str, 0));
		while(!que.empty()){
			P p = que.front();
			que.pop();
			string s = p.first;
			if(visited[s])
				continue;
			visited[s] = true;
			bool flag = false;
			for(int i = 0; i < s_size - 1; ++i){
				if(s[i] != s[i + 1])
					break;
				if(i == s_size - 2){
					ans = min(ans, p.second);
					flag = true;
					break;
				}
			}
			if(flag)
				continue;
			for(int i = 0; i < s_size - 1; ++i){
				if(s[i] != s[i + 1]){
					string t = change(s, i, i + 1);
					que.push(P(t, p.second + 1));
				}
			}
		}
		end:
		if(ans == INF)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}
	return 0;
}

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

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main()
{
    COMinit();
    long long n, m, k;
    cin >> n >> m >> k;
    long long ans = (m * m * n * (n - 1ll) * (n + 1ll) / 6ll) % MOD;
    ans = (ans + n * n * m * (m - 1ll) * (m + 1ll) / 6ll) % MOD; 
    cout << (ans * COM(n * m -2, k-2)) % MOD << endl;
}

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

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

Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
priority_queue<int>Q;
int n,m;
struct node
{
	int x,y;
}a[N];
int cmp(node x,node y)
{
	if(x.x==y.x)return x.y>y.y;
	return x.x<y.x;
}
int main()
{
	scanf("%d%d",&n,&m);
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		if(x<=m)a[++cnt].x=x,a[cnt].y=y;
	}
	sort(a+1,a+cnt+1,cmp);
	long long ans=0;int t=1;
	for(int i=1;i<=m;i++)
	{
		for(int j=t;j<=n;j++)
			if(a[j].x==i)Q.push(a[j].y);else{t=j;break;}
		if(!Q.empty())
		{
			ans+=Q.top();
			Q.pop();
		}
	}
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 1660
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<functional>
#include<string>
#include<iomanip>
#include<utility>
#include<string>
#include<map>
#include<unordered_map>
#include<queue>

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; }

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int gcd(int a, int b){return b ? gcd(b, a%b) : a;}
int lcm(int a, int b){return a*b / gcd(a, b);}


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

    ll ans = 0;
    bool flag = true;
    for(int i = 0; i < n; i++){
        if(a[i]%2 == 1) flag = false;
    }

    if(flag == true){
        if(p == 0){
            ans = pow(2, n);
        }
        else{
            ans = 0;
        }
    }
    else{ 
        ans = pow(2, n-1);
    }

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


Measured memory_bytes: 3972
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <random>
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (int) n; i++)

int main() {
    int n,m; cin >> n >> m;
    vector<pair<int,int>> road(m);
    rep(i,m) {
        cin >> road[i].first >> road[i].second;
        road[i].first--; road[i].second--;
    }
    vector<int> cnt(n,0);
    rep(i,m) {
        cnt[road[i].first]++;
        cnt[road[i].second]++;
    }
    rep(i,n) {
        cout << cnt[i] << endl;
    }
    return 0;
}

Measured memory_bytes: 256
import java.util.*;
 
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int hako[]  = new int[a];
        int cont = 0;
        int flag = 1;

      
      for(int i = 0;i<a;i++){
        hako[i] = sc.nextInt();
      }
      
      while(flag==1){
        for(int i = 0;i<a;i++){
          if(hako[i]%2==0){
            hako[i] =hako[i]/2; 
          }else{flag=0;}
        }
        if(flag==1){cont++;}
      }
 
      System.out.print(cont);
    }
}

Measured memory_bytes: 23124
#include <stdio.h>

int main(void)
{
    char S[100];
    int N,i,count=0;
    
    scanf("%d",&N);
    scanf("%s",S);

    for(i=0; i<N-2; i++){
        if( S[i] == 'A' && S[i+1] == 'B' && S[i+2] == 'C') count++;
    }
    
    printf("%d\n",count);

    return 0;
}

Measured memory_bytes: 128