akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define pb push_back
#define mp(x,y) make_pair(x,y)
#define scd(n) scanf("%d",&n)
#define sclf(n) scanf("%lf",&n)
#define scl(n) scanf("%I64d",&n)
#define repi(a,b,c) for(int i=a;i<b;i+=c)
#define repis(a,b,c) for(int i=a-1;i>=b;i-=c)
#define repj(a,b,c) for(int j=a;j<b;j+=c)
#define repjs(a,b,c) for(int j=a-1;j>=b;j-=c)
#define repk(a,b,c) for(int k=a;k<b;k+=c)
#define repks(a,b,c) for(int k=a-1;k>=0;k-=c)
#define fi first
#define se second

/*
 fast I/O

 ios::sync_with_stdio(0);
 cin.tie();

 freeopen

 freopen("input.txt","r",stdin);
 freopen("output.txt","w",stdout);
 */

typedef pair<ll,ll> ii;
const int mx = 200100;
const int md = 1000000007;
const double EULER = 2.71828182845904523536;

int main() {
    string a,b,temp;
    vector <string> ans;
    bool cek = false;
    cin >> a >> b;
    repi(0,a.length(),1) {
        cek = true;
        if(i+b.length() > a.length()) break;
        repj(0,b.length(),1) {
            if(a[i+j] != b[j] and a[i+j] != '?') {
                cek = false;
                break;
            }
        }
        if(cek) {
            temp = a;
            repj(0,b.length(),1) {
                temp[i+j] = b[j];
            }
            repj(0,temp.length(),1) if(temp[j] == '?') temp[j] = 'a';
            ans.pb(temp);
        }
    }
    sort(ans.begin(),ans.end());
    repi(0,a.length(),1) if(a[i] == '?') a[i] = 'a';
    if(ans.size() > 0) cout << ans[0] << endl;
    else cout << "UNRESTORABLE\n";
}


Measured memory_bytes: 256
def main():
    n = int(input())
    a = list(map(int, input().split()))
    f = [True]*n
    ans = 0
    for i in range(n):
        if f[i]:
            f[i] = False
            if a[a[i]-1]-1 == i:
                ans += 1
                f[a[i]-1] = False
    print(ans)

if __name__ == "__main__":
    main()


Measured memory_bytes: 14004
s = gets.to_s.chomp.bytes
q = gets.to_s.to_i

lr = [[], []]
state = 0

$<.each do |query|
  
  query.chomp!
  # p query
  if query == "1"
    state = 1 - state
    # s.reverse!
  else
    if (query[0,3] == "2 1" && state == 0) || (query[0,3] == "2 2" && state == 1)
      s.unshift query[-1].ord
    else
      s.push query[-1].ord
    end
  end
end

if state == 1
  # puts (lr[state].reverse + s.reverse + lr[1-state] ).pack("U*")
  puts s.reverse.pack("U*")
else
  puts s.pack("U*")
  # puts (lr[state].reverse + s + lr[1-state] ).pack("U*")
end

Measured memory_bytes: 7224
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>

using namespace std;

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

	cout << S[0] << S.length() - 2 << S[S.length() - 1] << endl;


}


Measured memory_bytes: 3540
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const int mod = 1000000007;

int main() {
    string s;
    cin >> s;
    int n = s.size();

    vector<int> dp0(n+1);  /* dp0[i+1] : aおよびbの上からi桁目(iは0-index)までを確定させ、
                                         その時点ではまだa+bがs以下になるかわかっていない
                                         (a, b)の組の個数 */
    
    vector<int> dp1(n+1);  /* dp1[i+1] : aおよびbの上からi桁目(iは0-index)までを確定させ、
                                         その時点で既にa+bがs以下であることがわかっている
                                         (a, b)の組の個数 */

    dp0[0] = 1;  // 1桁も確定していない時点ではまだs以下になるかわからない
    rep(i, n) {
        // (a_i, b_i) = (0, 0) の場合
        if(s[i] == '0') {
            dp0[i+1] = dp0[i];
            dp1[i+1] = dp1[i];
        }
        else if(s[i] == '1') {
            dp1[i+1] = (dp0[i] + dp1[i])%mod;
        } 
        // (a_i, b_i) = (0, 1), (1, 0) の場合
        if(s[i] == '0') {
            dp1[i+1] += 2*dp1[i]%mod;
            dp1[i+1] %= mod;
        }
        else if(s[i] == '1') {
            dp0[i+1] += 2*dp0[i]%mod;
            dp1[i+1] += 2*dp1[i]%mod;
            dp0[i+1] %= mod;
            dp1[i+1] %= mod;
        }
    }

    cout << (dp0[n] + dp1[n])%mod << endl;
    return 0;
}

Measured memory_bytes: 1280
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<sstream>
using namespace std;

void replaceAll(string &s,string a,string b){
  int pos = 0;
  while((pos=s.find(a))!=string::npos)s.replace(pos,a.size(),b);
}

int n;
vector<int> g[2000];
bool vis[2000];
map<int,bool> group;

int dfs(int x){
  if(vis[x])return 0;
  vis[x] = true;

  int res = (group[x])?0:1;
  for(int i=0;i<g[x].size();i++){
    res += dfs(g[x][i]);
  }
  return res;
}

int main(){
  while(cin >> n,n){
    map<string,int> m;
    string tmp;
    vector<string> v[100];
    group.clear();

    for(int i=0;i<n;i++){
      cin >> tmp;
      replaceAll(tmp,":"," ");
      replaceAll(tmp,","," ");
      replaceAll(tmp,".","");

      stringstream ss(tmp);
      while(ss >> tmp)v[i].push_back(tmp);

      for(int j=0;j<v[i].size();j++)
	if(m.find(v[i][j])==m.end())m[v[i][j]] = m.size();
    }

    for(int i=0;i<=m.size();i++){
      g[i].clear();
      vis[i] = false;
    }

    for(int i=0;i<n;i++){
      group[m[v[i][0]]] = true;
      for(int j=1;j<v[i].size();j++){
	g[m[v[i][0]]].push_back(m[v[i][j]]);
      }
    }

    cout << dfs(m[v[0][0]]) << endl;
  }
}

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

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define repr(i,a,b) for(int i=(int)(a-1);i>=b;i--)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout << #x" = " << x << endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define INF LONG_MAX/3

int main(){
    cin.sync_with_stdio(false);
    int n;
    cin >> n;
    ll a[3 * n] = {};
    ll sumf[n + 1] = {}, sumb[n + 1] = {};
    priority_queue<ll, vector<ll>, greater<ll>> front;
    priority_queue<ll> back;
    rep(i, 3 * n) {
        cin >> a[i];
        if (i < n) {
            front.push(a[i]);
            sumf[0] += a[i];
        } else if (i > 2 * n - 1) {
            back.push(a[i]);
            sumb[0] += a[i];
        }
    }

    repl(i, n, 2 * n) {
        sumf[i - n + 1] = sumf[i - n] + a[i];
        front.push(a[i]);
        ll t = front.top(); front.pop();
        sumf[i - n + 1] -= t;
    }

    repr(i, 2 * n, n) {
        sumb[2 * n - i] = sumb[2 * n - 1 - i] + a[i];
        back.push(a[i]);
        ll t = back.top(); back.pop();
        sumb[2 * n - i] -= t;
    }

    ll ans = -INF;
    rep(i, n + 1) maxch(ans, sumf[i] - sumb[n - i]);
    cout << ans << endl;
    return 0;
}

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

using namespace std;
using ll = long long;

//cout << std::fixed << std::setprecision(15) << y << endl;

int main() {
    int n = 0;
    cin >> n;
    vector<ll> v(n);
    for(int i = 0;i < n;i++){
        cin >> v[i];
    }
    sort(v.begin(),v.end());
    double ave = ((double)v[0] + (double)v[1]) / 2;
    for(int i = 2;i < n;i++){
        ave = (ave + v[i]) / 2;
    }
    cout << std::fixed << std::setprecision(6) << ave << endl;
    return 0;
}

Measured memory_bytes: 384
import Control.Applicative
import Control.Monad.State
import Data.Array.IO

main = do
  n <- readLn
  list <- map read.words<$>getLine
  list <- newListArray (1,n) list
  (arr,i) <- bubbleSort list
  getElems arr >>= putStrLn.unwords.map show
  print i


bubbleSort :: IOArray Int Int -> IO (IOArray Int Int, Int)
bubbleSort arr = runStateT (bubble' arr True) 0

bubble' :: IOArray Int Int -> Bool -> StateT Int IO (IOArray Int Int)
bubble' arr False = return arr
bubble' arr True  = do
  (st, en) <- liftIO $ getBounds arr
  bs <- forM [st..(en-1)] $ \i -> do
    ei <- liftIO $ readArray arr i
    ei' <- liftIO $ readArray arr (i+1)
    if ei <= ei' then return False
                 else do
                   liftIO $ writeArray arr i ei'
                   liftIO $ writeArray arr (i+1) ei
                   modify (+1)
                   return True
  bubble' arr (any id bs)

Measured memory_bytes: 4352
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) {
  cout << (expr ? "Yes" : "No") << "\n";
}
lint power(lint x, lint n, lint mod) {
  lint ret = 1;
  while(n > 0) {
    if(n & 1) (ret *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return ret;
}
lint gcd(lint a, lint b) {
  if(a < b) {
    swap(a, b);
  }
  while(b > 0) {
    lint r = a % b;
    a = b;
    b = r;
  }
  return a;
}
lint lcm(lint a, lint b) {
  return a * b / gcd(a, b);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    lint N;
    cin >> N;
    vector<lint> A(N);
    vector<lint> B(N);
    REP(i, N) {
        cin >> A[i] >> B[i];
        if(A[i] != 0 && B[i] != 0) {
            lint g = gcd(abs(A[i]), abs(B[i]));
            A[i] /= g; B[i] /= g;
            if(B[i] < 0) {
                A[i] = 0-A[i];
                B[i] = 0-B[i];
            }
        }
    }
    lint zerozero = 0;
    lint Azero = 0;
    lint Bzero = 0;
    map<lint, map<lint, lint>> cnt;
    REP(i, N) {
        if(A[i] == 0 && B[i] == 0) {
            zerozero++;
        } else if(A[i] == 0) {
            Azero++;
        } else if(B[i] == 0) {
            Bzero++;
        } else {
            
            cnt[A[i]][B[i]]++;
        }
    }
    vector<pair<lint, lint>> pa;
    pa.push_back(make_pair(Azero, Bzero));
    REP(i, N) {
        if(A[i] != 0 && B[i] != 0) {
            lint C = A[i] > 0 ? -B[i] : B[i];
            lint D = A[i] > 0 ? A[i] : -A[i];
            //cout << C << " " << D << endl;
            //cout << cnt[A[i]][B[i]] << " " << cnt[C][D] << endl;
            if(cnt[A[i]][B[i]] > 0 || cnt[C][D] > 0) {
                pa.push_back(make_pair(cnt[A[i]][B[i]], cnt[C][D]));
                cnt[A[i]][B[i]] = 0;
                cnt[C][D] = 0;
            }
        }
    }
    lint ans = 1;
    REP(i, pa.size()) {
        ans = ans * (((power(2, pa[i].first, MOD)-1) + (power(2, pa[i].second, MOD)-1) + 1 + MOD) % MOD) % MOD;
    }
    ans = (ans - 1 + zerozero + MOD) % MOD;
    
    cout << ans << endl;
}

Measured memory_bytes: 71800
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define rev(r) reverse(r.begin(),r.end())
#define SORT(ar) sort(ar.begin(), ar.end())
#define sz(x) (int)(x).size()
#define len(x) (int)x.length()
#define in(a) scanf("%lld",&a)
#define pr(x) printf("%d ",x)
#define endl '\n'
#define read(v,n) for(int h=0;h<n;h++) cin>>v[h];
#define fo(i,a,b) for(i=a;i<b;i++)
#define fi(i,a,b) for(i=a;i>=b;i--)
#define mo 1000000007
#define N 300007
ll cost[401][401],p[401];

int main()
{
    ll n,i,j,k,x,y;
    cin>>n;
    ll a[n+1];
    fo(i,1,n+1) cin>>a[i];
    fo(i,1,n+1) p[i]=p[i-1]+a[i];
    fi(i,n-1,1)
    {
        fo(j,i+1,n+1)
        {
            x=1e18;
            fo(k,i,j)
            {
                y=0;
                y+=p[k]-p[i-1];
                y+=(cost[i][k]);
                y+=p[j]-p[k];
                y+=(cost[k+1][j]);
                x=min(x,y);
            }
            cost[i][j]=x;
        }
    }
    cout<<cost[1][n]<<endl;
}


Measured memory_bytes: 1536
#define  _USE_MATH_DEFINES

#define  rep(i, n) for(int i = 0; i < n; i++)
#define  repx(i, a, n) for(int i = a; i < n; i++)
#define  loop while(1)
#define  lli long long int
#define  aryout(ary) rep(i, ary.size()) cout << ary[i] << (i < ary.size() - 1 ? " " : "\n");
#define  arysort(v) sort(v.begin(), v.end())
#define  aryreverse(v) reverse(v.begin(),v.end())
#define  arysum(v) accumulate(v.begin(), v.end(), 0)
 
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <utility>
#include <set>

using namespace std;

int main() {

  int x;
  cin >> x;

  rep(loopcnt, x) {
    int m, n;
    cin >> n >> m;

    int max_ab = max(abs(m), abs(n));
    bool flag = false;

    for(int a = -max_ab; a <= max_ab; a++) {
      for(int b = -max_ab; b <= max_ab; b++) {

        if(a*a + b*b == 0) continue;

        int y_numerator = a*n - b*m;
        int y_denominator = a*a + b*b;

        if(y_numerator % y_denominator == 0) {

          int y = y_numerator / y_denominator;
          int x = 1000000;
          int x_numerator = m + b*y;

          if(a != 0 && x_numerator % a == 0) {
            x = x_numerator / a;
          } else if(a == 0 && b != 0 && n % b == 0) {
            x = n / b;
          }

          if (x < 1000000) {
            flag = true;
            if (a*a + b*b == 1) {
              if (x == m && y == n || x == -m && y == -n || x == -n && y == m || x == n && y == -m) {
                flag = false;
              }
            }
            if (x*x + y*y == 1) {
              if (a == m && b == n || a == -m && b == -n || a == -n && b == m || a == n && b == -m) {
                flag = false;
              }
            }
          }
        }
        if(flag) break;
      }
      if(flag) break;
    }
    cout << (flag ? "C" : "P") << endl;
  }

  return 0;
}



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

int main()
{
    int n, y, i, j;
    cin >> n >> y;
    bool found = 0;
    for (i = 0; i <= n; i++)
    {
        for (j = 0; j <= n - i; j++)
        {
            if (((n - i - j) * 10000 + i * 5000 + j * 1000) == y)
            {
                found = 1;
                break;
            }
        }
        if (found)
            break;
    }
    if (found)
        cout << n - i - j << " " << i << " " << j << endl;
    else
        cout << -1 << " " << -1 << " " << -1 << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ldb = long double;
using pa = pair < ll, ll >;
using vec = vector < ll >;
#define pb push_back
#define po pop_back
#define mp make_pair
#define mt make_tuple
#define F  first
#define S  second
#define f(i,x,n) for(ll i=x;i<n;i++) 
#define all(c) c.begin(),c.end()
#define str string
#define edl "\n"
#define add insert
#define cot continue
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL)
const int MOD = 1e9 + 7, INF = INT_MAX, N = 1e5 + 10;
const double PI = acos(-1);
const ll LINF = LLONG_MAX;




int main() 
{

 fast();
  
  // #ifndef ONLINE_JUDGE
  //   freopen("input.txt", "r", stdin);
  //   freopen("output.txt","w",stdout);
  // #endif

  ll a,s;
  cin>>a>>s;
  cout<<((a*3)+s)/2;
  
  








  


    




    // cerr << "Time taken: " << int((clock() * 1000.) / CLOCKS_PER_SEC) << "ms\n";
    return 0;
}
 

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

using namespace std;

int main(void) {
  int n;
  cin >> n;
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  vector<int> result;
  std::vector<int>::iterator itr = b.end();
  LOOP:
  while(b.size() > 0) {
    for (vector<int>::iterator i = b.end() - 1; i >= b.begin(); i--) {
      if (*i == (i - b.begin() + 1)) {
        result.push_back(i - b.begin() + 1);
        b.erase(i);
        goto LOOP;
      }
    }
    cout << -1 << endl;
    return 0;
  }
  for (vector<int>::iterator i = result.end() - 1; i >= result.begin(); i--) {
    cout << *i << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#define forsn(i, s, n) for(int i=s;i<n;i++)
#define forn(i, n) forsn(i, 0, n)
using namespace std;
int main(){
	int n; cin >> n;
	string s; cin >> s;
	vector<int> resultados (n, 0);
	int minimo = n;
	forsn(i,1, n){
		 if(s[i]!='W'){
			resultados[0]++;
		}
	}
	forsn(i, 1, n){
		if(s[i]=='E' and s[i-1]=='E'){
			resultados[i]=resultados[i-1]-1;
		}
		if(s[i]=='W' and s[i-1]=='W'){
			resultados[i]=resultados[i-1]+1;
		}
		if(s[i]=='W' and s[i-1]=='E'){
			resultados[i]=resultados[i-1];
		}
		if(s[i]=='E' and s[i-1]=='W'){
			resultados[i]=resultados[i-1];
		}
	}
	forn(i, n){
		minimo = min(resultados[i], minimo);
	}
	cout << minimo << endl;
}


Measured memory_bytes: 1796
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}
fn main() {
    input!(
    a: usize,
    b: usize,
    c: usize,
    d: usize,
 );
    let m = if a % d == 0 { a / d } else { a / d + 1 };
    let n = if c % b == 0 { c / b } else { c / b + 1 };
    let ans = if m >= n { "Yes" } else { "No" };
    println!("{}", ans);
}


Measured memory_bytes: 2160
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <iomanip>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <functional>
#include <algorithm>
#include <climits>
#include <sstream>
#include <complex>

using namespace std;

using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
using vvi = vector<vi>;
using iii = pair<int, ii>;
using viii = vector<iii>;

using ll = long long int;
using vc = vector<char>;
using vb = vector<bool>;
using ib = pair<int, bool>;
using vib = vector<ib>;

#define MOD 1000 * 1000 * 1000 + 7
#define INF 1000 * 1000 * 1000
#define X real()
#define y imag()

div_t euc_div(int a, int d) {
	div_t divT = div(a, d);
	int I = divT.rem >= 0 ? 0 : (d > 0 ? 1 : -1);
	int qE = divT.quot - I;
	int rE = divT.rem + I * d;
	return div_t{ qE, rE };
}

bool resuelveCaso() {
	int n;
	cin >> n;
	if (!cin) return false;
	string res = "";
	if (n == 0) cout << "0\n";
	else {
		while (n != 1) {
			div_t respar = euc_div(n, -2);
			if (respar.rem == 0) res = "0" + res;
			else res = "1" + res;
			n = respar.quot;
		}

		cout << 1 << res << '\n';
	}
	return true;
}

int main() {
	while (resuelveCaso()) {}
	return 0;
}

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

int main() {
    string s;
    int K;
    cin >> s >> K;
    set<string> v;
    for (int i = 0; i < s.length(); i++) {
        for (int j = 1, lim = min((int)(s.length() - i), K); j <= lim; j++) {
            v.insert(s.substr(i, j));
        }
    }
    string x = *next(v.begin(), K - 1);
    cout << x << endl;
}

Measured memory_bytes: 4804
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long INF = 1e17;
struct lpl{
	int fa, deep;
	long long data, l, r;
	set<long long> s;
}node[maxn];
struct ld{
	int u, v;
}que[maxn];
int N, K, root, deg[maxn];
queue<int> q;
vector<int> point[maxn];
bool flag = true;

inline void putit(){
	scanf("%d", &N);
	for(int i = 1; i <= N; ++i){
		node[i].data = INF;
		node[i].l = -INF; node[i].r = INF;
	}
	for(int u, v, i = 1; i < N; ++i){
		scanf("%d%d", &u, &v); deg[u]++; deg[v]++;
		point[u].push_back(v); point[v].push_back(u);
	}
	scanf("%d" ,&K);
	for(int v, p, i = 1; i <= K; ++i){
		scanf("%d%d", &v, &p); node[v].data = p;
		q.push(v); node[v].l = node[v].r = p;
		que[i] = (ld){v, p};
	}
}

inline void prepare(){
	while(!q.empty()){
		int now = q.front(); q.pop(); root = now;
		for(int i = point[now].size() - 1; i >= 0; --i){
			int qwe = point[now][i]; deg[qwe]--;
			if(deg[qwe] == 1) q.push(qwe);
		}
	}
	node[root].deep = 1; node[root].fa = 0;
}

void build(int t){
	for(int i = point[t].size() - 1; i >= 0; --i){
		int now = point[t][i]; if(node[t].fa == now) continue;
		node[now].fa = t; node[now].deep = node[t].deep + 1; build(now);
	}
}

void dp(int t){
	for(int i = point[t].size() - 1; i >= 0; --i){
		int now = point[t][i]; if(now == node[t].fa) continue;
		dp(now); 
		node[t].l = max(node[t].l, node[now].l - 1); 
		node[t].r = min(node[t].r, node[now].r + 1);
	}
	if(node[t].l > node[t].r) flag = false;
}

void calc(int t){
	if(t != root){
		if(node[t].l <= node[node[t].fa].data + 1 && node[node[t].fa].data + 1 <= node[t].r) node[t].data = node[node[t].fa].data + 1;
		else node[t].data = node[node[t].fa].data - 1;
	}
	for(int i = point[t].size() - 1; i >= 0; --i){
		int now = point[t][i]; if(now == node[t].fa) continue;
		calc(now); 
	}
}

inline void check(int t){
//	for(int i = 1; i <= K; ++i)
//		for(int j = i + 1; j <= K; ++j)
//			if(((abs(node[que[i].u].deep - node[que[j].u].deep)) & 1) != ((abs(que[i].v - que[j].v)) & 1)) flag = false;
	int ji = 3, ou = 3;
	for(int i = 1; i <= K; ++i){
		if(node[que[i].u].deep & 1){
			if(ji == 3){ji = (que[i].v) & 1; continue;}
			if(ji != (que[i].v & 1)) flag = false;
		}
		else{
			if(ou == 3){ou = (que[i].v) & 1; continue;}
			if(ou != (que[i].v & 1)) flag = false;
		}
	}
}

inline void workk(){
	check(root); dp(root); 
	if(!flag){printf("No"); return;}
	node[root].data = node[root].l;
	calc(root); printf("Yes\n");
	for(int i = 1; i <= N; ++i) printf("%lld\n", node[i].data);
}

int main()
{
	//freopen("lpl.in", "r", stdin);
	putit();
	prepare();
	build(root);
	workk();
	return 0;
}

Measured memory_bytes: 23168
# 解説放送

def main():
    N = int(input())
    *A, = map(int, input().split())

    mis = [0]
    mas = [0]
    for x in reversed(A):
        mis.append((mis[-1] + 1) // 2 + x)
        mas.append(mas[-1] + x)

    if not (mis[-1] <= 1 <= mas[-1]):
        print(-1)
        return

    mas.reverse()

    ans = [1]
    for i, x in enumerate(A, start=1):
        ans.append(min(mas[i], (ans[-1] - x) * 2))

    print(sum(ans))


if __name__ == '__main__':
    main()


Measured memory_bytes: 26724
#include<iostream>
using namespace std;
int main()
{
  int n;
  int k;
  string s;
  cin >> n >> k >> s;
  
  //初期幸福人数をチェック
  int h=0;
  for(int i=0;i<n-1;++i){
    if(s.at(i)==s.at(i+1)){
      ++h;
    }
  }
  
  if(h+2*k>n-1){//規定回数内に最大人数（n-1人）に至れる場合
    cout << n-1 << endl;
  }else{//最大数に至れない場合。初期人数から2k人増えたところで終了
    cout << h+2*k << endl;
  } 
  return 0;
}

Measured memory_bytes: 512
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#include <functional>
using namespace std;
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define MOD (long long int)(1e9+7)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

ll mpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;}
  else return mpow(a,b-1) * a % MOD;
}
ll lpow(ll a, ll b){
  if(b==0) return 1;
  else if(b%2==0){ll memo = lpow(a,b/2); return memo*memo;}
  else return lpow(a,b-1) * a;
}
ll gcd(ll a, ll b){
  if(b==0) return a;
  else return gcd(b, a%b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n){
  if(kaijo_memo.size() > n) return kaijo_memo[n];
  if(kaijo_memo.size() == 0) kaijo_memo.push_back(1);
  while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD);
  return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n){
  if(gyaku_kaijo_memo.size() > n) return gyaku_kaijo_memo[n];
  if(gyaku_kaijo_memo.size() == 0) gyaku_kaijo_memo.push_back(1);
  while(gyaku_kaijo_memo.size() <= n) gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size()-1] * mpow(gyaku_kaijo_memo.size(), MOD-2) % MOD);
  return gyaku_kaijo_memo[n];
}

ll nCr(ll n, ll r){
  if(n == r) return 1;//0個の丸と-1個の棒みたいな時に時に効く？不安.
  if(n < r || r < 0) return 0;
  ll ret = 1;
  if(n <= 1e7){
    ret *= kaijo(n); ret %= MOD;
    ret *= gyaku_kaijo(r); ret %= MOD;
    ret *= gyaku_kaijo(n-r); ret %= MOD;
  }else{
    rep(i,r){
      ret *= n-i; ret %= MOD;
      ret *= mpow(r-i, MOD-2); ret %= MOD;
    }
  }
  return ret;
}



int main(void){
  fast_io
  cout<<fixed<<setprecision(15);

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

  ll p = 1;
  ll ans = 0;
  rep(i,n){
    if(A[i] == p){
      p++;
      continue;
    }
    ans += (A[i] - 1) / p;
    if(p==1){
      p++;
    }
  }

  cout<<ans<<endl;
}

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

for i in range(n):
    x[i] -= a

dp = [0] * (2 * n * 50 + 2)
dp[0] = 1

for i in range(n):
    h = dp[:]
    for j in range(-(n - 1) * 50, (n - 1) * 50 + 1):
        h[j] += dp[j - x[i]]
    dp = h

print(dp[0] - 1)

Measured memory_bytes: 40176
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll>vi;
vector<ll> :: iterator vt;
ll arr[200009];
int main()
{
    ll n,t,x,y,z,j,i,k,flag =0,maxi=LONG_LONG_MIN ,mini=LONG_LONG_MAX,sum=0,c=0;
    map<ll,ll>mp;
    map<ll,ll> :: iterator mt;
    string s, ss;
    cin>>x>>y>>z;
    maxi= max(max(x,y),z);
    sum+= abs(maxi -y) + abs(maxi-x) + abs(maxi -z);
    if(sum % 2 == 0)
        cout<<sum/2<<endl;
    else
    {
        sum+=3;
        cout<<sum/2<<endl;
    }

}


Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main()
{
	int n;
	cin>>n;
	int a[n+1];
	int b[n+1];
	for(int i=1;i<=n;i++)
	{
		cin>>a[n];
		b[a[n]]=i;
	}
	for(int i=1;i<=n;i++)
	{
		cout<<b[i]<<" ";
	 } 
} 

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

namespace Competition
{
    class Program
    {
        static void Main(string[] args)
        {
            var ab = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var a = ab[0]; var b = ab[1];

            if ((2 * b) < a) Console.WriteLine(a - 2 * b);
            else Console.WriteLine(0);
        }
    }
}


Measured memory_bytes: 13268
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep3(i, l, n) for (int i = l; i < n; ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define out(a) cout << a << endl
#define SZ(v) (int)v.size()
#define inf (int)(1e9+7)

int main() {
    string s; cin >> s;
    while (SZ(s)) {
        s.pop_back();
        if (SZ(s) % 2 != 0) continue;
        string a, b;
        a = s.substr(0, SZ(s) / 2);
        b = s.substr(SZ(s) / 2, SZ(s) / 2);
        if (a == b) break;
    }
    out(SZ(s));
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <list>
#include <queue>
using namespace std;

int inf = (1 << 30);
int mod = 1e9 + 7;
int64_t inf64 = (1LL << 60);

template <typename T>
T gcd(T a, T b) {
    if(b == 0) return a;
    else return gcd(b, a % b);
}

template <typename T>
T lcm(T a, T b) {
    T g = gcd(a, b);
    return a / g * b;
}

bool check(vector<int> a, int index, int key){
    if(a[index] >= key) return true;
    else return false;
}

int lower_bound(vector<int> a, int key){
    int left = -1, right = a.size();
    while(right - left > 1){
        int mid = left + (left + right) / 2;
        if(check(a, mid, key)) right = mid;
        else left = mid;
    }
    return right;
}

int main(){

    int H, W;
    cin >> H >> W;
    vector<vector<char>> C(H, vector<char>(W));
    for(int i = 0; i < H; i++) for(int j = 0; j < W; j++) cin >> C[i][j];

    for(int i = 0; i < 2 * H; i++){
        for(int j = 0; j < W; j++){
            cout << C[i / 2][j];
        }
        cout << endl;
    }

    return 0;
}

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

int main(){

  int N, l, r, sub;
  int total = 0;
  cin >> N;

  for (int i = 0; i < N; i++){
    cin >> l >> r;
    sub = r - l + 1;
    total += sub;
  }

  cout << total << endl;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main (){

  int l,a,count=0;
  cin >> l;
  for(int i=1; i<=l; i++){
    cin >> a;
    if(a%2==0){
      if(a%3==0 or a%5==0){
        count++;
      }
    }else{
      count++;
    }
  }

  if(count==l){
    cout<<"APPROVED"<<endl;
  }else{
    cout << "DENIED" << endl;
  }
}


Measured memory_bytes: 256
#include<stdio.h>

int main(void){
  int x,count[101],i,max=0;
  
  for(i=0;i<101;i++)
    count[i]=0;
  
  while(scanf("%d",&x)!=EOF){
    count[x]++;
  }

  for(i=0;i<101;i++)
    if(max<count[i])max=count[i];

  for(i=0;i<101;i++)
    if(count[i]==max)printf("%d\n",i);
  return 0;
}

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

using namespace std;

const int MAX_S = 300;
const int MAX_K = MAX_S;
int64_t dp[MAX_S+2][MAX_S+2][MAX_K+1];

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

  s = "0" + s;

  int n = s.size() - 1;

  for (int l = 0; l < n+2; l++)
    for (int r = 0; r < n+2; r++)
      for (int c = 0; c <= k; c++)
        dp[l][r][c] = 0;

  for (int l = n; l > 0; l--) {
    for (int r = l; r <= n; r++) {
      for (int c = 0; c <= k; c++) {
        if (s[l] == s[r])
          dp[l][r][c] = dp[l+1][r-1][c] + (l == r ? 1 : 2);
        dp[l][r][c] = max(dp[l][r][c], max(dp[l+1][r][c], dp[l][r-1][c]));
          if (c != 0)
            dp[l][r][c] = max(dp[l][r][c], dp[l+1][r-1][c-1] + (l == r ? 1 : 2));
      }
    }
  }
  cout << dp[1][n][k] << endl;
  
  return 0;
}


Measured memory_bytes: 214656
#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define int long long
#define gok ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
# define mo 1000000007

 signed main(){
    gok
   int a,b,m;
   cin>>a>>b>>m;
   int a1[a],b1[b],m1=100005,m2=100005;
   for(int i=0;i<a;i++){
    cin>>a1[i];
    m1 = min(m1,a1[i]);
   }
   for(int i=0;i<b;i++){
    cin>>b1[i];
    m2 = min(m2,b1[i]);
   }
   int an = m1+m2;
   while(m--)
   {
       int x,y,z;
       cin>>x>>y>>z;
       int cc = a1[x-1] + b1[y-1];
       an = min(an,cc-z);
   }
   cout<<an;
    return 0;
}


Measured memory_bytes: 1792
#include <cstdio>
#include <stack>
#include <cstring>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <climits>

using namespace std;

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main()
{
	char str1[8], str2[8];
	while(scanf("%s%s", str1, str2), strcmp(str1, "0")){
		int hit=0, blow=0;
		rep(i, 4){
			if(str1[i] == str2[i])
				++hit;
			else
				blow += find(str2, str2+4, str1[i]) != str2+4;
		}
		printf("%d %d\n", hit, blow);
	}
	return 0;
}

Measured memory_bytes: 968
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <unordered_map>
#include <cmath>
#include <numeric>
#include <cstdlib>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

int main(){

    cin.tie(0);
    ios::sync_with_stdio(false);

    long long a,b,c,k;
    cin >>a >>b >> c >>k;

    long long ans = 0;

    if(a<k){

        ans += a;
    }else{
        ans += k;
    }




    long long c_ = k-a-b;

    if(c_>0){
        ans-=c_;
    }

    cout << ans;


    return 0;
}

Measured memory_bytes: 3664
use std::io::{BufRead, Write};
use std::io;
use std::iter;

fn solve<R: BufRead, W: Write>(mut input: R, mut output: W) {
    let mut buf = String::new();
    input.read_line(&mut buf).unwrap();
    let n: usize = buf.trim_right().parse().unwrap();
    let xy: Vec<(i64, i64)> = (0..n).map(|_| {
        buf.clear();
        input.read_line(&mut buf).unwrap();
        let mut split = buf.split_whitespace();
        (split.next().unwrap().parse().unwrap(), split.next().unwrap().parse().unwrap())
    }).collect();
    for i in 0..n {
        let mut angles: Vec<f64> = (0..n).filter(|&j| j != i).map(|j| {
            let (a_x, a_y) = xy[i];
            let (b_x, b_y) = xy[j];
            ((b_y - a_y) as f64).atan2((b_x - a_x) as f64)
        }).collect();
        angles.sort_by(|a, b| a.partial_cmp(b).unwrap());
        let max_diff: f64 = angles.windows(2).map(|w| w[1] - w[0]).chain(
            iter::once(angles[0] - angles.last().unwrap() + 2. * std::f64::consts::PI)
        ).max_by(|a, b| a.partial_cmp(b).unwrap()).unwrap();
        writeln!(output, "{}", (max_diff / (2. * std::f64::consts::PI) - 0.5).max(0.)).unwrap();
    }
}

fn main() {
    let stdin = io::stdin();
    let stdout = io::stdout();
    solve(stdin.lock(), stdout.lock());
}

#[cfg(test)]
mod tests {
    fn test(input: &str, output: &str) {
        let mut v: Vec<u8> = Vec::new();
        ::solve(input.as_bytes(), &mut v);
        assert_eq!(String::from_utf8(v).unwrap(), output);
    }

    #[test]
    fn case1() {
        test(
            "2
0 0
1 1
",
            "0.5
0.5
",
        );
    }

    #[test]
    fn case2() {
        test(
            "5
0 0
2 8
4 5
2 6
3 10
",
            "0.43160120892732328768
0.03480224363653196956
0.13880483535586193855
0.00000000000000000000
0.39479171208028279727
",
        );
    }
}

Measured memory_bytes: 4476
//在 POJ 和小白菜中，万能库禁用！
//在 USACO中请用文件。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#define inf 2139062143   //2147483647.
#define mod 1000000007
//data:
using namespace std;
string s;
bool a[100005];
int ans,st,n;
bool OK(int k){
	if (2*k >= n) {
        for (int i=n-k+1;i<=k;i++) {
            if (a[i]!=a[n-k+1]) return false;
        }
        return true;
    } else {
        return true;
    }
}
int b_s(){
	int lft=1,rig=n;
	while(lft<rig){
		int mid=(lft+rig+1)/2;
		if(OK(mid)) lft=mid;
		else rig=mid-1;
	}
	return rig;
}
int main(){
//	freopen("ryz.in","r",stdin);
//	freopen("my.out","w",stdout);
//	ios::sync_with_stdio(false);
	cin>>s;
	n=s.size();
	a[1]=s[0]-'0';
	for(int i=2;i<=n;i++){
		a[i]=s[i-1]-'0';
		if(a[1]==0) a[i]=1-a[i];
	}
 	if(a[1]==0) a[1]=1;
 	ans=b_s();
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 512
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
#define Maxn 1005
int x[Maxn],y[Maxn];
bool flag=false;
bool ap[2];
int Move[Maxn][45];
bool in(int u,int d){
	ll dis=(ll)abs(x[u])+(ll)abs(y[u]);
	int up=((1<<d)-1)*2+1;
	return dis<=up;
}
void MMove(int u,int d){
	if(d==0){
		if(x[u]==1&&y[u]==0)Move[u][++Move[u][0]]=0;
		if(x[u]==-1&&y[u]==0)Move[u][++Move[u][0]]=1;
		if(x[u]==0&&y[u]==1)Move[u][++Move[u][0]]=2;
		if(x[u]==0&&y[u]==-1)Move[u][++Move[u][0]]=3;
		return;
	}
	x[u]-=(1<<d);
	if(in(u,d-1)){
		Move[u][++Move[u][0]]=0;
		return;
	}
	x[u]+=(1<<d);
	x[u]+=(1<<d);
	if(in(u,d-1)){
		Move[u][++Move[u][0]]=1;
		return;
	}
	x[u]-=(1<<d);
	y[u]-=(1<<d);
	if(in(u,d-1)){
		Move[u][++Move[u][0]]=2;
		return;
    }
	y[u]+=(1<<d);
	y[u]+=(1<<d);
	if(in(u,d-1)){
		Move[u][++Move[u][0]]=3;
		return;
	}
}
int main(){
	scanf("%d",&n);
	scanf("%d%d",&x[1],&y[1]);
	int now;
	now=((x[1]+y[1])%2+2)%2;
	ap[now]=true;
	for(int i=2;i<=n;++i){
		scanf("%d%d",&x[i],&y[i]);
		now=((x[i]+y[i])%2+2)%2;
		if(!ap[now]){
			puts("-1");
			return 0;
		}
	}
	if(ap[0]){
		flag=true;
		for(int i=1;i<=n;++i)x[i]--;
	}
    int all;
    for(all=0;all<=30;++all){
    	bool hehe=true;
    	for(int i=1;i<=n;++i)
    		if(!in(i,all)){
    			hehe=false;
    			break;
    		}
    	if(hehe)break;
    }
    if(flag)printf("%d\n",all+2);
    else printf("%d\n",all+1);
    for(int i=all;i>=0;--i){
    	for(int j=1;j<=n;++j)MMove(j,i);
    }
    if(flag)printf("1 ");
    for(int i=0;i<=all;++i)printf("%d ",1<<i);
    puts("");
    for(int i=1;i<=n;++i){
    	if(flag)printf("R");
    	for(int j=Move[i][0];j>=1;--j){
    		if(Move[i][j]==0)printf("R");
    		if(Move[i][j]==1)printf("L");
    		if(Move[i][j]==2)printf("U");
    		if(Move[i][j]==3)printf("D");
    	}
    	puts("");
    }
    return 0;
}

Measured memory_bytes: 512
#include <vector>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <map>
#include <complex>
#include <ctime>
#include <numeric>
#include <set>

using namespace std;

typedef long long ll;

typedef struct node{
    int s, e, val, pos;
    bool operator<(const node& n) const{
        if(e != n.e) return e < n.e;
        return val > n.val;
    }
    node(){};
    node(int ss, int ee, int v, int p){
        s = ss, e = ee, val = v, pos = p;
    }
}node;

int occs[(int)1e6 + 5], tree[500005], n, ans[500005];

void update(int x, int val){
    while(x <= n){
        tree[x] += val;
        x += x & -x;
    }
}

int query(int x){
    int ret = 0;
    while(x > 0){
        ret += tree[x];
        x -= x & -x;
    }
    return ret;
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
    
    int q, s, e;
    vector<node> v;
    scanf("%d", &n);
    scanf("%d", &q);
    
    for(int i = 1; i <= n; i++){
        scanf("%d", &s);
        v.push_back(node(i, i, s, -1));
    }
    for(int i = 0; i < q; i++){
        scanf("%d %d", &s, &e);
        v.push_back(node(s, e, 0, i));
    }
    
    sort(v.begin(), v.end());
    
    for(int i = 0;i < v.size(); i++){
        if(v[i].val == 0){
            ans[v[i].pos] = query(v[i].e) - query(v[i].s - 1);
        }else{
            if(occs[v[i].val] != 0) update(occs[v[i].val], -1);
            update(v[i].s, 1);
            occs[v[i].val] = v[i].s;
        }
    }
    
    for(int i = 0; i < q; i++){
        printf("%d\n", ans[i]);
    }
    
    return 0;
}





Measured memory_bytes: 25388
#include<bits/stdc++.h>
using namespace std;
struct node{
	string s;
	int a;
	int id;
}b[105];
int cmp(node a,node b){
	if(a.s==b.s)
		return a.a>b.a;
	return a.s<b.s;
}
int main(){
	int n;
	int c[105];
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		cin>>b[i].s;
		cin>>b[i].a;
		b[i].id=i+1;
	}
	sort(b,b+n,cmp);
	for(int i=0;i<n;i++)
		printf("%d\n",b[i].id);
}

Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#ifndef FAST_IO
 #define IOS ios_base::sync_with_stdio(false);cin.tie(nullptr);
#endif

#define int long long
#define FILE_IO freopen("input.txt","r",stdin)/#define int long long
#define f first
#define s second
#define endl '\n'
#define all(c) c.begin(),c.end()
#define bp __builtin_popcount
#define debug(x) cerr << #x << " = " << x << endl;

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

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>void print(T t){for (auto &it : t) cout << it << " ";}
inline int powerexp(int a, int b, int m = MOD){ int res = 1;while(b){if(b&1)res = res * a % m;a = a * a % m,b >>= 1;}return res % m;}
inline int modinv(int a){ return powerexp(a,MOD-2,MOD);}
inline int lcm(int a, int b){ int g = __gcd(a,b);return a/g*b;}
inline int add(int x, int y){ x += y; if (x >= MOD) x -=  MOD; return x;}
inline int mul(int x, int y){ return (x * 1ll * y) % MOD;}
inline int sub(int x, int y){ x -= y; if (x<0) x += MOD; return x ;}

//-------------------< start here >-------------------------// 

void solve(){
    int n;
    cin >> n;
    vector<int> a(n), b(n), c(n - 1);
    for (auto& it : a) cin >> it;
    for (auto& it : b) cin >> it;
    for (auto& it : c) cin >> it;
    int ret = 0;
    for (auto it : b) ret += it;
    for (int i = 0; i + 1 < n; i++){
	  if (a[i + 1] == a[i] + 1){
		   ret += c[a[i] - 1]; 
		 }	
	}
	cout << ret << endl;
}

//-----------------------------
//comment from here for TOPCODER 
int32_t main(){
   IOS;
   solve();
   //find();
   #ifdef LOCAL_DEFINE
   cerr << "time elapsed " << (double)clock()/CLOCKS_PER_SEC << endl;
   #endif
}


Measured memory_bytes: 256
const teststr = 
"""
3 2
2
1 3
1
3
"""

function lines()
  Channel{String}(32) do ch
    lines = true ? readlines() : split(teststr, "\n")
    for s in lines
      put!(ch, s)
    end
  end
end

function main()
  ch = lines()
  N, K = parse.(Int, split(take!(ch)))
  nkashi = Dict(n=>0 for n in 1:N)
  for k in 1:K
	d = parse(Int, take!(ch))
    aks = parse.(Int, split(take!(ch)))
    @assert length(aks) == d
    for a in aks
      nkashi[a] += 1
    end
  end
  println(count(x->iszero(last(x)), nkashi))
end

main()


Measured memory_bytes: 199732
function main(lines) {
  const X = +lines[0];

  return 10 - Math.floor(X / 200);
}


const ans = main( require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n") );
console.log( ans );

function splitN(s) {
  return s.split(' ').map(Number);
}

Measured memory_bytes: 29676
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>
#include<deque>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;
typedef deque<int> di;

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

signed main(void) {
    int i,j;
    int n;
    cin >> n;
    vi a(n+2),dp(n+2);
    loop(i,1,n+1){
        cin >> a[i];
        if(a[i] == 0) dp[i] = INF;
    }
    //rep(i,n+2)cout << " " << dp[i]; cout << endl;
    loop(i,1,n+1){
        dp[i] = max(dp[i], dp[i-1] - 1);
        if(a[i] < 0)dp[i] = min(dp[i],-a[i]);
    }
    //rep(i,n+2)cout << " " << dp[i]; cout << endl;
    for(int i = n; i >= 0; i--){
        dp[i] = max(dp[i], dp[i+1] - 1);
        if(a[i] < 0)dp[i] = min(dp[i],-a[i]);
    }
    //rep(i,n+2)cout << " " << dp[i]; cout << endl;
    int ans = 0;
    rep(i,n+2)if(a[i] > 0){
        ans += min(max(dp[i],0LL), a[i]);
    }
    cout << ans << endl;
}



Measured memory_bytes: 4408
n = int(input())
d = {"AC": 0, "WA": 0, "RE": 0, "TLE": 0}
for i in range(n):
    a = input()
    if a == "AC":
        d["AC"] += 1
    elif a == "WA":
        d["WA"] += 1
    elif a == "RE":
        d["RE"] += 1
    elif a == "TLE":
        d["TLE"] += 1
print("AC x", d["AC"])
print("WA x", d["WA"])
print("TLE x", d["TLE"])
print("RE x", d["RE"])


Measured memory_bytes: 9204
#include <bits/stdc++.h>
#include<math.h>
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main() {
  int x,a,b;
  cin>>x>>a>>b; 
  
    if(abs(x-a)<abs(b-x)){
      cout<<"A"<<endl;;
    }
    else{
      cout<<"B"<<endl;
    }
  
  
}



Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#define nmax_def 1100
#define Cmax_def 2200
#define mod 1000000007
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;

int main() {
	int H, W;
	int ans = -1;
	cin >> H >> W;
	bool A[nmax_def][nmax_def];
	vector<pii> BFS_que;
	rep(i, H) {
		string S;
		cin >> S;
		rep(j, W) {
			if (S[j] == '#') {
				BFS_que.push_back(pii(i, j));
				A[i][j] = 1;
			}
			else
				A[i][j] = 0;
		}
	}
	while (BFS_que.size() != 0) {
		vector<pii> BFS_que_nxt;
		int N = BFS_que.size();
		rep(i, N) {
			vector<pii> Pnei;
			pii P = BFS_que[i];
			int X = P.first;
			int Y = P.second;
			if (X != 0) {
				Pnei.push_back(pii(X - 1, Y));
			}
			if (X != H - 1) {
				Pnei.push_back(pii(X + 1, Y));
			}
			if (Y != 0) {
				Pnei.push_back(pii(X, Y - 1));
			}
			if (Y != W - 1) {
				Pnei.push_back(pii(X, Y + 1));
			}
			int M = Pnei.size();
			rep(j, M) {
				int x = Pnei[j].first;
				int y = Pnei[j].second;
				if (!A[x][y]) {
					A[x][y] = 1;
					BFS_que_nxt.push_back(pii(x, y));
				}
			}
		}
		BFS_que = BFS_que_nxt;
		ans++;
	}
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 14440
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;

const int MAXN = 200005;

void no() {
	puts("No");
	exit(0);
}

void yes() {
	puts("Yes");
}

vector <int> sol;
int a[MAXN];
int curr[MAXN];

int median(int p, int q, int r) {
	if (p > q) swap(p, q);
	if (p > r) swap(p, r);
	if (q > r) swap(q, r);
	return q;
}

int main() {
	int N, X;
	scanf("%d%d", &N, &X);
	
	if (X == 1 || X == 2 * N - 1)
		no();
	
	for (int i = 1; i < 2 * N; i++)
		if (i != X)
			sol.push_back(i);
			
	a[N - 1] = X;
	for (int i = 0; i < sol.size(); i++)
		a[(N + i) % (2 * N - 1)] = sol[i];
	
	yes();
	for (int i = 0; i < 2 * N - 1; i++)
		printf("%d\n", a[i]);
	
	/*for (int k = 1; k < N; k++) {
		for (int i = k; i < 2 * N - 1 - k; i++)
			curr[i] = median(a[i - 1], a[i], a[i + 1]);
		for (int i = k; i < 2 * N - 1 - k; i++)
			a[i] = curr[i];
	}
	
	printf("%d %d\n", X, a[N - 1]);*/
		
	return 0;
}

Measured memory_bytes: 3188
using System;
using System.Linq;
using System.Threading.Tasks;
using static System.Console;
using static System.Math;
class program{
  static void Main(string[] args){
    program p = new program();
    p.Sta();
  }
  void Sta(){
    WriteLine(Run());
  }
  string Run(){
	string[] inp = ReadLine().Split();
    int v = 0;
    int n = int.Parse(inp[0]), m = int.Parse(inp[1]);
    int ac=0,wa=0;
    int[] prob = new int[n+1];
    int[] penal = new int[n+1];
    for (int i=0;i<m;i++){
      inp = ReadLine().Split();
      v = int.Parse(inp[0]);
      if (prob[v]==0 & inp[1]=="WA")
        penal[v]++;
      else if (prob[v]==0 & inp[1]=="AC"){
        prob[v]=i+1;
        wa += penal[v];
        ac ++;
      }
    }
    return ac.ToString() + " " + wa.ToString();
  }
}


Measured memory_bytes: 15712