akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;
static const int MAX=10000;
static const int INFTY = (1<<21);

vector< pair<int,int> >M[MAX]; //?????\??¨
priority_queue< pair<int,int> > PQ; //????????§??????, first???d[i], second???i??? ?????¨??¶?????????first???????????°
bool visited[MAX]; //???????¢??????????
int d[MAX]; //?????????????????¬

void dijkstra_implementsPQ(int n)
{
    //init
    for(int i=0;i<n;i++)
    {
        d[i]=INFTY;
        visited[i]=false;
    }
    //select node 0
    d[0]=0;
    PQ.push(make_pair(0,0));

    while(!PQ.empty())
    {
        //currently the top of PQ is the shortest one
        pair<int,int> temp=PQ.top();PQ.pop();
        int u=temp.second;

        visited[u]=true;
        if(d[u]<temp.first*(-1)) continue;

        //update
        for(int j=0;j<M[u].size();j++)
        {
            int v=M[u][j].first;
            if(visited[v]==true) continue;
            //else try updating
            if(d[v]>d[u]+M[u][j].second)
            {
                d[v]=d[u]+M[u][j].second;
                PQ.push(make_pair(d[v]*(-1),v));
            }
        }
    }
    //print
    for(int i=0;i<n;i++)  cout<<i<<' '<<(d[i]==INFTY?-1:d[i])<<endl;

}
int main()
{
    int n;
    cin>>n;

    for(int i=0;i<n;i++)
    {
        int u,k;
        cin>>u>>k;
        for(int j=0;j<k;j++)
        {
            int v,d;
            cin>>v>>d;
            M[u].push_back(make_pair(v,d));
        }
    }

    dijkstra_implementsPQ(n);
}

Measured memory_bytes: 3344
use std::collections::VecDeque;
use std::io::*;
use std::str::FromStr;

#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let h = read::<i64>();
    let w = read::<i64>();
    let mut field = vec![vec![]; h as usize];
    let mut que = VecDeque::<(i64, i64, i64)>::new();
    let mut visit = vec![vec![false; w as usize]; h as usize];
    let mut dists = vec![vec![1i64 << 30; w as usize]; h as usize];
    for y in 0..h {
        field[y as usize] = read::<String>().chars().collect::<Vec<char>>();
        for x in 0..w {
            if field[y as usize][x as usize] == '#' {
                que.push_back((y, x, 0));
                dists[y as usize][x as usize] = 0;
            }
        }
    }
    let mut ans = 0;
    while let Some((y, x, cost)) = que.pop_front() {
        if visit[y as usize][x as usize] {
            continue;
        }
        visit[y as usize][x as usize] = true;
        ans = cost;
        for i in 0..4 {
            let dx = [1, 0, -1, 0];
            let dy = [0, 1, 0, -1];
            let nx = x + dx[i];
            let ny = y + dy[i];
            let ncost = cost + 1;
            if nx < 0
                || nx >= w
                || ny < 0
                || ny >= h
                || visit[ny as usize][nx as usize]
                || ncost >= dists[ny as usize][nx as usize]
            {
                continue;
            }
            dists[ny as usize][nx as usize] = ncost;
            que.push_back((ny, nx, ncost));
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 41208
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <tuple>

using ll = long long;
using namespace std;
#define modmod 1000000007

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

int main(){
    ll X;
    cin >> X;
    /*
    if(X<=6){
        cout << 1 << endl;
        return 0;
    }else if(X<=11){
        cout << 2 << endl;
        return 0;
    }
    */
    ll count;
    count = (X/11)*2;
    int amari = X%11;
    //printf("%d\n",amari);
    if(amari == 0){
        count += 0;
    }else if(amari <= 6){
        count +=1;
    }else{
        count +=2;
    }

    cout << count << endl;

    return 0;
}


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

int main() {
  ll N = 0,ans = 0;
  cin >> N;
  map<ll, ll> mp;
  rep(i,N){
    ll tmp = 0;
    cin >> tmp;
    mp[tmp]++;
  }
  for (auto p : mp){
    if(p.second % 2 != 0){
      ans++;
    }
  }
  cout << ans;
}

Measured memory_bytes: 9708
use std::io::*;
use std::str::FromStr;

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

type Area = (usize, usize);

fn main() {
    let token_str: String = read();

    let mut index_stack: Vec<usize> = vec![];
    let mut area_stack: Vec<Area> = vec![];

    let mut sum: usize = 0;

    for (i, token) in token_str.chars().enumerate() {
        match &token.to_string()[..] {
            "\\" => {
                index_stack.push(i);
            }
            "/" => {
                let j = index_stack.pop();
                match j {
                    Some(j) => {
                        let mut a = i - j;
                        sum += a;
                        loop {
                            let area_tuple = area_stack.pop();
                            match area_tuple {
                                Some(area) => {
                                    let (k, b) = area;
                                    if j < k {
                                        a += b;
                                    } else {
                                        area_stack.push((k, b));
                                        break;
                                    };
                                }
                                _ => {
                                    break;
                                }
                            };
                        }

                        area_stack.push((j, a));
                    }
                    _ => {}
                };
            }
            _ => {}
        }
    }

    println!("{}", sum);

    let area_stack_len = area_stack.len();
    if area_stack_len != 0 {
        print!("{} ", area_stack_len);
    } else {
        print!("{}", area_stack_len);
    }
    for i in 0..area_stack_len {
        let area = area_stack[i];
        if i != area_stack_len - 1 {
            print!("{} ", area.1);
        } else {
            print!("{}", area.1);
        }
    }
    println!("");
}



Measured memory_bytes: 5040
awk -M '\
function gcd(a, b,  tmp)
{
    if(a<b){tmp=a;a=b;b=tmp}
    if(b == 0)return a;
    return gcd(b, a%b);
}
function lcm(a, b)
{
    return a * b / gcd(a, b);
}
NR==1{N=$1}
NR>1{A[NR-1]=$1}
END{
    ans = 1
    for(i in A){
        ans = lcm(ans, A[i])
    }
    print ans
}'

Measured memory_bytes: 4544
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e3 + 5;
ll f[N][N];
pair<int, int> p[N];
ll cal(int cnt, int l, int r) {
    if(l > r) return 0;
    if(~f[l][r]) return f[l][r];
    ll ans = 1LL * p[cnt].first * abs(p[cnt].second - l) + cal(cnt + 1, l + 1, r);
    ans = max(ans, 1LL * p[cnt].first * abs(p[cnt].second - r) + cal(cnt + 1, l, r - 1));
    return f[l][r] = ans;
}
int main() {
    int n;
    scanf("%d", &n);
    for(int i = 1, x; i <= n; i++) {
        scanf("%d", &x);
        p[i] = {x, i};
    }
    sort(p + 1, p + n + 1,[](pair<int, int> a, pair<int, int> b) {
        return a.first > b.first;
    });
    memset(f, -1, sizeof f);
    printf("%lld\n", cal(1, 1, n));
}


Measured memory_bytes: 35368
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

int main(){
    int n,k[101]={0},max_k=0;
    
    while(scanf("%d",&n)!=EOF){
        k[n]++;
    }
    
    for(int i=1;i<=100;i++){
        if(max_k<k[i]) max_k=k[i];
    }
    
    for(int i=1;i<=100;i++){
        if(max_k==k[i]) printf("%d\n",i);
    }
    
    return 0;
}

Measured memory_bytes: 1188
n = int(input())
a = list(map(int, input().split()))

i = 0
j = 0
bt = 0

while i < n:
  while j < n - i:
    if a[i+j] == i+1:
      bt += 1
      break
    if j + i == n-1:
      if i == 0:
        bt = -1
      j = n + 1
      i = n + 1
      
    j += 1
  i += 1
  
if bt != -1:
  bt = n - bt
  
print(bt)

Measured memory_bytes: 26268
using System;

namespace ABC166D
{
    class Program
    {
        static void Main(string[] args)
        {
            long N = long.Parse(Console.ReadLine());
            long a=0;long b=0; Boolean f=false;
            for(long i=-200; i<200; i++){
                for(long j=-200; j<200; j++){
                        if( N == i*i*i*i*i - j*j*j*j*j ){
                            b = j; a = i; f = true; break;
                        }
                    if(f) break;                    
                }               
            }
            Console.WriteLine("{0} {1}", a, b);
//            Console.WriteLine("{0}", a*a*a*a*a - b*b*b*b*b);
        }
    }
}


Measured memory_bytes: 27532
#include <bits/stdc++.h>
using namespace std;
int main(void){
    int N,K,R,S,P,score=0;
    string T;
    cin>>N>>K>>R>>S>>P>>T;
    int vec[N];
    for(int i=0;i<N;i++){
        vec[i]=1;
    }
    for(int i=0;i<N;i++){
        if(i-K>=0){
        if(T.at(i)==T.at(i-K)){
            if(vec[i-K]){
                vec[i]=0;
            }
            else{
                vec[i]=1;
                if(T.at(i)=='r'){
                    score+=P;
                }
                if(T.at(i)=='s'){
                    score+=R;
                }
                if(T.at(i)=='p'){
                    score+=S;
                }
            }
        }
        else{
            if(T.at(i)=='r'){
                    score+=P;
                }
                if(T.at(i)=='s'){
                    score+=R;
                }
                if(T.at(i)=='p'){
                    score+=S;
                }
        }
        }
        else{
            if(T.at(i)=='r'){
                    score+=P;
                }
                if(T.at(i)=='s'){
                    score+=R;
                }
                if(T.at(i)=='p'){
                    score+=S;
                }
        }
    }
    cout<<score<<endl;
}

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

int getDigit(int64_t x) {
  
  int count = 0;
  
  while (x) { x /= 10; count++; }
  
  return count;
  
}

int main() {
  
  int64_t N; cin >> N; int ans = 11;
  
  for (int64_t i = 1; i * i <= N; i++) {
    
    if (N % i != 0) continue;
    
    int64_t A = i, B = N / i;
    
    int tmpA = getDigit(A), tmpB = getDigit(B);
    
    int F = max(tmpA, tmpB);
    
    ans = min(ans, F);
    
  }
  
  cout << ans << endl;
  
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <cstring>
#include <vector>
#include <map>
#include <cmath>
#include <climits>

//find out the use of the below
#include <list>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> set_t;
#define mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pdd pair<double,double>
#define pb push_back
#define x first
#define y second
#define sqr(x) ((x)*(x))
#define EPS 1e-100
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
#define pi acos(-1)

int main(){
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt","w", stdout);
    int l, r;
    cin >> l >> r;
    if (r-l >= 2019){
        cout << "0";
        return 0;
    }
    int ans = 2020;
    for (long long i = l; i <= r; ++i){
        for (int j = i+1; j <= r; ++j){
            ans = min(ans, (int)((i*j)%2019));
        }
    }
    cout << ans;
    return 0;
}
/* BEFORE submitting, check for:
 *  1. Integer overflow
 *  2. Float/Double precision errors
 *  3. Array index out of bounds
 *  4. Edge cases.(n=1, n=max, etc.)
 *  5. Time complexity. No TLEs
 */



Measured memory_bytes: 384
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;

void PUNKS_NOT_DEAD() {
    int h, n;
    cin >> h >> n;
    int sum = 0;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        sum += x;
    }
    cout << (sum < h ? "No" : "Yes");
}

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    PUNKS_NOT_DEAD();
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(),x.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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;}
using ll = long long;
using P = pair<int,int>;

int n,m,k,h,w;
string s,t;

int main() {
  cin >>n >> m;
  cout << m*(m-1)/2+n*(n-1)/2 << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long N,M,fd;
    cin>>N>>M;

    if(N==1 && M==1) fd=1;
    else if(N==1 || M==1) fd=max(N,M)-2;
    else fd=(N-2)*(M-2);
    cout <<fd<< endl;

    return 0;
}


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

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

  int a, b, c, d, e, f, g, h;
  cin >> a >> b >> c >> d;
  e = c - (d - b);
  f = d + (c - a);
  g = e - (f - d);
  h = f + (e - c);
  cout << e << ' ';
  cout << f << ' ';
  cout << g << ' ';
  cout << h << '\n';
  return 0;
}

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

int N;
const int MAX_N = 100005;
int vals[MAX_N];

int main() {
  scanf("%d", &N);
  std::set<std::pair<int, int>> s;
  for (int i = 0; i < N; ++i) {
    scanf("%d", vals + i);
  }
  int colcount = 0;
  for (int i = 0; i < N; ++i) {
    auto iter = s.upper_bound({vals[i] - 1, MAX_N});
    if (iter == s.begin() || s.empty()) {
      colcount++;
      s.insert({vals[i], colcount});
    } else {
      --iter;
      int col = iter->second;
      s.erase(iter);
      s.insert({vals[i], col});
    }
  }
  printf("%d\n", colcount);
}


Measured memory_bytes: 5376
//aea ctmr
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
//v.erase(unique(v.begin(),v.end()),v.end());
#define f(i,a,b) for( ll i = a; i < b ; i++ ) 
#define af(i,a,b) for( ll i = a; i >= b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < b ; i+= k )
#define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define PI 3.14159265358979323846
 
typedef long double ld;
typedef long long ll;
const ll N = 100500;
const ll mod = 1e9 + 7;
using namespace std;
 
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << endl;}
 
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1<<" | ";
  __f(comma+1, args...);}
#else
#define trace(...)
#endif




int main(){
    fastio;
    
    int n;
    cin >> n;
    string s;
    cin >> s;
    int ans = 0;
    for(int i = 0; i < n - 2; i++){
        if(s[i] == 'A' and s[i + 1] == 'B' and s[i + 2] == 'C') ans++;
    }

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


Measured memory_bytes: 256
#include <iostream> 
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<iomanip>
#include<sstream>
#include<cmath>
#include<bitset>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(int i = 1;i <=  n; i++)
#define rrep(i,n) for(int i = n -1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(obj) rbegin(obj),rend(obj)
typedef long long int ll;
typedef long double ld;
const ll INF = (1 << 30);
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
    fill((T*)array, (T*)(array + N), val);
}
struct UnionFind {
    vector<int> par;
    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    int size(int x) {
        return -par[root(x)];
    }
};
int main() {
    ll n, m; cin >> n >> m; vector<int> a(m), b(m);
    rep(i, m) {
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
    }vector<ll> ans; ll res = n * (n - 1) / 2;
    UnionFind tree(n);
    rrep(i, m) {
        ans.push_back(res);
        int c = a[i], d = b[i];
        if (tree.same(c, d)) continue;
        ll sa = tree.size(c), sb = tree.size(d);
        res -= sa * sb;
        tree.unite(c, d);
    }reverse(ALL(ans));
    for (ll i : ans) cout << i << endl;
}

Measured memory_bytes: 3444
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
import bisect
import numpy as np

N, K = map(int, input().split())
H = np.array(list(map(int, input().split())))

dp = np.ones(N)*float("inf")
dp[0] = 0
dp[1] = abs(H[1] - H[0])

for i in range(2, N):
    start = max(0, i-K)
    dp[i] = np.min(dp[start:i] + abs(H[start:i] - H[i]))

print(int(dp[N-1]))

Measured memory_bytes: 22872
import collections
n = int(input())
s = [list(input()) for i in range(n)]

cnts = [collections.Counter(si) for si in s]
inter = set(s[0])
for i in range(1, n):
  inter = inter & set(s[i])
  
ans = {k:v for k, v in cnts[0].items() if k in list(inter)}
for i in range(1, n):
  for j in inter:
    if j in cnts[i].keys():
      ans[j] = min(cnts[i][j], ans[j])

moji = ""
ans = sorted(ans.items(), key=lambda x:x[0])
for k, v in ans:
  moji += "".join([k] * v)
print(moji)

Measured memory_bytes: 3436
from sys import stdin
A,B,K=[int(x) for x in stdin.readline().rstrip().split()]
for i in range(K):
    if i%2==0:
        if A%2 != 0:
            A-=1
        B+=(A/2)
        A-=(A/2)
    else:
        if B%2 != 0:
            B-=1
        A+=(B/2)
        B-=(B/2)
print(int(A),int(B))

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

int main(){
  while(1){
    int n;
    int count = 0;
    cin >> n;
    if(n==0){break;}
    vector<string> str(n);
    for(int i = 0;i<n;i++){
      cin >> str.at(i);
    }
    for(int i=0;i < n-1;i++){
      if (str.at(i) == "lu"&&str.at(i+1)=="ru"){
        count++;
      }
     else if (str.at(i) == "ru"&&str.at(i+1)=="lu"){
        count++;
      }
       else if (str.at(i) == "rd"&&str.at(i+1)=="ld"){
        count++;
      }
       else if (str.at(i) == "ld"&&str.at(i+1)=="rd"){
        count++;
      }
    }
    cout << count<< endl;
  }
}
      
    
    
     


Measured memory_bytes: 3132
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    string s;
    cin>>n;
    cin>>s;
    int c=0;
    for(int i=2;i<=n;i++){
        if((s[i-1]=='B' && s[i-2]=='A')&& s[i]=='C'){
            c++;
        }
    }
    cout<<c;
}


Measured memory_bytes: 3612
#include <bits/stdc++.h>
#define rep(i,N) for(int i=0;i<N;i++)
#define rep2(i,N) for(int i=1;i<=N;i++)
using namespace std;
long long  INF=1000000000000000000;
long long mod=1000000007;
using namespace std;


int main(){

long long N,A,B;
cin>>N>>A>>B;

long long x[N];

rep(i,N){
    cin>>x[i];
}
long long now=x[0];
long long ans=0;
for(int i=1;i<N;i++){
    if((x[i]-now)*A<B){
    ans+=(x[i]-now)*A;
    }
    else{
    ans+=B;
    }
now=x[i];

}

cout<<ans<<endl;
return 0;

}
/*
4 2 5
1 2 5 7

11


7 1 100
40 43 45 105 108 115 124

84

7 1 2
24 35 40 68 72 99 103

12

*/


Measured memory_bytes: 1024
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#include <cmath>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define uid uniform_int_distribution<lli>
#define lli long long int
#define ff first
#define ss second
#define pb push_back
#define popf pop_front()
#define popb pop_back()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for(int i=a;i<b;i++)
#define forr(x,arr) for(auto& x:arr)
#define sz(x) (x.size())
#define mod 1000000007
#define INF 1000000000000000010
#define vi vector<int>
#define vlli vector<lli>
#define endl "\n"
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template<class my>
inline my aabs (const my& a)   { return (a > 0)  ? a : -a;         }
inline lli gcd(lli a, lli b)   { return (b == 0) ? a : gcd(b,a%b); }
inline lli LSOne(lli k)        { return (k&(-k));                  }

inline int inv(lli o, int m = mod) {
  int b = m, u = 0, v = 1, t, a=o%m;
  while (a) {
    t = b / a;
    b -= t * a; swap(a, b);
    u -= t * v; swap(u, v);
  }
  if (u < 0) u += m;
  return u;
}
inline int add(const int &a, const int &b) { return a + b >= mod ? a + b - mod : a + b; }
inline int sub(const int &a, const int &b) { return a - b < 0 ? a - b + mod : a - b; }
inline int mul(const int &a, const int &b) { return int(a * 1ll * b % mod); }
inline int _div(const int &a, const int &b) { return mul(a, inv(b)); }
inline int pow(int a, int b) {
    int ans = 1;
    while (b) {
        if (b & 1) ans = mul(ans, a);
        a = mul(a, a);
        b >>= 1;
    }
    return ans;
}

int fac[100050], infac[100050];
int nCk (int n, int k)
{
    if(n<0||k<0||n<k) return 0;
    return mul(fac[n],mul(infac[k],infac[n-k]));
}

void solve()
{
    lli n=0, m=0, k=0, q=0, temp=0, u=0, v=0, ans=0, ans1=0, ans2=0, val=0, mini=INF, maxi=-INF;
    cin>>n;
    int arr[n+5]={};
    fol(i,0,n) cin>>arr[i];
    fol(i,1,n-1)
    {
        if((arr[i]>arr[i-1]&&arr[i]<arr[i+1])||(arr[i]<arr[i-1]&&arr[i]>arr[i+1])) ans++;
    }
    cout<<ans<<endl;
}

int main()
{
    fastio cout<<endl;
    int t=1;
    //cin>>t;
    while(t--)
        solve();
}



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

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

void chmin(ll& a, ll b) {
  if (a > b)
    a = b;
}
void solve(ll A, ll B, ll C) {

  if ((A % 2) * (B % 2) * (C % 2) == 0)
    cout << 0 << endl;
  else
    cout << min(A * B, min(B * C, C * A)) << endl;
}

int main(){
  // int 3E4  long 2E9  ll 9E18
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll A;
  cin >> A;
  ll B;
  cin >> B;
  ll C;
  cin >> C;

  solve(A, B, C);

  return 0;
}


Measured memory_bytes: 256
import sys
d={}
f=0
for s in sys.stdin:
    if f==0:
        a,b=map(int,s.split(","))
        if a==0 and b==0:
            x=sorted(set(d.values()))[::-1]
            f=1
        else:d[a]=b
    else:
        print x.index(d[int(s)])+1

Measured memory_bytes: 4208
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)

const ll mod=1e9+7;
ll mu(ll a,ll b){
  return (ll)a*b%mod;
}
ll ad(ll a,ll b){
  if((a+=b)>=mod)a-=mod;
  return a;
}
ll pw(ll a,ll b,ll c=1LL){
  for(;b;b>>=1,a=mu(a,a))if(b&1)c=mu(c,a);
  return c;
}
ll dv(ll a,ll b){
  return mu(a,pw(b,mod-2));
}

ll ans=0;
const int n_max=2e5+10;
vector<int> g[n_max];
ll pwall;
ll n;


int dfs(int v, int p){
  vector<int> kv;
  int sum=1;
  for(auto i:g[v]){
    if(i==p) continue;
    int k=dfs(i,v);
    sum+=k;
    kv.pb(k);
  }
  kv.pb(n-sum);
  ll out=1;
  rep(j,kv.size()){
    //    cout << kv[j] << endl;
    out = ad(out,ad(pw(2,kv[j])-1+mod,0));    
  }
  ll ok = ad(pwall - out +mod,0);
  
  if(sum!=1){
    ans = ad(ans,ok);
  }
  return sum;
}

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

  cin >> n;
  
  pwall=pw(2,n-1);
  rep(i,n-1){
    int a,b;
    cin >> a >> b;
    --a;--b;
    g[a].pb(b);
    g[b].pb(a);
  }
  dfs(0,-1);
  //  cout << "場合の数　" << ans << endl;
  cout << dv(ans,pw(2,n)) << endl;

  
  //  cout << endl << endl;
  //  cout << static_cast<double>(clock()) / CLOCKS_PER_SEC << "s"<<endl;
  return 0;
    

}


Measured memory_bytes: 24832
import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad

main :: IO ()
main = readLn >>= main'
    where
        main' n = replicateM n breads >>= putStrLn . solve

solve :: [[Int]] -> String 
solve = check 0 0 0
    where
        check nt nx ny ([t, x, y] : xs)
            | dt == dd || (dt > dd && even (dt - dd)) = check t x  y xs
            | otherwise = "No"
            where
                dt = abs $ t - nt
                dd = abs (x - nx) + abs (y - ny)
        check _ _ _ _ = "Yes"

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: 28028
#include <bits/stdc++.h>
using namespace std;

int main(){
  int A,B,C,X,Y,Z;
  cin >> A >> B >> C >> X;
  int n=0;
  for(int i=0;i<=C;i++){
    if(X-i*50==0){
      n++;
      break;
    }else{
      Y=X-i*50;
      for(int j=0;j<=B;j++){
        if(Y-j*100==0){
          n++;
          break;
        }else{
          Z=Y-j*100;
          for(int k=0;k<=A;k++){
            if(Z-k*500==0){
              n++;
              break;
            }
          }
        }
      }
    }
  }
  cout << n << endl;
  return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
//#include <ext/numeric>
using namespace std;
//using namespace __gnu_cxx;
#define ll  long long int
#define bug cout <<"----------------------\n"


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll n ;

    cin >> n ;

    vector < ll > arr ;

    for(int i  = 0  ; i<n ; ++i)
    {
        ll  val ;
        cin >> val ;
        arr.push_back(val);

    }


 ll cnt = 0 ;

 ll mx = -1;

    for(int i = 0 ; i<n-1 ; ++i)
    {
        if(arr[i] >= arr[i+1])
        {
            cnt++;
        }
        else
        {
           mx = max(cnt,mx);
            cnt = 0 ;
        }
         mx = max(cnt,mx);

    }

   cout <<max(cnt,mx)<<endl;




    return 0 ;
}


Measured memory_bytes: 1400
#include<stdio.h>
#include<algorithm>
using namespace std;
char A[11];
char B[11];
int p[10];
int n;
int calc(int a,int b){
	if(a==0||b==n){
		for(int i=0;i<n;i++)if(p[i])return 0;
		return 1;
	}
	if(calc(a,b+1))return 1;
	for(int i=b;i<n;i++){
		int r=p[b];
		for(int j=b;j<=i;j++){
			p[j]=(p[j]+10-r)%10;
		}
		int res=calc(a-1,b+1);
		for(int j=b;j<=i;j++)p[j]=(p[j]+r)%10;
		if(res)return 1;
	}
	return 0;
}
int main(){
	int a;
	while(scanf("%d",&a),a){
		n=a;
		scanf("%s%s",A,B);
		for(int i=0;i<a;i++)p[i]=(B[i]-A[i]+10)%10;
//		for(int i=0;i<a;i++)printf("%d",p[i]);printf("\n");
		bool ok=false;
		for(int i=0;i<a;i++){
			if(calc(i,0)){
				ok=true;printf("%d\n",i);break;
			}
		}
		if(!ok)printf("%d\n",a);
	}
}

Measured memory_bytes: 1028
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define int long long
#define fre(x) freopen(x".in","r",stdin),freopen(x".out","w",stdout)
using namespace std;

inline int read(){
	int sum=0,f=1;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
	return f*sum;
}

const int N=250005;
const int INF=1e9+7;
struct node{int x,y;}a[N];
int n,Lmin=INF,Rmin=INF,Lmax=-1,Rmax=-1;
int minn[N],maxn[N],ans;

bool cmp(node a,node b){return a.x<b.x;}

signed main(){
	n=read();
	for(int i=1;i<=n;i++) a[i].x=read(),a[i].y=read();
	for(int i=1;i<=n;i++) if(a[i].x>a[i].y) swap(a[i].x,a[i].y);
	for(int i=1;i<=n;i++){
		Lmin=min(Lmin,a[i].x),Rmax=max(Rmax,a[i].y);
		Lmax=max(Lmax,a[i].x),Rmin=min(Rmin,a[i].y);
	}
	ans=(Lmax-Lmin)*(Rmax-Rmin);
	Rmin=Lmin;sort(a+1,a+n+1,cmp);
	minn[1]=maxn[1]=a[1].y;int ret=INF;
	for(int i=2;i<=n;i++){
		maxn[i]=max(maxn[i-1],a[i].y);
		minn[i]=min(minn[i-1],a[i].y);
		if(i!=n) ret=min(ret,max(maxn[i],a[n].x)-min(minn[i],a[i+1].x));
	}
	ret*=(Rmax-Rmin);
	printf("%lld\n",min(ans,ret));
	return 0;
}


Measured memory_bytes: 6912
import math
def main():
  N = int(input())
  s = 0
  if N % 2 == 0:
    s = 2 + N // 2

  for i in range(3, math.ceil(math.sqrt(N)) + 3):
    if N % i == 0 and (not s or s > i + N // i):
      s = i + N // i

  if not s:
    s = N + 1

  print(s-2)
  
main()

Measured memory_bytes: 9072
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
    public static void exec(InputStream in, PrintStream out){
        Scanner sc = new Scanner(in);
        int n = sc.nextInt();
        int result = 1;
        int min = sc.nextInt();
        for (int i = 1; i < n; i++) {
            int c = sc.nextInt();
            if (min > c) {
                min = c;
                result ++;
            }
        }
        out.println(result);
    }

    public static void main(String[] args) {
        exec(System.in, System.out);
    }
}

Measured memory_bytes: 63504
h,w = gets.chomp.split.map(&:to_i)
n = gets.to_i
as = gets.chomp.split.map(&:to_i)
ans = Array.new(h){Array.new(w,0)}
cur_x = 0
cur_y = 0

as.each_with_index do |a,i|
  a.times do
    ans[cur_y][cur_x] = i + 1
    if (cur_y.even? && cur_x == w -1) || (cur_y.odd? && cur_x == 0)
      cur_y += 1
    elsif cur_y.even?
      cur_x += 1
    elsif cur_y.odd?
      cur_x -= 1
    end
  end
end

ans.each do |s|
  puts s.join(" ")
end

Measured memory_bytes: 2428
S = list(input())
T = list(input())

dic1 = {}
dic2 = {}

for i in range(len(T)):
    tmp1 = T[i]
    tmp2 = S[i]
    if tmp1 not in dic1:
        dic1[tmp1] = tmp2
    else:
        if dic1[tmp1] != tmp2:
            print('No')
            exit()

    if tmp2 not in dic2:
        dic2[tmp2] = tmp1
    else:
        if dic2[tmp2] != tmp1:
            print('No')
            exit()

print('Yes')

Measured memory_bytes: 12572
#include <iostream>
#include <cmath>
 
using namespace std;
 
int main()
{
    int countNum[100001] = {0};
    int maxCount = 0;
 
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        int a; cin >> a;
        countNum[a]++;
        if (a > 0) countNum[a - 1]++;
        if (a < 100000) countNum[a + 1]++;
 
        if (a > 0 && a < 100000) {
            maxCount = max(max(maxCount, countNum[a]), max(countNum[a - 1], countNum[a + 1]));
        } else if (a == 0) {
            maxCount = max(max(maxCount, countNum[a]), countNum[a + 1]);
        } else if (a == 99999) {
            maxCount = max(max(maxCount, countNum[a]), countNum[a - 1]);
        }
    }
    cout << maxCount << endl;
 
    return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
 
#define rep(i, n) for (int i = 0; i < (n); i++)
 
int main(){
    int N;
    string S;
    cin >> N >> S;
    vector<int> I(10,-1);
    rep(i,10){
        int index = 0;
        while(index < N){
            if(i == S[index] - '0'){
                I[i] = index;
                break;
            }
            index++;
        }
    }
    int res=0;
    rep(i,10){
        int i1 = I[i];
        if(i1 < 0 || N-2 <= i1) continue;
        rep(j,10){
            if(I[j] < 0) continue;
            int i2 = i1+1;
            bool flag2 = false;
            while(i2<N){
                if(S[i2] - '0' == j){
                    flag2 = true;
                    break;
                }
                i2++;
            }
            if(!flag2) continue;
            rep(k,10){
                if(I[k] < 0) continue;
                int i3 = i2+1;
                bool flag3 = false;
                while(i3<N){
                    if(S[i3] - '0' == k){
                        flag3 = true;
                        break;
                    }
                    i3++;
                }
                if(!flag3) continue;
                res++;
            }
        }
    }
    cout << res << endl;
 
    return 0;
}


Measured memory_bytes: 256
from sys import stdin
from itertools import repeat
def main():
    n = int(stdin.readline())
    dat = map(int, stdin.read().split(), repeat(10, 2 * n - 2))
    la = [None] * (n + 1)
    xt = [None] * (2 * n - 2)
    j = 0
    for i in xrange(n - 1):
        x = dat[j]
        y = dat[j+1]
        xt[j] = la[y]
        la[y] = j
        j += 1
        xt[j] = la[x]
        la[x] = j
        j += 1
    c = [None] * (n + 1)
    par = [0] * (n + 1)
    st = [1]
    pu = st.append
    po = st.pop
    f = 1
    c[0] = 0
    while st:
        x = po()
        if c[x] is None:
            c[x] = -1
            pu(x)
            y = la[x]
            while y is not None:
                z = dat[y]
                if c[z] is None:
                    par[z] = x
                    pu(z)
                y = xt[y]
        else:
            if c[x] == -1:
                if c[par[x]] == 0:
                    print "First"
                    return
                c[par[x]] = 0
                f = 0
            c[x] = 0
    print "Second"
main()


Measured memory_bytes: 20080
#include <bits/stdc++.h>
//#define int long long

using namespace std;
using LL = long long;

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

#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e19;
const int MOD = (int)1e9 + 7;

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

    int n, k;
    cin >> n >> k;
    vector<int> h(n);
    REP(i, n) cin >> h[i];

    vector<int> dp(n+105, INF);

    dp[0] = 0;
    REP(i, n) {
        REP(j, k) {
            dp[i + j + 1] = min(dp[i] + abs(h[i + j + 1] - h[i]), dp[i + j + 1]);
        }
    }
    cout << dp[n-1] << endl;
}

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

int ans;

void dfs(int i){
if(i==1){ans++; return;}
if(i%2==0)dfs(i/2);
else if(i%3==0)dfs(i/3);
else if(i%5==0)dfs(i/5);
return;
}

int main(){
int m, n;
while(cin>>m, m){
cin>>n;
ans=0;

for(int i=m; i<=n; i++)dfs(i);
cout<<ans<<"\n";
}
return 0;
}

Measured memory_bytes: 860
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

def main():
    N = int(input())
    H = list(map(int,input().split()))

    for i in range(N-1):
        if H[i+1] - 1 >= H[i]:
            H[i+1] -= 1
        elif H[i+1] == H[i]:
            continue
        else:
            print('No')
            exit()

    print('Yes')



if __name__ == "__main__":
    main()


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

int main() {
    long long int x,t1,t2;
    cin>>x;
    long long int a[150];
    for(long int i=0;i<150;i++)
    {
        a[i]=i*i*i*i*i;
    }
    for(long int i=0;i<150;i++)
    {
        t1=x+a[i];
        t2=x-a[i];
        for(long int j=0;j<150;j++)
        {
            if(t1==a[j])
            {
                cout<<j<<" "<<i;
                return 0;
            }
            else if(t2==a[j])
            {
                cout<<j<<" "<<(-1*i);
                return 0;
            }
            else if(t2==(-1*a[j]))
            {
                cout<<(-1*j)<<" "<<(-1*i);
                return 0;
            }
        }
    }
	return 0;
}

Measured memory_bytes: 3632
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<sstream>
#include<iostream>
using namespace std;
const int Lcalendar[] = {366,31,29,31,30,31,30,31,31,30,31,30,31};
const int Ncalendar[] = {365,31,28,31,30,31,30,31,31,30,31,30,31};
long long int GetTotalDays(int y,int m,int d){
    long long int sum = 0;
    sum = y * 365 + ((y-1)/4) - ((y-1)/100) + ((y-1)/400);
    if(y % 4 == 0 &&(y % 400 == 0 || y % 100 != 0)){
        for(int i = 1; i <= m-1; i++)sum += Lcalendar[i];
    }

    else{
        for(int i = 1; i <= m-1; i++) sum += Ncalendar[i];
    }
    sum += d;
    return sum;
}
int main(void){

    int y,m,d,Y,M,D;
    while(cin>>y>>m>>d>>Y>>M>>D,y!=-1)
        cout<<GetTotalDays(Y,M,D) - GetTotalDays(y,m,d)<<endl;

    return 0;
}

Measured memory_bytes: 1156
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		int b = scan.nextInt();
		int h = scan.nextInt();
		
		System.out.println((a + b) * h / 2);
	}
}

Measured memory_bytes: 35740
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# AISing Programming Contest 2019：B - Contests


def main():
    N = int(raw_input().strip())
    A, B = [int(i) for i in raw_input().strip().split(' ')]
    P = [int(i) for i in raw_input().strip().split(' ')]

    q1, q2, q3 = 0, 0, 0
    for p in P:
        if p <= A:
            q1 += 1
        elif p <= B:
            q2 += 1
        else:
            q3 += 1
    print min(q1, q2, q3)


if __name__ == '__main__':
    main()


Measured memory_bytes: 2568
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

typedef long long ll;
//typedef pair<int,int> Pint;
//typedef pair<ll, ll> P;
//typedef pair<ll, pair<ll, ll>> P;
//typedef tuple<int,int,int> T;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define revrep(i, n) for(ll i = n-1; i >= 0; i--)
#define pb push_back
#define f first
#define s second
ll INFL = 1LL << 60;//10^18 = 2^60
int INF = 1 << 30;//10^9
ll MOD = 1e9 + 7;
//ll MOD  = 998244353;
vector<ll> dy = {0, 0, 1, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, -1, 0, 0, 1, -1, 1, -1, 0};

ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll pow_long(ll x, ll k){
  ll res = 1;
  while(k > 0){
    if(k % 2) res *= x;
    x *= x;
    k /= 2;
  }
  return res;
}

ll pow_mod(ll x, ll k){
  x %= MOD; x += MOD; x %= MOD;
  ll res = 1;
  while(k > 0){
    if(k % 2){
      res *= x; res %= MOD;
    }
    x *= x; x %= MOD;
    k /= 2;
  }
  return res;
}
ll inverse(ll x){return pow_mod(x, MOD - 2);};

ll gcd(ll a, ll b){
    if(b == 0) return a;
    return gcd(b, a % b);
}
ll lcm(ll x, ll y){return x / gcd(x, y) * y % MOD;};

int main(){
  ll N; cin >> N;
  vector<ll> A(N);
  rep(i, N){
    cin >> A[i];
  }


  ll now = 0;

  for(int i = 0; i < N; i++){
    if(i % 2 == 0) now += A[i];
    else now -= A[i];
  }
  cout << now << endl;
  now /= 2;
  for(int i = 1; i < N; i++){
    cout << (A[i-1] - now) * 2 << endl;
    now = A[i-1] - now;
  }


}


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

using LL = long long;

struct LCA {
    int n, log2_n;
    vector<int> depth;
    vector<vector<int>> par;

    void dfs(const vector<vector<int>>& G, int v, int p, int d) {
        depth[v] = d;
        par[0][v] = p;
        for (auto to : G[v]) {
            if (to != p) dfs(G, to, v, d+1);
        }
    }

    LCA(const vector<vector<int>>& G, int root=0) :
        n(G.size()), log2_n(log2(n)), depth(n),
        par(log2_n+1, vector<int>(n,-1)) {

            dfs(G, root, -1, 0);
            for (int k = 0; k < log2_n; ++k) {
                for (int v = 0; v < n; ++v) {
                    if (par[k][v] != -1) {
                        par[k+1][v] = par[k][par[k][v]];
                    }
                }
            }
        }

    int query(int u, int v) {
        if (depth[u] > depth[v]) swap(u, v);

        // align the depth of u and v
        for (int k = 0; k <= log2_n; ++k) {
            if ((depth[v] - depth[u]) >> k & 1) {
                v = par[k][v];
            }
        }
        if (u == v) return u;

        // go back until u and v's parents do not match
        for (int k = log2_n; k >= 0; --k) {
            if (par[k][u] != par[k][v]) {
                u = par[k][u];
                v = par[k][v];
            }
        }
        return par[0][u];
    }
};

void dfs(const vector<vector<int>>& G, vector<int>& par, int v, int p) {
    par[v] = p;
    for (auto u : G[v]) {
        if (u == p) continue;
        dfs(G, par, u, v);
    }
}

#define __builtin_popcount __builtin_popcountll

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

    int m; cin >> m;
    vector<int> u(m), v(m);
    for (int i = 0; i < m; ++i) {
        cin >> u[i] >> v[i];
        --u[i], --v[i];
    }

    vector<int> par(n);
        // par[i] := 頂点0を根とする根つき木における,
        //           頂点i の親
    dfs(G, par, 0, -1);

    LCA lca(G);

    vector<LL> cover(m);
        // cover[i] := u[i],v[i] がカバーする辺の集合
    for (int i = 0; i < m; ++i) {
        int t = lca.query(u[i], v[i]);
        int U = u[i], V = v[i];
        while (U != t) {
            cover[i] |= 1LL<<U;
            U = par[U];
        }
        while (V != t) {
            cover[i] |= 1LL<<V;
            V = par[V];
        }
    }

    LL ans = 0;

    for (int s = 0; s < 1<<m; ++s) {
        LL used = 0;
            // used := u[i],v[i] (i in s) がカバーする辺の集合
        for (int i = 0; i < m; ++i) if (s & 1<<i) {
            used |= cover[i];
        }

        if (__builtin_popcount(s) & 1) {
            ans -= 1LL<<(n-1-__builtin_popcount(used));
        } else {
            ans += 1LL<<(n-1-__builtin_popcount(used));
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define _repargs(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _repargs(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    int N, M;
    cin >> N >> M;
    vector<ll> X(N), Y(M);
    rep(i, N) cin >> X[i];
    rep(i, M) cin >> Y[i];
    
    ll ax = 0, ay = 0;
    rep(i, N - 1){
        ll diff = X[i + 1] - X[i];
        diff %= mod;
        (diff *= i + 1) %= mod;
        (diff *= N - 1 - i) %= mod;
        (ax += diff) %= mod;
    }
    
    rep(i, M - 1){
        ll diff = Y[i + 1] - Y[i];
        diff %= mod;
        (diff *= i + 1) %= mod;
        (diff *= M - 1 - i) %= mod;
        (ay += diff) %= mod;
    }
    
    output(ax * ay % mod);
    
    
    
    return 0;
}

Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int quickpow(int x,int y){
    int answ = 1,res = x;
    while(y){
        if(y%2) answ = answ * res % mod;
        y /= 2,res = res * res % mod;
    }
    return answ;
}
int n,m,x,y;
int ans = 0;
signed main(){
    scanf("%lld%lld%lld%lld",&n,&m,&x,&y);
    int res = 1,res2 = 1,res3 = 1,res4 = 1;
    for(int i = 1;i<y;i++) res = res * i % mod;
    res = quickpow(res,mod-2);
    for(int i = 1;i<m-y;i++) res2 = res2 * i % mod;
    res2 = quickpow(res2,mod-2);
    for(int i = 1;i<y;i++) res3 = res3 * i % mod;
    for(int i = n+m-y-2;i>=n;i--) res4 = res4 * i % mod; 
    int res5 = 0;
    ans = ( ans + res * res2 % mod * res3 % mod * res4 % mod ) % mod;
    for(int i = 2;i<=n-x;i++){
        res3 = res3 * quickpow(i-1,mod-2) % mod * (y+i-2) % mod;
        res4 = res4 * quickpow(n+m-y-i,mod-2) % mod * (n-i+1) % mod;
        ans =  ( ans + res * res2 % mod * res3 % mod * res4 % mod ) % mod;
    }
    printf("%lld",ans);
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
map<int,int> mp;
map<int,int>::iterator it;
int main()
{
	int n,j;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>j;
		mp[j]++;
	}
	int sum=0;
	for(it=mp.begin();it!=mp.end();it++)
		if(it->second%2!=0) sum++;
	cout<<sum<<endl;
	return 0;
}


Measured memory_bytes: 4864
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <cctype>
using namespace std;
 
typedef long long lli;
 
lli modPow(lli x, lli n, lli mod) {
  if(n == 0) return 1;
  lli tmp = modPow(x, n/2, mod);
  if(n % 2 == 0) return tmp*tmp % mod;
  else return tmp*tmp % mod * x % mod;
}
 
 
int pos;
string str;
lli P;
 
lli exp();
lli term();
lli fact();
 
lli exp() {
  lli x = term();
  while(1) {
    if(str[pos] == '+') {
      ++pos;
      x = (x + term()) % P;
    } else if(str[pos] == '-') {
      ++pos;
      x = x - term();
      while(x < 0) x += P;
      x %= P;
    } else {
      break;
    }
  }
  return x;
}
 
lli term() {
  lli x = fact();
  while(1) {
    if(str[pos] == '*') {
      ++pos;
      x = (x * fact()) % P;
    } else if(str[pos] == '/') {
      ++pos;
      lli y = fact();
      if(y == 0) throw "";
      lli d = modPow(y, P-2, P);
      x = (x * d) % P;
    } else {
      break;
    }
  }
  return x;
}
 
lli fact() {
  lli x = 0;
  if(str[pos] == '(') {
    ++pos;
    x = exp();
    ++pos;
  } else {
    string num = "";
    while(isdigit(str[pos])) {
      num += str[pos++];
    }
    x = atoll(num.c_str()) % P;
  }
  return x;
}
 
int main() {
  string line;
  while(getline(cin, line) && line != "0:") {
    int k = line.find(':');
    P = atoll(line.substr(0, k).c_str());
    string tmp = line.substr(k+1);
    str = "";
    for(int i = 0; i < tmp.size(); ++i) {
      if(tmp[i] == ' ') continue;
      str += tmp[i];
    }
    pos = 0;
    try {
      lli val = exp();
      cout << str << " = " << val << " (mod " << P << ")" << endl;
    } catch(const char *err) {
      cout << "NG" << endl;
    }
  }
  return 0;
}

Measured memory_bytes: 1532