akhauriyash/Code-Regression huggingface dataset:
n, a, b, c = [int(i) for i in input().split()]
p = 10 ** 9 + 7

ans = 0

def fact(n, p=10**9 + 7):
    f = [1]
    for i in range(1, n+1):
        f.append(f[-1]*i%p)
    return f

def invfact(n, f, p=10**9 + 7):
    inv = [pow(f[n], p-2, p)]
    for i in range(n, 0, -1):
        inv.append(inv[-1]*i%p)
    return inv[::-1]

f = fact(2 * n)
invf = invfact(2 * n, f)

def comb(a, b):
    if a < b:
        return 0
    if a < 0 or b < 0:
        return 0
    return f[a] * invf[b] * invf[a-b] % p

pow_a = [1]
pow_b = [1]
inv_100 = pow(100, p - 2, p)
a = a * inv_100 % p
b = b * inv_100 % p
c = c * inv_100 % p

inv_ab = pow(a + b, p - 2, p)
a = a * inv_ab % p
b = b * inv_ab % p

for i in range(n):
    pow_a.append(pow_a[-1] * a % p)
    pow_b.append(pow_b[-1] * b % p)

for m in range(n, 2 * n):
    ans += comb(m - 1, n - 1) * (pow_a[n] * pow_b[m - n] + pow_a[m - n] * pow_b[n]) * m
    ans %= p

ans *= pow(1 - c, p - 2, p)

print(ans % p)

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

using namespace std;
typedef long long ll;
//typedef unsigned long long ll;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define sz(x) ll(x.size())
//typedef pair<ll, int> P;
//typedef pair<ll, ll> P;
//const double INF = 1e10;
const ll INF = LONG_LONG_MAX / 100;
//const ll INF = 1e15;
const ll MINF = LONG_LONG_MIN;
//const int INF = INT_MAX / 10;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)

bool contain(set<char> &s, int a) { return s.find(a) != s.end(); }


//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;
const int mod = 1000000007;

bool isPrime(int i) {
    for (int j = 2; j * j <= i; j++) {
        if (i % j == 0) {
            return false;
        }
    }
    return true;
}

int main() {
    int x;
    cin >> x;
    for (int k = x;; k++) {
        if (isPrime(k)) {
            cout << k << endl;
            return 0;
        }
    }
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N=1e5+5;

int n, m;
int indeg[N], idx[N];
vector<int> topo; //Stores lexicographically smallest toposort
vector<int> g[N], rg[N];

bool toposort() //Returns 1 if there exists a toposort, 0 if there is a cycle
{	
	priority_queue<int, vector<int>, greater<int> > pq;
	for(int i=1;i<=n;i++)
		for(auto &it:g[i])
			indeg[it]++;
	for(int i=1;i<=n;i++)
	{
		if(!indeg[i])
			pq.push(i);
	}
	while(!pq.empty())
	{
		int u=pq.top();
		pq.pop();
		topo.push_back(u);
		for(auto &v:g[u])
		{
			indeg[v]--;
			if(!indeg[v])
				pq.push(v);
		}
	}
	if(topo.size()<n)
		return 0;
	int cur=0;
	for(auto &it:topo)
		idx[it]=++cur;
	return 1;
}
int32_t main()
{
	IOS;
	cin>>n>>m;
	for(int i=1;i<=n-1+m;i++)
	{
		int u, v;
		cin>>u>>v;
		g[u].push_back(v);
		rg[v].push_back(u);
	}
	toposort();
	for(int i=1;i<=n;i++)
	{
		int par=0, cur=0;
		for(auto &it:rg[i])
		{
			if(idx[it]>cur)
			{
				cur=idx[it];
				par=it;
			}
		}
		cout<<par<<endl;
	}
	return 0;
}


Measured memory_bytes: 14200
#include <bits/stdc++.h>
#define ll long long
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
using namespace std;
const int tINF = (int)INT_MAX;
const long long lINF = (long long)LLONG_MAX;
vector <int> used;
vector <vector <int>> gr;
vector <set<int>> vr;
map<int,int> af;
int dfs(int v){
    used[v] = 1;
    vr.back().insert(v);
    af[v] = vr.size() - 1;
    int res = 1;
    for(auto u : gr[v]){
        if(!used[u]){
            res += dfs(u);
        }
    }
    return res;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	#ifdef DEBUG
			assert(freopen("inp", "r", stdin));
			assert(freopen("out", "w",stdout));
	#else
			#define cerr if(false)cerr
	#endif
    int n, m, k;
    cin >> n >> m >> k;
    gr.resize(n); 
    used.resize(n);
    for(int i = 0; i < m; ++i){
        int a, b;
        cin >> a >> b;
        --a,--b;
        gr[a].push_back(b);
        gr[b].push_back(a);
    }
    int cnt = 0;
    vector <int> res(n);
    vector <int> sz(n);
    for(int i = 0; i < n; ++i){
        if(!used[i]){
            vr.push_back(set<int>());
            cnt = dfs(i);
            sz[vr.size() - 1] = cnt;
        }
    }
    for(int i = 0; i < k; ++i){
        int a, b;
        cin >> a >> b;
        --a,--b;
        int cm = af[a];
        if(vr[cm].find(b) != vr[cm].end()){
            --res[a];
            --res[b];
        }
    }
    for(int i = 0; i < n; ++i){
 //       cout << cc[i] << " " <<gr[i].size() << '\n';
  //      cout << cc[i] - gr[i].size() << '\n';
        int cm = af[i];
        res[i] += sz[cm] - 1;
        res[i] -= gr[i].size();
        cout << res[i] << " ";
    }
    cout << '\n';
   cerr << "Time execute: " << (double)clock() / (double)CLOCKS_PER_SEC 
    << " sec" << endl;
}

Measured memory_bytes: 19884
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fr first
#define sc second
#define pb push_back
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int> >
const int INF = 1e9+7;
const int MAXN = 1e5 + 200;
const  long double eps = 1e-16;
const long double pi = acos(-1.0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


void solve(){
    int n, m;
    cin >> n >> m;
    if (n % m ){
        cout << n / m + 1 << endl;
        return;
     }
     cout << n / m << endl;

}



int main() {
ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
cout.setf(ios::fixed), cout.precision(7);
 //freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
 int step;
 step = 1;
  for (int i = 1; i <= step; i++){
         solve();
    }
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<numeric>
#define ll long long

using namespace std;

int main() {
  int N;
  string S;
  vector<int>R,G,B;
  cin >> N >> S;
  ll ans=0;
  for(int i=0;i<N;i++){
    if(S.at(i)=='R') R.push_back(i);
    else if(S.at(i)=='G') G.push_back(i);
    else B.push_back(i);
  }

  int r = R.size(), g = G.size(), b = B.size();
  for(int i=0;i<r;i++){
    for(int j=0;j<g;j++){
      ans += b;
      int minn = min(R.at(i),G.at(j));
      int maxx = max(R.at(i),G.at(j));
      int d = abs(R.at(i) - G.at(j));

      if(minn - d >= 0 && S.at(minn - d) == 'B') --ans;
      if(maxx + d < N && S.at(maxx + d) == 'B') --ans;
      if((R.at(i) + G.at(j)) % 2 == 0 && S.at(minn + d / 2) == 'B') --ans;
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 3212
#include<iostream>
#include <algorithm>
using namespace std;
int main(){
int x;
cin>>x;
int arr[x],arr2[x];
for(int i=0;i<x;i++)
{
    cin>>arr[i];
    arr2[i]=arr[i];
}
int sam=0;
sort(arr2, arr2 + x);
for(int i=0;i<x;i++)
{
    if(arr[i]!=arr2[i])
        sam++;



}
if(sam>2)
    cout<<"NO";
else cout<<"YES";
return 0;}


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

int main(){
 string s;
 cin >> s;
 if(s[0] == s[1] && s[1] == s[2])cout << "Yes" << endl;
 else if(s[1] == s[2] && s[2] == s[3])cout << "Yes" << endl;
 else cout << "No" << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int k, n;
    cin >> k >>  n;
   int a[n];
 
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    a[n]=a[0]+k;
    int tmp = 0;
    for (int i = 0; i < n; i++){
        tmp = max(tmp, a[i + 1] - a[i]);
    }
    cout <<k -tmp << endl;
 
    return 0;
}


Measured memory_bytes: 1024
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {

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

        String c = sc.next();

        List<String> alphabet = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");

        int index = 0;
        for (int i = 0; i < alphabet.size(); ++i) {
            if (alphabet.get(i).equals(c)) {
                index = i + 1;
                break;
            }
        }

        System.out.println(alphabet.get(index));
    }

}

Measured memory_bytes: 21460
def main():
    x = int(input())
    ans = x // 11
    x -= ans * 11
    ans *= 2
    if x <= 0:
        print(ans)
    elif x <= 6:
        print(ans + 1)
    else:
        print(ans + 2)


if __name__ == '__main__':
    main()


Measured memory_bytes: 38384
N = int(input())
name = []
S = ''
num = 26
for i in range(20):
    amari = int(N % num)
    if amari == 0:
        amari = 122
        N=int(N/num)-1
    else:
        amari += 96
        N = int(N / num)
    amari = chr(amari)    
    name.insert(0, amari)
    if N / num == 0:
        break

for l in name:
    S = S + l
print(str(S))

Measured memory_bytes: 61756
#include <bits/stdc++.h>
#define int int64_t
using namespace std;
typedef pair<int, int> P;
const int INF = 100000000;

// グローバル変数


// 関数：


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int r, s;
	cin >> r;
	s = 3 * r * r;
	cout << s << endl;
	
}


Measured memory_bytes: 256
import Control.Applicative

solve :: Int -> Int -> Int -> Int -> Int
solve row col h w = row * col - ((col * h) + (row - h) * w)

main :: IO ()
main = do
  [row, col] <- map read . words <$> getLine
  [h, w] <- map read . words <$> getLine
  putStrLn $ show (solve row col h w)


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

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

    for (int i = 1; i < s.size(); i++) {
        if (s[i-1] == s[i]) {
            cout << "Bad" << endl;
            return 0;
        }
    }

    cout << "Good" << endl;

    return 0;
}


Measured memory_bytes: 256
#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;
#define Mod 1000000007
using VP = vector<P>; using VVP = vector<VP>;
using VI = vector<ll>; using VVI = vector<VI>; using VVVI = vector<VVI>;
#define INF 1ll<<59

ll gcd(ll a,ll b){
  if(a<b) swap(a,b);
  if(a%b==0) return b;
  else return gcd(b,a%b);

}

int main(){
  ll x,y,z,k;
  ll i,j;
  cin>>x>>y>>z>>k;
  ll a[x],b[y],c[z];
  for(i=0;i<x;i++) cin>>a[i];
  for(i=0;i<y;i++) cin>>b[i];
  for(i=0;i<z;i++) cin>>c[i];
  sort(a,a+x,greater<ll>());
  sort(b,b+y,greater<ll>());
  sort(c,c+z,greater<ll>());

  vector<ll> v;

  for(i=0;i<x;i++){
    for(j=0;j<y;j++){
      for(ll m=0;m<z;m++){
        if((i+1)*(j+1)*(m+1)>k) break;
        else v.pb(a[i]+b[j]+c[m]);
      }
    }
  }
  sort(v.begin(),v.end(),greater<ll>());
  for(i=0;i<k;i++) cout<<v[i]<<endl;


  return 0;
}


Measured memory_bytes: 1400
#include <iostream>
#include <algorithm>

#define SIZE 200000

using namespace std;

int main() {
    long long int A,B,i,j,k,N,M,K,Q,X;
    long long int x[6];
  //  long long int A[100][100];

 for (i=1;i<=5;i++)
   cin >> x[i];

 for (i=1;i<=5;i++)
    if (x[i]==0)
      cout << i << endl;


  return 0;

}


Measured memory_bytes: 3580
#include<bits/stdc++.h>
using namespace std;
#define pb          push_back
#define vi          vector<int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define endl        "\n"
#define fast        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define int         long long int
#define mod         1000000007
#define test        int t;cin>>t;while(t--)
#define REP(i,a,b)  for(int i=a,i<=b;i++)
#define MAXN        200001

int32_t main()
{
    int n;cin>>n;
    int arr[n];

    map<int,int>mp;
    for(int i=0;i<n;i++){
        cin>>arr[i];
        mp[arr[i]]++;
    }

    bool ok=false;
    int low,last,cnt=0;
    for(auto x:mp){
        
        if(ok){
            last=x.F;
            break;
        }
        cnt+=x.S;
        if(cnt==(n)/2){
            low=x.F;
            ok=true;
        }
    }
    
    if(!ok)
        cout<<0<<endl;
    else
        cout<<last-low<<endl;

    return 0;
}

Measured memory_bytes: 8340
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

N, M = map(int, input().split())

A = [0] * (N + 1)
for i in range(M):
    l, r = map(int, input().split())
    l -= 1
    A[l] += 1
    A[r] -= 1
for i in range(1, N):
    A[i] += A[i - 1]
ans = 0
for a in A:
    if a == M:
        ans += 1
print(ans)



Measured memory_bytes: 13204
#include <cstdio>

using namespace std;

int prime(int n){
	for(int i=2;i<=n/2;i++){
		if((n%i)==0)return 0;
	}
	return 1;
}
int main()
{
	int n;
	while(~scanf("%d\n",&n)){
		for(int i=n-1;0<=i;i--){
			if(prime(i)==1){
				printf("%d ",i);
				break;
			}
		}
		for(int i=n+1; ;i++){
			if(prime(i)==1){
				printf("%d\n",i);
				break;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 0
#include <stdio.h>
int main(void){
    // Your code here!
    int N, T;
    int i;
    int min_cost;
    scanf("%d %d", &N, &T);
    int c[N], t[N];
    for(i = 0; i < N; i++){
        scanf("%d %d", &c[i], &t[i]);
    }
    min_cost = 1001;
    for(i = 0; i < N; i++){
        if(t[i]<=T){
            if(min_cost > c[i])
                min_cost = c[i];
        }
    }
    if(min_cost == 1001)
        printf("TLE");
    else
        printf("%d", min_cost);
    

    return 0;
}


Measured memory_bytes: 128
import java.util.Scanner;
import java.io.IOException;

class Main{
  public static void main(String[] args)throws IOException{
    Scanner scan = new Scanner(System.in);
    int N = scan.nextInt();
    int[] rabbit = new int[N];
    for(int i=0;i<N;i++){
      rabbit[i] = scan.nextInt();
    }
    int count=0;
    for(int i=0;i<rabbit.length;i++){
        if(rabbit[rabbit[i]-1]-1 ==i) count++;
    }
    System.out.println(count/2);
  }
}


Measured memory_bytes: 50476
I = lambda : list(map(int,input().split()))
n,k,m = I();p=0;l=k
s=input();x=[];y=[]
for i in range(n):
    if s[i]=='o' and p==0:
        k-=1;x.append(i+1)
        if k==0:
            break
        p=m
    else:
        p-=1;p=max(0,p)
p=0;k=l
for i in range(n-1,-1,-1):
    if s[i]=='o' and p==0:
        k-=1;y.append(i+1)
        if k==0:
            break
        p=m
    else:
        p-=1;p=max(0,p)
y=y[::-1]
for i in range(len(x)):
    if x[i]==y[i]:
        print(x[i])

Measured memory_bytes: 20836
#include <iostream>
#include <string>
#include <stdio.h>

void change(std::string a,std::string b,bool cup[]){
	int ir[2];
	if(a == "A"){
		ir[0]=0;
	}
	else if(a == "B"){
		ir[0]=1;
	}
	else{
		ir[0]=2;
	}
	if(b == "A"){
		ir[1]=0;
	}
	else if(b == "B"){
		ir[1]=1;
	}
	else{
		ir[1]=2;
	}
	bool ex = cup[ ir[0] ];
	cup[ ir[0] ] = cup[ ir[1] ];
	cup[ ir[1] ] = ex;
}

int main(){
	using namespace std;
	bool cup[3];
	cup[0] = true;
	cup[1] = false;
	cup[2] = false;
	std::string pl[3];
	char mj[32];
	while( scanf("%s",&mj)!=EOF ){
		pl[0] = mj;
		pl[1] = pl[0].substr(0,1);
		pl[2] = pl[0].substr(2,1);
		change(pl[1],pl[2],cup);
	}
	if(cup[0]){
		cout << "A" << endl;
	}
	else if(cup[1]){
		cout << "B" << endl;
	}
	else{
		cout << "C" << endl;
	}
	return 0;
}

Measured memory_bytes: 1200
#include <bits/stdc++.h>
#define itn int
#define REP(i, n) for (ll i = 0; i < n; i++)
#define IREP(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i,a, b) for (ll i = a; i < b + 1; i++)
#define all(v) v.begin(), v.end()
#define SENTINEL 2000000000
#define NIL -1
using namespace std;
typedef long long ll;

const ll INF = 1LL << 60;
const ll MOD = 1000000007;
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;}


int main()
{
    int r;
    cin >> r;
    double a = 6.28318530717958623200;
    cout << fixed << setprecision(10);
    cout << r*a << endl;
}


Measured memory_bytes: 3784
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;
typedef long long ll ;

vector <ll> a[100010] ;
int colored[100010] ;
queue <ll> q1 ;
queue <ll> q2 ;

vector <ll> vec1 ;
vector <ll> vec2 ;

int main()
{
    ll n ;
    scanf("%lld",&n) ;
    for (int i=0 ; i<n-1 ; i++)
    {
        ll aa,bb ;
        scanf("%lld %lld",&aa,&bb) ;
        a[aa].push_back(bb) ;
        a[bb].push_back(aa) ;
    }
    colored[1]=1 ;
    colored[n]=2 ;
    ll nodes=2 ;
    q1.push(1) ;
    q2.push(n) ;
    while (nodes<n)
    {
        while (!q1.empty())
        {
            ll n1=q1.front() ;
            vec1.push_back(n1) ;
            q1.pop() ;
        }
        for (int j=0 ; j<vec1.size() ; j++)
        {
            ll raso1=vec1[j] ;
            for (int i=0 ; i<a[raso1].size() ; i++)
            {
                if (colored[a[raso1][i]]==0)
                {
                    q1.push(a[raso1][i]) ;
                    colored[a[raso1][i]]=1 ;
                    nodes++ ;
                }
            }
        }
        vec1.clear() ;
        while (!q2.empty())
        {
            ll n2=q2.front() ;
            vec2.push_back(n2) ;
            q2.pop() ;
        }
        for (int j=0 ; j<vec2.size() ;j++)
        {
            ll raso2=vec2[j] ;
            for (int i=0 ; i<a[raso2].size() ; i++)
            {
                if (colored[a[raso2][i]]==0)
                {
                    q2.push(a[raso2][i]) ;
                    colored[a[raso2][i]]=2 ;
                    nodes++ ;
                }
            }
        }
        vec2.clear() ;
    }
    ll black=0,white=0 ;
    for (int i=1 ; i<=n ; i++)
    {
        if (colored[i]==1)
        {
            black++ ;
        }
        else
        {
            white++ ;
        }
    }
    if (black>white)
    {
        puts("Fennec") ;
    }
    else
    {
        puts("Snuke") ;
    }
    return 0;
}


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

typedef long long unsigned int llu;
typedef long long int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
  int N;
  cin >> N;
  int D1, D2;
  int flag = 0;
  for (int i=0; i<N; i++){
    cin >> D1 >> D2;
    if (D1 == D2){
      flag++;
    }else{
      flag = 0;
    }
    if (flag == 3){
      cout << "Yes" << endl;
      break;
    }
    if (i == N-1){
      cout << "No" << endl;
    }
  }
}


Measured memory_bytes: 3644
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll,ll> P;
typedef vector<P> vp;
typedef vector<string> vs;
typedef vector<bool> vb;

const ll mod=1e9+7;
const ll inf=1e16;

#define rep(i,m,n) for(ll i=m;i<n;i++)
#define repr(i,m,n) for(ll i=m-1;i>=n;i--)
#define fi first
#define se second
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define eb(x) emplace_back(x)
#define all(x) x.begin(),x.end()
#define sum(x) accumulate(all(x),0)
#define gll greater<ll>()


void solve(){
 ll d;
  cin >> d;
 vll c(26);
  rep(i,0,26) cin >> c[i];
 vvll s(d,vll(26));
  rep(i,0,d){
    rep(j,0,26) cin >> s[i][j]; 
    cout << max_element(all(s[i]))-s[i].begin()+1 << endl;
  }
  
  
  
}

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

Measured memory_bytes: 3756
//raja1999
#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <iomanip> 
//setbase - cout << setbase (16)a; cout << 100 << endl; Prints 64
//setfill -   cout << setfill ('x') << setw (5); cout << 77 <<endl;prints xxx77
//setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//cout.precision(x)  cout<<fixed<<val;  // prints x digits after decimal in val

using namespace std;
#define f(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) f(i,0,n)
#define fd(i,a,b) for(i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define vi vector< int >
#define vl vector< ll >
#define ss second
#define ff first
#define ll long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
#define inf (1000*1000*1000+5)
#define all(a) a.begin(),a.end()
#define tri pair<int,pii>
#define vii vector<pii>
#define vll vector<pll>
#define viii vector<tri>
#define mod (1000*1000*1000+7)
#define pqueue priority_queue< int >
#define pdqueue priority_queue< int,vi ,greater< int > >

//std::ios::sync_with_stdio(false);
vector<vl>vec(10);
ll x[100005],y[100005], z[100005],ans,res=0;
int main(){
	std::ios::sync_with_stdio(false);
	ll n,i,j,m,m1;
	cin>>n>>m;
	rep(i,n){
		cin>>x[i]>>y[i]>>z[i];
		vec[1].pb(x[i]+y[i]+z[i]);
		vec[2].pb(x[i]-y[i]+z[i]);
		vec[3].pb(x[i]+y[i]-z[i]);
		vec[4].pb(x[i]-y[i]-z[i]);
		vec[5].pb(-1LL*x[i]+y[i]-z[i]);
		vec[6].pb(-1LL*x[i]-y[i]+z[i]);
		vec[7].pb(-1LL*x[i]+y[i]+z[i]);
		vec[8].pb(-1LL*x[i]-y[i]-z[i]);

	}	
	rep(i,9){
		sort(all(vec[i]));
	}
	f(i,1,9){
		ans=0;
		m1=m;
		j=vec[i].size()-1;
		while(m1--){
			ans+=vec[i][j];
			j--;

		}
		//cout<<ans<<endl;
		res=max(res,ans);
	}
	cout<<res<<endl;
} 
	

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

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define show(a, n) rep(i,n) {cout<<a[i]<<' ';} cout<<endl;

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<long long, long long> pll;

const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100000;

int main() {
	ll n,k;cin>>n>>k;
	ll a[N];
	rep(i,n) cin>>a[i];
	ll c[50];
	rep(i,50) c[i]=0;
	int imax=-1;
	rep(i,n) rep(j,50) if((1LL<<j)&a[i]) c[j]++;
	rep(i,50) if(c[i]>0) imax=i;
	rep(i,50) if((1LL<<i)&k) imax=max(imax,i);

	ll ans=0;
	for(int i=imax;i>=0;i--){
		if(((1LL<<i)&k)==0) continue;
		ll tans=0;
		for(int j=imax;j>=0;j--){
			if(j>i){
				if((1LL<<j)&k) tans+=(1LL<<j)*(n-c[j]);
				else tans+=(1LL<<j)*c[j];
			}
			else if(j==i) tans+=(1LL<<j)*c[j];
			else tans+=(1LL<<j)*max(c[j],n-c[j]);
		}
		ans=max(tans,ans);
	}
	ll tans=0;
	for(int i=imax;i>=0;i--){
		if((1LL<<i)&k) tans+=(1LL<<i)*(n-c[i]);
		else tans+=(1LL<<i)*c[i];
	}
	ans=max(tans,ans);
	cout<<ans<<endl;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n ; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;

// 最大公約数
int gcd(int a, int b){
    int c;
    if (a < b) {
        a+=b; b=a-b; a-=b;
    }
    while (b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return a;
}
// 最小公倍数
int lcm( int m, int n ){
    // 引数に０がある場合は０を返す
    if ( ( 0 == m ) || ( 0 == n ) ) return 0;
    return ((m / gcd(m, n)) * n); // lcm = m * n / gcd(m,n)
}
int main(void){
    int n; cin >> n;
    int ans = lcm(n, 2);
    cout << ans << endl;
}   

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
  while(true){
    int n;
    int num = 0;
    int ans[52];
    cin >> n;
    if(n<0)break;
    while(n>0){
      int p = n & 3;
      n >>= 2;
      //cout << p << endl;
      ans[num++]=p;
    }
    for(int i = num-1; i >= 0; --i){
      putchar('0'+ans[i]);
    }
    if(num==0)putchar('0');
    putchar('\n');
  }
  return 0;
}

Measured memory_bytes: 0
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

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

using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;

	vector<int> dp(n + 1, 0);
	vector<bool> is_ok(n + 1, true);
	int res = 0;
	ll mod = 1e9 + 7;
	rep (i, m) {
		int tmp;
		cin >> tmp;
		is_ok[tmp] = false;
		if (tmp + 1 != n && is_ok[tmp + 1] == false) {
			cout << res << endl;
			return 0;
		}
	}

	dp[0] = 1;
	rep (i, n) {
		for (int j = 1; j <= 2; j++) {
			if (i + j < is_ok.size() && is_ok[i + j]) {
				dp[i + j] += dp[i];
				dp[i + j] %= mod;
			}
		}
	}

	cout << dp[n] << endl;

	return 0;
}


Measured memory_bytes: 640
n,s = map(int,input().split())
a = list(map(int,input().split()))
mod = 998244353

dp = [[0] * (s+1) for _ in range(n+1)]

dp[0][0] = 1
for i in range(1,n+1):
    for j in range(0,s+1):
        dp[i][j] += dp[i-1][j] * 2
        if(j>=a[i-1]):
            dp[i][j] += dp[i-1][j-a[i-1]]
        dp[i][j] %= mod

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

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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '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;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  int A, B; cin >> A >> B;
  cout << 6 - A - B << endl;
}


Measured memory_bytes: 3624
#include<stdio.h>

int main()
{
    int n;
    char buf[16];
    long long cnt[] = {0, 0, 0, 0, 0};
    long long ans;
    int i, j, k;

    scanf("%d\n", &n);
    for(i = 0; i < n; i++){
        fgets(buf, sizeof(buf), stdin);
        switch(buf[0]){
            case 'M':
                cnt[0]++;
                break;
            case 'A':
                cnt[1]++;
                break;
            case 'R':
                cnt[2]++;
                break;
            case 'C':
                cnt[3]++;
                break;
            case 'H':
                cnt[4]++;
                break;
        }
    }

    ans = 0;
    for(i = 0; i < 5; i++) for(j = i+1; j < 5; j++) for(k = j+1; k < 5; k++)
        ans += cnt[i] * cnt[j] * cnt[k];

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

    return 0;
}


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

typedef long long ll;

int main() {
    int N;
    cin >> N;
    int P=0,Q=0;
    for (int i=0;i<N;i++){
        int c;
        cin >> c;
        P = P*10 + c;
    }
    for (int i=0;i<N;i++){
        int c;
        cin >> c;
        Q = Q*10 + c;
    }
    vector<int> p(N+1);
    p[0] = 1;
    for (int i=0;i<N;i++){
        p[i+1] = p[i]*(i+1);
    }

    vector<int> all;
    vector<int> num(N);
    for (int i=0;i<N;i++){
        num[i] = i+1;
    }
    do {
        int tmp = 0;
        for (int i=0;i<N;i++){
            tmp = tmp*10 + num[i];
        }
        all.push_back(tmp);
    }while (next_permutation(num.begin(),num.end()));
    sort(all.begin(),all.end());

    int op = -1, oq = -1;
    for (int i=0;i<all.size();i++){
        if (all[i] == P) op = i;
        if (all[i] == Q) oq = i;
        if (op != -1 && oq != -1) break;
    }
    cout << abs(op - oq) << endl;

    return 0;
}

Measured memory_bytes: 640
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

int n,k;
string s;
string ans;
string one(string str){
	string str2 = str;
	reverse(str2.begin(),str2.end());
	string ans = min(str,str2);
	str += str2;
	for(int i=1;i<str2.size();i++){
		string S; S.resize(str2.size());
		rep(j,str2.size()) S[j] = str[i+j];
		ans = min(ans,S);
	}
	return ans;
}
string t;
int main(){
	cin >> n >> k >> s;
	ans = one(s);
	if(k == 1){
		cout << ans << endl;
		return 0;
	}
	string ss = s; reverse(ss.begin(),ss.end());
	t = s+ss;
	char ch = 'z';
	rep(i,n) ch = min(ch,s[i]);
	if(k >= 15){
		string ret = "";
		rep(i,n) ret.pb(ch);
		cout << ret << endl;
		return 0;
	}
	vector<P>vec;
	int cur = 0;
	rep(i,n){
		if(s[i] == ch) cur++;
		else {
			if(cur){
				int a = i-cur, b = i-1;
				if(cur * (1<<(k-1)) >= n){
					string ret = "";
					rep(i,n) ret.pb(ch);
					cout << ret << endl;
					return 0;
				}
				string mk ; mk.resize(n);
				rep(i,cur*(1<<(k-1))) mk[i] = ch;
				int pt = 2*n-2-b;
				int nxt = cur*(1<<(k-1));
				while(nxt < n) {
					mk[nxt++] = t[pt--];
				}
				ans = min(ans,mk);
				cur = 0;
			}
		}
	}
	if(cur){
			int a = n-cur, b = n-1;
			if(cur * (1<<k) >= n){
				string ret = "";
				rep(i,n) ret.pb(ch);
				cout << ret << endl;
				return 0;
			}
			string mk ; mk.resize(n);
			rep(i,cur*(1<<k)) mk[i] = ch;
			int pt = n-cur-1;
			int nxt = cur*(1<<k);
			while(nxt < n) {
				mk[nxt++] = t[pt--];
			}
			ans = min(ans,mk);
			cur = 0;
	}
	cout << ans << endl;
}
	

Measured memory_bytes: 256
n,m=map(int,input().split())
*a,=map(int,input().split())

cost={1:2,2:5,3:5,4:4,5:5,6:6,7:3,8:7,9:6}
dp=[-1]*(n+1)
v=[0]*(n+1)

dp[0]=0
for i in range(1,n+1):
    for j in range(10)[::-1]:
        if not j in a or i<cost[j]:continue
        if dp[i]<dp[i-cost[j]]+1:
            dp[i]=dp[i-cost[j]]+1
            v[i]=j

p=n
while p!=0:
    print(v[p],end="")
    p-=cost[v[p]]
print()

Measured memory_bytes: 4316
from math import ceil
import sys
sys.setrecursionlimit(10**7)
t=int(input())
for _ in range(t):
    memo=dict()
    n,a,b,c,d=map(int,input().split())
    def dfs(x):
        if x==0:
            return 0
        if x==1:
            return d
        if x in memo:
            return memo[x]
        l2=2*(x//2)
        r2=2*((x+1)//2)
        l3=3*(x//3)
        r3=3*((x+2)//3)
        l5=5*(x//5)
        r5=5*((x+4)//5)
        mini=d*x
        mini=min(mini,abs(l2-x)*d+a+dfs(l2//2))
        mini=min(mini,abs(r2-x)*d+a+dfs(r2//2))
        mini=min(mini,abs(l3-x)*d+b+dfs(l3//3))
        mini=min(mini,abs(r3-x)*d+b+dfs(r3//3))
        mini=min(mini,abs(l5-x)*d+c+dfs(l5//5))
        mini=min(mini,abs(r5-x)*d+c+dfs(r5//5))
        memo[x]=mini
        return mini
    print(dfs(n))

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

int main() {
  string A;
  cin >> A;
  if (regex_search(A, std::regex("[A-Z]"))){
    cout << "A" << endl;
  } else {
    cout << "a" << endl;
  }
    return 0;
}

Measured memory_bytes: 3772
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <limits>
#include <numeric>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;

int main()
{
	int n, t,tempc,tempt,min=10000;
	cin >> n >> t;
	for (int i = 0; i < n; i++){
		cin >> tempc >> tempt;
		if (tempc < min && tempt <= t) {
			min = tempc;
		}
	}

	if (min == 10000) {
		cout << "TLE" << endl;
	}
	else {
		cout << min << endl;
	}
	return 0;

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
#define int long long
#define ull unsigned long long
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define print0(a) cout << (a) << ' '
#define print1(a) cout << (a) << '\n'
#define print2(a,b) cout << (a) << ' ',print1(b)
#define print3(a,b,c) cout << (a) << ' ',print2(b,c)
#define print4(a,b,c,d) cout << (a) << ' ',print3(b,c,d)
#ifdef i_am_noob
#define debug(a) print2(#a,a)
#else
#define debug(a) 
#endif
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
 
using namespace std;
using namespace __gnu_pbds;
 
const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod2;
 
inline int add(int a, int b){return (a+b>=MOD?a+b-MOD:a+b);}
inline int sub(int a, int b){return (a+MOD-b>=MOD?a-b:a+MOD-b);}
int Pow(int a, int b){
	if(!b) return 1;
	int tmp=Pow(a*a%MOD,b>>1);
	return (b&1?tmp*a%MOD:tmp);
}
inline int inverse(int n){return Pow(n,MOD-2);}

const int maxn=200005;
//i_am_noob
int k,n,a[maxn],maxx=-1;

signed main(){
	ios_base::sync_with_stdio(0),cin.tie(0);
	cin >> k >> n;
	rep(n) cin >> a[i];
	rep(n-1) maxx=max(maxx,a[i+1]-a[i]);
	maxx=max(maxx,k+a[0]-a[n-1]);
	print1(k-maxx);
	return 0;
}

Measured memory_bytes: 1792
#include <stdio.h>
 
int cnt;
 
void insertionSort(int A[], int n, int g);
void shellSort(int A[], int n);
 
int n;
int A[1000000];
 
int main() {
  int i;
  scanf("%d", &n);
  for(i = 0; i < n; ++i) scanf("%d", A+i);
  shellSort(A, n);
  printf("%d\n", cnt);
  for(i = 0; i < n; ++i) printf("%d\n", A[i]);
  return 0;
}
 
void insertionSort(int A[], int n, int g) {
  int tmp;
  int i, j;
  for(i = g; i < n; ++i) {
    tmp = A[i];
    j = i-g;
    while(j >= 0 && A[j] > tmp) {
      A[j+g] = A[j];
      j -= g;
      ++cnt;
    }
    A[j+g] = tmp;
  }
}
 
void shellSort(int A[], int n) {
  int m = 0;
  int G[100];
  int i;
  G[0] = 1;
  for(m = 1; 3*G[m-1]+1 <= n; ++m) G[m] = 3*G[m-1]+1;
  printf("%d\n", m);
  cnt = 0;
  for(i = m-1; i >= 0; --i) {
    printf("%d%c", G[i], " \n"[i==0]);
    insertionSort(A, n, G[i]);
  }
}



Measured memory_bytes: 5688
import sys


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def build(self, arr):
        #assert len(arr) <= n
        for i, a in enumerate(arr):
            self.add(i+1, a)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

    def range_sum(self, l, r):
        #assert 0 <= l <= r <= self.n
        return self.sum(r) - self.sum(l)


def main():
    n, q = map(int, sys.stdin.buffer.readline().split())
    bit = Bit(n)
    bit.build(map(int, sys.stdin.buffer.readline().split()))
    for x in sys.stdin.buffer.readlines():
        q, p, x = map(int, x.split())
        if q:
            print(bit.range_sum(p, x))
        else:
            bit.add(p+1, x)


if __name__ == "__main__":
    main()


Measured memory_bytes: 68284
#include<stdio.h>

int main(void)
{
	int k,a,b,c,x,y;
	int ca,cb,cc,cx;
	int sum=0;

	scanf("%d",&k);

	for(a=1;a<=k;a++){
		for(b=1;b<=k;b++){
			ca=a;
			cb=b;
			while(x!=0){
				x=ca%cb;
				ca=cb;
				cb=x;
			}
			x=ca;
			for(c=1;c<=k;c++){
				cc=c;
				cx=x;
				while(y!=0){
					y=cc%cx;
					cc=cx;
					cx=y;
				}
				y=cc;
				sum+=y;
			}
		}
	}

	printf("%d\n",sum);

	return 0;
}


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

int main() {

	int m, f, r;

	for (int i = 1;; i++) {
		cin >> m >> f >> r;
		
		if (m == -1 && f == -1 && r == -1) break;
		else if (m == -1 || f == -1) cout << "F\n";
		else if ((m + f) >= 80) cout << "A\n";
		else if ((m + f) >= 65 && (m + f) < 80) cout << "B\n";
		else if ((m + f) >= 50 && (m + f) < 65) cout << "C\n";
		else if ((m + f) >= 30 && (m + f) < 50) {
			if (r < 50) cout << "D\n";
			else cout << "C\n";
		}
		else cout << "F\n";
	}

	return 0;
}

Measured memory_bytes: 1120
import sys
input = sys.stdin.readline
from collections import *

MAX = 2*10**5+100
MOD = 998244353
fact = [0]*MAX #fact[i]: i!
inv = [0]*MAX #inv[i]: iの逆元
finv = [0]*MAX #finv[i]: i!の逆元
fact[0] = 1
fact[1] = 1
finv[0] = 1
finv[1] = 1
inv[1] = 1
    
for i in range(2, MAX):
    fact[i] = fact[i-1]*i%MOD
    inv[i] = MOD-inv[MOD%i]*(MOD//i)%MOD
    finv[i] = finv[i-1]*inv[i]%MOD

def C(n, r):
    if n<r:
        return 0
    if n<0 or r<0:
        return 0
    return fact[n]*(finv[r]*finv[n-r]%MOD)%MOD

N, M, K = map(int, input().split())
ans = 0

for i in range(N-1-K, N+1):
    ans += C(N-1, i)*M*pow(M-1, i, MOD)
    ans %= MOD

print(ans)

Measured memory_bytes: 76024
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;

int main() {
    int N;
    cin >> N;
    vector<int> C(N), S(N), F(N);
    rep(i, N - 1) { cin >> C[i] >> S[i] >> F[i]; }
    vector<int> res(N);
    rep(i, N - 1) {
        int t = 0;
        for(int j = i; j < N - 1; j++) {
            if(t < S[j])
                t = S[j];
            if(t % F[j] != 0) {
                t += F[j] - t % F[j];
            }
            t += C[j];
        }
        res[i] = t;
    }
    rep(i, N) { cout << res[i] << endl; }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <deque>

int main(){
  int N;
  scanf("%d", &N);
  std::string S;
  std::cin >> S;
  std::vector<int > array(N);
  for (int i=0; i<N; i++){
    if (S[i]=='R') array[i]=0;
    if (S[i]=='G') array[i]=1;
    if (S[i]=='B') array[i]=2;
  }
  //std::vector<int > Rcount(S.length()+1), Bcount(S.length()+1), Gcount(S.length()+1);
  std::vector<std::vector<int > > count(3, std::vector<int > (N+1, 0));
  count[0][N]=0;
  count[1][N]=0;
  count[2][N]=0;
  for (int i=N-1; i>=0; i--){
    for (int j=0; j<3; j++){
      if (array[i]==j){
        count[j][i] = count[j][i+1]+1;
      } else {
        count[j][i] = count[j][i+1];
      }
    }
  }
  long long ans = 0;
  for (int i=0; i<N; i++){
    for (int j=i+1; j<N-1; j++){
      if(array[j]==array[i]) continue;
      int amari = 3-array[i]-array[j];
      ans += (long long)count[amari][j+1];
      if (2*j-i<N){
        if (array[2*j-i]==amari) ans--;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}


Measured memory_bytes: 3320
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

const int MAX = 510000;
const int MOD = 1000000007;


// 約数の列挙
vector<ll> divisor(ll n) {
    vector<ll> res;
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            res.push_back(i);
            if (i != n / i) {
                res.push_back(n / i);
            }
        }
    }
    return res;
}

int main(){
  ll N; cin>>N;
  ll ans = 0;
  for(ll i = 1; i <= N; i+=2){
    ll a = divisor(i).size();
    if(a == 6) ans ++;
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 256
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#include<cstring>
#define ll long long
#define sci(a) scanf("%d",&a)
#define scl(a) scanf("%I64d",&a)
#define scs(a) scanf("%s",a)
#define pri(a) printf("%d",a)
#define prl(a) printf("%I64d",a)
#define prc(a) printf("%c",a)
const int N=1e3+5;
const int M=1e5+5;
const ll INF=1e15+7;
const double PI=acos(-1);
using namespace std;
ll a[N],b[N],c[N];
struct Pair{
	ll i,j,k;
	bool operator<(const Pair& B)const {
		return a[i]+b[j]+c[k]<a[B.i]+b[B.j]+c[B.k];
	}
	Pair(ll m=0,ll n=0,ll o=0):i(m),j(n),k(o){}
};
struct Apair{
	ll i,j,k;
	bool operator<(const Apair& B)const {
		if(i==B.i){
			if(j==B.j) return k<B.k;
			return j<B.j;
		}
		return i<B.i;
	}
	Apair(ll _i=0,ll _j=0,ll _k=0):i(_i),j(_j),k(_k){}
};
map<Apair,int>mp;
ll x,y,z,k;
bool cmp(ll m,ll n){
	return m>n;
}
void solve(){
	priority_queue<Pair>que;
	que.push(Pair(0,0,0));
	mp[Apair(0,0,0)]=1;
	int cnt=0;
	while(!que.empty()&&cnt<k){
		Pair temp=que.top();que.pop();
		cout<<a[temp.i]+b[temp.j]+c[temp.k]<<endl;
		++cnt;
		if(temp.i<x-1){
			Apair ttemp(temp.i+1,temp.j,temp.k);
			if(mp[ttemp]==0){
				que.push(Pair(temp.i+1,temp.j,temp.k));
				mp[Apair(temp.i+1,temp.j,temp.k)]=1;
			}
		}
		if(temp.j<y-1){
			Apair ttemp(temp.i,temp.j+1,temp.k);
			if(mp[ttemp]==0){
				que.push(Pair(temp.i,temp.j+1,temp.k));
				mp[Apair(temp.i,temp.j+1,temp.k)]=1;
			}
		}
		if(temp.k<z-1){
			Apair ttemp(temp.i,temp.j,temp.k+1);
			if(mp[ttemp]==0){
				que.push(Pair(temp.i,temp.j,temp.k+1));
				mp[Apair(temp.i,temp.j,temp.k+1)]=1;
			}
		}
	}
}
int main(){
	cin>>x>>y>>z>>k;
	for(ll i=0;i<x;++i) scanf("%lld",&a[i]);
	for(ll i=0;i<y;++i) scanf("%lld",&b[i]);
	for(ll i=0;i<z;++i) scanf("%lld",&c[i]);
	sort(a,a+x,cmp);
	sort(b,b+y,cmp);
	sort(c,c+z,cmp);
	solve();
}

Measured memory_bytes: 896
package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		row := strings.Split(scanner.Text(), " ")
		var sum int
		for _, v := range row {
			num, _ := strconv.Atoi(v)
			sum += num
		}
		fmt.Println(len([]rune(strconv.Itoa(sum))))
	}
}


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

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

using pii = pair<int, int>; using vpii = vector<pii>;
using vi = vector<int>; using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>; using vpll = vector<pll>;
using vll = vector<ll>; using vvll = vector<vll>;

int main() {
    int n; cin >> n;
    vvll cnt(20, vll(10));
    vll sum(20);
    FOR(i, 0, 9)
        cnt[1][i] = 1;
    sum[1] = 9;
    FOR(i, 2, 11) FOR(j, 0, 9) {
        if (j > 0) cnt[i][j] += cnt[i - 1][j - 1];
        cnt[i][j] += cnt[i - 1][j];
        if (j + 1 <= 9) cnt[i][j] += cnt[i - 1][j + 1];
        if (j > 0) sum[i] += cnt[i][j];
    }
    int p = 1;
    while (n > sum[p])
        n -= sum[p++];
    int ls = 2;
    ROF(i, p, 1) {
        int c = max(0, ls - 1);
        // if (i == p) c = 1;
        // else c = 0;
        while (n > cnt[i][c]) {
            n -= cnt[i][c], c++;
        }
        cout << c;
        ls = c;
    }
    cout << endl;
}

Measured memory_bytes: 3620
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <tuple>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
using namespace std;
using ll = long long;


ll const mod = 1e9 + 7;
int n, a;

ll dp[51][51][2570]; // iコメまでからj個選んで和をkにする

int main() {
    int x[55];
    while (cin >> n >> a) {
        int s = 0;
        for (int i = 0; i < n; i++) {
            cin >> x[i];
            s += x[i];
        }

        memset(dp, 0, sizeof dp);
        dp[0][0][0] = 1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                for (int k = 0; k <= s; k++) {
                    dp[i + 1][j + 1][k + x[i]] += dp[i][j][k];
                    dp[i + 1][j][k] += dp[i][j][k];
                }
            }
        }
        ll ans = 0;
        for (int i = 1; i <= n; ++i) {
            ans += dp[n][i][i*a];
        }
        cout << ans << endl;
    }
}

Measured memory_bytes: 52480
#include <iostream>
#include<string>
#include<algorithm>
#include<cmath>

using namespace std;

int main() {
	//code
	   
     long long i,j,k,n,m,p=0;
      cin>>n>>k;
	 cout<<min(n%k,abs((n%k)-k))<<endl;
    
	
	return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#define F first
#define S second
#define ALL(a) a.begin(),a.end()
#define mp make_pair
#define pb push_back
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORN(i,a,b) for(int i=a;i<=b;i++)
#define FORB(i,a,b) for(int i=a;i>=b;i--)
#define INT_MAXI 2147483647
#define INT_MINI -2147483648
#define REV(a) reverse(a.begin(),a.end())
#define NP(a) next_permutation(a.begin(),a.end())

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

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //cout<<"input"<<endl;freopen("input.txt","r",stdin);
    //cout<<"output"<<endl;freopen("output.txt","w",stdout);
    
    int n;
    cin>>n;
    int sum = 0;
    for(int i=1;i<=n-1;i++){
        sum+=(n-1)/i;
    }
    cout<<sum<<endl;
    
    
    
    
    
    return 0;
}

Measured memory_bytes: 3652
#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
#define l(i,a,b) for(auto i=a;i<b;i++)
#define lr(i,a,b) for(auto i=a;i>=b;i--)
#define ll long long
#define lli long long int
#define ld long double
#define vec vector<int>
#define mii map<ll,ll>
#define vl vector<long long>
#define se set <long long int>
#define pi pair<long long,long long>
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define all(v) v.begin(), v.end()
#define in(x) cin>>x
#define o(x) cout<<x
#define SYNC ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265359
using namespace std;
 
ll modulo(ll a, ll b, ll n){
    long long x=1, y=a; 
    while (b > 0) {
        if (b%2 == 1) {
            x = (x*y) % n; 
        }
        y = (y*y) % n;
        b /= 2;
    }
    return x % n;
}
 
int main()
{
    SYNC
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll n;
    in(n);
    vl h;
    ll temp;
    ll dp[n+1];
    l(i,0,n)
    {
        in(temp);
        h.pb(temp);
    }
    dp[0]=0;
    for(ll i=1;i<=n-1;i++)
    {
        dp[i]=dp[i-1]+abs(h[i]-h[i-1]);
        if(i>1)
        dp[i]=min(dp[i],dp[i-2]+abs(h[i]-h[i-2]));
    }
    o(dp[n-1])<<endl;
 
    
 
}


Measured memory_bytes: 1908