akhauriyash/Code-Regression huggingface dataset:
/*  -*- coding: utf-8 -*-
 *
 * 3042.cc: Gridgedge
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_H = 500;
const int MAX_W = 500;
const int INF = 1 << 30;
const int MOD = 1000000007;

/* typedef */

typedef pair<int,int> pii;
typedef queue<pii> qpii;

/* global variables */

int ds[MAX_H][MAX_W], cs[MAX_H][MAX_W];

/* subroutines */

inline void check(int vx, int vy, int vd, int uc, qpii &q) {
  if (ds[vy][vx] > vd) {
    ds[vy][vx] = vd, cs[vy][vx] = uc;
    q.push(pii(vx, vy));
  }
  else if (ds[vy][vx] == vd)
    cs[vy][vx] = (cs[vy][vx] + uc) % MOD;
}

/* main */

int main() {
  int h, w, sy, sx, gy, gx;
  scanf("%d%d%d%d%d%d", &h, &w, &sy, &sx, &gy, &gx);

  for (int y = 0; y < h; y++) fill(ds[y], ds[y] + w, INF);

  ds[sy][sx] = 0, cs[sy][sx] = 1;
  qpii q;
  q.push(pii(sx, sy));

  while (! q.empty()) {
    pii u = q.front(); q.pop();
    int &ux = u.first, &uy = u.second;
    if (ux == gx && uy == gy) continue;

    int vd = ds[uy][ux] + 1, uc = cs[uy][ux];

    if (ux > 0) {
      check(ux - 1, uy, vd, uc, q);
      check(0, uy, vd, uc, q);
    }
    if (ux + 1 < w) {
      check(ux + 1, uy, vd, uc, q);
      check(w - 1, uy, vd, uc, q);
    }
    if (uy > 0) {
      check(ux, uy - 1, vd, uc, q);
      check(ux, 0, vd, uc, q);
    }
    if (uy + 1 < h) {
      check(ux, uy + 1, vd, uc, q);
      check(ux, h - 1, vd, uc, q);
    }
  }

  printf("%d %d\n", ds[gy][gx], cs[gy][gx]);
  return 0;
}



Measured memory_bytes: 5156
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char tmp[2000];
int num[2000];
int main() {
//	freopen("in","r",stdin);
	memset(num,0,sizeof(num));
	while(cin.getline(tmp,2000,'\n')) {
		for(int i=0;i<strlen(tmp);i++) {
			if('A'<=tmp[i]&&tmp[i]<='Z') tmp[i] += 32;
		}

		for(int i=0;i<strlen(tmp);i++) {
			num[tmp[i]]++;
		}
	}

	for(int i='a';i<='z';i++) {
			printf("%c : %d\n",i,num[i]);
		}
	
	return 0;
}


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

char str[50];

int main()
{
	cin >> str;
	if (str[0] == 'Y' && str[1] == 'A' && str[2] == 'K' && str[3] == 'I') {
		puts("Yes");
	} else puts("No");
	return 0;
}


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

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

    int r = 100;
    for (int k = 0; k < 26; k++) {
        string t = s;
        char c = 'a' + k;
        for (int j = 0; j < n; j++) {
            if (all_of(t.begin(), t.end() - j, [&](char a) { return a == c; })) {
                r = min(r, j);
                break;
            }
            int i1 = n - 1 - j;
            for (int i = 0; i < i1; i++) {
                t[i] = t[i] == c ? t[i] : t[i + 1];
            }
        }
    }

    cout << r << endl;

    return 0;
}

Measured memory_bytes: 256
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

# 最大公約数 
def gcd(a, b):
  if a < b:
      a, b = b, a
  while a % b != 0:
      a, b = b, a % b
  return b

# 最小公倍数
def lcm(a, b):
  y = a*b / gcd(a, b)
  return int(y)

n, m = MAP()


L = []
for i in range(m):
  l = S_LIST()
  L.append(l)

# wa

wa = [0] * n
ac = [0] * n


for i in range(m):
  p = int(L[i][0])-1
  ans = L[i][1]
  if ans == "WA":
    # まだacない
    if ac[p] == 0:
      wa[p] = wa[p] + 1
  # acあった
  if ans == "AC":
    ac[p] = 1

for i in range(n):
  if ac[i] == 0:
    if wa[i] > 0:
      wa[i] = 0


print(sum(ac), sum(wa))


# for p in range(1, n+1):
#   if L[i][0] == str(p):
#     for i in range(m):
#       if L[i][1] == "WA":
#         wa += 1

#     for i in range(m):
#       if L[i][1] == "AC":
#         ac += 1
#   print(wa)
#   print(ac)

  

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

typedef long long   ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll>    pl;
typedef pair<ld,ld>    pd;

typedef vector<int> vi;
typedef vector<ll>  vl;
typedef vector<ld>  vd;
typedef vector<pi>  vpi;
typedef vector<pl>  vpl;
typedef vector<cd>  vcd;

#define rep(i, n)    for (int i = 0 ; i < (n) ; i++)
#define rep1(i, n)   for (int i = 1 ; i <= (n) ; i++)
#define repd(i,n)    for (int i = (n)-1 ; i >= 0 ; i--)
#define For(i, a, b) for (int i = (a) ; i< (b) ; i++)
#define Ford(i,a,b)  for (int i = (b)-1 ; i >= (a) ; i--)

    
#define co(x)   cout << (x) << "\n"
#define cos(x)  cout << (x) << " "

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

#define priority_queue pq;

#define Would
#define you
#define please

const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int MX = 100001; //check the limits

void fastIOS()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
}
// ========================================================================================================
// ========================================================================================================

// read the question correctly (ll vs int)

int main()
{
	fastIOS();

    string s; cin >> s;
	int q; cin >> q;
    vector<char> bef , aft;
    bool x = false;
    while(q--)
    {
        int t; cin >> t;
        if(t==2)
        {
            int f; char c;
            cin >> f >> c;
            
            if (f == 1 && !x || f == 2 && x) {
                bef.pb(c);
            } else {
                aft.pb(c);
            }
        }
        else{
            x = !x;
        }
    }
    
    if(x)
    {
        repd(i,sz(aft)) { cout << aft[i]; }
        reverse(all(s));
        cout << s;
        rep(i,sz(bef)) { cout << bef[i]; }
        cout << endl;
    }
    else{
        repd(i,sz(bef)) { cout << bef[i]; }
        cout << s;
        rep(i,sz(aft)) { cout << aft[i]; }
        cout << endl;
    }
    
    Would you please return 0;
}

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

#define REP(i,s,n) for(int (i)=s; (i)<(int)(n);(i)++)
#define RIT(it,c) for(__typeof(c.begin()) it = c.begin();it!=c.end();it++)
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)(x).size()
#define MSET(m,v) memset(m,v,sizeof(m))


using namespace std;


typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<LL> vL;
typedef vector<bool> vb;
typedef pair<int,vi> iv;
typedef pair<LL,LL> pLL; 



int main(){
	string s;
	cin>>s;
	if(s.find('9')!=string::npos) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	
	return 0; 
}


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

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

    string s;
    cin >> s;
    if (k < s.size()) {
        for (int i = 0; i < k; i++) {
            cout << s.at(i);
        }
        cout << "..." << endl;
    }
    else {
        for (int i = 0; i < s.size(); i++) {
            cout << s.at(i);
        }
    }
}

Measured memory_bytes: 3604
#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;

int main() {
int N,L;
cin >> N >> L;
vector<int> vec(N);
int gou = 0;
for (int i = 0;i < N;i++) {
    vec.at(i) = L+i;
    gou += vec.at(i);
}
if (L > 0) cout << gou-vec.at(0) << endl;
else {
    int mini = INF;
    int ans = 0;
    for (int i = 0;i < N;i++) {
        if (vec.at(i) == 0) {
            ans = 0;
            break;
        }else if (abs(vec.at(i)) < mini) mini = abs(vec.at(i));
        ans = vec.at(i);
    }
    cout << gou - ans << endl;
}
return 0;
}


Measured memory_bytes: 256
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")
}

fn main() {
    let l: f64 = read();
    let w: f64 = l / 3.0;

    let ans = w * w * w;
    println!("{}", ans);
}


Measured memory_bytes: 4352
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void){
  int n,m;
  int min;
  int i,j,k;
  int ans=0;
  scanf("%d %d",&n,&m);

  for(i=0;i<m/2;i++){
    if(m%(i+n)==0){
      printf("%d\n",m/(i+n));
      return 0;
    }
  }
  printf("1\n");
  return 0;
}


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

int main(){
  int n,k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i,n) cin >> v[i];
  sort(v.begin(),v.end());
  int ans = 0;
  rep(i,k){
    ans += v[i];
  }
  cout << ans << endl;


  return 0;
}


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

#define FOR(i, j, k) for (int i = j; i < k; i++)
#define REP(i, j) FOR(i, 0, j)
#define fst first
#define snd second
#define pb push_back
#define eb emplace_back
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  int n;
  cin >> n;
  vector<string> s(n);
  cin >> s;

  int a = 0;
  int b = 0;
  int ba = 0;

  ll ans = 0;
  REP(i, n) {
    if (s[i][0] == 'B' and s[i].back() == 'A')
      ba++;
    else if (s[i][0] == 'B')
      a++;
    else if (s[i].back() == 'A')
      b++;

    REP(j, s[i].size() - 1) {
      if (s[i][j] == 'A' and s[i][j + 1] == 'B')
        ans++;
    }
  }

  ans += min(a, b);
  if (ba > 1)
    ans += ba - 1;
  if (ba > 0 and (a > 0 or b > 0))
    ans++;

  cout << ans << endl;

  return 0;
}


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

int main(){
int N;
cin>>N;
vector<int> A(N+1);
vector<int> B(N);
for(int i=0;i<=N;i++)cin>>A[i];
for(int i=0;i<N;i++)cin>>B[i];
ll a=0;
 for(int i=0;i<N;i++){
    int x=min(A[i],B[i]);
    a+=x;
    B[i]-=x;
    x=min(A[i+1],B[i]);
    A[i+1]-=x;
    a+=x;
 }
 cout<<a<<endl;
return 0;
}

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


int main() {
    int A, B, C;
    cin>>A>>B>>C;
    long long ans = 0;
    if(A > C) {
        ans = C + B;
    }
    else {
        if(C <= A + B) {
            ans = C + B;
        }
        else {
            ans = (A + B) + B + 1;
        }
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n, k, x, y, s = 0;
	cin >> n >> k >> x >> y;
	s += x * ((k < n)?k:n) + ((k < n)?(n-k)*y:0);
	cout << s << '\n';
	return 0;
}

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

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define for1(i, n) for (int i = 1; i <= int(n); ++i)
#define F first
#define S second
#define all(x) (x).begin(),(x).end()
#define sz(x) int(x.size())
#define pb push_back

using namespace std;
using ll=long long;
using pii= pair<int, int>;
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
template<typename... T> void rd(T&... args) {((cin>>args), ...);}
template<typename... T> void wr(T... args) {((cout<<args<<" "), ...);cout<<endl;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    const int mod=1e9+7;
    int n,k;
    cin>>n>>k;
    vector<int> pos,neg;
    forn(i,n){
        int x;
        cin>>x;
        if(x>0) pos.pb(x);
        else neg.pb(x);
    }
    ll res=1;
    if(k==n){
        for(auto it:pos) (res*=it)%=mod;
        for(auto it:neg) (res*=it)%=mod;
    }else{
        sort(pos.rbegin(),pos.rend());
        sort(all(neg));
        if(k%2){
            if(pos.empty()){
                for(int i=sz(neg)-1;i>=sz(neg)-k;i--) (res*=neg[i])%=mod;
                goto end;
            }
            res=pos[0];
            pos.erase(pos.begin());
            k--;
        }
        vector<ll> pair;
        for(int i=0;i+1<sz(pos);i+=2) pair.pb((ll)pos[i]*pos[i+1]);
        for(int i=0;i+1<sz(neg);i+=2) pair.pb((ll)neg[i]*neg[i+1]);
        sort(pair.rbegin(),pair.rend());
        forn(i,k/2){
            (res*=pair[i]%mod)%=mod;
        }
    }
end:
    cout<<(res+mod)%mod;
    return 0;
}



Measured memory_bytes: 5516
r, c = gets.split.map(&:to_i)

a = r.times.map { gets.split.map(&:to_i) }
a.each { |line| line.push line.sum }

bottom = (0..c).map do |col|
  r.times.map { |row| a[row][col] }.sum
end

a.push bottom

a.each do |line|
  puts line.join(' ')
end



Measured memory_bytes: 7212
### SNIPPET
  # n = gets.split.map(&:to_i)
  # array = n.times.map { gets.split.map(&:to_i) }
  # [].all?(&:even?)
  # a = [*1..m].repeated_combination(n).to_a
  # [1,2,3,4,5].select { |num| num.even? }  # => [2, 4]
  # ["a","a","b"].group_by(&:itself).map{|k,v| [k, v.count]}.to_h #=> {"a"=>2, "b"=>1}
  # 切り捨て: .floor(2).to_f ,切り上げ： .ceil(2).to_f,四捨五入: round(2)
  # 3.upto(6) do |i|, 6.downto(3) do |i|
  # 公約数125.gcd(100)、公倍数125.lcm(100)
  # PI = Math::PI
  # 高さ = a * Math.sin(w / 180.0 * Math::PI), 底辺 = a * Math.cos(w / 180.0 * Math::PI)
  def chmax(a, b) a > b ? a : b end
  # INF = Float::INFINITY
  # def chmin(a, b) a < b ? a : b end

n = gets.to_i
a = gets.split.map(&:to_i)

ladder_sum = 0
height = Array.new(n+1)
height[0] = a[0]
1.upto(n) do |i|
  height[i] = chmax(height[i-1], a[i-1])
  ladder_sum += height[i] - a[i-1] if height[i] - a[i-1] >= 0
end
puts ladder_sum


Measured memory_bytes: 29688
fun main(args: Array<String>): Unit {
    readLine()!!;
    println(readLine()!! // "abc xyz"
        .split(" ") // ["abc", "xyz"]
        .map { it.toList() } //[['a', 'b', 'c'], ['x', 'y', 'z']]
        .let { it[0].zip(it[1]).map { it.toList() } } // [('a', 'x'), ('b', 'y'), ('c', 'z')]
        .flatten() // ['a', 'x', 'b', 'y', 'c', 'z']
        .joinToString("")); // "axbycz"
}

Measured memory_bytes: 36004
N = input()
A = [int(x) for x in input().split()]
Alist=[]
count=0
for a in A:
    while(True):
        if a%2==0:
            count+=1
            a/=2
        else:
            Alist.append(count)
            count=0
            break
print(min(Alist))

Measured memory_bytes: 3060
#include <iostream>
#include <vector>
#include <utility>
#define lim 100010

using namespace std;

typedef long long int LL;
typedef pair<int, int> P;

vector<P> Graph[lim];
LL depth[lim];

void dfs (int v, int p, LL d) {
    depth[v] = d;
    for (int i = 0; i < Graph[v].size(); i++) {
        if (Graph[v][i].first == p) continue;
        dfs(Graph[v][i].first, v, d + Graph[v][i].second);
    }
}

int main() {
    int N, a, b, c;
    int Q, K, x, y;

    cin >> N;

    for (int i = 0; i < N - 1; i++) {
        cin >> a >> b >> c;
        a--, b--;
        Graph[a].push_back(P(b, c));
        Graph[b].push_back(P(a, c));
    }

    cin >> Q >> K;
    K--;

    dfs(K, -1, 0);

    for (int i = 0; i < Q; i++) {
        cin >> x >> y;
        x--; y--;
        cout << depth[x] + depth[y] << endl;
    }
    return 0;
}

Measured memory_bytes: 14336
#include <bits/stdc++.h>
#define int long long
#define ALL(v) (v).begin(),(v).end()
#define Vi vector<int>
#define pb push_back
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repr(i,a,b) for(int i=a;i>b;i--)
#define leng(n) (int)(log10(n)+1)
#define INF 9000000000000000000
using namespace std;

int gcd(int a,int b){return b?gcd(b,a%b):a;}  //  最大公約数gcd
int lcm(int a,int b){return a/gcd(a,b)*b;}  //  最小公倍数lcm

signed main() {
  int n , m;
  cin >> n >> m;
  vector<pair<int,int>> stu(n);
  vector<pair<int,int>> flag(m);
  rep(i,0,n){
    int a,b;
    cin >> a >> b;
    stu[i] = make_pair(a,b);
  }
  rep(i,0,m){
    int a,b;
    cin >> a >> b;
    flag[i] = make_pair(a,b);
  }
  vector<int> ans(n);
  int dismin = 0;
  int dis = 0;
  rep(i,0,n){
    dismin = INF;
    rep(j,0,m){
      dis = 0;
      dis += abs(stu[i].first-flag[j].first);
      dis += abs(stu[i].second-flag[j].second);
      if(dis < dismin) {
        dismin = dis;
        ans[i] = j+1;
      }
    }
  }
  rep(i,0,n) cout << ans[i] << endl;
}


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

typedef long long ll;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1e9 + 7;

template<class T> std::ostream& operator << (std::ostream& os, std::vector< T > v)
{
  ll i = 0;
  for (auto x : v) {os << " [" << i++ << "]" << x; if (i % 10 == 0) os << std::endl;}
  return os;
}

template<class T, class S> std::ostream& operator << (std::ostream& os, std::vector< std::pair<T, S> > vp)
{
  ll i = 0;
  for (auto p : vp) {os << " [" << i++ << "]" << p.first << " " << p.second; if (i % 10 == 0) os << std::endl;}
  return os;
}

void print(){ std::cout << std::endl; }
template<typename H> void print(H head) { std::cout << head << std::endl; }
template<typename H, typename... T> void print(H head, T... tail){ std::cout << head << " ", print(tail...); }

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

  std::string a, b, c;
  std::cin >> a >> b >> c;

  char str[3];
  str[0] = a[0];
  str[1] = b[1];
  str[2] = c[2];

  print(str);
  return 0;
}


Measured memory_bytes: 256
n,d =[int(i) for i in input().split()]
place=[[int(i) for i in input().split()] for j in range(n)]

count=0
for i in range(n-1):
    for j in range(i+1,n):
        a=place[i]
        b=place[j]
        distance=0
        for u in range(d):
            distance+=(a[u]-b[u])**2
        distance=distance**0.5
        
        if distance.is_integer():
            count+=1
        
        
print(count)        
        
    

Measured memory_bytes: 38640
#include <bits/stdc++.h>
#define Max_size 100000
using namespace std;
int main () {
    int n, a[Max_size], sum = 0;
    cin >> n;
    a[0] = 0;
    a[n + 1] = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        sum += abs(a[i - 1] - a[i]);
    }
    sum += abs(a[n] - a[n + 1]);
    for (int i = 1 ; i <= n ; i++){
        cout << sum - abs(a[i - 1] - a[i]) - abs(a[i] - a[i + 1]) + abs(a[i - 1] - a[i + 1]) << endl;
    }
    return 0;
}

Measured memory_bytes: 1664
#include <stdio.h>

int main(void) {
	int a,b,c,n=0,i;
	scanf("%d %d %d",&a,&b,&c);
	if(a>=1&&a<=10000&&b>=1&&b<=10000&&c>=1&&c<=10000){
		if(a<=b){
			for(i=a;i<=b;i++){
				if(c%i==0) n++;
			}
		}
	}
	printf("%d\n",n);
	return 0;
}



Measured memory_bytes: 2128
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,k,x,y;
int main(){
	scanf("%d%d%d%d",&n,&k,&x,&y);
	if(n<=k)printf("%d\n",n*x);
	else printf("%d\n",k*x+(n-k)*y);
	return 0;
}

Measured memory_bytes: 256
s=list(input())
t=list(input())

kainashi="UNRESTORABLE"
ans1=[]
for i in range(len(s)-len(t)+1):
    tmp=s[:]
    flag=1
    for j in range(len(t)):
        if s[len(s)-i-len(t)+j]==t[j] or s[len(s)-i-len(t)+j]=="?":
            tmp[len(s)-i-len(t)+j]=t[j]
        else:
            flag=0
        
    if flag==1:
        ans=""
        for k in range(len(s)):
            if tmp[k]=="?":
                tmp[k]="a"
            ans+=tmp[k]
        ans1.append(ans)

if len(ans1)>0:
    ans1.sort()
    print(ans1[0])
else:
    print(kainashi)

Measured memory_bytes: 38640
#include <stdio.h>

int main(void){

  int n;
  scanf("%d", &n);
  int d[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &d[i]);
  }

  int count = 0;
  int flag;
  for (int i = 0; i < n; i++) {
    flag = 1;
    for (int j = 0; j < i; j++) {
      if (d[j] == d[i]) {
        flag = 0;
        break;
      }
    }
    if (flag == 1) {
      count++;
    }
  }
  printf("%d\n", count);

  return 0;
}

Measured memory_bytes: 128
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
typedef long long ll;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0) ; 
    int n , k ; 
    cin >> n >> k ; 
    vector<int> v(n) ; 
    for(int &x : v){
    	cin >> x ; 
    }
    sort(v.begin(),v.end());
    int ans = 1e9 + 10 ; 
    for(int i=0;i<=n-k;i++){
    	ans = min(ans, v[i+k-1] - v[i] );
    }
    cout << ans << endl;
    return 0 ;
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define mp make_pair
#define fi fisrt
#define se second

using namespace std;
const int N=1e5+5;
const int mod=1e9+7;
const int inf=0x3f3f3f3f3f3f3f;


signed main()
{
	int n;
	cin>>n;
	int maxx=-inf;
	int ans=0;
	for (int i=1;i<=n;i++)
	{
		int a;
		cin>>a;
		if (i==1) maxx=a;
		else
		{
			if (a>maxx) maxx=a;
			else ans+=maxx-a;
		}
	} 
	cout<<ans<<endl;
}


Measured memory_bytes: 3596
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

#define mp make_pair
#define pb push_back
#define pf push_front
#define ss second
#define ff first
#define sz(x) (int)x.size()
#define newl "\n"
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define vl vector<ll>
#define pll pair<ll, ll>
#define vll vector<pll>
#define coutp cout << fixed << setprecision(12)
#define mem(x, val) memset(x, val, sizeof(x))
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define all(v) (v).begin(), (v).end()

const ld pi = 3.14159265359;
ll INF = 1e18 + 10;
ll MOD = 998244353;
ll mod = 1e9 + 9;

inline ll add(ll a, ll b, ll m)
{
    if ((a + b) >= m)
        return (a + b) % m;
    return a + b;
}

inline ll mul(ll a, ll b, ll m)
{
    if ((a * b) < m)
        return a * b;
    return (a * b) % m;
}

ll power(ll x, ll y, ll m)
{
    ll res = 1;
    x = x % m;
    if (x == 0)
        return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % m;
        y = y >> 1;
        x = (x * x) % m;
    }
    return res;
}


void solve()
{
    int a,b;
    cin>>a>>b;
    cout<<a*b;
}

int main()
{
    fastio;
    int t;

    t = 1;
    //cin >> t;
    //int test=t;
    while (t-- > 0)
    {
        //cout<<"Case #"<<(test-t)<<": ";
        solve();
        cout << newl;
    }

    return 0;
}

Measured memory_bytes: 3652
#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<utility>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;

typedef long long ll;
const int mod=1e9+7;

int main(){
	int a, b;
	string s;
	cin >> a >> b >> s;
	if(s.size()!=a+b+1 || s[a]!='-'){
		cout << "No" << endl;
		return 0;
	}
	for(int i=0; i<a; i++){
		if(s[i]<'0' || s[i]>'9'){
			cout << "No" << endl;
			return 0;
		}
	}
	for(int i=a+1; i<s.size(); i++){
		if(s[i]<'0' || s[i]>'9'){
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
	return 0;
}

Measured memory_bytes: 256
////////
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <complex>
#include <iomanip>
using namespace std;
typedef long long LL;
void guan() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ls (rt<<1)
#define rs (rt<<1|1)
#define F first
#define S second
#define mst(x,a) memset(x,a,sizeof(x))
#define Pr pair<int,int>
#define Prl pair<LL, LL>
#define Prd pair<double, double>
#define root 1,cnum,1
#define For(i,b,c) for(int i=b;i<=c;i++)
#define lowbit(x) (x&(-x))
const int maxn = 200020;
const LL mod = 998244353;
const LL mod2 = mod - 1;
const int INF = 0x7FFFFFFF;
const LL LINF = 0x7FFFFFFFFFFFFFFF;
const double pi = acos(-1.0);
const LL maxx = 1LL<<61;
const double eps = 1e-8;
//*********
 
int n, m, q, k;
inline int read() { return
    cin >> n
?1:0; }

void solve() {
    if(n < 1200) cout << "ABC" << endl;
    else if(n < 2800) cout << "ARC" << endl;
    else cout << "AGC" << endl;
}

int main() {
    guan();
    int t = 1;
    //cin >> t;
    int Cas = 1;
    while(t--) {
        while(read())
            //cout << "Case #" << Cas++ << ": " << endl;
            solve();
    }
    //cin >> t;
    return 0;
}


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

void Main()
{
    int l, r, d;
    cin >> l >> r >> d;
    int ans = 0;
    for (int i = l; i <= r; i++)
    {
        if (i % d == 0) {
            ans++;
        }
    }
    
    cout << ans << endl;
}

int main(int argc, char **argv)
{
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    Main();
    return 0;
}


Measured memory_bytes: 3608
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

#define ll long long

int main(){
	ll n,w;
	std::cin >> n >> w;
	
	std::vector<ll> dp(w+1);
	
	for(int i = 0; i < n; ++i){
		ll weight, value;
		std::cin >> weight >> value;
		std::vector<ll> toInsert;
		for(int j = w-weight; j >= 0; j--){
			if(j+weight >= 0 && dp[j] + value > dp[j+weight]){
				dp[j+weight] = dp[j] + value;
			}
		}
	}
	
	ll answer = 0;
	for(int i = 0; i < dp.size(); ++i){
		if(dp[i] > answer)
			answer = dp[i];
	}
	std::cout << answer;
}


Measured memory_bytes: 1024
#include <stdio.h>

int main(void)
{
	int n, i, j, num[10], sum[200] = {0}, max, min;
	
	while (scanf("%d", &n), n != 0){
		max = 0;
		min = 0;
		for (i = 0; i < 200; i++)sum[i] = 0;
		for (i = 0; i < n; i++){
			for (j = 0; j < 5; j++){
				scanf("%d", &num[j]);
				sum[i] += num[j];
			}
		}
		
		for (i = 0; i < n; i++){
			if (i == 0){
				max = sum[i];
				min = sum[i];
			}
			else if (max < sum[i])max = sum[i];
			else if (min > sum[i])min = sum[i];
		}
		
		printf("%d %d\n", max, min);
	}
	
	return (0);
	
}

Measured memory_bytes: 600
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const double PI = acos(-1);
const ll MOD = 1000000007;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
 
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
///////////////////////////////////////////////////////////////
 
int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr); //入出力高速化
  int N,C; cin >> N >> C;
  vector<vector<int>> A(100010, vector<int>(C));
  vector<tuple<int, int, int>> time(N);
  rep(i,N) {
    int s,t,c; cin >> s >> t >> c;
    s--; c--;
    time[i] = make_tuple(s,t,c);
  }
  sort(all(time));
  rep(i,N) {
    int s,t,c;
    tie(s,t,c) = time[i];
    if (A[s+1][c] == -1) {
      A[s+1][c]++;
      A[t][c]--;
    } else {
      A[s][c]++;
      A[t][c]--;
    }
  }

  rep(i,100010) {
    if (i == 0) continue;
    rep(j,C) A[i][j] += A[i-1][j];
  }
  int ans = 0;
  rep(i,100010) {
    int count = 0;
    rep(j,C) count += A[i][j];
    chmax(ans,count);
  }

  cout << ans << endl;
}

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

int main()
{
  int h, w;
  cin >> h >> w;
  string board[50]; 
  rep(i,h) cin >> board[i];
  //8方向
  const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

  rep(i,h)
  {
    rep(j,w)
    {
      if(board[i][j] == '#') continue;
      int num = 0;
      rep(d,8)
      {
        const int ni = i + dy[d];
        const int nj = j + dx[d];
        //境界を超えるとき 
        if(ni < 0 || h <= ni) continue;
        if(nj < 0 || w <= nj) continue;
        if(board[ni][nj] == '#') num++;
      }

      board[i][j] = char(num + '0');
    }
  }
  rep(i,h) cout << board[i] << endl;
}

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

using namespace std;

#define ALL(x) (x).begin(), (x).end()
#define AND(x,y,z) set_intersection((x).begin(),(x).end(),(y).begin(),(y).end(),inserter((z),(z).end()))
#define CONCAT(x,y) (x).insert((x).end(),(y).begin(),(y).end())
#define DUMP(xs) for (auto x:xs) cout<<x<<' ';cout<<endl
#define FOR(i,a,b) for (int i=(int)(a);i<(int)(b);++i)
#define OR(x,y,z) set_union((x).begin(),(x).end(),(y).begin(),(y).end(),inserter((z),(z).end()))
#define OUT(x) cout<<x<<endl
#define REP(i,n) FOR(i,0,n)

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<int> VECINT;
typedef vector<ll> VECLL;

int main() {
  int N;cin>>N;
  int ans = INT_MAX;
  REP(i,N) {
    int a;cin>>a;
    int tmp = 0;
    while (a%2==0) {
      a /= 2;
      tmp++;
    }
    ans = min(ans,tmp);
  }
  OUT(ans);

  return 0;
}

Measured memory_bytes: 256
import sys
input = sys.stdin.readline
from itertools import accumulate

n, k = map(int, input().split())
A = tuple(map(int, input().split()))
for _ in range(k):
  C = [0]*n
  for i, a in enumerate(A):
    C[max(0, i-a)] += 1
    if i+a < n-1:
      C[i+a+1] -= 1
  A = tuple(accumulate(C))
  if min(A) == n:
    break
print(*A)

Measured memory_bytes: 225836
#include <stdio.h>

int main(){
	int W,H,x,y,r;

	scanf("%d",&W);
	scanf("%d",&H);
	scanf("%d",&x);
	scanf("%d",&y);
	scanf("%d",&r);

	if( (x + r) <= W &&
		(x - r) >= 0 &&
		(y + r) <= H &&
		(y - r) >= 0    ) printf("Yes\n");
	else printf("No\n");

	return 0;

}

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

namespace AtCoder
{
    class Program
    {
        static void Main(string[] args)
        {
            //[summary]C - K-th Substring
            string s = Console.ReadLine();
            int k = int.Parse(Console.ReadLine());

            //K文字以内のsubstringをすべてリストアップ
            var substrings = new List<string>();

            for(int i = 0; i < s.Length; i++)
            {
                for(int j = 1; j <= k; j++)
                {
                    if (i + j <= s.Length)
                    {
                        var substring = s.Substring(i, j);
                        substrings.Add(substring);
                    }
                }
            }

            var result = substrings.Distinct().OrderBy(x => x).ToList();
            Console.WriteLine(result[k - 1]);
        }
    }
}

Measured memory_bytes: 14176
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005,inf=0x3f3f3f3f;
int read(){
	int f=1,g=0;
	char ch=getchar();
	for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1;
	for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0';
	return f*g;
}
int n,m,a[N],b[N],pos[N],fa[N],h[N];
ll f[N],g[N];
vector<int> e[N];
bool cmp(int x,int y){return a[x]<a[y];}
int find(int x){return (x==fa[x]) ? x : fa[x]=find(fa[x]);}
void merge(int x,int y){
	y=find(y);fa[y]=x;
	g[x]+=g[y];
	f[x]=min(f[x],max(f[y],a[x]-g[y]));
}
int main(){
	n=read();m=read();
	for (int i=1;i<=n;i++){
		a[i]=read();b[i]=read();a[i]=max(a[i]-b[i],0);
		pos[i]=fa[i]=i;f[i]=a[i];g[i]=b[i];
	}
	while (m--){
		int u=read(),v=read();
		e[u].push_back(v);
		e[v].push_back(u);
	}
	sort(pos+1,pos+n+1,cmp);
	for (int i=1;i<=n;i++){
		int x=pos[i];h[x]=1;
		for (auto y : e[x])
		if (h[y]&&(find(y)!=x)) merge(x,y);
	}
	printf("%lld\n",f[find(1)]+g[find(1)]);
	return 0;
}

Measured memory_bytes: 9468
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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


		// 読み込み
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line;
		String[] temp;

		line=br.readLine();
		temp=line.split(" ");

		int a = Integer.valueOf(temp[0]);
		int b = Integer.valueOf(temp[1]);


		if (a+b>=10){
			System.out.println("error");
		}else{
			System.out.println(a+b);
		}

	}
}


Measured memory_bytes: 21076
#include <bits/stdc++.h>
#include <boost/math/common_factor_rt.hpp>
using namespace std;
#define LL long long

void print() { std::cout << std::endl; }
template <class Head, class... Tail> void print(Head &&head, Tail &&... tail) {
    std::cout << head;
    if(sizeof...(tail) != 0)
        std::cout << " ";
    print(std::forward<Tail>(tail)...);
}
template <class T> void print(std::vector<T> &vec) {
    for(auto &a : vec) {
        std::cout << a;
        if(&a != &vec.back())
            std::cout << " ";
    }
    std::cout << std::endl;
}
template <class T> void print(std::vector<std::vector<T>> &df) {
    for(auto &vec : df) {
        print(vec);
    }
}

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

    LL aDc = (a - 1) / c;
    LL aDd = (a - 1) / d;
    LL aDcd = (a - 1) / boost::math::lcm(c, d);
    LL bDc = b / c;
    LL bDd = b / d;
    LL bDcd = b / boost::math::lcm(c, d);

    // print(aDc, aDd, aDcd, bDc, bDd, bDcd);
    // print(boost::math::lcm(c, d));

    result = (b - a + 1) - ((bDc + bDd - bDcd) - (aDc + aDd - aDcd));
    print(result);
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int tag[2000005];
void print()
{
    for(int j=1;j<tag[0];j++)
        cout<<tag[j]<<' ';
    cout<<tag[tag[0]]<<endl;
}
int main()
{
    for(int i=0;i<=tag[0];i++)
        cin>>tag[i];
    print();
    for(int i=2;i<=tag[0];i++)
    {
        int key=tag[i],j=i-1;
        while(key<tag[j]&&j>=1)
            tag[j+1]=tag[j--];
        tag[j+1]=key;
        print();
    }
}

Measured memory_bytes: 3120
#include<bits/stdc++.h>
#define low(x) (x&(-x))
using namespace std;
typedef unsigned long long ll;
const int N=2005;
struct bit{
	ll z[32];
	void flip(int x){
		z[x>>6]^=1ull<<(x&63);
	}
	bool getv(int x){
		return z[x>>6]&(1ull<<(x&63));
	}
}f[N][N],v,h[N],g[N];
void operator^=(bit &a,bit b){
	for (int i=0;i<32;i++)a.z[i]^=b.z[i];
}
void operator&=(bit &a,bit b){
	for (int i=0;i<32;i++)a.z[i]&=b.z[i];
}
void operator|=(bit &a,bit b){
	for (int i=0;i<32;i++)a.z[i]|=b.z[i];
}
const int P=67;
int Mrk[P];
void init_Mrk(){
	Mrk[1]=0;
	for (int i=1,now=1;i<=64;i++)
		Mrk[now=now*2%P]=i;
}
int getid(ll x){return Mrk[x%P];}
typedef pair<int,int> pi;
pi d[2001000];
int w[N*2],ne[N*2],la[N],t,n,m,d1;
int E;
void link(int x,int y){
	w[++t]=y;
	ne[t]=la[x];
	la[x]=t;
}
void dfs(int x,int fa){
	v.flip(x);
	f[E][x]=v;
	for (int y=la[x];y;y=ne[y]){
		int z=w[y];
		if (z==fa)continue;
		dfs(z,x);
		f[E][x]|=f[E][z];
	}
	v.flip(x);
}
void add(int x,int y){
	g[x].flip(y);
	g[y].flip(x);
	h[x].flip(y);
	h[y].flip(x);
	d[d1++]=make_pair(x,y);
}
int main(){
	init_Mrk();
	cin>>n>>m;
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		link(x,y);
		link(y,x);
	}
	for (E=1;E<=n;E++)dfs(E,0);
	for (int i=1;i<=n;i++){
		for (int j=1;j<i;j++){
			f[i][j]|=f[j][i];
			f[j][i]|=f[i][j];
		}
	}
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			if (i!=j)g[i].flip(j);
	int ans=0;
	while (m--){
		int x,y;
		scanf("%d%d",&x,&y);
		if (h[x].getv(y))continue;
		add(x,y);
		for (int i=0;i<d1;i++){
			int x=d[i].first,y=d[i].second;
			bit z=f[x][y];
			z&=g[x];z&=h[y];
			for (int k=0;k<32;k++){
				for (ll p=z.z[k];p;p-=low(p))
					add(x,getid(low(p))+(k<<6));
			}
			z=f[x][y];
			z&=h[x];z&=g[y];
			for (int k=0;k<32;k++){
				for (ll p=z.z[k];p;p-=low(p))
					add(y,getid(low(p))+(k<<6));
			}
		}
		ans+=d1;
		d1=0;
	}
	cout<<ans<<endl;
}

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

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, SZ(s)) {
    cout << s[i] << " ";
  }
  cout << endl;
}

int solve(int n) {
  int res = 0;
  for(int i=0;i*i<=n;i++) {
    REP(j, i+1) {
      REP(k, j+1) {
        REP(l, k+1) {
          if(i*i + j*j + k*k + l*l == n) {
/*             cout << n << ":" << i << ":" << j << ":" << k << ":" << l << endl; */
            res++;
          }
        }
      }
    }
  }
  return res;
}

int main () {
  cin.tie(0);
  cout << setprecision(10);

  while(1) {
    int n; cin >> n;
    if(n == 0) break;
    cout << solve(n) << endl;
  }
}



Measured memory_bytes: 3104
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <cmath>
#include <cassert>
#include <cstdio>

using namespace std;
using namespace placeholders;

using LL = long long;
using ULL = unsigned long long;
using VI = vector< int >;
using VVI = vector< vector< int > >;
using VS = vector< string >;
using SS = stringstream;
using PII = pair< int, int >;
using VPII = vector< pair< int, int > >;
template < typename T = int > using VT = vector< T >;
template < typename T = int > using VVT = vector< vector< T > >;
template < typename T = int > using LIM = numeric_limits< T >;

template < typename T > inline istream& operator>>( istream &s, vector< T > &v ){ for ( T &t : v ) { s >> t; } return s; }
template < typename T > inline ostream& operator<<( ostream &s, const vector< T > &v ){ for ( int i = 0; i < int( v.size() ); ++i ){ s << ( " " + !i ) << v[i]; } return s; }
template < typename T > inline T fromString( const string &s ) { T res; istringstream iss( s ); iss >> res; return res; }
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); }

#define REP2( i, n ) REP3( i, 0, n )
#define REP3( i, m, n ) for ( int i = ( int )( m ); i < ( int )( n ); ++i )
#define GET_REP( a, b, c, F, ... ) F
#define REP( ... ) GET_REP( __VA_ARGS__, REP3, REP2 )( __VA_ARGS__ )
#define FOR( e, c ) for ( auto &&e : c )
#define ALL( c ) begin( c ), end( c )
#define AALL( a, t ) ( t* )a, ( t* )a + sizeof( a ) / sizeof( t )
#define DRANGE( c, p ) ( c ).begin(), ( c ).begin() + ( p ), ( c ).end()

#define SZ( v ) ( (int)( v ).size() )
#define EXIST( c, e ) ( ( c ).find( e ) != ( c ).end() )

template < typename T > inline bool chmin( T &a, const T &b ){ if ( b < a ) { a = b; return true; } return false; }
template < typename T > inline bool chmax( T &a, const T &b ){ if ( a < b ) { a = b; return true; } return false; }

#define PB push_back
#define EM emplace
#define EB emplace_back
#define BI back_inserter

#define MP make_pair
#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

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

	int N;
	scanf( "%d", &N );

	VI A( N );
	for_each( ALL( A ), []( int &a ){ scanf( "%d", &a ); } );
	transform( ALL( A ), begin( A ), bind( minus< int >(), _1, 1 ) );

	int res = 0;
	REP( i, N )
	{
		res += A[ A[i] ] == i;
	}
	cout << res / 2 << endl;

	return 0;
}


Measured memory_bytes: 640
a, b, K = map(int, input().split())
count = 0
while True:
    if a % 2 == 1:
        a -= 1
    b += a / 2
    a -= a / 2
    count += 1
    if count == K:
        break
    if b % 2 == 1:
        b -= 1
    a += b / 2
    b -= b / 2
    count += 1
    if count == K:
        break
print(int(a), int(b))


Measured memory_bytes: 3064
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#define INF 1000000009
#define LINF 1000000000000000009
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
template<class T> bool chmax(T &a, const T&b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T&b) { if (a > b) { a = b; return 1; } return 0; }
ll gcd(ll n, ll m) { return (m ? gcd(m, n%m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m)*m; }

vector<vector<int>> tr;
vector<int> col(100010,-1);
vector<bool> vis(100010);

void dfs(int n, int p, bool ok) {
	if (ok) col[n] = col[p] ^ 1;
	else col[n] = 0;
	for (int i : tr[n]) {
		if (i != p && !vis[i]) {
			dfs(i, n, true);
		}
	}
}

int main() {
	int n, m;
	cin >> n >> m;
	tr.assign(n, vector<int>());
	vector<int> d(n);
	for (int i = 0; i < n; i++) cin >> d[i];
	vector<vector<int>> graph(n, vector<int>());
	vector<int> u(m), v(m);
	for (int i = 0; i < m; i++) {
		cin >> u[i] >> v[i];
		u[i]--; v[i]--;
		graph[u[i]].push_back(v[i]);
		graph[v[i]].push_back(u[i]);
	}
	vector<P> p(n);
	for (int i = 0; i < n; i++) {
		p[i].first = d[i];
		p[i].second = i;
	}
	sort(all(p));
	map<P, int> mp;
	set<P> sts;
	for (int i = 0; i < n; i++) {
		int ver = p[i].second;
		bool flag = false;
		sort(all(graph[ver]));
		for (int x : graph[ver]) {
			P ps;
			if (d[x] <= d[ver]) {
				ps.first = min(x, ver);
				ps.second = max(x, ver);
				mp[ps] = d[ver];
				if (!sts.count(P(ver, x)) && !sts.count(P(x, ver))) {
					tr[x].push_back(ver);
					tr[ver].push_back(x);
					sts.insert(P(x, ver));
					sts.insert(P(ver, x));
					//cout << ver << ' ' << x << endl;
				}
				flag = true;
				break;
			}
		}
		if (!flag) {
			cout << -1 << endl;
			return 0;
		}
	}
	for (int i = 0; i < n; i++) {
		if (col[i] == -1) dfs(i, -1, false);
	}
	for (int i = 0; i < n; i++) cout << (col[i] ? "B" : "W");
	cout << endl;
	for (int i = 0; i < m; i++) {
		if (u[i] > v[i]) swap(u[i], v[i]);
		P re;
		re.first = u[i]; re.second = v[i];
		if (mp[re] != 0) cout << mp[re] << endl;
		else cout << 1000000000 << endl;
	}
}

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

int main(){

    int n;

    cin >> n;

    int a[n];
    int b[n];
    int d[n] = {};

    for(int i=0;i<n;i++) {
        cin >> a[i];
        a[i]--;
    }

    int m=0;
    int c=0;

    d[0] =1;

    for(int i=0;i<n;i++){
        m += 1;

        if(i==0){
            b[i] = a[i];
        }

        else 
            b[i] = a[b[i-1]];

        if (b[i] == 1)
            break;

        
        if (d[b[i]] == 1){
            m=-1;
            break;
        }

        
        d[b[i]] = 1;
        
    }
       
    cout << m << endl;


}


Measured memory_bytes: 1408
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define F(i,a,n) for(int i=a; i<n; i++)

int gcd(int x, int y)
{
    int r;
    while((r = x % y) != 0) 
    {
        x = y;
        y = r;
    }
    return y;
}

int main(){
    int n;
    scanf("%d",&n);
    int sum=0;
    F(i,1,n+1){
        F(j,1,n+1){
            F(k,1,n+1){
                sum+=gcd(gcd(i,j),gcd(j,k));
            }
        }
    }
    printf("%d",sum);

    return 0;
}

Measured memory_bytes: 1732
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdio>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
using namespace std;
#define MOD 1000000007
#define endl "\n" 
#define rep(i,n) for(int i = 0; i < (n); ++i)
typedef long long ll;


int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  ll n, a, b, c, d, e;
  cin >> n >> a >> b >> c >> d >> e;
  ll mi = min(a,min(b,min(c,min(d,e))));
  ll ans = 4 + n/mi + min(n%mi, ll(1));
  cout << ans << endl;


  return 0;
}


Measured memory_bytes: 256
// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 08/22/20 18:13:52
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  int n,x,t; cin>>n>>x>>t;
  int ans = ((n-1)/x+1) * t;
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 3648
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<deque>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg,(x).ed
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep1(i,n) for(ll i=1;i<=(n);i++)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(ll i=(n);i>=1;i--)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
const ll MOD=1000000007;
const ll INF=1000000000000000;
string s;
ll N;
vector<vector<ll> > memo(100006,vector<ll>(2,-INF));
//check:L以下が確定した
ll rec(ll now,ll check){
    if(memo[now][check]!=-INF) return memo[now][check];
    
    if(now==0){
        if(check==1){
            return memo[now][check]=(s[now]=='1'?1:0);
        }
        else{
            return memo[now][check]=(s[now]=='1'?2:1);
        }
    }
    
    if(check==1){
        return memo[now][check]=((rec(now-1,1)*3)%MOD + (rec(now-1,0)*(s[now]=='1'?1:0))%MOD)%MOD;
    }
    else{
        return memo[now][check]=(rec(now-1,0)*(s[now]=='1'?2:1))%MOD;
    }
}
int main(){
    cin>>s;
    N=sz(s);
    cout<<(rec(N-1,1)+rec(N-1,0))%MOD<<endl;
}




Measured memory_bytes: 13468