akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define in(x,y,h,w) x >= 0 && x < h && y >= 0 && y < w

#define int long long
//typedef    long long          ll;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      P;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
 
const int INF=1e+18;
const double EPS=1e-9;
const int MOD=1000000007;
 
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

signed main(){
	string s,a[5];
	int n,k;
	cin >> s >> k;
	n = s.length();
	for(int i = 0;i < 5;i++) a[i] = "zzzzzzzzzz";
	for(int i = 0;i < n;i++){
		for(int j = 1;j <= 5 && i + j <= n;j++){
			string t = s.substr(i,j);
			for(int l = 0;l < 5;l++){
				if(t == a[l]) break;
				else if(t < a[l]) swap(t,a[l]);
			}
		}
	}
	cout << a[k - 1] << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int n;
  while(cin >> n && n){
    int data[n];
    unsigned long long int sum=0,wait=0;
    for(int i=0;i<n;i++) cin >> data[i];
    sort(data,data+n);
    for(int i=0;i<n;i++){
      sum += wait;
      wait += data[i];
    }
    cout << sum << endl;
  }
}

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

template <typename T>
vector<pair<T, int>> factorize(T x) {
	vector<pair<T, int>> res;
	for (T i = 2; i * i <= x; i++) {
		if (x % i == 0) {
			res.emplace_back(i, 0);
			while (x % i == 0) {
				res.back().second++;
				x /= i;
			}
		}
	}
	if (x > 1) {
		if (!res.empty() && res.back().first == x)
			res.back().second++;
		else
			res.emplace_back(x, 1);
	}
	return res;
}

int main()
{
	ios::sync_with_stdio(false), cin.tie(0);
	ll n;
	cin >> n;
	auto ps = factorize(n);
	ll mi = ps.size(), ma = 1;
	for (auto pp : ps) {
		ma *= pp.second + 1;
	}
	cout << mi << ' ' << ma - 1 << endl;
	return 0;
}



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

using namespace std;

int main()
{
    int N, M, K, A, Sum = 0;
    cin >> N >> M;
    vector<int> v;

    for (int i = 0; i < N; i++)
    {
        cin >> K;

        for (int a = 0; a < K; a++)
        {
            cin >> A;
            v.push_back(A);
        }
    }

    for (int i = 1; i <= 30; i++)
    {
        if (count(v.begin(), v.end(), i) == N)
        {
            Sum++;
        }
    }

    cout << Sum;
}

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

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

int main(){
  string S;
  cin>>S;
  int ans=0;
  rep(i,4){
    if(S[i]=='+')
      ans++;
    else
      ans--;
  }

  cout<<ans<<endl;

  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){
	string str,ans;
	int cnt=0;
	ans = "CODEFESTIVAL2016";
	cin >> str;
	for(int i=0;i<str.size();i++){
		if(str[i]!=ans[i])	cnt++;
	}
	cout << cnt << endl;
}


Measured memory_bytes: 256
#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

fn main() {
    let mut sc = Scanner::new();
    let s: String = sc.cin();
    let t: String = sc.cin();
    if s.len() + 1 == t.len() && s == t[0..t.len() - 1] {
        put!("Yes");
    } else {
        put!("No");
    }
}

use std::collections::VecDeque;
use std::io::{self, Write};
use std::str::FromStr;

struct Scanner {
    stdin: io::Stdin,
    buffer: VecDeque<String>,
}
impl Scanner {
    fn new() -> Self {
        Scanner {
            stdin: io::stdin(),
            buffer: VecDeque::new(),
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.is_empty() {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> {
        self.cin::<String>().chars().collect()
    }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.cin()).collect()
    }
}


Measured memory_bytes: 2108
n = int(input())
s = [0] * n
for i in range(n):
    s[i] = input()
d = [0] * 26
for c in s[0]:
    d[ord(c) - ord('a')] += 1
for i in range(1, n):
    e = [0] * 26
    for c in s[i]:
        e[ord(c) - ord('a')] += 1
    for i in range(26):
        d[i] = min(d[i], e[i])
x = []
for i, k in enumerate(d):
    x.extend(chr(ord('a') + i) * k)
print(''.join(x))


Measured memory_bytes: 3064
#!/usr/bin/env python
# coding: utf-8

# In[26]:


N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))


# In[28]:


ans = 0
for i in range(N):
    ans += min(A[i], B[i])
    B[i] = max(B[i]-A[i], 0)
    ans += min(A[i+1], B[i])
    A[i+1] = max(A[i+1]-B[i], 0)
print(ans)


# In[ ]:






Measured memory_bytes: 24008
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

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

//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する
class Solve {
public:
    void input() {

    }

    void solve() {
        input();
        int a, b, c;
        cin >> a >> b >> c;
        if ((c - b) * (b - a) > 0 && a < b) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }


    }
};


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

    Solve().solve();


    return 0;
}



Measured memory_bytes: 3120
#include <bits/stdc++.h>
#include <unistd.h>
#define ll long long
#define inf 1000000007
#define inf16 0x3f3f3f3f
#define INF 1000000000000000007LL
#define VI vector<int>
#define VPII vector<pair<int, int> >
#define VLL vector<ll>
#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define ALL(c) (c).begin(), (c).end()
using namespace std;

#define int long long
const int LEN = 13888888;
const int MOD = 1e9+7;

int s, ans;

int t[18*1000*1000];

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

	if(b==1)
		return a%MOD;

	if(b%2)
		return (ll)a*fpow(a, b-1)%MOD;

	int q = fpow(a, b/2);
	return (ll)q*q%MOD;
}

int32_t main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);

	cin >> s;

	for(int len = 1; len <= min(LEN, s); ++len)
	{
		if(s%len==0)
		{
			int val = s/len, pot = fpow(10, val-1);

			if(val>8 || len<=9*pot)
			{
				ans += 9*pot-len+1;
				ans %= MOD;
			}
		}
		else
		{
			int val = s/len;
			int x = s-val*len;
			int pot = fpow(10, val-1);

			if((val>8 || len-x<=9*pot) && (val+1>8 || x<=9*10*pot))
				++ans;
		}
	}

	int curr = 1, zostalo = 9;

	for(int i = 1; i < 18*1000*1000; ++i)
	{
		t[i] = curr;
		--zostalo;

		if(!zostalo)
		{
			++curr;
			zostalo = 9*fpow(10, curr-1);
		}
	}

	int r = 0, sum = 0;

	for(int l = 1; l < 18*1000*1000; ++l)
	{
		while(sum<s && r+1<18*1000*1000)
			sum += t[++r];
	
		if(sum==s && t[r]>=t[l]+2)
			++ans;

		sum -= t[l];
	}

	ans %= MOD;
	ans += MOD;
	ans %= MOD;

	cout << ans << endl;
}

Measured memory_bytes: 140928
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repp(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define repm(i,n) for(int (i)=(n-1);(i)>=0;(i)--)
#define INF (1ll<<60)
#define all(x) (x).begin(),(x).end()
typedef long long lint;
const lint MOD =1000000007;
const lint MAX = 10000000;
using Graph =vector<vector<lint>>;

 
lint fac[MAX], finv[MAX], inv[MAX];

void COMinit() // 二項係数を求める時は前処理としてCOMinit()をする
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (lint i = 2; i < MAX; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
 
// 二項係数計算
long long COM(lint n, lint k)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

lint primary(lint num)
{
    if (num < 2) return 0;
    else if (num == 2) return 1;
    else if (num % 2 == 0) return 0; // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return 0;
        }
    }

    // 素数である
    return 1;
}
  lint lcm(lint a,lint b){
      return a/__gcd(a,b)*b;
  }
int main(){
  lint H,W;
  cin>>H>>W;
  char ab[H][W];
  char b[H][W];
  vector<pair<lint,lint>>f;
  rep(i,H){
    rep(j,W){
      cin>>ab[i][j];
      if(ab[i][j]=='.')f.push_back(make_pair(i,j));
      b[i][j]=ab[i][j];
    }
  }
  vector<lint> ans1;
  vector <lint> x={-1,1,0,0};
  vector <lint> y={0,0,1,-1};
    queue <tuple<lint,lint,lint>> que;
  rep(i,f.size()){
      repp(j,i+1,f.size()){
            rep(k,H){
                rep(l,W){
                    ab[k][l]=b[k][l];
                }
            }
          lint a,b,c,d;
          tie(a,b)=f[i];
          tie(c,d)=f[j];
          while(!que.empty())que.pop();
          que.emplace(a,b,0);
    while(!que.empty()){
      lint xx,yy,ans;
      tie(yy,xx,ans)=que.front();
    // cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<ans<<endl;
      que.pop();
      if(yy==(c)&&xx==(d)){
          ans1.push_back(ans);
          break;
      }
      rep(i,4){
             if((xx+x[i])<0||(xx+x[i])>(W-1)||(yy+y[i])<0||(yy+y[i])>(H-1)){
                
                    continue;
                }
        if(ab[yy+y[i]][xx+x[i]]=='.'){
          que.emplace(yy+y[i],xx+x[i],ans+1);
         ab[yy+y[i]][xx+x[i]]='#';
        }
      }
 
    }
  }
  }
  lint ans2=0;

  rep(i,ans1.size()){
     ans2=max(ans2,ans1[i]);
  }
  cout<<ans2;
 }


Measured memory_bytes: 1408
#include<bits/stdc++.h>
using namespace std;
int main(){
  int m,n;
  int a,b,x[51]={};
  cin>>n>>m;
  for(int i=0;i<m;++i){
    cin>>a>>b;
    x[a]++;
    x[b]++;
  }
  for(int i=1;i<=n;++i) cout<<x[i]<<endl;
  return 0;
}


Measured memory_bytes: 256
gets
p_a = gets.split.map(&:to_i)

count = 0
p_a.each.with_index(1) do |p, i|
  if p == i
    p_a[i - 1], p_a[i] = p_a[i], p_a[i - 1]
    count += 1
  end
end

puts count


Measured memory_bytes: 8068
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <stack>
#include <set>
#include <queue>
#include <algorithm>
#include <deque>
#include <map>
#include <cmath>
using namespace std;

#define INF 1e18
#define int long long

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

signed main() {
    int n; cin >> n;
    vector<int> dp(n + 1, INF);

    dp[0] = 0;

    for(int i = 1; i <= n; i++) {
        int power = 1;
        while(power <= i) {
            chmin(dp[i], dp[i - power] + 1);
            power *= 6;
        }

        power = 1;
        while(power <= i) {
            chmin(dp[i], dp[i - power] + 1);
            power *= 9;
        }
    }

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


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

signed main(){
    int A[3];
    cin >> A[0] >> A[1] >> A[2];
    
    sort(A,A+3,greater<int>());
    
    int ans = 0;
    ans = A[0]*3-A[0]-A[1]-A[2];
    
    if(ans%2==0) cout << ans/2 << endl;
    else cout << (ans+3)/2 << endl;
    
    return 0;
}


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

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

    int sum = 0;

    for (int i = 0; i < N; i++) {
        /* code */
        int l, r;
        cin >> l >> r;

        sum += r - l + 1;
    }
    cout << sum << endl;
}

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

using namespace std;

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

	int n = s.size();

	auto s2 = s;
	reverse(s2.begin(), s2.end());


	bool ok = s == s2;

	if (ok) {
		string t = s.substr(0, (n - 1) / 2);
		string u = s.substr((n + 3) / 2 - 1);


		auto t2 = t;
		auto u2 = u;
		reverse(t2.begin(), t2.end());
		reverse(u2.begin(), u2.end());

		ok = t == t2 && u == u2;
	}

	cout << (ok ? "Yes" : "No") << endl;
	return 0;
}


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

using namespace std;

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

	int n, k;
	cin >> n >> k;

	string s;
	cin >> s;

	for(int i = 0; i < k; ++i) {
		if(*min_element(s.begin(), s.end()) == *max_element(s.begin(), s.end())) {
			break;
		}
		string t = s;
		reverse(t.begin(), t.end());
		string x = s + t;
		
		vector<string> tmp;
		for(int i = 0; i < n; ++i) {
			tmp.emplace_back(x.substr(i, n));
		}
		sort(tmp.begin(), tmp.end());
		s = *min_element(tmp.begin(), tmp.end());
		reverse(s.begin(), s.end());
	}

	reverse(s.begin(), s.end());

	cout << s << '\n';

	return 0;
}


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

int gcd(int c, int d){//c*d=0をもとから除外
    if(c<d)
        swap(c,d);
    while(c%d!=0){
        c %= d;
        swap(c,d);
    }
    return d;
}

int main(){
  int n; cin >>n;
  vector<int> a(n);
  
  for(int i=0; i<n; i++){
    cin >> a[i];
  }
  int ans = a[0];
  
  for(int i=1; i<n; i++){
    ans = gcd( ans, a[i] );
  }
  cout << ans << endl;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
#define ll long long int
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define pb(v, a) v.push_back(a)
#define Sort(v) sort(v.begin(), v.end())
#define For(m) for (auto it = m.begin(); it != m.end(); it++)
#define mod 1000000007
#define PI 3.14159265358979323846
using namespace std;

int main()
{
   ll arr[3];
   ll b[3];
   vector<ll>vec[5];
   fr(i,0,3)
   {
       cin >> arr[i];
       cin >> b[i];
       pb(vec[arr[i]],b[i]);
       pb(vec[b[i]],arr[i]);
   }
   fr(i,1,5)
   {
       if(vec[i].size()>2)
       {
           cout << "NO";
           return 0;
       }
   }
   cout << "YES";
  return 0;
}


Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<iostream>
#define in inline
#define re register
#define int long long
using namespace std;
in int read() {
	re int t=0;
	re char v=getchar();	
	while(v<'0'||v>'9'){
	v=getchar();
	}	
	while(v>='0'&&v<='9'){		
	t=(t<<3)+(t<<1)+(v^48);		
	v=getchar();	}
	return t;
}
int f[4][30002],a,b,c,d,lst[12],sum;
char s[30002];
signed main(){
	cin>>a;
scanf("%s",s);
for(re int i=0;i<a;++i){
	f[1][i]=1;
}
for(re int i=a-1;i>=0;--i){
f[2][i]=sum;
	sum-=lst[s[i]-'0'];
	lst[s[i]-'0']=f[1][i];
	sum+=lst[s[i]-'0'];
	
}
sum=0;
memset(lst,0,sizeof(lst));
for(re int i=a-1;i>=0;--i){	f[3][i]=sum;
	sum-=lst[s[i]-'0'];
	lst[s[i]-'0']=f[2][i];
	sum+=lst[s[i]-'0'];

}
sum=0;
memset(lst,0,sizeof(lst));
for(re int i=a-1;i>=0;--i){
	sum-=lst[s[i]-'0'];
	lst[s[i]-'0']=f[3][i];
	sum+=lst[s[i]-'0'];
}
cout<<sum;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define forin(x,y) for(ll i=x;i<y;i++)
#define vii vector<ll>
#define pii  pair<ll,ll>
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define inf 1e17
#define M (ll)(1e9+7)
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void evaluate()
{
    ;
}
int main()
{
    ios::sync_with_stdio(false);    cin.tie(0);     cout.tie(0);
    ll n,temp;  cin>>n;
    vector<vector<short int>> compat(n+1);
    for(auto &i: compat) i.resize(n+1);
    vii dp(1<<n);
    

    forin(1,n+1)
    {
        for(ll j=1;j<n+1;j++)
        {
            cin>>compat[i][j];
        }

    }

   dp[0] = 1;
            
    for(ll j=0; j<(1<<n) ; ++j)
    {
        ll a = __builtin_popcount(j);
        for(ll k=1;k<n+1;++k)
        {
            if((compat[a][k]) && (j>>(k-1)&1))
            {
                dp[j] += dp[j^(1<<(k-1))];
                dp[j] %=M;
            }
        }
    }
    
    cout<<dp[(1<<n)-1]<<endl;

    
}


Measured memory_bytes: 16768
#include <bits/stdc++.h>
#define REP(i, n) for(int i=0; i<(int)(n); i++)
#define REP1(i,a,b) for(int i=a; i<=(int)(b); i++)
#define ALL(x) begin(x),end(x)
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef vector<int> VI;
typedef pair<int,int> PII;

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}

const int N=2e5+5;
int a[N];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    string x;
    cin >> n >> x;
    // init
    // vector<LL> a(n+1);
    for ( int i=1; i<=n; i++ ) {
        a[i]=a[i%__builtin_popcount(i)]+1;
    }

    int M=0;
    REP(i,n) if ( x[i]=='1' ) M++;

    vector<int> ans(n);
    if ( M<n ) {
        int p=1;
        int res=0;
        for ( int i=n-1; i>=0; i-- ) {
            if ( x[i]=='1' ) {
                res=(res+p)%(M+1);
            }
            p=(LL)p*2%(M+1);
        }

        p=1;
        for ( int i=n-1; i>=0; i-- ) {
            if ( x[i]=='0' ) {
                int y=(res+p)%(M+1);
                ans[i]=a[y]+1;
            }
            p=(LL)p*2%(M+1);
        }
    }
    if ( M>1 ) {
        int p=1;
        int res=0;
        for ( int i=n-1; i>=0; i-- ) {
            if ( x[i]=='1' ) {
                res=(res+p)%(M-1);
            }
            p=(LL)p*2%(M-1);
        }

        p=1;
        for ( int i=n-1; i>=0; i-- ) {
            if ( x[i]=='1' ) {
                int y=(res-p)%(M-1);
                if ( y<0 ) y+=M-1;
                ans[i]=a[y]+1;
            }
            p=(LL)p*2%(M-1);
        }
    }
    REP(i,n) cout << ans[i] << '\n';
    return 0;
}


Measured memory_bytes: 5164
import sys
import bisect


def main():
    n, K, L, R = map(int, sys.stdin.readline().split())
    a = tuple(map(int, sys.stdin.readline().split()))
    m = (n+1)//2
    ls = [[] for _ in range(m+1)]
    for i in range(1 << m):
        cnt = 0
        val = 0
        for j in range(m):
            if i >> j & 1:
                cnt += 1
                val += a[j]
        ls[cnt].append(val)
    for i in range(m+1):
        ls[i].sort()
    ans = 0
    for i in range(1 << n-m):
        cnt = 0
        val = 0
        for j in range(n-m):
            if i >> j & 1:
                cnt += 1
                val += a[m+j]
        if K-m <= cnt <= K:
            ans += bisect.bisect_right(ls[K-cnt], R-val) - bisect.bisect_right(ls[K-cnt], L-val-1)
    print(ans)


if __name__ == '__main__':
    main()



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

signed main() {
    int n;
    cin >> n;

    if(n == 2) {
        cout << 1 << endl;
        return 0;
    }

    set<int> st;
    vector<int> v;
    for(int i = 2; i * i <= n; i++){
        int x = n;
        while(x % i == 0) x /= i;
        if(x % i == 1) {
            v.push_back(i);
            st.insert(i);
        }
    }

    int m = v.size();
    int x = n - 1;
    for(int i = 0; i < m; i++){
        if(x % v[i] == 0) st.insert(x / v[i]);
    }
    cout << st.size() + 2<< endl;
}

    




Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
struct iofix
{
	ios_base::Init i;
	iofix() {
		cin.sync_with_stdio(0);
		cin.tie(0);
		cout.sync_with_stdio(0);
		cout.tie(0);
		cout.precision(10);
		cout << fixed;
	}
} iofix;

void max_self(int64_t  &a, int64_t b){
	a = max(a, b);
}
void solve(){
	int N, W; cin >> N >> W;
	vector <int64_t > dp(W + 1);
	for(int i = 0; i < N; ++i){
		int weight, value; cin >> weight >> value; 
		for(int j = W - weight;j >= 0; --j){
		     max_self(dp[j + weight], dp[j] + value);
		}
	}
	int64_t ans = 0;
	for(int i = 0;i <= W; ++i)
		 max_self(ans, dp[i]);
	cout << ans << "\n";
}

int main(){

	solve();

#ifndef Time
	cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}



Measured memory_bytes: 3984
//
#include <cstdio>
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};//0 right 1 up 2left 3 down
bool map[2005][2005];
int dir[2005][2005][4];
int h,w,ans=0,tot;
int check(const int &x,const int &y,const int &d){
	if(dir[x][y][d]!=0)	return dir[x][y][d];
	if(map[x][y]||x>h||x<1||y>w||y<1)	return 0;
	return dir[x][y][d]=1+check(x+dx[d],y+dy[d],d);
}
int main(){
	scanf("%d %d ",&h,&w);
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++)
			if(getchar()=='#')	map[i][j]=true;
		getchar();
	}
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(!map[i][j]){
				tot=1;
				for(int d=0;d<4;d++)	tot+=check(i+dx[d],j+dy[d],d);
				if(tot>ans)	ans=tot;
			}
		}
	}
	printf("%d",ans);
	return 0;
}

Measured memory_bytes: 66944
#include <iostream>
#include <set>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll n, k;
ll a[2000000];

vector<ll> v;
set<ll> s;

int main(int argc, char const *argv[]) {
    cin >> n >> k;
    for (ll i = 1; i <= n; i++) {
        cin >> a[i];
    }

    ll pos = 1;
    v.push_back(pos);
    s.insert(pos);
    for (ll i = 0; i < k; i++) {
        ll nextPos = a[pos];
        if (s.find(nextPos) != s.end()) {
            auto result = find(v.begin(), v.end(), nextPos);
            ll relPos = distance(v.begin(), result);
            ll repeatNum = distance(result, v.end());
            pos = v[(k - relPos) % repeatNum + relPos];
            cout << pos << endl;
            return 0;
        }
        v.push_back(nextPos);
        s.insert(nextPos);
        pos = nextPos;
    }
    cout << pos << endl;
    return 0;
}

Measured memory_bytes: 13084
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <limits.h>
#include <math.h>

#define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i))
#define debug(x) cerr << #x << ": " << x << '\n'
#define debugArray(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i] << '\n'

using namespace std;

typedef long long ll;
typedef pair<int,int> Pii;
typedef vector<int> vint;
const ll INF = INT_MAX;

int main() {
  ll N,K;
  cin >> N >> K;
  ll ans = 0;
  if(K==0){
    ans = N*N;
  }else{
    for(ll b = K+1;b<=N;b++){
      ll q = (b-K)*((N-K)/b+1);
      ll r = (N-K)%b;

      if(r<=b-1-K){
        q -= (b-1-K-r);
      }
      ans += q;
    }
  }
  cout << ans << endl;
  return 0;
}


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

#define debug(...) fprintf(stderr, __VA_ARGS__)

#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif

namespace IO {
	static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;

	inline char myGetchar() {
		static char buf[IN_BUF], *ps = buf, *pt = buf;
		if (ps == pt) {
			ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
		}
		return ps == pt ? EOF : *ps++;
	}

	template<typename T>
	inline bool read(T &x) {
		bool op = 0;
		char ch = getchar();
		x = 0;
		for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
			op ^= (ch == '-');
		}
		if (ch == EOF) {
			return false;
		}
		for (; isdigit(ch); ch = getchar()) {
			x = x * 10 + (ch ^ '0');
		}
		if (op) {
			x = -x;
		}
		return true;
	}

	inline int readStr(char *s) {
		int n = 0;
		char ch = getchar();
		for (; isspace(ch) && ch != EOF; ch = getchar())
			;
		for (; !isspace(ch) && ch != EOF; ch = getchar()) {
			s[n++] = ch;
		}
		s[n] = '\0';
		return n;
	}

	inline void myPutchar(char x) {
		static char pbuf[OUT_BUF], *pp = pbuf;
		struct _flusher {
			~_flusher() {
				fwrite(pbuf, 1, pp - pbuf, stdout);
			}
		};
		static _flusher outputFlusher;
		if (pp == pbuf + OUT_BUF) {
			fwrite(pbuf, 1, OUT_BUF, stdout);
			pp = pbuf;
		}
		*pp++ = x;
	}

	template<typename T>
	inline void print_(T x) {
		if (x == 0) {
			putchar('0');
			return;
		}
		static int num[40];
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		for (*num = 0; x; x /= 10) {
			num[++*num] = x % 10;
		}
		while (*num){
			putchar(num[*num] ^ '0');
			--*num;
		}
	}

	template<typename T>
	inline void print(T x, char ch = '\n') {
		print_(x);
		putchar(ch);
	}

	inline void printStr_(const char *s, int n = -1) {
		if (n == -1) {
			n = strlen(s);
		}
		for (int i = 0; i < n; ++i) {
			putchar(s[i]);
		}
	}

	inline void printStr(const char *s, int n = -1, char ch = '\n') {
		printStr_(s, n);
		putchar(ch);
	}
}
using namespace IO;

const int N = 200005, INF = 0x3f3f3f3f;

int n;
long long A[N], B[N], C[N];

int top, sta[N];

bool cmp(int i, int j, int k) {
	return (A[i] - A[j]) * (j - k) < (A[j] - A[k]) * (i - j);
}

int main() {
	read(n);
	for (int i = 1; i <= n; ++i) {
		read(A[i]);
	}
	for (int i = 1; i <= n; ++i) {
		read(B[i]);
	}
	int max_pos = std::max_element(A + 1, A + 1 + n) - A;
	std::rotate(A + 1, A + max_pos, A + 1 + n);
	std::rotate(B + 1, B + max_pos, B + 1 + n);
	A[++n] = A[1], B[n] = B[1];
	C[1] = C[2] = 0;
	for (int i = 2; i < n; ++i) {
		C[i + 1] = 2 * (B[i] + C[i]) - C[i - 1];
	}
	for (int i = 1; i <= n; ++i) {
		A[i] -= C[i];
	}
	top = 1, sta[1] = 1;
	for (int i = 2; i <= n; ++i) {
		while (top > 1 && !cmp(i, sta[top], sta[top - 1])) {
			--top;
		}
		sta[++top] = i;
	}
	long long ans = 0;
	for (int i = 1; i < top; ++i) {
		ans += (A[sta[i + 1]] + A[sta[i]]) * (sta[i + 1] - sta[i] + 1);
		ans -= A[sta[i + 1]] * 2;
	}
	for (int i = 1; i < n; ++i) {
		ans += C[i] * 2;
	}
	printf("%.15lf\n", 1.0 * ans / 2 / (n - 1));
}


Measured memory_bytes: 12260
#include<bits/stdc++.h>
using namespace std;
vector<int> *node;
int *path;
void calculate(int u)
{
    int max_val=0;
    for(int i=0;i<node[u].size();i++)
    {
        if(path[node[u][i]]==-1)
        {
            calculate(node[u][i]);
        }
        max_val=max(max_val,path[node[u][i]]);
    }
    path[u]=max_val+1;
}
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
    int n,m;
    cin>>n>>m;
    node=new vector<int>[n];
    path=new int[n];
    for(int i=0;i<n;i++)
        path[i]=-1;
    int in[n]={0};
    int u,v;
    for(int i=0;i<m;i++)
    {
        cin>>u>>v;
        u--;
        v--;
        in[v]++;
        node[u].push_back(v);
    }
    for(int i=0;i<n;i++)
    {
        if(in[i]==0 && path[i]==-1)
        {
            calculate(i);
        }
    }
    int max_val=0;
    for(int i=0;i<n;i++)
    {
        max_val=max(max_val,path[i]);
    }
    cout<<max_val-1;
    return 0;
}

/*
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
*/


Measured memory_bytes: 12800
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>

using namespace std;

#ifdef LDEBUG
#define debug(x) cout << #x << ":" << x << endl
#else
#define debug(...)
#endif

#define bzero(m)  memset(m,0,sizeof(m))
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)

typedef unsigned long long u64;
typedef long long i64;

u64 gcd(u64 a, u64 b){
        u64 tmp;
        if (b > a) {
                tmp = a;
                a = b;
                b = tmp;
        }

        if ((a % b) == 0)
                return b;

        return gcd(b, a%b);
}


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

        int n;
        vector<u64> a;

        cin >> n;

        REP(i, n) {
                u64 tmp;
                cin >> tmp;
                a.push_back(tmp);
        }

        u64 r = a[0];
        REP(i, n) {
                r = min(r, gcd(a[i], r));
        }

        cout << r << endl;


        return 0;
}


Measured memory_bytes: 1400
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000
typedef pair<ll, ll> pll;

int N;
bool binary_search(int a, vector<int> A) {
    int ng = -1, ok = N - 1;

    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;
        if (A[mid] == a) {
            ok = mid;
            break;
        } else if (A[mid] > a)
            ok = mid;
        else
            ng = mid;
    }

    if (A[ok] == a)
        return true;
    else
        return false;
}

int main() {
    cin >> N;
    vector<int> A(N);
    rep(i, N) { cin >> A[i]; }

    int q, counter = 0;
    cin >> q;
    rep(i, q) {
        int a;
        cin >> a;
        if (binary_search(a, A))
            counter++;
    }

    cout << counter << endl;
}


Measured memory_bytes: 3588
#include <stdio.h>
#include <math.h>

int main(){
	int n,i;
	double a[1000];
	a[0] = 0.0;
	for(i = 1;i < 1000;i++){
		a[i] = a[i - 1] + asin(1.0 / sqrt(i + 1));
	}
	while(1){
		scanf("%d",&n);
		if(n == -1) break;
		printf("%f\n%f\n",sqrt(n) * cos(a[n - 1]),sqrt(n) * sin(a[n - 1]));
	}
	return 0;
}

Measured memory_bytes: 708
import java.util.Scanner;
import java.lang.Math;

class Main{
    static Scanner s;
    
    public static void main(String args[]){
        s = new Scanner(System.in);
        
        int amount = s.nextInt();
        
        int count = 0;
        for(int i = 0; i < amount; i++){
            if(isPrime(s.nextInt())){
                count++;
            }
        }
        
        System.out.println(count);
    }
    
    static double sqrt;
    public static boolean isPrime(int x){
        sqrt = Math.sqrt(x);
        if(x == 2) {
        	return true;
        }
        if(x%2 == 0){
            return false;
        }
        else {
        	for(int i = 3; i <= sqrt; i = i + 2){
        		//System.out.println("Checking " + x +" against "+ i);
        		if(x%i == 0){
        			return false;
        		}
            }
        }
        
        return true;
    }
    
}


Measured memory_bytes: 42476
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define s(x)   scanf("%d",&x);
#define bug(x) printf("  %s = %d \n",#x,x);
#define mem(arr,x) memset(arr,x,sizeof arr);
const int maxn = 2e5 + 5;
typedef long long LL;
using namespace std;
vector<int> vec;
int f[maxn];

int main() {
	int N,M;
	while(scanf("%d%d",&N,&M) != EOF) {
		vec.clear();
		mem(f,0)
		int a,b;
		for(int i = 1; i <= M; i++) {
			scanf("%d%d",&a,&b);
			if(b == N)	 f[a] = 1;
			if(a == 1)	 vec.push_back(b);
		}
		bool jud = 0;
		for(int i = 0 ; i < vec.size() ; i++) {
			int v = vec[i];
			if(f[v]){
				jud = true;
				break;
			}
		}
		if(jud)
			puts("POSSIBLE");
		else
			puts("IMPOSSIBLE");
	}
}


Measured memory_bytes: 1660
#dijkstra

from heapq import heappop as pop
from heapq import heappush as push

INF = 10 ** 20

n = int(input())
m = int(input())

edges = [[] for _ in range(n)]

for _ in range(m):
  a, b, c, d = map(int, input().split(","))
  a, b = a - 1, b - 1
  edges[a].append((b, c))
  edges[b].append((a, d))

s, g, v, p = map(int, input().split(","))
s, g = s - 1, g - 1

def dij(s, g):
  que = [(0, s)]
  costs = [INF for _ in range(n)]
  costs[s] = 0
  
  while que:
    cost, num = pop(que)
    if num == g:
      return cost
    for edge in edges[num]:
      to, fee = edge
      if costs[to] > cost + fee:
        costs[to] = cost + fee
        push(que, (cost + fee, to))

print(v - p - dij(s, g) - dij(g, s))


Measured memory_bytes: 5668
/**
*    author:  yuya1234
*    created: 30.06.2020 09:34:59
**/

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define SORT(s) sort((s).begin(),(s).end())
#define SORTD(s) sort((s).rbegin(),(s).rend())
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

#define SZ(x) ((int)(x).size())
#define MEMSET(v, h) memset((v), h, sizeof(v))

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

    int n;
    cin>>n;

    string s;
    cin>>s;

    int tmpE,tmpW,ans;
    tmpE=tmpW=0;
    ans=INT_MAX;

    int cntE[1000000],cntW[1000000];

    REP(i,n)
    {
        cntE[i]=tmpE;
        if(s[i]=='W')tmpE++;
    }
    REPD(i,n)
    {
        cntW[i]=tmpW;
        if(s[i]=='E')tmpW++;
    }

    REP(i,n)
    {
        ans=min(ans,cntE[i]+cntW[i]);
    }
  
    cout<<ans<<endl;

    return 0;
}

Measured memory_bytes: 6128
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#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)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> d;
    int ed;
    REP(i, k) {
        cin >> ed;
        d.push_back(ed);
    }
    for(int i = n; i <= 10*n; i++) {
        int tn = i;
        bool is_valid = true;
        while(tn > 0) {
            int num = tn % 10;
            tn /= 10;
            if(find(d.begin(), d.end(), num) != d.end()) {
                is_valid = false;
                break;
            }
        }
        if(is_valid) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
typedef long long LL;
int main(){
	int A,B,C;
	cin>>A;
	cin>>B>>C;
	if(A<=C&&C<=B){
           cout<<"Yes"<<endl;
        }else{
           cout<<"No"<<endl;
        }
	return(0);
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#define swap(type,a,b) do{type t=a;a=b;b=t;}while(0);
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define ll long long
#define INF 100000000
#define FOR(i,a,n) for(i=(a);i<(n);i++)
int comp(const void* a,const void* b){
	return *(int*)a-*(int*)b;
}
void fill(int a[],int b,int c){
	int i;
	FOR(i,0,b) a[i]=c;
	return;
}
char str[200001];
int cnt[26];
int main(void)
{
	ll ans;
	int len,i;
	scanf("%s",str);
	len=strlen(str);
	FOR(i,0,len) cnt[str[i]-'a']++;
	ans=(ll)len*(len-1)/2+1;
	FOR(i,0,26) ans-=(ll)cnt[i]*(cnt[i]-1)/2;
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 384
import unittest
from io import StringIO
import sys


def resolve():
    input = sys.stdin.readline
    N = int(input())
    # S = input()
    # A, B = map(int, input().split())
    A = list(map(int, input().split()))

    # P = []
    # S = []
    # for i in range(N):
    #     pp, ss = map(int, input().split())
    #     P.append(pp)
    #     S.append(ss)

    isBalls = [0] * (N + 1)
    for k in reversed(range(1, N + 1)):
        num = sum([isBalls[k * i] for i in range(1, N // k + 1)])
        if num % 2 != A[k - 1]:
            isBalls[k] = 1

    M = sum(isBalls)
    ans = [i for i in range(1, N + 1) if isBalls[i] > 0]

    print(M)
    if ans:
        print(' '.join(map(str, ans)))

resolve()


Measured memory_bytes: 22668
use proconio::{input, fastout};

const MOD : i64 = 1e9 as i64 + 7;

#[fastout]
fn main() {
    input! {
        n : i64,
        k : i64
    }
    let mut ans : i64 = 0;
    for pick in k..=std::cmp::max(k, n + 1) {
        let smallest = (pick - 1) * pick / 2;
        let largest = (n - pick + 1) * pick + (pick - 1) * pick / 2;
        let range = largest - smallest + 1;
        ans += range;
        ans %= MOD;
    }
    println!("{}", ans);
}


Measured memory_bytes: 2168
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define countof(array) (sizeof(array) / sizeof(array[0]))
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = (n)-1; i >= 0; --i)
#define rep2(i,n) for(int i = 1; i <= (n); ++i)
#define rrep2(i,n) for(int i = (n); i > 0; --i)
#define srep(i,s,n) for(int i = s; i < (n); ++i)
#define rsrep(i,s,n) for(int i = (n)-1; i >= s; --i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define aall(a) (a), (a)+countof(a)//for array sorting
#define raall(a) (a), (a)+countof(a), greater<>()
#define show(x) cout<<#x<<" = "<<(x)<<endl;
#define vfind(v, a) find(all(v), a) != v.end()
#define yn(f) { if (f) puts("YES"); else puts("NO"); }
#define yns(f) { if (f) puts("Yes"); else puts("No"); }
#define show_ary(...) { cout<<#__VA_ARGS__<<" = "; for (const auto& x : (__VA_ARGS__)) { cout<<x<<" "; } cout<<endl; }
#define show_pair(...) cout<<#__VA_ARGS__<<" = "<<endl; for (const auto& x : (__VA_ARGS__)) { cout<<"  "<<x.fi<<" : "<<x.se<<endl; }
#define out_ary(...) { int n = (__VA_ARGS__).size(); rep(i, n) printf("%d%s", (__VA_ARGS__)[i], i != n-1 ? " " : "\n"); }
#define argmax(v) distance((v).begin(), max_element(all(v)))
#define argmin(v) distance((v).begin(), min_element(all(v)))
#define vmax(v) *max_element(all(v))
#define vmin(v) *min_element(all(v))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<P> vpair;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<string> vstr;
typedef vector<bool> vbool;
typedef vector<vint> vvint;
typedef vector<vll> vvll;
typedef vector<vstr> vvstr;
typedef vector<vbool> vvbool;
const ll LINF = 2000000000000000000ll;
const int INF = 1000000100;
const ll MOD = 1e9+7;

vll divisor(ll n) {
    vll ans;
    for (ll i = 1; i*i <= n; ++i) {
        if (n%i == 0) {
            ans.push_back(i);
            if (i*i != n) ans.push_back(n/i);
        }
    }
    sort(rall(ans));
    return ans;
}

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

    ll sum = accumulate(all(a), 0ll);
    vll d = divisor(sum);
    ll ans = 0;
    for (ll x : d) {
        vint m(n);
        rep(i, n) m[i] = a[i] % x;
        sort(all(m));
        vint sump(n), summ(n);
        sump[0] = m[0];
        summ[0] = m[0]-x;
        srep(i, 1, n) {
            sump[i] = sump[i-1] + m[i];
            summ[i] = summ[i-1] + (m[i] - x);
        }
        rep(i, n) {
            if (sump[i] == -(summ[n-1] - summ[i])) {
                if (sump[i] <= k) ans = x;
                else break;
            }
        } 
        if (ans != 0) break;
    }

    cout << ans << endl;
    return 0;
}

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

long long n, s, t;
vector<vector<long long>> dist;

void solve();
long long query(long long x, long long y) {
  cout << "? " << x << " " << y << endl;
  long long i;
  cin >> i;
  return i;
}

int main() {
  cin >> n >> s >> t;
  dist.assign(n + 1, vector<long long>(n + 1, 0));
  solve();
  return 0;
}

void solve() {
  dist[s][t] = dist[t][s] = query(s, t);
  vector<long long> v, res;
  res.push_back(s);
  for(int i = 1; i <= n; ++i)
    if(i != s && i != t) {
      long long in1 = query(s, i), in2 = query(i, t);
      dist[s][i] = in1;
      dist[i][t] = in2;
      if(in1 + in2 == dist[s][t]) v.push_back(i);
    }
  auto f = [](long long &l, long long &r) {
    return dist[s][l] < dist[s][r];
  };
  int vsize = v.size();
  sort(v.begin(), v.end(), f);
  for(int i = 0; i < vsize; ++i) {
    long long now = query(res.back(), v[i]);
    dist[res.back()][v[i]] = now;
    if(dist[s][res.back()] + now == dist[s][v[i]])
      res.push_back(v[i]);
  }
  res.push_back(t);
  cout << "!";
  for(int i = 0; i < res.size(); ++i) cout << " " << res[i];
  cout << endl;
}



Measured memory_bytes: 4672
#include <stdio.h>
#include <stdlib.h>
#define int long long
#define inf (int)(1e18)

int ABS(int a){
	return a >= 0 ? a : -a;
}

int min(int a, int b){
	return a <= b ? a : b;
}

signed main(){
	int N, K, i, ans = inf;
	scanf("%lld%lld", &N, &K);
	int *x = (int *)malloc(sizeof(int) * N);
	for(i = 0; i < N; i++){
		scanf("%lld", &x[i]);
	}
	for(i = 0; i + K <= N; i++){
		ans = min(ans, x[i + K - 1] - x[i] + min(ABS(x[i]), ABS(x[i + K - 1])));
	}
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 896
//#include <boost/multiprecision/cpp_dec_float.hpp>
//#include <boost/multiprecision/cpp_int.hpp>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <algorithm>
#include <bitset>
#include <iomanip>
using namespace std;
//namespace mp = boost::multiprecision;
//using llll = mp::cpp_int;
// 仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
//using Real = mp::number<mp::cpp_dec_float<1024>>;
using ll = long long;
const double EPS = 1e-10;
const ll MOD = 7+(1e+9);

int main(){
  ll n;
  cin>>n;
  vector<ll>x(n,0);
  vector<ll>y(n,0);
  for(ll i=0;i<n;i++){
    cin>>x[i]>>y[i];
  }
  double z=0;
  for(ll i=0;i<n;i++){
    for(ll j=i+1;j<n;j++){
      z+=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
    }
  }
  z=z*2/n;
  cout << fixed;
  cout<< setprecision(10)<<z<<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
const int INF = 1e8;
using namespace std;

const int N = 105;

int h, w, a;
char m[N][N];
int dis[N][N][15][15][4];

struct T{
	int c, y, x, p, q, d;
	T(int c, int y, int x, int p, int q, int d) : c(c),y(y),x(x),p(p),q(q),d(d) {}
	void tie(int& C, int& Y, int& X, int& P, int& Q, int& D){
		C=c;Y=y;X=x;P=p;Q=q;D=d;
	}

	bool operator < ( const T &right ) const {
		return c < right.c;
	}
	bool operator > ( const T &right ) const {
		return c > right.c;
	}
};

//typedef tuple<int,int,int,int,int,int> T;
int dy[4] = {0,1,0,-1};
int dx[4] = {1,0,-1,0};

int bfs(int h, int w, pair<int, int> s, pair<int, int> g){
	memset(dis, -1, sizeof(dis));
	dis[s.first][s.second][a][a][1] = 0;

	priority_queue<T, vector<T>, greater<T>> que;
	que.push(T(0, s.first, s.second, a, a, 1));

	while(not que.empty()){
		int c, y, x, p, q, d;
		T tmp = que.top(); que.pop();
		tmp.tie(c,y,x,p,q,d);
		dis[y][x][p][q][d] = c;
		if(y == g.first and x == g.second) return dis[y][x][p][q][d];

		rep(i,4){
			if((d + 2) % 4 == i) continue;

			int ny = y + dy[i];
			int nx = x + dx[i];
			int np = p, nq = q;
			int nc = c;
			
			if(d == 0 || d == 2){
				if((d + 1) % 4 == i){
					np = p - 1;
					nc++;
				}else if((d - 1 + 4) % 4 == i){
					nq = q - 1;
					nc++;
				}
			}else{
				if((d + 1) % 4 == i){
					nq = q - 1;
					nc++;
				}else if((d - 1 + 4) % 4 == i){
					np = p - 1;
					nc++;
				}
			}
			if(np < 0 || nq < 0) continue;

			if(nx < 0 || nx >= w || ny < 0 || ny >= h) continue;

			if(m[y][x] == 'S' and d != i) continue;
			if(m[ny][nx] == 'S' and i % 2 == 1) continue;
			//cout << ny << ' ' << nx << ' ' << np << ' ' << nq << endl;
				//cout << next.x << ' ' << next.y << ' ' << next.p << ' ' << next.q << ' ' << next.d << endl;
			if(dis[ny][nx][np][nq][i] == -1 && m[ny][nx] != '#'){
				//cout << next.x << ' ' << next.y << ' ' << next.p << ' ' << next.q << ' ' << next.d << endl;
				que.emplace(nc,ny,nx,np,nq,i);
			}
		}
	}

	return -1;
}


int main(){
	cin >> h >> w >> a;

	pair<int, int> s, g;
	rep(i,h) rep(j,w){
		cin >> m[i][j];
		if(m[i][j] == 'S') s = make_pair(i,j);
		if(m[i][j] == 'G') g = make_pair(i,j);
	}

	int ans = bfs(h, w, s, g);
	cout << (ans == INF ? -1 : ans) << endl;
}



Measured memory_bytes: 47868
#define _GLIBCXX_DEBUG
typedef long long ll;
#include <bits/stdc++.h>

using namespace std;

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

ll modpow(ll a, ll n, ll mod)
{
	ll res = 1;
	while (0 < n)
	{
		if (n & 1)
		{
			res = res * a % mod;
		}
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

int main()
{
	ll mod = 1000000007;
	int n;
	cin >> n;
	vector<ll> a(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	ll ans = 0;
	for (int bit = 0; bit < 60; bit++)
	{
		ll cnt0 = 0;
		ll cnt1 = 0;
		ll tmp = (1LL << bit) % mod;
		for (int i = 0; i < n; i++)
		{
			if ((1LL << bit) & a[i])
			{
				cnt1++;
			}
			else
			{
				cnt0++;
			}
		}
		tmp = (tmp * cnt0) % mod;
		tmp = (tmp * cnt1) % mod;
		ans = (ans + tmp) % mod;
	}
	cout << ans << endl;
}

Measured memory_bytes: 2560