akhauriyash/Code-Regression huggingface dataset:
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
=begin
ITP1_7-C
 
Structured Program II - Spreadsheet
Time Limit : 1 sec, Memory Limit : 65536 KB
??¨?¨????

??¨?¨??????????????°???????????????°???????????????????????????

??¨????????°r??¨?????°c???r ?? c ???????´????????????¨?????????????????§???????????¨??????????¨?????????\????????°????????¨???????????????????????°?????????????????????????????????
Input

???????????????r??¨c????????????????????§??????????????????????¶????r??????????????????c????????´??°????????????????????§?????????????????????
Output

(r+1) ?? (c+1) ?????°????????¨??????????????????????????????????????£???????????´??°????????????????????§????????£??????????????????????????????????????¨??????????????????????¨?????????????????????????????????¨??????????????????????¨?????????????????????????????????¨??¨??????????¨????????????\??????????????????
Constraints

    1 ??? r, c ??? 100
    0 ??? ????´? ??? 100

Sample Input

4 5
1 1 3 4 5
2 2 2 4 5
3 3 0 1 1
2 3 4 4 6

Sample Output

1 1 3 4 5 14
2 2 2 4 5 15
3 3 0 1 1 8
2 3 4 4 6 19
8 9 9 13 17 56

=end

fh=STDIN
fl=fh.gets.chomp.split.map{|x| x.to_i}
rn = fl[0]
cn = fl[1]
#puts "rn=#{rn} cn=#{cn}"
v = Array.new(rn+1).map{Array.new(cn+1)}
(0..(rn)).each {|r|
  (0..(cn)).each {|c|
    v[r][c] = 0}}

(0..(rn-1)).each {|r|
  fl = fh.gets.chomp.split.map{|x| x.to_i}
  (0..(cn-1)).each {|c|
    v[r][c] = fl[c] }}
# for debug
#(0..(rn-1)).each {|r|
#  puts v[r].map{|x| x.to_s}.join(":")}

# calc
(0..(rn-1)).each {|r|
#  puts v[r].map{|x| x+1}.join("|")
#puts "wa=" + (v[r].inject(0) {|acc, item| acc + item}).to_s
  v[r][cn] = v[r].inject(0) {|acc, item| acc + item}}


(0..(cn)).each {|c|
  s = 0
  (0..(rn-1)).each {|r| 
    s += v[r][c]
  }
  v[rn][c] = s}

(0..(rn)).each {|r|
  puts v[r].map {|x| x.to_s}.join(" ")}

Measured memory_bytes: 8980
#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 main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    int max_num = (n-1)*(n-2) / 2;
    if(k > max_num){
        cout << -1 << endl;
        return 0;
    }
    vector<P> v;
    for(int i = 1; i < n; i++){
        for(int j = 1; j < i; j++){
            v.emplace_back(j+1, i+1);
        }
    }
    vector<P> ans;
    for(int i = 1; i < n; i++){
        ans.emplace_back(1, i+1);
    }
    for(int i = 0; i < max_num - k; i++){
        ans.push_back(v[i]);
    }
    cout << ans.size() << endl;
    for(P &p : ans){
        cout << p.first << " " << p.second << endl;
    }
    return 0;
}

Measured memory_bytes: 384
#include<stdio.h>
#include<string.h>

int main()
{
	int m,n,k,count,num,ans;
	int left[1000];

	while(1)
	{
		scanf("%d %d",&n,&m);
		if(m==0&&n==0)break;
		for(k=0;k<n;k++)
		{
			left[k]=1;
		}
		num=0;
		for(k=0;k<n-1;k++)
		{
			for(count=0;count<m;count++)
			{
				num++;
				if(num>n)num-=n;
				
				if(left[num-1]==0)
				{
					count--;
					continue;
				}
			}
			left[num-1]=0;
		}
		for(num=1;num<=n;num++)
		{
			if(left[num-1]==1)
			{
				ans=num;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

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

//#include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// using cint = cpp_int;
//#define input(a) scanf("%lld", &(a))
//#define output(a) printf("%lld\n", (a))

// Define
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1e9 + 7;
const ll INF = LONG_MAX;
const ull MAX = ULONG_MAX;
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define endl '\n'
#define space ' '
#define def inline auto
#define func inline constexpr ll
#define run __attribute__((constructor)) def _
#define all(v) begin(v), end(v)

// Debug
#define debug(...)                                                             \
    {                                                                          \
        cerr << __LINE__ << ": " << #__VA_ARGS__ << " = ";                     \
        for (auto &&X : {__VA_ARGS__}) cerr << "[" << X << "] ";               \
        cerr << endl;                                                          \
    }

// Loop
#define inc(i, a, n) for (ll i = (a), _##i = (n); i <= _##i; ++i)
#define dec(i, a, n) for (ll i = (a), _##i = (n); i >= _##i; --i)
#define each(i, a) for (auto &&i : a)
#define rep(i, n) inc(i, 0, n - 1)

// Stream
#define fout(n) cout << fixed << setprecision(n)
#define fasten cin.tie(0), ios::sync_with_stdio(0)

// Speed
run() { fasten, fout(10); }
#pragma GCC optimize("O3")
#pragma GCC optimization_level 3
#pragma GCC target("avx")

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

def input() {
    ll A;
    cin >> A;
    return A;
}

ll N, X, Y;

signed main() {
    cin >> X >> Y;
    cout << X + Y / 2 << endl;
}

// for compilation: g++ -Ofast -march=native -o _ _.cpp -std=c++17


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

using namespace std;

int main(int argc, char *argv[]){
  multimap<int,int> mp;
  int N,M;
  int A,B;
 long long int sum=0;
  int count=0;
  cin>>N>>M;
  for(int i=0; i<N; i++){
    cin>>A>>B;
    mp.insert(make_pair(A, B));
  }
  for(auto itr=mp.begin(); itr!=mp.end(); ++itr){
    for(int i=0; i<itr->second; i++){
      if(count>=M) break;
      sum+=itr->first;
      count++;
    }
    if(count>=M) break;
  }
  cout<<sum<<endl;
}




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

ll N,K,L;
vector<int> a;

ll Count(ll mid){
  ll res=0;
  int j=0;
  int sum=0;
  for(int i=0;i<N;i++){
    
    while( j < N && sum < K ){
      sum+=(a[j]<=mid?1:0);
      j++;
    }

    if( sum >= K ){
      res+= min( N-j+1 , N-(i+K)+1 );
    }

    sum -= (a[i]<=mid?1:0);
  }

  return res;
}

int main(){
  cin>>N>>K>>L;
  a.resize(N);
  for(int i=0;i<N;i++){
    cin>>a[i];
  }

  int left=1, right=N;
  while( left < right ){
    int mid=(left+right)/2;
    if( Count(mid) < L ){
      left=mid+1;
    }else{
      right=mid;
    }
  }

  cout<< left <<endl;
  return 0;
}



Measured memory_bytes: 3620
#include<iostream>

using namespace std;

int main(){
  int n,t,j,max;
  
  //N列（１以上１００以下の整数）
  cin>> n;
  //a[i][j](1以上１００以下の整数)
  int a[2][n];
  for(j=0;j<n;j++){
    cin>> a[0][j];
  }
  for(j=0;j<n;j++){
    cin>> a[1][j];
  }
    
  /*int x;
  max=1;
    
 for(t=0;t<n;t++){
    x=0;
  	for(j=0;j<=t;j++){
      x+=a[0][j];
    }
    for(j=t;j<n;j++){
      x+=a[1][j];
    }
    if(max<=x)
      max=x;
  }*/
  
  int x=a[0][0];
  for(j=0;j<n;j++){
  	x+=a[1][j];
  }
  
  max=x;
  if(n>1){
  for(t=0;t<n-1;t++){
  x=x+a[0][t+1]-a[1][t];
  if(x>max)
    max=x;
  }
  }
  cout<< max <<endl;
  return 0;
}


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

int main() {
    string s;cin>>s;
    ll K;cin>>K;
    int N = s.size();
    //'a'との距離
    vector<int> v(N,0);
    rep(i,N){
        v[i] = 'z'-s[i]+1;
        if(s[i] == 'a')v[i] = 0;
    }
    rep(i,N){
        if(v[i]<=K){
            K-=v[i];
            v[i] = 0;
        }
    }
    K%=26;
    /*
    bool flag = true;
    for (int i = N-1; i >= 0; --i){
        if(v[i]>0){
            v[i] -=K;
            flag = false;
            break;
        }
    }
    //全部'a'になっているということ
    if(flag){
        v[N-1] +=26-K;
    }
    */
    if(v[N-1]==0)v[N-1] += 26-K;
    else{
        v[N-1]-=K;
    }
    string T = "";
    rep(i,N){
        if(v[i] == 0){
            T.push_back('a');
        }
        else{
            T.push_back(char('z'-v[i]+1));
        }
    }
    cout<<T<<endl;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
namespace mp = boost::multiprecision;
using Bint = mp::cpp_int;                        // 任意長整数
using Real = mp::number<mp::cpp_dec_float<1024>>;// 仮数部が1024bitの浮動小数点数
using namespace std;
using ll = long long;
using P = pair<int,int>;
using PL = pair<ll,ll>;

vector<pair<Bint,int>> ma = {{1,2}, {2,5}, {3,5}, {4,4}, {5,5}, {6,6}, {7,3}, {8,7}, {9,6}};

Bint Max(Bint a, Bint b){
    return (a > b ? a : b);
} 

int main() { 
    int n, m;
    cin >> n >> m;
    vector<pair<Bint,int>> in(m);
    for(int i = 0; i < m; i++) {
        int t; cin >> t;
        in[i] = ma[t - 1];
        cerr << in[i].first << " : " << in[i].second << endl;
    }

    vector<Bint> dp(n + 1, Bint("-1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")); //dp[i] := i個のマッチを使って作れる最大の整数
    dp[0] = 0;
    for(int i = 0; i < n; i++) {
        for(auto j : in) {
            if(i + j.second > n)continue;
            dp[i + j.second] = Max(dp[i + j.second], dp[i] * 10 + j.first);

        }
    }
    cout << dp[n] << endl;
}

Measured memory_bytes: 11392
/*
    問題をよく読もう！
    論理的に考えよう！
    サンプルを確認しよう!
    絶対に諦めるな！
    工夫をしろ！
    配列は少し多めにとっておく

    Twitterは終わるまでログアウト！
    （間違えて解法をツイートしてはいけないから）

*/
//include
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
#include<stdio.h>
#include<cstring>
//namespace
using namespace std;
//繰り返し
#define REP(i, m, n) for(int i=(int)m; i<(int)n; ++i)
#define rep(i, n) REP(i, 0, n)
//イテレータ
#define all_range(C) begin(C), end(C)
//簡略化
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
typedef pair<string,int> pst;

const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
//最大最小
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) {if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) {if(a<b) a=b;}

//関数

//最大公約数を求める
ll my_gcd(ll x, ll y) {
  ll r;

  while(y!=0) {
    r = x%y;
    x = y;
    y = r;
  }

  return x;
}

//最小公約数を求める
ll my_lcm(ll x, ll y) {

  return (x*y)/my_gcd(x,y);
}

//xのy乗を求める ※参考：10^18乗まで
ll my_pow(ll x, ll y) {
  ll sum = 1;

  while(y>0) {
    sum *= x;
    --y;
  }

  return sum;
}

/*考えを書くスペース

*/
//main.cpp----------------------------


void solve();

ll n;
ll a[101];

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];

  solve();

  return 0;
}

void solve() {

  sort(a, a+n);
  ll min = inf;
  for(int i=a[0]; i<=a[n-1]; ++i) {
    ll cost = 0;
    for(int j=0; j<n; ++j) {
      cost += my_pow(a[j]-i, 2);
    }
    chmin(min, cost);
  }

  cout << min << endl;
}


Measured memory_bytes: 256
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N=input()
    SN=0
    for x in N:
        SN+=int(x)
    YesNo(not int(N)%SN)
    

if __name__ == '__main__':
    main()


Measured memory_bytes: 38384
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
#include <ctime>
#include <tuple>
#include <bitset>
using namespace std;

/*
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long  ll;

#define fi first
#define se second

using Graph = vector<vector<int>>;




int main() {
    ll k;
    cin>>k;
    ll ans=0;
    for(ll i=1;i<=k;i++){
        for(ll j=1;j<=k;j++){
            ll tmp = __gcd(i,j);
            for(ll m=1;m<=k;m++){
                ans+=__gcd(tmp,m);
            }
        }
    }
    cout<<ans<<endl;






    return 0;
}


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

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

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

  int n = s.size();
  int m = t.size();
  bool ok=true;
  
  rep(i,n-m+1){
    ok = true;
    int k = n-m-i;
    rep(j,m){
      if(s.at(k+j)!=t.at(j)){ok = false; break;}
    }
    if(ok){
      rep(j,m){
        s.at(k+j) = t.at(j);
      }
      break;
    }
  }

  if(!ok){
    rep(i,n-m+1){
      ok = true;
      int k = n-m-i;
      rep(j,m){
        if(s.at(k+j)!=t.at(j)&&s.at(k+j)!='?'){ok = false; break;}
      }
      if(ok){
        rep(j,m){
          s.at(k+j) = t.at(j);
        }
        break;
      }
    }
  }
  
  rep(i,n){
    if(s.at(i)=='?') s.at(i)='a';
  }

  if(!ok||m>n) cout << "UNRESTORABLE";
  else cout << s;
}



Measured memory_bytes: 3624
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define imfor(i, n) for(int i = 0; i < n; i++)
using namespace std;
ll MOD = 1000000007;

int main() {
    string s;
    string t;
    cin >> s;
    cin >> t;
    sort(s.begin(), s.end());
    sort(t.begin(), t.end());
    reverse(t.begin(), t.end());
    if (s < t) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}

Measured memory_bytes: 256
S = input()

MOD = 10 ** 9 + 7
N = len(S)
dp = [[0] * 4 for _ in range(N + 1)]
dp[-1][-1] = 1

for i in reversed(range(N)):
    m = 3 if S[i] == '?' else 1
    for j in reversed(range(3)):
        dp[i][j] = m * dp[i + 1][j]
        if S[i] == '?' or S[i] == 'ABC'[j]:
            dp[i][j] += dp[i + 1][j + 1]
        dp[i][j] %= MOD
    
    # j == 3
    dp[i][3] = m * dp[i + 1][3] % MOD
            
print(dp[0][0] % MOD)



Measured memory_bytes: 56044
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define INF 2e9
#define LINF 1e18
#define mod 1000000007
template<class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> P;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};


int main() {
    string s; cin >> s;
    int n = sz(s);
    rep(i,n) s[i] = 'x';
    cout << s << endl;
}  

Measured memory_bytes: 3588
#include <iostream>

using namespace std;

int main() {
  int A, B;
  cin >> A >> B;
  int res = 0;
  for (int i = A; i <= B; i++) {
    int s = i%10, t = i/10000%10;
    int u = i/10%10, v = i/1000%10;
    if (s==t && u==v) res++;
  }

  cout << res << endl;
}


Measured memory_bytes: 256
h, w = map(int, input().split())

def div(h, w):
    S1, S2, S3 = 0, 0, 0
    mw = w // 2
    ans = 10 ** 10
    for i in range(1, h):
        mh = (h-i) // 2
        S1 = i * w
        for j in range(2):
            if j == 0:
                S2 = (h-i) * mw
                S3 =  (h-i) * (w-mw)
            else:
                S2 = mh * w
                S3 = (h-i-mh) * w

            maxS = max(S1, S2, S3)
            minS = min(S1, S2, S3)
            diff = maxS-minS
            ans = min(ans, diff)
    return ans

ans = min(div(h, w), div(w, h))
print(ans)


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

const int mod = 1000000007;

int mpow(int a, int b)
{
    if (b == 0) return 1;

    if (b % 2) {
        return 1LL * a * mpow(a, b - 1) % mod;
    }
    else {
        int d = mpow(a, b / 2);
        return 1LL * d * d % mod;
    }
}
int main()
{
    int h, w, a, b;
    cin >> h >> w >> a >> b;

    // 0,0 to 0,b..a,b
    // 0,b..a,b to h,w

    int fact[200010] = {1,1};
    for (int i = 1; i < 200010; i++) {
        fact[i] = 1LL * fact[i - 1] * i % mod;
    }
    
    int ans = 0;
    for (int i = 1; i <= h - a; i++) {
        int t = 1;
        t = 1LL * t * fact[b + i - 2] % mod * mpow(1LL * fact[b - 1] * fact[i - 1] % mod, mod - 2) % mod;
        t = 1LL * t * fact[h - i + w - b - 1] % mod * mpow(1LL * fact[h - i] * fact[w - b - 1] % mod, mod - 2) % mod;
        // cerr << t << " " ;
        ans = (ans + t) % mod;
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 1024
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
//var input = Console.ReadLine().Split().Select(int.Parse).ToArray();
namespace AtCoderSolve
{
    class Solve
    {
        const int mod = 1000000007;
        static void Main(string[] args)
        {
            //var watch = new Stopwatch();
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);

            long N = long.Parse(Console.ReadLine());
            Console.WriteLine(N % 2 == 0 ? N : 2 * N);
            
            
            //watch.Start();

           

            Console.Out.Flush();
            /*watch.Stop();

            Console.WriteLine($"time:{watch.ElapsedMilliseconds} ms"); //Debug Only.
            Console.Out.Flush();*/
        }
    }
    


    public class Calculation
    {
        
    }

    public class Graph
    {

    }
}
    

Measured memory_bytes: 13140
# -*- coding: utf-8 -*-
import math,string,itertools,fractions,heapq,collections,re,array,bisect

def s():
    return input()
def n():
    return int(input())
def d():
    return float(input())

def ls():
    return input().split()
def ln():
    return map(int, input().split())
def ld():
    return map(float, input().split())

def fs():
    return [input() for i in range(input())]
def fn():
    return [int(input()) for i in range(input())]
def fd():
    return [float(input()) for i in range(input())]

N,K = ln()
if N%K == 0:
    print(0)
else:
    print(1)


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

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

  int n; cin >> n;
  vector<int> a(n);
  int mx = 0;
  for(int i = 0; i < n; i++){
    cin >> a[i];
    mx = max(mx,a[i]);
  }

  int mid = mx / 2,r;
  int res = 1e9;
  for(int i = 0; i < n; i++){
    if(a[i] == mx) continue;
    if(abs(mid - a[i]) < res){
      res = abs(mid - a[i]);
      r = a[i];
    }
  }
  cout << mx << " " << r << endl;

  return 0;
}


Measured memory_bytes: 1024
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#from __future__ import print_function
import time
import sys
import io
import re
import math
start = time.clock()
i = 0
l=map(str, raw_input().split())
k=[]
for i in l:
    if i=='-' or i=='+' or i=='*':
        t=k.pop()
        s=k.pop()
        u=str(s)+i+str(t)
        k.append(eval(u))
    else:
        k.append(i)
print k[0]

Measured memory_bytes: 4576
import sys
input = sys.stdin.readline


def read():
    K, T = map(int, input().strip().split())
    A = list(map(int, input().strip().split()))
    return K, T, A


def solve(K, T, A):
    if T == 1:
        return K-1
    A.sort()
    return max(A[-1] - sum(A[:-1]) - 1, 0)


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))


Measured memory_bytes: 3060
#include <iostream>
#include <string>
#include <algorithm>
#define S str[ima]
#define F formula(P,Q,R)
using namespace std;
string str;
int ima;
int formula(int P,int Q,int R) {
  int res;
  while(1){
    if(('0'<= S && S<='2')||('P'<=S&& S<='R')){
      res = S-'0';
      if(S=='P') res = P;
      if(S=='Q') res = Q;
      if(S=='R') res = R;
      ima++;
    }
    else if(S=='-') {
      ima++;
      res = F;
      if(res == 0) res = 2;
      else if(res == 2) res = 0;
    }
    else if(S=='(') {
      ima++;
      res = F;
      if(S=='*')ima++, res = min(F,res);
      if(S=='+')ima++, res = max(F,res);
      ima++;
    }
    else break;
  }
  return res;
}

int main() {
  while(1) {
    cin >> str;
    if(str==".") break;
    int ans = 0;
    for(int i=0;i<3;i++)
      for(int j=0;j<3;j++)
	for(int k=0;k<3;k++){
	  ima = 0;
	  if(formula(i,j,k) == 2) ans++;
	}
    cout <<ans <<endl;
  }
  return 0;
}

Measured memory_bytes: 1192
#include<iostream>
#include<algorithm>
using namespace std;
long  a[100010];
long  cont[100010]={};
int main(){
    long long n,q,x,y;
    while(~scanf("%lld",&n)){
        long long total=0;  
        for(int i=0;i<n;i++){
            scanf("%ld",&a[i]);
            cont[a[i]]++;
            total+=a[i];
        }
        scanf("%lld",&q);
        for(int i=0;i<q;i++){
            scanf("%lld %lld",&x,&y);
            total += (cont[x])*(y-x);
            cont[y]+=cont[x];
            cont[x]=0;
            cout << total << endl;
        } 
    }
    
}

Measured memory_bytes: 5208
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main()
{
    int x, a;
    cin >> x >> a;

    if (x < a) cout << 0 << endl;
    else cout << 10 << endl;
    
}


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

	int H, W;
	
	while (1){
		cin >> H >> W;
		if (H == 0 && W == 0)break;


		for (int i = 0; i < H ; i++)
		{
			for (int j = 0; j < W ; j++)
			{
				if ((i+j)%2==0)
				{
					cout << "#";
				}
				else
				{
					cout << ".";
				}
			}
			cout << endl;
		}
		cout << endl;
	}






	return 0;
}

Measured memory_bytes: 3052
N,M=gets.split.map &:to_i
dp=[1<<28]*(1<<N)
dp[0]=0
M.times{
	a=gets.to_i
	c=gets.split.inject(0){|s,x|s|1<<(x.to_i-1)}
	(1<<N).times{|i|dp[i|c]=[dp[i|c],dp[i]+a].min}
}
p dp[(1<<N)-1]<1<<28?dp[(1<<N)-1]:-1

Measured memory_bytes: 1916
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

int main(){
    int A, B,C, D;
    cin >> A >> B >> C >> D;
    int ans = 0;
    if(A<= B){
        ans += A;
    }else{
        ans += B;
    }

    if(C <= D){
        ans += C;
    }else{
        ans += D;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()


int main(){
    ll N;
    cin>>N;
    
    for(ll h=1;h<=3500;h++){
        for(ll n=1;n<=3500;n++){
            ll tmp1=4*h*n-N*n-N*h;
            ll tmp2=N*h*n;

            if(tmp1<=0) continue;
            if(tmp2%tmp1==0){
                cout<<h<<" "<<n<<" "<<tmp2/tmp1<<endl;
                return 0;
            }
        }
    }
}

Measured memory_bytes: 3628
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) throws Exception {

        InputStreamReader is = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(is);

        List<String> mylist = new ArrayList<>();

        // 標準入力からの値を変数strinputに代入
        String strinput = reader.readLine();

        while (strinput != null) {

            mylist.add(strinput);
            strinput = reader.readLine();

        }

        String arr01[] = mylist.get(0).split(" ");

        int n = Integer.valueOf(arr01[0]);

        boolean chkflg = false;

        outside: for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= 9; j++) {
                if (i * j == n) {

                    chkflg = true;
                    break outside;
                }
            }
        }

        System.out.println(chkflg ? "Yes" : "No");

    }

}

Measured memory_bytes: 21588
d = int(input())

c = list(map(int,input().split()))

s = []

for _ in range(d):
    temp = list(map(int,input().split()))

    s.append(temp)

last = [0] * 26

result = 0

for i in range(d):
    # temp = [x - c[i] * day for i, (x, day) in  enumerate(zip(s[i], last))]

    # print(temp.index(max(temp)) + 1)

    # last = [x + 1 for x in last]

    # last[temp.index(max(temp))] = 0

    print(i % 26 + 1)

Measured memory_bytes: 67852
N = int(input())
L = list(map(int,input().split()))
L = sorted(L)
count=0
for i in range(len(L)):
  for j in range(i,len(L)):
    for k in range(j,len(L)):
      if (L[i]!=L[j]!=L[k]) and (L[i]+L[j]>L[k]):
        count += 1
print(count)

Measured memory_bytes: 9188
#include<iostream>
using namespace std;
int main()
{
    double w,h,x,y;
    double ans1,ans2,ans3,ans4,answer1,answer2;
    cin >> w >> h >> x >> y;
    ans1=( h-y ) * w;
    ans2=y * w;
    ans3=( w-x ) * h;
    ans4=x * h;
    if(ans1<ans2){
        answer1=ans1;
    }
    else{
        answer1=ans2;
    }
    if(ans3<ans4){
        answer2=ans3;
    }
    else{
        answer2=ans4;
    }
    if(( w / h == x / y )&&( answer1!=w * h/2||answer2!=w * h/2)){
        cout << w * h/2 << " 0" << endl;
    }
    else if(( w == h )&&( answer1!=w * h/2||answer2!=w * h/2)){
        cout << w * h/2 << " 0" << endl;
    }
    else if(answer1 < answer2){
        cout << answer2 << " 0" << endl;
    }
    else if(answer2 < answer1 ){
        cout << answer1 << " 0" << endl;
    }
    else{
        cout << answer1 << " 1" << endl;
    }
    return 0;
}


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

// definition {{{ 1

// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x,y) scanf("%d%d", &x, &y)
#define Scd3(x,y,z) scanf("%d%d%d", &x, &y, &z)

#define Scll(x) scanf("%llu", &x)
#define Scll2(x,y) scanf("%llu%llu", &x, &y)
#define Scll3(x,y,z) scanf("%llu%llu%llu", &x, &y, &z)

#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2

// constants {{{ 2
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// }}} 2

// systems {{{ 2
#define Rep(x,y) for(int x = 0; x < y; x++)
#define Repe(x,y,z) for(int x = z; x < y; x++)
// }}} 2

// output {{{ 2
#define YesNo(a) (a)?printf("Yes\n"):printf("No\n");
// }}} 2

// }}} 1

struct edge{
    int v,c,r;
    // u->v cap() u<-v
};

int V,E;
vector< vector<edge> > e; // e[u] : e.first = u->v ; e.second = cap(u);
vector< bool > used;


void addedge(int a, int b, int c){
    e[a].push_back( edge{b,c,(int)e[b].size()} );
    e[b].push_back( edge{a,0,(int)e[a].size()-1} );
}

void scan(){

    Scd2(V,E);
    e.assign( V, vector<edge>(0) );
    int u,v,c;
    Rep(i,E){
        Scd3(u,v,c);
        addedge(u,v,c);
    }

}

// v:now , t:sink , c:capacity
int dfs( int v, int t, int c){
    if( v==t ) return c;
    used[v] = true;
    // Ford-Fulkerson algorithm
    for( int ei = 0; ei < e[v].size(); ei++ ){
        edge &ez = e[v][ei];
        if( !used[ez.v] && ez.c > 0){
            int d = dfs( ez.v,t,min(c,ez.c));
            if( d>0 ){
                ez.c -= d;
                e[ez.v][ez.r].c += d;
                return d;
            }
        }
    }
    return 0;
}

int ans( int s , int t ){
    int flow = 0;
    while(1){
        used.assign( V, false );
        int f = dfs(s,t,INF);
        if( f ) flow+=f;
        else break;
    }
    return flow;
}

int main() {

    scan();
    printf ("%d\n", ans(0,V-1) );

    return 0;
}




Measured memory_bytes: 3304
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int x = Integer.parseInt(sc.next());

    if(x==7 || x==5 || x==3){
      System.out.println("YES");
    }else{
      System.out.println("NO");
    }
  }
}

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


/* エイリアス */
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define endl '\n'

#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define em emplace
#define mp make_pair


/* 関数マクロ */
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; ++i)
#define rep3(i, n, m) for (ll i = n; i >= m; --i)
#define all(v) v.begin(), v.end()
#define si(v) int(v.size())
#define UNIQUE(v) sort(all(v)), v.erase(unique(all(v)),v.end())


/* 定数 */
const ll mod = 1e9 + 7;
const ll infll = (1LL << 62) - 1;
const ll inf = (1LL << 30) - 1;


/* その他 */
template<class S, class T> inline bool chmax(S &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class S, class T> inline bool chmin(S &a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;


signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  ll n; cin >> n;
  deque<ll> que;

  rep(i, n) {
    ll a; cin >> a;
    que.push_back(a);
  }

  sort(all(que));
  deque<ll> ans;

  ans.push_front(que.front()); que.pop_front();

  while (!que.empty()) {
    if (si(que) == 1) {
      ll v = que.front(); que.pop_front();
      if (abs(v - ans.back()) < abs(v - ans.front())) ans.push_front(v);
      else ans.push_back(v);
      break;
    }

    ll left = que.front(); que.pop_front();
    ll right = que.back(); que.pop_back();

    ll l = max(abs(left - ans.front()), abs(left - ans.back()));
    ll r = max(abs(right - ans.front()), abs(right - ans.back()));

    if (l < r) {
      que.push_front(left);
      if (abs(right - ans.front()) < abs(right - ans.back())) ans.push_back(right);
      else ans.push_front(right);
    } else {
      que.push_back(right);
      if (abs(left - ans.front()) < abs(left - ans.back())) ans.push_back(left);
      else ans.push_front(left);
    }
  }

  ll sum = 0;
  ll v = ans.front(); ans.pop_front();
  while (!ans.empty()) {
    ll a = ans.front(); ans.pop_front();
    sum += abs(v - a);
    v = a;
  }

  cout << sum << endl;
}


Measured memory_bytes: 4424
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using std::cin;
using std::cout;
using std::endl;

int main(void) {
    int h, w;
    cin >> h >> w;
    std::string s;
    for (int i = 0; i < w + 2; i++) {
        s += '#';
    }
    cout << s << endl;

    for (int i = 0; i < h; i++) {
        std::string t, u;
        t += '#';
        cin >> u;
        t += u;
        t += '#';
        cout << t << endl;
    }
    cout << s << endl;

    return 0;
}


Measured memory_bytes: 888
import java.util.Scanner;

public class Main{
    String[][] tb = {
	{},
	{"", "a", "f", "k", "p", "u", "z"},
	{"", "b", "g", "l", "q", "v", "."},
	{"", "c", "h", "m", "r", "w", "?"},
	{"", "d", "i", "n", "s", "x", "!"},
	{"", "e", "j", "o", "t", "y", " "}
    };

    Main(){
	Scanner sc = new Scanner(System.in);

	while(sc.hasNext()){
	    String res = "";
	    char[] ch = (sc.next()).toCharArray();
	    if(ch.length%2 == 1){
		res = "NA";
	    } else {
		for(int i = 0; i < ch.length; i+=2){
		    int w = (int)(ch[i]-'0'), h = (int)(ch[i+1]-'0');
		    if(h > 5 || h == 0 || w > 6 || w == 0){
			res = "NA";
			break;
		    }
		    res += tb[h][w];
		}
	    }

	    System.out.println(res);
	}
    }

    public static void main(String[] args){
	new Main();
    }
}

Measured memory_bytes: 12000
#include "bits/stdc++.h"
#define rep(i,n) for(ll i = 0; i < n; i++)
typedef long long ll;
using namespace std;
#define llMAX numeric_limits<long long>::max()
#define intMAX numeric_limits<int>::max()
#define d_5 100000
#define d9_7 1000000007
#define vll vector<vector<long long>>
#define vl vector<long long>
#define vi vector<int>
#define vii vector<vector<int>>
#define pb push_back
#define pf push_front
#define ld long double
#define ll long long
//階乗

ll Factrial(ll num){
  if(num==1||num==0){
    return 1;
  }else if(num<0){
    printf("ERROR_minus\n");
    return 0;
  }else{
    return num*Factrial(num-1);
  }
}

long long modinv(long long a, long long m) {//modの逆元
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m; 
    if (u < 0) u += m;
    return u;
}
int main(void){
  ll n;
  cin>>n;
  vl a(n+1);
  rep(i,n+1)cin>>a[i];
  vl test(n,-1);
  ll loc1,loc2;
  rep(i,n+1){
    if(test[a[i]-1]!=-1){
      loc1=test[a[i]-1];
      loc2=i;
      break;
    }
    test[a[i]-1]=i;
  }
  vl nCk(n+2,1);
  vl lCk(n+2,1);
  rep(i,n+1){
    //cout<<i<<endl;
    if(i==0){
      nCk[i+1]=n+1;
      lCk[i+1]=1;
    //cout<<nCk[i+1]<<" "<<lCk[i+1]<<" "<<rCk[i+1]<<endl;
      cout<<n<<endl;
      continue;
    }
    //nCk-(n-loc2)Ck - rCk
    nCk[i+1]=nCk[i]*(n+1-(i+1)+1);
    nCk[i+1]=max(nCk[i+1],(ll)0);
    nCk[i+1]%=d9_7;
    if(nCk[i+1]%(i+1)==0){
      nCk[i+1]/=i+1;
    }else{
      nCk[i+1]*=modinv(i+1,d9_7);
    }
    //cout<<nCk[i+1];
    nCk[i+1]=max(nCk[i+1],(ll)0);
    nCk[i+1]%=d9_7;
    lCk[i+1]=lCk[i]*(loc1+(n-loc2)-(i+1-1)+1);
    lCk[i+1]=max(lCk[i+1],(ll)0);
    lCk[i+1]%=d9_7;
    if(lCk[i+1]%(i+1-1)==0 && loc1+(n-loc2)>=i+1-1){
      lCk[i+1]/=i+1-1;
    }else if(loc1+(n-loc2)>=i+1-1){
      lCk[i+1]*=modinv(i+1-1,d9_7);
    }
    lCk[i+1]=max(lCk[i+1],(ll)0);
    lCk[i+1]%=d9_7;
    
    //cout<<nCk[i+1]<<" "<<lCk[i+1]<<" "<<rCk[i+1]<<endl;
    cout<<(nCk[i+1]-max(lCk[i+1],(ll)0) + d9_7)%d9_7<<endl;
  }
  return 0;
}

Measured memory_bytes: 4352
#include <stdio.h>
#include <cmath>
#include <bits/stdc++.h>



using namespace std;

typedef vector<int> VI;
typedef vector<string> VS;


//container util
#define SORT(c) sort((c).begin(),(c).end())

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



int main(void){

	int N; cin >> N;
	set<string> st;
	char before;
	string W; cin >> W;
	before = W.back();
	st.insert(W);


	bool ans = true;
	REP(i,N-1){
		cin >> W;
		if(before != W.front() or st.count(W) == 1){
			ans = false;
		}
		before = W.back();
		st.insert(W);
	}

	if(ans == true){
		cout << "Yes" << endl;
	}else{
		cout << "No" << endl;
	}

	return 0;

}


Measured memory_bytes: 256
def CountComp(N,S):
    Ncomp = 0
    for i in range(N):
        for j in range(1,(N-i+1)//2):
            # print(i,i+j,i+2*j)
            if S[i]==S[i+j] or S[i]==S[i+2*j] or S[i+j]==S[i+2*j]:
                pass
            else:
                Ncomp+=1
    return Ncomp

N = int(input())
S = input()

# print(S.count('R'))
NR = S.count('R')
NG = S.count('G')
NB = S.count('B')
# print(NR,NG,NB,NR*NG*NB)
# print(CountComp(N,S))
Nmatch = NR*NG*NB-CountComp(N,S)
print(Nmatch)

Measured memory_bytes: 9212
#include <bits/stdc++.h>
#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long; using pint = pair<int, int>; using pll = pair<ll, ll>;
const long long MOD = 1000000007;

ll N;
string s[100010];
map<string, ll> mp;

signed main(){
    cin >> N;
    REP(i, N){
        cin >> s[i];
        sort(ALL(s[i]));
        mp[s[i]]++;
    }
    ll ans = 0;
    for(auto p : mp){
        ll n = p.second;
        ans += n * (n - 1) / 2;
    }
    PRINT(ans);
    return 0;
}

Measured memory_bytes: 14492
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, ans;
    cin >> n;
    vector <int> b(n - 1);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> b[i];
    }
    ans = b[0];
    for (int i = 0; i < n - 2; i++)
    {
        ans += min(b[i], b[i + 1]);
    }
    ans += b[n - 2];
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define maxn 500010
#define ms(x,y) memset(x,y,sizeof x)
#define inf 0x3f3f3f3f
#define fir first
#define sec second
#define lb(x) (x&(-x))
const int mod=1e9+7;
using namespace std;
typedef long long ll;
set<int>::iterator tmp;
int n,k;
int a[maxn],pos[maxn],deg[maxn];
set<int>front,back;
vector<int>graph[maxn];
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]),pos[a[i]]=i;
	for(int i=1;i<k;i++)front.insert(a[i]);
	for(int i=1;i<=n;i++){
		front.erase(a[i-1]);
		if(i+k-1<=n)front.insert(a[i+k-1]);
		back.insert(a[i]);
		if(i-k>=1)back.erase(a[i-k]);
		tmp=front.upper_bound(a[i]);
		if(tmp!=front.end()){
			graph[pos[(*tmp)]].push_back(i);
			deg[i]++;
		}
		tmp=back.upper_bound(a[i]);
		if(tmp!=back.end()){
			graph[pos[(*tmp)]].push_back(i);
			deg[i]++;
		}
	}
	front.clear();
	for(int i=1;i<=n;i++)if(!deg[i])front.insert(-i);
	int top=0;
	while(!front.empty()){
		int u=-(*front.begin());
		a[++top]=u;
		front.erase(front.begin());
		for(int i=0;i<graph[u].size();i++){
			int e=graph[u][i];
			if(--deg[e]==0)front.insert(-e);
		}
	}
	for(int i=1;i<=n;i++)pos[a[i]]=n-i+1;
	for(int i=1;i<=n;i++)printf("%d\n",pos[i]); 
	return 0;
}


Measured memory_bytes: 60800
#include<iostream>
using namespace std;
typedef long long ll;
ll h[200005];
int a[200005];
int main()
{
	ll n,m,i,ans=0;
	cin>>n>>m;
	for(i=1;i<=n;i++)
	cin>>h[i];
	while(m--)
	{
		ll x,y;
		cin>>x>>y;
		if(a[x]==0&&h[x]<=h[y])
		a[x]=1;
		if(a[y]==0&&h[y]<=h[x])
		a[y]=1;
	}
	for(i=1;i<=n;i++)
	{
		if(a[i]==0)
		ans++;
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 4760
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<int, pair<int, int>> PP;
typedef pair<P, int> PPi;
constexpr double PI = 3.1415926535897932;   // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1e+9 + 7;
// constexpr int mod = 998244353;

int n;
vector<int> a;

template<typename Monoid>
struct SegmentTree{
    using F = function<Monoid(Monoid, Monoid)>;

    int sz;
    vector<Monoid> seg;

    const F f;      // モノイドに対して二項演算を行う関数オブジェクト
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1){
        sz = 1;
        while(sz < n)   sz <<= 1;
        seg.assign(2 * sz, M1);
    }

    void set(int k, const Monoid &x){
        seg[k + sz] = x;
    }

    void build(){
        for(int k = sz - 1; k > 0; --k){
            seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
        }
    }

    void update(int k, const Monoid &x){
        k += sz;
        seg[k] = x;
        while(k >>= 1){
            seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
        }
    }

    Monoid query(int a, int b){
        Monoid L = M1, R = M1;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1){
            if(a & 1)   L = f(L, seg[a++]);
            if(b & 1)   R = f(seg[--b], R);
        }
        return f(L, R);
    }

    Monoid operator[](const int &k) const{
        return seg[k + sz];
    }
};

bool check(int x){
    vector<P> sum(n + 1);
    sum[0] = P(0, 0);
    for(int i = 0; i < n; ++i){
        sum[i + 1].first = sum[i].first + ((a[i] >= x) ? 1 : -1);
        sum[i + 1].second = i + 1;
    }
    sort(sum.begin(), sum.end(), greater<P>());
    SegmentTree<int> seg(n + 1, [](int a, int b){return a + b;}, 0);
    ll cnt = 0;
    for(int i = 0; i <= n; ++i){
        int pos = sum[i].second;
        cnt += seg.query(pos, n + 1);
        seg.update(pos, seg[pos] + 1);
    }
    ll comparision = (ll)n * (n + 1) / 2;
    comparision = (comparision + 1) / 2;
    return cnt < comparision;
}

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

    cin >> n;
    a.resize(n);
    for(int i = 0; i < n; ++i)  cin >> a[i];
    int ok = 1, ng = INF;
    while(ng - ok > 1){
        int mid = (ng + ok) / 2;
        if(check(mid))  ng = mid;
        else    ok = mid;
    }
    cout << ok << endl;
}

Measured memory_bytes: 2440
/**
*    created: 27.06.2020 19:18:44
**/
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define pcnt(bit) __builtin_popcountll(bit)
#define mod 1000000007
#define pi acos(-1.0)
using namespace std;
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;}

signed main() {

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

    int a, b, c;
    cin >> a >> b >> c;
    cout << min({abs(b-a)+abs(c-a),abs(a-b)+abs(c-b),abs(a-c)+abs(b-c)}) << endl;
    return 0;
}

Measured memory_bytes: 3616
$x=0
$y=0
$bord = []

loop{
	line = gets
	if line==nil then
		break
	end
	sp = line.split(nil)
	
	if sp.count() != 2 then
		break
	else
		$x = sp[0].to_i()
		$y = sp[1].to_i()
		if $x == 0 and $y == 0 then
			break
		end
		$bord = Array.new($y).map{Array.new($x,0)}
		max = 0
		for i in 0..$y-1
			line = gets
			left = 0
			for j in 0..$x-1
				tmp = 0
				if line[j] =~ /\d/ then
					tmp = line[j].to_i() 
					if i > 0 then
						up = $bord[i-1][j]
					else
						up = 0
					end

					if up > left then
						tmp += up * 10					
					else
						tmp += left * 10
					end
					
					if tmp > max then
						max = tmp
					end
				end
				$bord[i][j] = tmp
				left = tmp
			end
		end
		print max
		print("\n")
	end
}

Measured memory_bytes: 6144
#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;
int n,k,i,j,x,l;
long long dp0[100005];
long long dp1[100005];
int main()
{

    cin>>n>>k;
    dp1[k]=1;

    for(i=1;i<=n;i++)
    {
        cin>>x;

        for(j=0;j<=k;j++)
        {
            dp0[j]=dp1[j];
            dp1[j]=0;
        }

        for(j=0;j<=k;j++)
        {
            l=j-x;
            if(l<0)
                l=0;

            dp1[l]+=dp0[j];
            if(dp1[l]>mod)
                dp1[l]-=mod;

            dp1[j+1]-=dp0[j];
            if(dp1[j+1]<0)
                dp1[j+1]+=mod;
        }

        for(j=1;j<=k;j++)
        {
            dp1[j]+=dp1[j-1];
            if(dp1[j]>mod)
                dp1[j]-=mod;
        }

    }

    cout<<dp1[0];
    return 0;
}


Measured memory_bytes: 5172
#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=(int)(j);i<(int)(n);i++)
#define REP(i,j,n) for(int i=(int)(j);i<=(int)(n);i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(),(a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int,int>
#define priq priority_queue<int>
#define disup(A,key) distance(A.begin(),upper_bound(ALL(A),(int)(key)))
#define dislow(A,key) distance(A.begin(),lower_bound(ALL(A),(int)(key)))
#define tii tuple<int,int,int>
signed main(){
  string S; cin>>S;
  int ans=0,N=S.size();
  char memo=S[0];
  rep(i,1,N){
    if(memo!=S[i]){
      ans++;
      memo=S[i];
    }
  }
  cout<<ans<<endl;
  }



Measured memory_bytes: 512
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr


n,p=map(int,input().split())
f = factorization(p)
ans = 1
for i in range(len(f)):
    if f[i][1]>=n:
        ans*=f[i][0]**(f[i][1]//n)
print(ans)

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

int n, m;
int l[150];
int k[150][10][2];
long long int dp[150][10][75];
long long int INF = 10e15+7;
long long int z = 0;

int main(){
	while(1){
		memset(k, 0, sizeof(k));
		memset(l, 0, sizeof(l));
		memset(dp, INF, sizeof(dp));
		fill(dp[0][0], dp[149][0], INF);
		long long int ans = INF;

		scanf("%d %d", &n, &m);
		if(n == 0 && m == 0) break;

		for(int i=0; i<n; i++){
			scanf("%d", &l[i]);
			for(int j=0; j<l[i]; j++){
				scanf("%d %d", &k[i][j][0], &k[i][j][1]);
			}
		}
/*
		for(int i=0; i<n; i++){
			printf("%d\t", l[i]);
		}
		printf("\n");
		for(int i=0; i<n; i++){
			for(int j=0; j<l[i]; j++){
				printf("%d %d\t", k[i][j][0], k[i][j][1]);
			}
			printf("\n");
		}
*/
		for(int i=0; i<l[0]; i++)
			dp[0][i][0] = 0;

		for(int i=0; i<l[1]; i++)
			if(m > 0)
				dp[1][i][1] = 0;

		for(int i=0; i<l[1]; i++){
			for(int j=0; j<l[0]; j++){
				dp[1][i][0] = min(dp[1][i][0], z + (k[1][i][1] + k[0][j][1]) * abs(k[1][i][0] - k[0][j][0]));
			}
		}

		for(int i=2; i<n; i++){ //gyou
			for(int i1=0; i1<l[i]; i1++){ //nowishi
				for(int i2=0; i2<l[i-1]; i2++){ //previshi, ichitobi0
					dp[i][i1][0] = min(dp[i][i1][0], dp[i-1][i2][0] + (k[i-1][i2][1] + k[i][i1][1]) * abs(k[i][i1][0] - k[i-1][i2][0]));
				}
				for(int i2=0; i2<l[i-2]; i2++){ //previshi, ichitobiari
					for(int i3=1; i3<m+1; i3++){ //ichitobi
						dp[i][i1][i3] = min(dp[i][i1][i3], dp[i-2][i2][i3-1] + (k[i-2][i2][1] + k[i][i1][1]) * abs(k[i-2][i2][0] - k[i][i1][0]));
					}
				}
				for(int i2=0; i2<l[i-1]; i2++){
					for(int i3=1; i3<m+1; i3++){
						dp[i][i1][i3] = min(dp[i][i1][i3], dp[i-1][i2][i3] + (k[i-1][i2][1] + k[i][i1][1]) * abs(k[i-1][i2][0] - k[i][i1][0]));
					}
				}
			}
		}

		for(int i=0; i<l[n-1]; i++){
			for(int j=0; j<m+1; j++){
				ans = min(ans, dp[n-1][i][j]);
//				if(ans == 12) printf("%d %d\n", i, j);
			}
		}
		for(int i=0; i<l[n-2]; i++){
			for(int j=0; j<m; j++){
				ans = min(ans, dp[n-2][i][j]);
			}
		}

		printf("%lld\n", ans);
	}
}

Measured memory_bytes: 3532
#include<bits/stdc++.h>
typedef long long ll;
#define REP(i,n) for(int i = 0;i < (n);i++)
using namespace std;
const int INF = 1e9;

string s;
int main(){
  cin>>s;
  for(int i=0;i<s.size()-1;i++){
    if(s[i]=='A' && s[i+1]=='C'){cout<<"Yes"<<endl;return 0;}
  }
  cout<<"No"<<endl;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int inf = 1000000000; //10e9
 
int main() {
    
    int N;
    cin >> N;
    vector<int> X(N);
    for(int i=0; i < N; i++){
        cin >> X.at(i);
    }

    int L, R = 0;
    for(int i = 0; i < N; i++){
        L = min(L, X[i]);
        R = max(R, X[i]);
    }

    int ans = inf;
    for(int i = L; i <= R; i++){
        int cost = 0;
        for(int j = 0; j < N; j++){
            cost += (X[j] - i)*(X[j] - i);
        }
        ans = min(ans, cost);
    }
    
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3592