akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

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

    int n, k;
    cin >> n >> k;
    int kirai[k];
    for (int i = 0; i < k; i++) {
        cin >> kirai[i];
    }

    for (int i = n; i < 10 * n; i++) {
        int t = i;
        bool valid = true;

        while (t > 0) {
            bool possible = true;
            for (int j = 0; j < k; j++) {
                if (t % 10 == kirai[j]) {
                    possible = false;
                }
            }

            if (!possible) {
                valid = false;
                break;
            }

            t /= 10;
        }

        if (valid) {
            cout << i << endl;
            return 0;
        }
    }
}


Measured memory_bytes: 256
N,K = map(int,input().split())
A = list(map(int,input().split()))
D = {}
for i in A :
    if i in D :
        D[i] += 1
    else :
        D[i] = 1
D = sorted(D.items(),key=lambda x:x[1])
counter = 0
answer = 0
for j in range(len(D)) :
    if len(D) - counter > K :
        counter += 1
        answer += D[j][1]
    else :
        print(answer)
        break


Measured memory_bytes: 39068
N = int(input())

digits_sum = lambda x: sum(map(int, str(x)))

min_num = 10**18
for i in range(1, (N+1)//2+1):
    # print(i)
    m = N-i
    l = digits_sum(i) + digits_sum(m)
    min_num = min(l, min_num)

print(min_num)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
#define ld long double
const ld pi = 3.1415926535897932384626433;
const ld eps = 0.0000001;

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

	int ans = 0;
	for(int i = 0; i < n; i++) {
		int sum = 0;
		for(int j = 0; j <= i; j++)
			sum += a[j];
		for(int j = i; j < n; j++)
			sum += b[j];
		ans = max(ans, sum);
	}
	cout << ans << endl;
}

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

string Replace(string s,int a,int b,string t){
    for(int i=a;i<=b;i++){
        s[i]=t[i-a];
    }
    return s;
}

string Reverse(string s,int a,int b){
    int i=a,j=b;
    while(i<j){
        swap(s[i],s[j]);
        i++;
        j--;
    }
    return s;
}

void Print(string s,int a,int b){
    for(int i=a;i<=b;i++){
        cout<<s[i];
    }
    cout<<endl;
}

int main(){
    string str,command,t;
    cin>>str;
    int q,a,b;
    cin>>q;
    for(int i=0;i<q;i++){
        cin>>command;
        cin>>a>>b;
        if(command=="replace"){
            cin>>t;
            str=Replace(str,a,b,t);
        }else if(command=="reverse"){
            str=Reverse(str,a,b);
        }else if(command=="print"){
            Print(str,a,b);
        }
    }
    return 0;
}


Measured memory_bytes: 3176
n = int(input())
s = input()
cur = s[0]
ans = 0
# if cur*n==s:
#     print(1)
# else:
for i in range(1,n):
    if s[i]==cur:
        pass
    else:
        cur = s[i]
        ans+=1
ans+=1
print(ans)

Measured memory_bytes: 9188
#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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")
    };
}

use std::cmp;


fn main() {
    input!(
        a: i32,
        b: i32,
        k: i32
    );
    
    let mut count = 0;
    
    let mut i = cmp::min(a, b);
    
    loop {
        if a % i == 0 && b % i == 0 {
            count += 1;
        }
        if count == k {
            println!("{}", i);
            break;
        }
        i -= 1;
    }
    
    
}


Measured memory_bytes: 4352
#include <bits/stdc++.h>
#include <vector>
#include <cmath>
#include <fstream>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP2(i, m, n) for (int i = (m); i < (int)(n); ++i)
#define REPR(i, n) for (int i = (n)-1; i >= 0; --i)
#define REPR2(i, m, n) for (int i = (n)-1; i >= (m); --i)
#define REPx(x, a) for(auto x : a)
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define SORTG(a) sort(ALL(a), greater<int>())
#define REVERSE(a) reverse(ALL(a))
#define bit_search(bit, n) REP(bit, 1<<(n))
#define bit_check(bit, i) (bit>>(i)) & 1
#define setpre(n) fixed << setprecision((n))
#define UNIQUE(a) do {SORT(a); (a).erase(unique(ALL(a)), (a).end());} while(0)
#define MAX(a) *max_element(ALL(a))
#define MIN(a) *min_element(ALL(a))
#define bisect_left(a, x) lower_bound(ALL(a), (x)) - a.begin()
#define bisect_right(a, x) upper_bound(ALL(a), (x)) - a.begin()
#define INPUT(a) REP(i, a.size()) cin >> a[i];
#define ENDL cout << endl;
using namespace std;
using ll = long long;
using ld = long double;
using PII = pair<int, int>;
using VPII = vector<PII>;
using VI = vector<int>;
using VVI = vector<VI>;
using Vll = vector<ll>;
using VVll = vector<Vll>;
using VC = vector<char>;
using VS = vector<string>;
using VB = vector<bool>;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

/*
  ifstream in("test.txt");
  cin.rdbuf(in.rdbuf());
*/

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, A, B;
  cin >> N >> A >> B;
  ll ans = 0, now;
  REP(i, N)
  {
    ll X;
    cin >> X;
    if (i == 0)
    {
      now = X;
      continue;
    }
    ans += min(B, A*(X-now));
    now = X;
  }
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\n'
using namespace std;
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
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;
}

using ll = long long;
using ull = unsigned long long;
const int INF = 2100100100;
// const int MOD = 1e9 + 7;

int main() {
    int N;
    cin >> N;
    vector<int> a(N), used(N);
    for (int i = 0; i < N; i++) {
        cin >> a.at(i);
        a[i]--;
    }
    used[0] = 1;
    int now = 0;
    int ans = 0;
    while (1) {
        if (used[a[now]] == 0) {
            ans++;
            used[a[now]] = 1;
            now = a[now];
            if (now == 1) {
                cout << ans << endl;
                return 0;
            }
        } else {
            cout << -1 << endl;
            return 0;
        }
    }
}


Measured memory_bytes: 1152
//  Created by conan1024hao in 2019.
//  Copyright © 2019 conan1024hao. All rights reserved.
//  専用ライブラリです、自由にコピーして構いません。
//  感谢看我的代码！Wechat:conan1024hao QQ:810396815
#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define INF 10e16
#define MOD 1000000007
#define  mod 998244353
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
using namespace std;
//using Int=int_fast64_t;
typedef long long int ll;
typedef pair<ll,ll>Pll;
typedef pair<int,int>Pin;
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int dx[8]={-1,0,1,0};
int dy[8]={0,-1,0,1};
char dir[4]={'u','l','d','r'};
ll cmp1(pair<ll,string>a,pair<ll,string> b){
        if(a.fi!=b.fi)
        return a.fi<b.fi;
        else
        return a.se<b.se;
}
//--------------------------------------------------------------------------

//---------------------------------------------------------------------------
int main(){//問題をちゃんと見ろ!!!!!!!!!!!!!!!!!      llか??????????        memset()!!!!!!!!!!!!       ペナを減らせ!!!!!!!!!!!!!
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    int a,b,c;cin>>a>>b>>c;
    int aa=abs(a-b);
    int bb=abs(b-c);
    int cc=abs(a-c);
    cout<<aa+bb+cc-max(aa,max(bb,cc))<<endl;
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------






Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <numeric>
#include <functional>
#include <stack>

int main (){
	std::string s;

	std::cin >> s;

	int a, b;

	a = std::stoi(s.substr(0, 2));
	b = std::stoi(s.substr(2, 2));

	bool yymm = false;
	bool mmyy = false;

	// aを年として仮定
	if (b >= 1 && b <= 12) {
		yymm = true;
	}

	// bを年として仮定
	if (a >= 1 && a <= 12) {
		mmyy = true;
	}

	if (yymm && mmyy) {
		std::cout << "AMBIGUOUS" << std::endl;
	}
	else if (yymm) {
		std::cout << "YYMM" << std::endl;
	}
	else if (mmyy) {
		std::cout << "MMYY" << std::endl;
	}
	else {
		std::cout << "NA" << std::endl;
	}
	return 0;
}

Measured memory_bytes: 256
package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	var s string
	var k int
	fmt.Scan(&s, &k)

	for i, v := range strings.Split(s, "") {
		n, _ := strconv.Atoi(v)
		if i+1 == k {
			fmt.Println(n)
			return
		}
		if n != 1 {
			fmt.Println(n)
			return
		}
	}
}

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

int main(){
    int D, N, i;
    int ans=1;


    cin >> D >> N;

    for (i=1; i < D+1; i++){
        ans = ans*100;
        }
    if (N!=100)
        cout << ans*N << endl;
    else
        cout << ans*101 << endl;
    }



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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll LongINF=1e13+7;
const int INF=1e9+7;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template <class T, class Y>T GCD(T a, Y b){if(a<b){int c=a;a=b;b=c;}if (a % b == 0){return b;}return GCD(b, a % b);}
template<class T,class Y>T LCM(T a,Y b){return (a*b)/GCD(a,b);}
void clear(queue<pair<int, int>> &q){queue<pair<int, int>> empty;swap(q, empty);} //queueの中身の型は適時変更を忘れない

using vi=vector<int>;
using vii=vector<vi>;

#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define rv reverse
#define ALL(a) (a).begin(),(a).end()
#define decimal(x) fixed<<setprecision(x)


int main(){
    int n,m,cnt=0,s=0;
    cin>>n>>m;
    vi a(n);
    vector<pair<int,int>> bc(m);
    rep(i,n)cin>>a[i];
    rep(i,m){
        int x,y;
        cin>>x>>y;
        bc[i]={y,x};
    }
    sort(ALL(bc),greater<pair<int,int>>());
    vi d;
    for(auto x:bc){
        rep(i,x.second){
            d.pb(x.first);
            cnt++;
            if(cnt>=n)break;
        }
        if(cnt>=n)break;
    }
    sort(ALL(a));
    cnt=0;
    s=d.size();
    ll sum=0;
    rep(i,n){
        if(a[i]<d[cnt]&&cnt<s)a[i]=d[cnt],cnt++;
        sum+=a[i];
    }
    cout<<sum<<endl;
    return 0;
}

//a=97,z=122

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

long long mod=1e9+7;
long long dp[1001][1001];

int solve(int i,int j,int n,int m,char a[][1001])
{
    if(i==n-1 && j==m-1) return 1;
    if(a[i][j]=='#') return 0;
    if(i<0 || i>=n || j>=m || j<0) return 0;
    if(dp[i][j]!=-1) return dp[i][j];
    dp[i][j]=(solve(i+1,j,n,m,a)+solve(i,j+1,n,m,a))%mod;
    return dp[i][j];
}

int main()
{
    int n,m;
    cin>>n>>m;
    char a[n][1001];
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            cin>>a[i][j];
    memset(dp,-1,sizeof(dp));
    cout<<solve(0,0,n,m,a)%mod<<endl;
    return 0;
}


Measured memory_bytes: 9216
s_ = input()
t = input()

answers = []
for i in range(len(s_)-len(t)+1):
    ans = s_
    for j in range(len(t)):
        matched = True
        if s_[i+j] != t[j] and s_[i+j] != '?':
            matched = False
            break
        else:
            ans = ans[:i+j] + t[j] + ans[i+j+1:]
    if matched:
        ans = ans.replace('?', 'a')
        answers.append(ans)
# print(answers)

if len(answers) == 0:
    print('UNRESTORABLE')
else:
    print(sorted(answers)[0])


Measured memory_bytes: 3064
#include <cstdio>
#include <algorithm>
#include <cmath>
#define Rint register int
#define LL long long
#define F(x,y,z) for(Rint x=y;x<=z;++x)
using namespace std;
inline LL R(){
    char c='!';LL x=0,z=1;
    for( ; c>'9'||c<'0';c=getchar()) if(c=='-')z=-1;
    for(;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0';
    return x*z;
}
int n,m;
LL anss;
int a[1000010];

int main(){
	n=R();
	F(i,1,n) a[i]=R();
	int la=0,too=0,lanow=0;
	F(i,1,n){
		int now=lanow-a[i-1];
		anss+=la-i+1;
		F(j,la+1,n){
			
			if ((now^a[j])==(now+a[j])) ++anss,now^=a[j];
			else break;
			too=j;
		}
		la=too;lanow=now;
	}
	printf("%lld\n",anss);
	return 0;
}



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

class BIT {
private:
  int n;
  vector<int> bit;

public:
  BIT(int _n) {
    n = _n;
    bit = vector<int>(n, 0);
  }

  int sum(int i) {
    int s = 0;
    while(i > 0) {
      s += bit[i];
      i -= i & (-i);
  }
    return s;
  }

  void add(int i, int val) {
    while(i <= n) {
      bit[i] += val;
      i += i & (-i);
    }
  }

  ~BIT() {
    vector<int> tv;
    bit.swap(tv);
  }
};

int main () {
  int n, k;
  scanf("%d%d", &n, &k);

  vector<ll> s(n+1);

  s[0] = 0;
  rep(i,n) {
    int a;
    scanf("%d", &a);
    s[i+1] = s[i] + (a - k);
  }

  vector<ll> ss(s);
  sort(ss.begin(), ss.end());

  // map {s} to [1, n+1]
  // BIT operations: O(log(s_max)) -> O(log(n))
  for(int i = 0; i <= n; i++) {
    s[i] = distance(ss.begin(), lower_bound(ss.begin(), ss.end(), s[i])) + 1;
  }

  BIT bit(n+2);
  ll c = 0;
  for(int i = n; i >= 0; i--) {
    c += bit.sum(s[i] - 1);
    bit.add(s[i], 1);
  }

  printf("%lld\n", ll(n)*(n+1)/2 - c);

  return 0;
}


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

void q_sort(long numbers[], int left, int right);

int main(void){
  int j,N,K,i,min;
  long H[200000]={};
  long ans;  
  if(scanf("%d %d",&N,&K)>2){
    exit(0);
  }
  for(i=0;i<N;i++){
  if(scanf("%ld",&H[i])>1){
     exit(0);
   }
   }
  ans=0;
  if(N<=K){
    printf("%ld",ans);
  }
  else if(K==0){
    for(i=0;i<N;i++){
      ans+=H[i];
    }
     printf("%ld",ans);
  }
  else{
  q_sort(H,0,N-1);
  //printf("%d %d %d\n",H[0],H[1],H[2]);
  for(i=0;i<N-K;i++){
    ans+=H[i];
  }
  printf("%ld",ans);
  }
  return 0;
}


void q_sort(long numbers[], int left, int right)
{
    int l_hold, r_hold;
    long pivot,pa;

    l_hold = left;
    r_hold = right;
    pivot = numbers[left];
    while (left < right)
    {
        while ((numbers[right] >= pivot) && (left < right))
            right--;
        if (left != right)
        {
            numbers[left] = numbers[right];
            left++;
        }
        while ((numbers[left] <= pivot) && (left < right))
            left++;
        if (left != right)
        {
            numbers[right] = numbers[left];
            right--;
        }
   }
    numbers[left] = pivot;
    pivot = left;
    left = l_hold;
    right = r_hold;
    if (left < pivot)
      q_sort(numbers, left, pivot-1);
    if (right > pivot)
      q_sort(numbers, pivot+1, right);
}







Measured memory_bytes: 1664
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <cstring>

#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))

#define INF (int)1e9+7

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

typedef long long ll;

using namespace std;

int main(){
    string s;
    const string s1 = "10";
    const string s2 = "01";
    string a, b;

    cin >> s;

    rep(i, (s.size()+1)/2){
        a += s1;
        b += s2;
    }
 
    int cnt1 = 0;
    int cnt2 = 0;
    rep(i, s.size()){
        if(s[i] != a[i]){
            cnt1++;
        }
        if(s[i] != b[i]){
            cnt2++;
        }
    }
    cout << min(cnt1, cnt2) << endl;
    return 0;
}

Measured memory_bytes: 768
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll,ll> P;
using VP = vector<P>; using VVP = vector<VP>;
using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>;
const int inf=1e9+7;
const ll INF=1LL<<61;
const ll mod=1e9+7;

int main(){
  ll i,j;
  ll a,b;
  ll x,y;
  x=0;
  y=0;
  cin>>a>>b;
  for(i=0;i<b;i++){
    x*=10;
    x+=a;
  }
  for(i=0;i<a;i++){
    y*=10;
    y+=b;
  }
  //cout<<x<<y<<endl;
  if(a<=b) cout<<x<<endl;
  else cout<<y<<endl;

  return 0;
}


Measured memory_bytes: 256
N = int(input())
A = [[0 for _ in range(N+1)]]
for _ in range(N):
    a = list(map(int,input().split()))
    a.insert(0,0)
    A.append(a)
tot = 0
for i in range(1,N):
    for j in range(i,N+1):
        tot += A[i][j]
for i in range(1,N):
    for j in range(i+1,N+1):
        for k in range(1,N+1):
            if k!=i and k!=j:
                if A[i][j]>A[i][k]+A[k][j]:
                    tot = -1
                    break
                elif A[i][j]==A[i][k]+A[k][j]:
                    tot -= A[i][j]
                    break
        if tot<0:break
    if tot<0:break
print(tot)

Measured memory_bytes: 74696
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int main(){
  int N,a=0,b=0,ans=100000;
  cin>>N;
  int W[N];
  for(int i=0;i<N;i++){
    cin>>W[i];
    a+=W[i];
  }
  for(int i=0;i<N;i++){
    ans=min(ans,abs(a-b));
    a-=W[i];
    b+=W[i];
  }
  cout<<ans<<endl;
}


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

using namespace std;

#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll,ll> P;

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

	ll n;cin >> n;
	string str;cin >> str;

	ll ans = 1;
	ll cou = 0;
	REP(i, str.size()){
		if(str[i] == 'W'){
			if(cou % 2 == 0){
				ans *= cou;
				ans %= MOD;
				cou--;
			}
			else {
				cou++;
			}
		}
		else {
			if(cou % 2 == 0){
				cou++;
			}
			else {
				ans *= cou;
				ans %= MOD;
				cou--;
			}
		}

		if(cou < 0){
			cout << 0 << endl;
			return 0;
		}
	}

	if(cou != 0){
		cout << 0 << endl;
		return 0;
	}

	for(int i = 1;i <= n;i++){
		ans = (ans * i) % MOD;
	}

	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 720
N = int(input())
dp = [[0]*64 for i in range(N-2)]
# 3進数で管理
mod = 10**9+7

d = dict()
d2 = dict()
d2[0] = 'A'
d2[1] = 'G'
d2[2] = 'C'
d2[3] = 'T'
for i in range(64):
    k = i
    ret = ''
    for j in range(3):
        ret += d2[k % 4]
        k //= 4
    d[i] = ret


for i in range(64):
    if d[i] == 'AGC' or d[i] == 'GAC' or d[i] == "ACG":
        continue

    else:
        dp[0][i] = 1


for i in range(N-3):
    for j in range(64):
        if dp[i][j] == 0:
            continue
        tmp = j // 4
        for k in range(4):
            idx = tmp + k * 16
            if d[idx] == "AGC" or d[idx] == 'ACG' or d[idx] == "GAC" or (d[j][0] == 'A' and d[j][1] == 'G' and k == 2) or (d[j][0] == 'A' and d[j][2] == 'G' and k == 2):
                #print(d[j] + d2[k])
                continue
            dp[i+1][idx] += dp[i][j]


print(sum(dp[-1]) % mod)


Measured memory_bytes: 9264
#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int a[3][3];

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	rep(i,3)rep(j,3) cin >> a[i][j];
	int n;
	cin >> n;
	rep(i,n){
		int b;
		cin >> b;
		rep(j,3)rep(k,3){
			if(a[j][k] == b) a[j][k] = -1;
		}
	}
	bool yes = false;
	rep(i,3){
		bool ok = true;
		rep(j,3){
			if(a[i][j] != -1) ok = false;
		}
		if(ok) yes = true;
	}
	rep(j,3){
		bool ok = true;
		rep(i,3){
			if(a[i][j] != -1) ok = false;
		}
		if(ok) yes = true;
	}
	if(a[0][0] == -1 && a[1][1] == -1 && a[2][2] == -1) yes = true;
	if(a[0][2] == -1 && a[1][1] == -1 && a[2][0] == -1) yes = true;
	cout << (yes ? "Yes" : "No") << endl;
	return 0;
}


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

int main(){
    int N,a[100000];
    cin>>N;
    for(int i=0;i<N;i++){
        cin>>a[i];
    }
    int ans=0;
    for(int i=0;i<N;i++){
        if(i+1==a[a[i]-1]){a[a[i]-1]=-1;ans++;}
    }
    cout<<ans<<endl;
    return 0;
}


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

int main(){
    int x;
    cin>>x;
    
    while(true){
    int flg=0;
    for(int i=2;i*i<x;i++)
     { 
       if(x%i==0)
         flg=1;
     }
    if(flg==1){
      x++;
    }
    else{
      cout<<x;
      break;}
    }
}

Measured memory_bytes: 256
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Scanner;

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

    final int INF = 100_000;
    int K = scanner.nextInt();

    List<int[]>[] edges = new List[K];
    for (int i = 0; i < K; i++) {
      edges[i] = new ArrayList<>();
      edges[i].add(new int[] {(i + 1) % K, 1});
      if ((i * 10) % K != i) edges[i].add(new int[] {(i * 10) % K, 0});
    }

    int[] dist = new int[K];
    for (int i = 0; i < K; i++) dist[i] = INF;
    dist[1] = 0;

    PriorityQueue<Node> queue = new PriorityQueue<>(Comparator.comparing(node -> node.dist));
    queue.add(new Node(1, 0));

    while (!queue.isEmpty()) {
      Node node = queue.remove();
      if (dist[node.id] < node.dist) continue;
      for (int[] e : edges[node.id]) {
        int i = e[0];
        int c = e[1];
        int newCost = dist[node.id] + c;
        if (dist[i] > newCost) {
          dist[i] = newCost;
          queue.add(new Node(i, newCost));
        }
      }
    }

    System.out.println(dist[0] + 1);
  }

  static class Node {
    public final int id;
    public final int dist;

    Node(int id, int dist) {
      this.id = id;
      this.dist = dist;
    }

    @Override public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;
      Node node = (Node) o;
      return id == node.id;
    }

    @Override public int hashCode() {
      return Objects.hash(id);
    }
  }
}


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

int main(void) {
    string S;
    cin >> S;
    if (S == "MON") cout << 6 << endl;
    if (S == "TUE") cout << 5 << endl;
    if (S == "WED") cout << 4 << endl;
    if (S == "THU") cout << 3 << endl;
    if (S == "FRI") cout << 2 << endl;
    if (S == "SAT") cout << 1 << endl;
    if (S == "SUN") cout << 7 << endl;
    return 0;
}

Measured memory_bytes: 256
package main

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

var sc = bufio.NewScanner(os.Stdin)

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

type Lim struct {
	pos   int
	depth int
}

func binary_search(x int, t_lim_list []Lim) int {
	l, r := 0, len(t_lim_list)-1
	for r-l > 1 {
		c := (l + r) / 2
		if t_lim_list[c].depth < x {
			r = c
		} else {
			l = c
		}
	}

	if x <= t_lim_list[r].depth {
		return t_lim_list[r].pos - 1
	} else {
		return t_lim_list[l].pos - 1
	}

	// for i := range t_lim_list {
	// 	lim := t_lim_list[len(t_lim_list)-i-1]
	// 	if x <= lim.depth {
	// 		return lim.pos - 1
	// 	}
	// }
}

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	N, Q := nextInt(), nextInt()
	black := (N - 2) * (N - 2)
	r_lim_list := make([]Lim, 0)
	d_lim_list := make([]Lim, 0)

	r_lim_list = append(r_lim_list, Lim{N, N})
	d_lim_list = append(r_lim_list, Lim{N, N})
	for q := 0; q < Q; q++ {
		f, x := nextInt(), nextInt()
		d := -1
		if f == 1 {
			// find lim[i-1].depth < x <= lim[i].depth
			d = binary_search(x, d_lim_list)
			black -= d - 1
			if x < r_lim_list[len(r_lim_list)-1].pos {
				r_lim_list = append(r_lim_list, Lim{x, d})
			}
		} else {
			d = binary_search(x, r_lim_list)
			black -= d - 1
			if x < d_lim_list[len(d_lim_list)-1].pos {
				d_lim_list = append(d_lim_list, Lim{x, d})
			}
		}
	}
	fmt.Println(black)
}


Measured memory_bytes: 14036
//#pragma GCC optimize("Ofast")

#pragma GCC optimize("O3")
//#pragma GCC target("avx,avx2,fma")
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdlib>
#include<set>
#include<vector>
#include<sstream>
#include<queue>
#include <iomanip>
#include<unordered_set>
#include <stack>
#include <unordered_map>
#include <map>
constexpr auto INF = 9223372036854775807;
typedef long long int ll;
typedef unsigned long long int ull;
typedef unsigned long int ul;
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i+=1)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i-=1)
#define endl '\n'
#define N 1000000007 // prime modulo value 
#define M 998244353
#define all(x) x.begin(),x.end()
using namespace std;
// From Geeksforgeeks
inline ll gcd(ll a, ll b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
// Power function took from geeksforgeeks
ll power(ll x, ll y)
{
	ll res = 1;

	x = x;
	if (x == 0) return 0;
	while (y > 0)
	{
		if (y & 1)
			res = (res * x);
		y = y >> 1;
		x = (x * x);
	}
	return res;
}
double dist(ll x1,ll y1, ll x2,ll y2)
{
	double ans = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
	return ans;
}
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	string s1, s2;
	cin >> s1 >> s2;
	string flag = "Yes";
	f(i, 0, s1.length())
	{
		if (s1[i] != s2[i])
		{
			flag = "No"
				;
			break;
		}
	}
	cout << flag;
	return 0;
}


Measured memory_bytes: 3628
#include<bits/stdc++.h>
using namespace std;
#define MAX 1005
#define INF (1<<21)
#define WHITE 0
#define GRAY  1
#define BLACK 2
int M[MAX][MAX];
int n,x;
int prim(){
  vector<int> v;
  int u,minv;
  int d[MAX],p[MAX],color[MAX];
  for(int i=0;i<n;i++){
    d[i]=INF;
    p[i]=-1;
    color[i]=WHITE;
  }
  d[0]=0;
  while(1){
    minv=INF;
    u=-1;
    for(int i=0;i<n;i++){
      if(minv>d[i]&&color[i]!=BLACK){
	u=i;
	minv=d[i];
      }
    }
    if(u==-1) break;
    color[u]=BLACK;
    for(int i=0;i<n;i++){
      if(color[i]!=BLACK&&M[u][i]!=INF){
	if(d[i]>M[u][i]){
	  d[i]=M[u][i];
	  p[i]=u;
	  color[i]=GRAY;
	}
      }
    }
  }
  for(int i=0;i<n;i++){
    if(~p[i]) v.push_back(M[i][p[i]]);
  }
  sort(v.begin(),v.end());
  if(v.size()%2) return v[v.size()/2];
  return (v[v.size()/2]+v[v.size()/2-1])/2;
}
int main(){
  while(cin>>n>>x,n||x){
    int s,t,c;
    for(int i=0;i<MAX;i++){
      for(int j=0;j<MAX;j++){
	M[i][j]=INF;
      }
    }
    for(int i=0;i<x;i++){
      cin>>s>>t>>c;
      M[s-1][t-1]=c;
      M[t-1][s-1]=c;
    }
    cout << prim() << endl;
  }
  return 0;
}

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

#define ll long long
const int mod = 1000000007;

int main(){

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ios_base::sync_with_stdio(false); 

	int n, k;
	cin >> n >> k;
	int ans = 0;

	for (int i = 0; i < n; i++){
		double p, q;
		cin >> p >> q;
		double d;
		d = sqrt(p*p+q*q);
		if (d <= k){
			ans++;
		}
	}

	cout << ans;


	
}

Measured memory_bytes: 3844
#include <bits/stdc++.h>
using namespace std;
#define bolt ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define test int t; cin>>t; while(t--)
#define ll long long
#define vi vector<ll>
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend() 
const int N = 2e5+5;
const ll MAX = 1e9 +7;


int main()
{
	bolt;
	string ans="",s,t;
	int n;
	cin>>n;
	cin>>s>>t;
	for(int i=0; i<n; i++)
	{
		ans.pb(s[i]);
		ans.pb(t[i]);
	}
	cout<<ans<<"\n";
}


Measured memory_bytes: 256
#include <stdio.h>

int main(void){
  
  int a, b, i, small_tower=0;

  scanf("%d %d", &a, &b);

  for (i=0; i<(b-a); i++){
    small_tower += i;
  }

  printf("%d", small_tower-a);

  return 0;
}

Measured memory_bytes: 128
N, M, X, Y = map(int, input().split())
x_lst = list(map(int, input().split()))
y_lst = list(map(int, input().split()))
if (X < Y and max(x_lst) < min(y_lst) and X < min(y_lst) and max(x_lst) < Y):
  print("No War")
else:
  print("War")

Measured memory_bytes: 3060
#include<bits/stdc++.h>
#pragma warning(disable:4996)
using namespace std;
using ll = long long;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
//#define int long long
long long GCD(long long a, long long b) {
	if (b == 0) return a;
	else return GCD(b, a % b);
}
long long multi_GCD(vector<long long>&v) {
	long long m = v[0];
	for (int i = 0; i < v.size() - 1; i++) {
		m = GCD(m, v[i + 1]);
	}
	return m;
}
signed main() {
	int n, k;
	cin >> n >> k;
	vector<ll>v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i];
	}
	sort(v.begin(), v.end());
	
	ll m = multi_GCD(v);
	if (v[v.size()-1] < k) {
		cout << "IMPOSSIBLE" << endl;

	}
	else if (k%m == 0) {
		cout << "POSSIBLE" << endl;

	}
	else {
		cout << "IMPOSSIBLE" << endl;
	}
	return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <set>
#include <utility>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>
#include <cstdio>
#include <map>

using namespace std;

#define rep(i,x) for(ll i = 0; i < (ll)(x); i++)
#define rrep(i,x) for(ll i = ((ll)(x) - 1); i >= 0; i--)
#define _upgrade ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define pb push_back

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

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

//ll gcd(ll a, ll b){return b?gcd(b,a%b):a;}
//ll lcm(ll x, ll y) {return x / gcd(x, y) * y;}

int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
const ll mod = 1e9 + 7;
const double PI = 3.14159265358979323846;


// ****************************************CODE***************************************//



int main(){
        int n;
        cin >> n;
        set<int> st;
        int cnt8 = 0;
        for(int i = 0; i < n; i++){
                int a;
                cin >> a;
                chmin(a,3200);
                a /= 400;
                st.insert(a);
                if(a==8) cnt8++;
        }

        int mn = st.size() - (cnt8>0);
        if(mn==0) mn = 1;
        int mx = st.size() - (cnt8>0) + cnt8;

        cout << mn << " " << mx << endl;
        return 0;
}


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

using namespace std;
int shrink(int n, int start, int s[]) {
  int peek = start;
  while(peek < n) {
    if(s[peek] == 1)
      peek++;
    else
      break;
  }
  if (peek == n)
    return n;
  while(peek < n && s[peek] == 0) {
    peek++;
  }
  return peek;
}
int expand(int n, int start, int s[]) {
  int peek = shrink(n, start, s);
  while(peek < n) {
    if(s[peek] == 1)
      peek++;
    else
      break;
  }
  return peek;
}

int handstand(int n, int k, int s[]){
  int right = 0;
  for(int i=0;i<k;i++) {
    right = expand(n, right, s);
  }
  int ans = right;
  int left = 0;
  while(right < n) {
    left = shrink(n, left, s);
    right = expand(n, right, s);
    ans = max(ans, right - left);
  }

  return ans;
}

int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  int s[n];
  cin.get();
  for (int i=0;i<n;i++) {
    s[i] = cin.get() - 48;
  }
  cout << handstand(n, k, s);
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
#define maxn 100000+5
using namespace std;
int a[maxn],b[maxn],c[maxn],dp[maxn][3];
int main()
{
	int N;
	cin >> N;
	for(int i = 0; i < N; i++)
	{
		cin >> a[i] >> b[i] >> c[i];
	}

	dp[0][0]=a[0],dp[0][1]=b[0],dp[0][2]=c[0];
	for (int i = 1; i < N ;i ++)
	{
		dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + a[i];
		dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + b[i];
		dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + c[i];
	}

	cout << max(dp[N-1][0],max(dp[N-1][1], dp[N-1][2])) <<endl;	

}



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

typedef struct node_t {
	int key;
	struct node_t *parent;
	struct node_t *left;
	struct node_t *right;
} node;

node* newNode(int);
void print(node *);
void inorder(node *);
void preorder(node *);
void insert(int, node *);
node* find(int, node *);
void free_tree(node *);

int main(void)
{
	int i, m, k;
	char com[10];
	node *tree_root;
	
	tree_root = newNode(0);
	
	scanf("%d",&m);
	for (i=0; i<m; i++) {
		scanf("%s",com);
		if(com[0] == 'p') {
			print(tree_root->left);
		} else {
			scanf("%d",&k);
			if(com[0] == 'i') {
				insert(k, tree_root);
			} else if(com[0] == 'f') {
				if(find(k, tree_root) != NULL) {
					printf("yes\n");
				} else {
					printf("no\n");
				}
			}
		}
	}
	
	free_tree(tree_root);
	
	return 0;
}

node* newNode(int key)
{
	node *tmp;
	
	tmp = (node *)malloc(sizeof(node));
	if (tmp == NULL) exit(0);
	
	tmp->key = key;
	tmp->parent = NULL;
	tmp->left = NULL;
	tmp->right = NULL;
	
	return tmp;
}

void print(node *root)
{
	inorder(root);
	printf("\n");
	preorder(root);
	printf("\n");
}

void inorder(node *node)
{
	
	if(node->left != NULL) inorder(node->left);
	printf(" %d",node->key);
	if(node->right != NULL) inorder(node->right);
}

void preorder(node *node)
{
	printf(" %d",node->key);
	if(node->left != NULL) preorder(node->left);
	if(node->right != NULL) preorder(node->right);
}

void insert(int k, node *tree_root)
{
	node *x, *y, *z;
	
	z = newNode(k);
	y = tree_root;
	x = tree_root->left;
	while (x != NULL) {
		y = x;
		if(z->key < x->key) {
			x = x->left;
		} else {
			x = x->right;
		}
	}
	z->parent = y;
	if(y == tree_root || z->key < y->key) {
		y->left = z;
	} else {
		y->right = z;
	}
}

node* find(int k, node *tree_root) 
{
	node *x;
	
	x = tree_root->left;
	while (x->key != k) {
		if(x->key > k) {
			if(x->left == NULL) return NULL;
			x = x->left;
		} else {
			if(x->right == NULL) return NULL;
			x = x->right;
		}
	}
	return x;
}

void free_tree(node *root)
{
	if(root->left != NULL) free_tree(root->left);
	if(root->right != NULL) free_tree(root->right);
	free(root);
}



Measured memory_bytes: 20440
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename TYPE>
void print_vec(const vector<TYPE>& v){
	for(int i=0; i<v.size(); i++){
		cout << v[i] << " ";
	}
	cout << endl;
}

template <typename TYPE>
void print_vec2(const vector<vector<TYPE>>& v){
  cout << endl;
  for(int i=0; i<v[0].size(); i++) cout << i << "   "; 
  cout << endl;
	for(int i=0; i<v.size(); i++){
		cout << "i=" << i << ":   " << endl;
		for(int j=0; j<v[i].size(); j++){
			if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << "   ";
      else cout << "\x1B[31m" << v[i][j] << "   ";//https://stackoverrun.com/ja/q/12618775
		}
		cout << "\x1B[0m" << endl;
	}
}
 

int main(){
  ll X;
  cin >> X;


  //A^5 - B^5 = (A-B)^5 + 5AB(A-B)^3 + 5{(AB)^2}(A-B)
  const ll SIZE = 1000;
  for(ll A=-SIZE; A<=SIZE; A++){
    for(ll B=-SIZE; B<=SIZE; B++){
      ll ko1 = (A-B)*(A-B)*(A-B)*(A-B)*(A-B);
      ll ko2 = 5 * A * B * (A-B)*(A-B)*(A-B);
      ll ko3 = 5 * A*A*B*B * (A-B);
      if(ko1+ko2+ko3 == X){
        cout << A << " " << B << endl;
        return 0;
      }
    }
  }
  cout << " Nothing!!" << endl;
  return 0;

  //以下の解法だと X=3とかで答えが出なかった
  // const ll SIZE = 1000;
  // for(ll A=-SIZE; A<=SIZE; A++){
  //   for(ll B=-SIZE; B<=SIZE; B++){
  //     if(A*A*A*A*A == B*B*B*B*B + X){
  //       cout << A << " " << B << endl;
  //       return 0;
  //     }
  //   }

  // }
  // return 0;

    
}


Measured memory_bytes: 3596
from heapq import*
def g(s,g):
 F=[1e7]*-~n;F[s]=0
 H=[(0,s)]
 while H:
  c,u=heappop(H)
  if u==g:return c
  for f,v in E[u]:
   t=c+f
   if t<F[v]:F[v]=t;heappush(H,(t,v))
 return-1
for e in iter(input,'0 0'):
 n,k=map(int,e.split())
 E=[[]for _ in[0]*-~n]
 for _ in[0]*k:
  f=[*map(int,input().split())]
  if f[0]:
   c,d,e=f[1:]
   E[c]+=[(e,d)];E[d]+=[(e,c)]
  else:print(g(*f[1:]))


Measured memory_bytes: 5924
#!/usr/bin/env python
# -*- coding: utf-8 -*-
N = int(raw_input())
a = raw_input().split(" ")
output = 0

b = []

for i in a:
    b.append(int(i))

b.sort(reverse=True)

for i in range(N):
    output = output + int(b[2*i+1])

print output


Measured memory_bytes: 32208
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);  
const ld EPS = 0.0000000001;
#define REP(i, n) for(ll i=0; i<(ll)(n); i++)
#define REPD(i, n) for(ll i=n-1; 0<=i; i--)
#define FOR(i, a, b) for(ll i=a; i<(ll)(b); i++)
#define FORD(i, a, b) for(ll i=a; (ll)(b)<=i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))


int main(){
    ll n;   cin >> n;
    vector<ll> div;
    for(ll i=1; i*i<=n; i++){
        if(n%i==0){
            div.push_back(i);
            if(n/i!=i) div.push_back(n/i);
        }
    }
    sort(ALL(div));
  
    ll sum=0;
    for(auto x: div){
        ll obj = n/x-1;
        if(obj<1) continue;
        if(n/obj == n%obj) sum += obj;
    }
    cout << sum << endl;
}

Measured memory_bytes: 384
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<deque>
#include<queue>

using namespace std;

using lli = long long int;
using ulli = unsigned long long int;

//#define EVEL 1

#ifndef EVEL
#define dbug(X) std::cout << #X << ":" <<X<<" " ;
#define dbugf(s) std::cout << s << " ";
#define dbugln std::cout<<"\n";
#else
#define dbug(X) {}
#define dbugf(s) {}
#define dbugln {}
#endif

int N, in;
vector<int> a;

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

	cin >> N;
	for(int i = 0; i < N; i++){
		cin >> in;
		a.push_back(in);
	}
	sort(a.begin(), a.end());
	cout << a[N-1] - a[0] << endl;

  return 0;
}


Measured memory_bytes: 256
n = int(input())
la = list(input().split())

if n % 2 == 0:
    l = list(reversed(la))[::2]
    l = l + la[::2]
    print(" ".join(l))

else:
    l = list(reversed(la))[::2]
    l = l + la[1::2]
    print(" ".join(l))

Measured memory_bytes: 27040
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i,m,n) for(lli i = m;i < n;i++)
#define rrep(i,m,n) for(lli i=m-1;i>=n;i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(),N.end()),N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(),n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K,L,N,S) vector<L> K(N,S)
#define DV(K,L,N,M,S) vector<vector<L>> K(N,vector<L>(M,S))
#define TV(K,L,N,M,R,S) vector<vector<vector<L>>> K(N,vector<vector<L>>(M,vector<L>(R,S)))
#define pint pair<lli,lli>
#define paf(L,R) pair<L,R>
#define mod 1000000007
#define MAX 51000000
#define ALL(a)  a.begin(),a.end()
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
int main(){
  lli A,B,C,D,E,F,N,M,K,L,X,Y,Z,H,W,sum=0,num=0,flag=0;string S,T;
   DV(Q,lli,3,3,0);
   rep(i,0,3)rep(j,0,3)cin >> Q[i][j];
   cin >> N;
   Vec(P,lli,N,0);
   rep(i,0,N)cin >> P[i];
   DV(isok,bool,3,3,0);
   rep(i,0,N)rep(j,0,3)rep(k,0,3)if(P[i]==Q[j][k])isok[j][k]=1;
   rep(i,0,3)if(isok[i][0]==isok[i][1]&&isok[i][2]==1&&isok[i][0]==isok[i][2])flag=1;
   rep(i,0,3)if(isok[0][i]==isok[1][i]&&isok[2][i]==1&&isok[0][i]==isok[2][i])flag=1;
   if(isok[0][0]==isok[1][1]&&isok[2][2]==1&&isok[0][0]==isok[2][2])flag=1;
   if(isok[0][2]==isok[1][1]&&isok[2][0]==1&&isok[0][2]==isok[2][0])flag=1;
   if(flag==1)Out("Yes");
   else Out("No");
}

Measured memory_bytes: 256
var
    a,b,p:longint;
    s:string;
    minus:boolean;
begin
    readln(s);
    p:=pos('+',s);
    if p=0 then
    begin
        p:=pos('-',s);
        minus:=true;
    end;
    val(copy(s,1,p-2),a);
    val(copy(s,p+2,length(s)-p-1),b);
    if minus then writeln(a-b)
    else writeln(a+b);
end.




Measured memory_bytes: 128
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int MOD = 1000000007;
int INF = numeric_limits<int>::max() / 2;


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

  int N;
  cin >> N;
  int ans = 1;
  for(int i=2;i<=N;i++){
    ans *= i;
    ans %= MOD;
  }
  cout << ans << endl;

}

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

int main() {
  
  int N, M; cin >> N >> M;
  
  set<int> memo;
  
  vector<pair<int, int>> vec(M);
  
  for (int i = 0; i < M; i++) {
    
    int a, b; cin >> a >> b;
    
    vec[i].first = a; vec[i].second = b;
    
  }
  
  sort(vec.begin(), vec.end());
  
  for (int i = 0; i < M; i++) {
    
    int tmpA = vec[i].first, tmpB = vec[i].second;
    
    if (tmpA == 1) memo.insert(tmpB);
    
    if (tmpB == N) {
      
      if (memo.count(tmpA)) { cout << "POSSIBLE" << endl; return 0; }
      
    }
    
  }
  
  cout << "IMPOSSIBLE" << endl;
  
}

Measured memory_bytes: 9508
n = int(input())
w = list(map(int, input().split()))
ans = 0

for i in range(n-1):
  s1 = sum(w[:i+1])
  s2 = sum(w[i+1:])
  if i == 0:
    ans = abs(s1-s2)
  if abs(s1-s2) < ans:
    ans = abs(s1-s2)
  
print(ans)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD=998244353;

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

Measured memory_bytes: 256
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;

int main(){
  ll N;
  scanf("%lld", &N);
  vector<ll> vll;
  for (int i = 0; i < 5; i++) {
    ll tmp;
    scanf("%lld", &tmp);
    vll.push_back(tmp);
  }
  sort(vll.begin(), vll.end());
  
  printf("%lld", N / vll[0] + 5 - (((N % vll[0]) == 0)?1:0));
  
  return 0;
}


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

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

	int maxv = 0;
	for (int center = 1; center < n; center++) {
		int num = 0;
		for (char c = 'a'; c <= 'z'; c++) {
			bool found_l = false;
			bool found_r = false;
			for (int i = 0; i < center; i++) {
				if (s[i] == c) found_l = true;
			}
			for (int i = center; i < n; i++) {
				if (s[i] == c) found_r = true;
			}
			if (found_l && found_r) num++;
		}
		maxv = max(maxv, num);
	}
	cout << maxv << endl;

	return 0;
}

Measured memory_bytes: 256
a, b, c, d = map(int, input().split())
#print(a, b, c, d)

distance_ab = abs(a - b)
distance_bc = abs(b - c)
distance_ac = abs(a - c)

    # aとcが間接的に話せる。
if distance_ab <= d and distance_bc <= d:
    print('Yes')
    # aとcが直接話せる。
elif distance_ac <= d:
    print('Yes')
else:
    print('No')

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

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

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

void func(long long D, long long G, std::vector<long long> p, std::vector<long long> c){

    lli ans = INF;
    for(lli bit=0;bit<(1LL<<D);bit++){
        //prepare
        lli score = 0;
        lli cnt = 0;
        REP(i,0,D){
            if(!(bit&(1LL<<i)))continue;
            score += (i+1)*100*p[i] + c[i];
            cnt += p[i];
        }

        if(score >= G){
            chmin(ans,cnt);
            continue;
        }

        for(lli i = D-1;i>=0;i--){
            if((bit&(1LL<<i)))continue;
            lli left = max(0LL,G-score);
            if(left > (i+1)*100*p[i]){
                score += (i+1)*100*p[i]+c[i];
                cnt += p[i];
            }
            else{
                lli plusC = (left)/((i+1)*100);
                if((left%(100*(i+1)))!=0)plusC++;
                cnt += plusC;
                break;
            }
        }
        chmin(ans,cnt);
    }

    cout<<ans<<endl;

}

int main(){
    // cout << fixed << setprecision(5);

    long long D;
    scanf("%lld",&D);
    long long G;
    scanf("%lld",&G);
    std::vector<long long> p(D);
    std::vector<long long> c(D);
    for(int i = 0 ; i < D ; i++){
        scanf("%lld",&p[i]);
        scanf("%lld",&c[i]);
    }
    func(D, G, std::move(p), std::move(c));
    return 0;
}


Measured memory_bytes: 256