akhauriyash/Code-Regression huggingface dataset:
//
// Created by Myabu on 2020/01/18.
//

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <utility>
#include <math.h>
#include <stack>
#include <queue>
#include <cstdio>
#include <numeric>

using namespace std;


int main(void){
    string S;
    cin>>S;


    int year = stoi(S.substr(0,5));
    int month = stoi(S.substr(5,7));
    int day = stoi(S.substr(8,10));
    if(year >= 2019){
        if(month > 4){
            cout<<"TBD"<<endl;
        }else{
            cout<<"Heisei"<<endl;
        }
    }else{
        cout<<"Heisei"<<endl;
    }
    


    return 0;
}

Measured memory_bytes: 256
n, m = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(m)]

max_L = 0
min_R = float("inf")
for i in range(m):
    L, R = LR[i]
    max_L = max(max_L, L)
    min_R = min(min_R, R)
if min_R - max_L < 0:
    print(0)
else:
    print(min_R - max_L + 1)

Measured memory_bytes: 27332
N, M = map(int, input().split())
ksList = [list(map(int, input().split())) for i in range(M)]
pList = list(map(int, input().split()))

ans = 2**N

for i in range(2**N):
 tentouList = format(i,  '0' + str(N) + 'b')
 #print(tentouList)
 for j in range(M):
     tentouSum = 0
     for k in range(1, ksList[j][0]+1):
         tentouSum = tentouSum + int(tentouList[N - ksList[j][k]])
         #print("ksList[j][k]" + str(ksList[j][k]))
     #print("tentoSum =" + str(tentouSum))
     #print("pList = " + str(pList[j]))
     if tentouSum % 2 != pList[j]:
         #print("false")
         ans = ans - 1
         break

#print("ans")
print(ans)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#define FIN ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef vector<int> vi;
typedef vector<vi> vvi;

int main() {FIN
    
    int n, m, i, j, s;
    cin >> n >> m;
    vvi mt(n, vi(m));
    vi ar(m);
    for (i = 0; i < n; i++) {
        for (j = 0; j < m; j++) cin >> mt[i][j];
    }
    for (i = 0; i < m; i++) cin >> ar[i];
    for (i = 0; i < n; i++) {
        s = 0;
        for (j = 0; j < m; j++) s += mt[i][j] * ar[j];
        cout << s << "\n";
    }
    
    return 0;
}


Measured memory_bytes: 3216
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

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

    vector<int> x(n), y(n);
    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i];
    }

    vector<int> w(n), z(n);
    for (int i = 0; i < n; i++) {
        w[i] = x[i] + y[i];
        z[i] = x[i] - y[i];
    }

    cout << max(*max_element(w.begin(), w.end()) - *min_element(w.begin(), w.end()),
            *max_element(z.begin(), z.end()) - *min_element(z.begin(), z.end())) << endl;
}


Measured memory_bytes: 6372
s = gets.chomp.split(//)
t = gets.chomp.split(//)
sarr, tarr = [], []
flag=true
s.zip(t).each do |i,j|
    
    if sarr.index(i) == nil and tarr.index(j) == nil
        sarr.push(i)
        tarr.push(j)
    elsif sarr.index(i) == tarr.index(j)
        next
    else
        flag = false
        break
    end
end

puts flag ? "Yes" : "No"


Measured memory_bytes: 33020
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> 
#define ff first 
#define endl "\n"
#define ss second 
#define li list<int>
#define vi vector<int>
#define ll long long int
#define pii pair<int,int>
#define vii vector<pair<int,int> >
#define mp make_pair
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define custom  pair<int,pair<int,int> >
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds; 
#define mod 1000000007
double dp[3001][3001]={0};
double solve(double* arr,int i,int n,int count){
    if(i==n){
        if(n-count>count){
        return 1;
      }
        return 0;
    }
    if(dp[i][count]>-0.9){
        return dp[i][count];
    }
    double one=arr[i]*solve(arr,i+1,n,count);
    double two=(1-arr[i])*solve(arr,i+1,n,count+1);
 //   cout<<one+two<<endl;
    return dp[i][count]=one+two;
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    double arr[n];
   
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    //base case
    for(int j=0;j<=n;j++)
        if(n-j>j)
            dp[n][j]=1;
        else
            dp[n][j]=0;
    for(int i=n-1;i>=0;i--)
        for(int j=n;j>=0;j--)
            dp[i][j] = arr[i]*dp[i+1][j] + (1-arr[i])*dp[i+1][j+1];
    cout<<fixed<<setprecision(9);
    cout<<dp[0][0];
    //double prob= solve(arr,0,n,0);
    
    return 0;
}

Measured memory_bytes: 74132
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>

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

using ll = long long;
using namespace std;

constexpr int INF = 2147483647; // 2 * 1e9
constexpr ll LLINF = 9223372036854775807; // 9 * 1e18

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	if(n <= 999) cout << "ABC\n";
	else cout << "ABD\n";
	return 0;
}


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

int main()
{
	int64_t n, m;
	cin >> n >> m;
	int64_t x[n], y[n], z[n];
	for (int i = 0; i < n; i++)
	{
		cin >> x[i] >> y[i] >> z[i];
	}
	int64_t ans = 0;
	for (int i = 0; i < (1 << 3); i++)
	{
		int64_t val[n];
		for (int j = 0; j < n; j++)
		{
			val[j] = x[j] * ((i & 1) ? -1 : 1) + y[j] * ((i & 2) ? -1 : 1) + z[j] * ((i & 4) ? -1 : 1);
		}
		sort(val, val + n);
		reverse(val, val + n);
		ans = max(ans, accumulate(val, val + m, int64_t(0)));
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

s2ss = lambda s: [int(c) for c in s.split(' ')]

def main():
    N = i2n()
    t_total = 0
    d = {}
    for _ in range(N):
        s,t = sys.stdin.readline().rstrip().split(' ')
        t_total += int(t)
        d[s] = t_total
    X = i2s()
    print(t_total - d[X])

main()





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

using namespace std;

int main() {
    int a[6], ans = 0;
    for (int i = 0; i < 6; i++) {
        cin >> a[i];
    }
    sort(a, a + 4);
    sort(a + 4, a + 6);
    for (int i = 1; i < 4; i++) {
        ans += a[i];
    }
    ans += a[5];
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1180
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int a,b;
    cin >> a >> b;
    if(a < 0){
        if(b < 0){
            if((b - a) % 2 == 0)
            cout << "Negative" << endl;
            else if( (b - a) % 2 == 1)
            cout << "Positive" << endl;
        }
        if(b >= 0)
        cout << "Zero" << endl;
    }
    if(a == 0)
    cout << "Zero" << endl;
    if(a > 0){
        cout << "Positive" << endl;
    }
}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(void){
    int M, N;
    cin >> M >> N;

    int max = 0;
    for(int i = 1; i <= M; i++){
        int sum = 0, x;
        for(int j = 1; j <= N; j++){
            cin >> x;
            sum += x;
        }
        if(sum > max) max = sum;
    }
    cout << max << endl;
}



Measured memory_bytes: 3132
#!python3

# input
H, W = list(map(int, input().split()))
a = [input() for _ in range(H)]


def main():
    r, c = [], []
    # check rows
    for i in range(H):
        if a[i] != "." * W:
            r.append(i)

    # check columns
    for j in range(W):
        for i in range(H):
            if a[i][j] == "#":
                c.append(j)
                break

    for i in r:
        for j in c:
            print(a[i][j], end="")
        print("")


if __name__ == "__main__":
    main()


Measured memory_bytes: 4468
#include <stdio.h>

int main() {
    int i, j, N, D, X, A, eaten = 0;

    scanf("%d%d%d", &N, &D, &X);
    for (i=0; i < N ;i++) {
        scanf("%d", &A);
        for (j=1; j <= D ;j+=A) {
            // printf("    %d %d\n", i, j);
            eaten++;
        }
    }
    printf("%d\n", eaten + X);
    return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
long long dp[401][401];
long long sum[401][401];

long long func(long long i,long long j,long long *a){
    if(i==j){
        return 0;
    }if(dp[i][j]!=-1){
        return dp[i][j];
    }dp[i][j]=LLONG_MAX;
    for(long long x=i;x<j;x++){
        dp[i][j]=min(func(i,x,a)+func(x+1,j,a)+sum[i][j],dp[i][j]);
    }
    return dp[i][j];

}
int main() {
	long long n;
	cin>>n;
	memset(dp,-1,sizeof(dp));
	long long a[n+1];
	for(long long i=1;i<=n;i++){
	    cin>>a[i];
	    
	}for(long long i=1;i<=n;i++){
	    for(long long j=i;j<=n;j++){
	        if(i==j){
	            sum[i][j]=a[i];
	            continue;
	        }
	        sum[i][j]=sum[i][j-1]+a[j];
	    }
	}
	cout<<func(1,n,a)<<endl;

	
	
    
}




Measured memory_bytes: 2816
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

template<int M>
struct ModInt {
public:
	using value_type = long long;
	
	ModInt(value_type val = 0) : val(val < 0 ? (M - (-val % M)) % M : val % M) {}
	
	explicit operator bool() const noexcept { return val; }
	bool operator ==(const ModInt & rhs) const noexcept { return val == rhs.val; }
	bool operator !=(const ModInt & rhs) const noexcept { return !(*this == rhs); }
	ModInt operator +() const noexcept { return ModInt(*this); }
	ModInt operator -() const noexcept { return ModInt(0) -= *this; }
	ModInt operator +(const ModInt & rhs) const noexcept { return ModInt(*this) += rhs; }
	ModInt operator -(const ModInt & rhs) const noexcept { return ModInt(*this) -= rhs; }
	ModInt operator *(const ModInt & rhs) const noexcept { return ModInt(*this) *= rhs; }
	ModInt operator /(const ModInt & rhs) const noexcept { return ModInt(*this) /= rhs; }
	
	ModInt & operator +=(const ModInt & rhs) noexcept {
		val += rhs.val;
		if (val >= M) val -= M;
		return *this;
	}
	ModInt & operator -=(const ModInt & rhs) noexcept {
		if (val < rhs.val) val += M;
		val -= rhs.val;
		return *this;
	}
	ModInt & operator *=(const ModInt & rhs) noexcept {
		val = val * rhs.val % M;
		return *this;
	}
	ModInt & operator /=(const ModInt & rhs) noexcept {
		*this *= rhs.inverse();
		return *this;
	}
	
	ModInt pow(value_type n) const {
		ModInt res = 1, x = val;
		if (n < 0) { x = x.inverse(); n = -n; }
		while (n) { if (n & 1) res *= x; x *= x; n >>= 1; }
		return res;
	}
	
	ModInt inverse() const {
		long long a = val, a1 = 1, a2 = 0, b = M, b1 = 0, b2 = 1;
		while (b > 0) {
			value_type q = a / b, r = a % b;
			value_type nb1 = a1 - q * b1, nb2 = a2 - q * b2;
			a = b; b = r;
			a1 = b1; b1 = nb1;
			a2 = b2; b2 = nb2;
		}
		assert(a == 1);
		return a1;
	}
	
	const value_type & get() const noexcept { return val; }
	static decltype(M) get_mod() noexcept { return M; }
	
	friend std::ostream & operator <<(std::ostream & os, const ModInt & rhs) { return os << rhs.val; }
	friend std::istream & operator >>(std::istream & is, ModInt & rhs) {
		value_type x;
		is >> x;
		rhs = ModInt(x);
		return is;
	}
private:
	value_type val;
};
using mint = ModInt<MOD>;

int main() {
	int N;
	cin >> N;
	vector<mint> A(N);
	REP(i, N) cin >> A[i];
	
	mint sum = 0;
	REP(i, N) sum += A[i];
	mint ans = sum * sum;
	REP(i, N) ans -= A[i] * A[i];
	ans /= 2;
	cout << ans << endl;
	
	return 0;
}


Measured memory_bytes: 4852
"""
DFS（深さ優先探索）
"""


def main():
    S = input()
    N = len(S)

    def dfs(i, s):
        if i == N - 1:
            return sum(map(int, s.split('+')))
        return dfs(i + 1, s + S[i + 1]) + dfs(i + 1, s + '+' + S[i + 1])

    print(dfs(0, S[0]))
    return


if __name__ == '__main__':
    main()






Measured memory_bytes: 9100
#include<iostream>
#include<stdio.h>
#include<fstream>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<set>
#include<string>
#include<queue>
#include<map>
#include<iomanip>
#include<cassert>
#include<complex>
#include<functional>
#include<list>
#include<cmath>
#include<stack>
#include<assert.h>
#define LL long long
#define PB push_back
#define MP make_pair
#define PII pair<int,int>
#define F first
#define S second
using namespace std;
int n,a;
int main(){
  cin>>n>>a;
  cout<<(n+a-1)/a<<"\n";
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h> 
using namespace std; 
const int mod = 998244353;
int dp[3001][3000];

int modu(int a){
	return ((a%mod)+mod)%mod;
}
long long modu(long long a){
	return ((a%mod)+mod)%mod;
}
int main() 
{ 
	
	int N,S;
	cin>>N>>S;
	vector<int>numbers(N);
	for(int i = 0;i<N;i++){
		cin>>numbers[i];
	}
	if(numbers[0]<=S){
		dp[numbers[0]][0] = 1;
	}
	dp[0][0] =1;
	long long ways = 0;
	
    for(int i =1;i<N;i++){
    	dp[0][i] = 1;
    	if(numbers[i]<=S){
	    	dp[numbers[i]][i] = modu(dp[numbers[i]][i] + i+1);
	    }
		for(int j=1;j<=S;j++){
			dp[j][i] = modu(dp[j][i]+dp[j][i-1]);
		}
    	for(int j=1;j<=S;j++){
    		
    		if(numbers[i]+j>S){
    			break;
			}
			else{
				dp[numbers[i]+j][i] = modu(dp[j][i-1] + dp[numbers[i]+j][i]);
			
			}
		}
		
	}
//	for(int i =0;i<=S;i++){
//		for(int j =0;j<N;j++){
//			cout<<dp[i][j]<<" ";
// 		}
//		cout<<endl;
//	}
	for(int j =1;j<N;j++){
		ways = modu(ways + dp[S][j]);
	}
	
    cout<<ways<<endl;
    return 0; 
} 


Measured memory_bytes: 35456
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vvp = vector<vector<pair<int,int>>>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}




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

    int N;
    cin >> N;
    vl T(N),A(N);
    in(N,T);
    in(N,A);
    
    vl t(N,-1),a(N,-1);
    t[0] = T[0];
    a[N-1] = A[N-1];
    
    
    reps(i,1,N){
        if(T[i-1] < T[i]) t[i] = T[i];
    }
    for (int i = N-1; i > 0; i--) {
        if(A[i] < A[i-1]) a[i-1] = A[i-1];
    }
    
    //outve(t);
    //outve(a);
    
    ll res = 1;
    
    
    rep(i,N){
        if(t[i] != -1 || a[i] != -1){
            if(t[i] != -1 && a[i] != -1) if(t[i] != a[i]) res = 0;
            
            if(t[i] == -1) if(T[i] < a[i]) res = 0;
            if(a[i] == -1) if(A[i] < t[i]) res = 0;

        }else{
            res *= min(A[i],T[i]);
            res %= mod;
            
        }

    }
    
    cout << res << endl;
    
    
    
    

    return 0;
}


Measured memory_bytes: 3456
N, K = map(int, input().split())
A = list(map(int, input().split()))
# A.sort()
totalA = sum(A)
# print (totalA)

def make_divisors(n): #nの約数をO(root(n))で全列挙、sortして取り出すときはO(root(n) log(n))
    import math
    divisors = []
    for i in range(1, int(math.sqrt(n)) + 1):
        if n % i == 0: #割り切れるとき
            divisors.append(i)
            if i != n//i:
                divisors.append(n//i)
    divisors.sort(reverse = True)
    return divisors

divisors_lst = make_divisors(totalA)
# print (divisors_lst)
for i in divisors_lst: #答えの候補を全探索
    lst = []
    for j in A:
        lst.append(j % i)
    lst.sort()
    total_lst = 0
    for j in lst:
        total_lst += i - j
    tmp = 0
    for k in lst:
        tmp += k
        total_lst -= (i - k)
        if max(tmp, total_lst) <= K:
            print (i)
            exit()


Measured memory_bytes: 3188
#include<stdio.h>


int main(void){

  int n,m,i,j,an;

  scanf(" %d %d",&n,&m);

  int a[n][m];
  int b[m];

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

  for(j=0;j<m;j++){
    scanf("%d",&b[j]);
  }


  for(i=0;i<n;i++){
    an=0;
    for(j=0;j<m;j++){
      an+=a[i][j]*b[j];
    }
    printf("%d\n",an);
  }


  return 0;
}

Measured memory_bytes: 1812
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    
    public static void main(String[] args) {
        
        var sc = new Scanner(System.in);
    
        int D = Integer.parseInt(sc.next());
        var c = new int[26];
        for(int i = 0; i < 26; i++){
            c[i] = Integer.parseInt(sc.next());
        }
        var s = new int[D][26];
        for(int d = 0; d < D; d++){
            for(int i = 0; i < 26; i++){
                s[d][i] = Integer.parseInt(sc.next());
            }
        }
        var t = new int[D];
        for(int d = 0; d < D; d++){
            t[d] = Integer.parseInt(sc.next()) -1;
        }
        
        long score = 0;
        var last = new int[26];
        Arrays.fill(last, -1);
        var sb = new StringBuilder();
        for(int d = 0; d < D; d++){
            score += s[d][t[d]];
            last[t[d]] = d;
            for(int i = 0; i < 26; i++){
                score -= c[i] * (d - last[i]);
            }
            sb.append(score).append(System.lineSeparator());
        }
        System.out.print(sb);
    }
}

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

int main() {
  int sx, sy, tx, ty, i;
  cin >> sx >> sy >> tx >> ty;

  // s -> t 1st
  for (i=0; i < tx-sx; i++) {cout << "R";}
  for (i=0; i < ty-sy; i++) {cout << "U";}
    
  // t -> s 1st
  for (i=0; i < tx-sx; i++) {cout << "L";}
  for (i=0; i < ty-sy; i++) {cout << "D";}
    
  // s -> t 2nd
  cout << "D";
  for (i=0; i < tx-sx+1; i++) {cout << "R";}
  for (i=0; i < ty-sy+1; i++) {cout << "U";}
  cout << "L";
    
  // t -> s 2nd
  cout << "U";
  for (i=0; i < tx-sx+1; i++) {cout << "L";}
  for (i=0; i < ty-sy+1; i++) {cout << "D";}
  cout << "R";
    
  return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>
using ll = long long;
using itn = int;
using namespace std;
int main(){
    int a,b;
    cin >> a >> b;
    int diff=b-a;
    int fact=0;
    for(int i=1; i<diff; i++){
        fact+=i;
    }
    fact -= a;
    cout << fact << endl;

    return 0;
}


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

using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int> p;
	copy_n(istream_iterator<int>(cin), n, back_inserter(p));
	
	int count = 0;
	for (int i = 1; i < n - 1; ++i)
	{
		if (p[i] > p[i - 1] && p[i] < p[i + 1])
			count++;
		else if (p[i] < p[i - 1] && p[i] > p[i + 1])
			count++;
	}
	
	cout << count << '\n';
}


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

#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); } 
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
#define ll long long

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

    ll s,w;
    cin>>s>>w;
    if(s>w)
        cout<<"safe";
    else 
        cout<<"unsafe";
    return 0;
}

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

int main() {
  int x;
  cin >> x;
  int n500 = x/500;
  x %= 500;
  int n5 = x/5;
  cout << 1000*n500 + 5*n5 << endl;
  return 0;
}


Measured memory_bytes: 3616
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include <boost/multiprecision/cpp_int.hpp>
//typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

struct mini{ ll a,kosu;};
struct maxi{ ll a,kosu;};
            
int main(){
    ll n,t;cin>>n>>t;
    ll a[n];rep(i,0,n-1)cin>>a[i];
    mini m;m.a=inf;m.kosu=0;
    maxi M;M.a=-1;M.kosu=0;
    rep(i,0,n-1){
        if(a[i]-m.a==M.a){
            M.kosu+=m.kosu;
        }else if(a[i]-m.a>M.a){
            M.kosu=m.kosu;
            M.a=a[i]-m.a;
        }
        if(a[i]==m.a){
            m.kosu++;
        }else if(a[i]<m.a){
            m.a=a[i];
            m.kosu=1;
        }
        
    }
    cout<<M.kosu<<endl;
    
    return 0;
}


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

#define NUM 250000

int main(void) {// think about up to 250000
	
	int p[NUM+1], i, j, k, lim, sum[NUM];
	
	for(i=2;i<=NUM;i++){
		p[i]=1;
	}
	
	lim=(int)sqrt((double)NUM);
	
	for(i=2;i<=lim;i++){
		if(p[i]==1){
			for(j=2*i;j<=NUM;j++){
				if(j%i==0)
				p[j]=0;
			}
		}
	}
	
	sum[1]=0;
	for(i=2;i<NUM;i++){
		sum[i]+=sum[i-1];
		
		if(p[i])
		sum[i]++;
	}
	
	while(1){
		scanf("%d", &k);
		
		if(k==0)
		break;
		
			printf("%d\n", sum[k*2]-sum[k]);
	}


	return 0;
}

Measured memory_bytes: 2552
#include <stdio.h>

int gcd(int a, int b, int c);

int main(void)
{
    int K,a,b,c;
    long long sum=0;

    scanf("%d",&K);

    for(a=1; a<=K; a++){
        for(b=1; b<=K; b++){
            for(c=1; c<=K; c++){
                sum+=gcd(a,b,c);
            }
        }
    }

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

    return 0;
}

int gcd(int a, int b, int c){
    int tmp;
    if(a < b) {
        tmp = a;
        a = b;
        b = tmp;
    }
    int r = a%b;
    while(r!=0){
        a = b;
        b = r;
        r = a%b;
    }

    if(c < b) {
        tmp = c;
        c = b;
        b = tmp;
    }
    r = c%b;
    while(r!=0){
        c = b;
        b = r;
        r = c%b;
    }

    return b;
}

Measured memory_bytes: 1732
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <cassert>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define srep(i,n,m) for(int i=n;i<m;i++)
#define elif else if
#define INF 1000000007
#define pi 3.141592653589793
using namespace std;
int saidaikouyakusuu(int a,int b){
    if(b==0) return a;
    return saidaikouyakusuu(b,a%b);
}
int saisyoukoubaisuu(int a,int b){
    return a*b/saidaikouyakusuu(a,b);
}
int n,m,cnt=0,aa[220000],ab[40],ac[40],o,p;
pair<int,int> pp[220000];
string s,ss[220000];
char c;
queue<int> que;
signed main(){
    cin>>n;
    rep(i,n){
        cin>>aa[i];
        m=aa[i];
        rep(j,32){
            if(m%2==1){
                ab[j]=(ab[j]+1)%2;
            }
            m/=2;
        }
    }
    rep(i,n){
        cnt=0;
        rep(j,32){
            if(aa[i]%2==1){
                cnt+=pow(2,j)*((ab[j]+1)%2);
            }else{
                cnt+=pow(2,j)*ab[j];
            }
            aa[i]/=2;
        }
        cout<<cnt<<" ";
    }
}


Measured memory_bytes: 12576
//g++ -std=c++11 -Wall test.cpp -o test -lm -g -O2
#include <bits/stdc++.h>
using namespace std;
#define sws                           \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define endl '\n'
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define qi queue<int>
#define si stack<int>
#define s string
#define ll long long;
typedef struct
{
    int start;
    int end;
    int pos;
} task_t;
bool organiza(pair<char, int> a, pair<char, int> b)
{
    return a.ss < b.ss;
}
bool compara(task_t a, task_t b)
{
    return a.start < b.start;
}
void mostra(vector<pair<char, int>> v)
{
    for (int i = 0; i < (int)v.size(); i++)
    {
        cout << v[i].ff;
    }
    cout << endl;
}
int main()
{
    sws;
    int n;
    cin >> n;
    int i = n;
    while (true)
    {
        if (i % 2 == 0 && i % n == 0)
            break;
        i++;
    }
    cout << i << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> ii;
const int MAXN = 1e5 + 8;
 
 
priority_queue<ii, vector<ii>, greater<ii> > pq[MAXN];
int val[MAXN];
int fa[MAXN], _rank[MAXN];
inline void init(int n){
    for(int i = 0; i <= n; i++){
        fa[i] = i;
        _rank[i] = 0;
    }
}
inline int _find(int u){
    return fa[u] = fa[u] == u ? u : _find(fa[u]);
}
inline void _merge(int u, int v){
    int x = _find(u), y = _find(v);
    if(x == y) return;
    if(_rank[x] < _rank[y]){
        fa[x] = y;
    }
    else{
        fa[y] = x;
        if(_rank[x] == _rank[y]) _rank[x]++;
    }
}
 
vector<ii> vec;
int main()
{
    #ifdef LOCAL
    freopen("d.txt", "r", stdin);
    //freopen("d.out", "w", stdout);
    int T = 1;
    while(T--){
    #endif 
    ios::sync_with_stdio(false); cin.tie(0);
 
    int n, m, i, u, v, findu, findv;
    LL ans = 0;
    cin >> n >> m;
    init(n);
    for(i = 0; i < n; i++){
        cin >> val[i];
    }
    for(i = 0; i < m; i++){
        cin >> u >> v;
        findu = _find(u), findv = _find(v);
        if(findu != findv){
            _merge(findu, findv);
        }
    }
    for(i = 0; i < n; i++){
        pq[_find(i)].push(ii(val[i], i));
    }
    for(i = 0; i < n; i++){
        if(!pq[i].empty()){
            vec.push_back(pq[i].top());
            val[pq[i].top().second] = 2e9;
            ans += pq[i].top().first;
        }
    }
    if(vec.size() == 1){
        cout << 0 << endl;
    }
    else if(n < (vec.size()-1) * 2) cout << "Impossible" << endl;
    else{
        sort(val, val + n);
        n = (vec.size()-1) * 2 - vec.size();
 
        for(i = 0; i < n; i++){
            ans += val[i];
        }
        cout << ans << endl;
    }
 
 
 
    #ifdef LOCAL
    cout << endl;
    }
    #endif 
    return 0;
}

Measured memory_bytes: 8828
#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<vector>
//#include<unordered_map> 
#include<map>
using namespace std ;
typedef long long ll ;

int main(){
    ll a ;
   cin >> a ;
   cout << 2*(long double)a*3.141592 << endl  ;
   
    return 0 ;
}

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

using namespace std;

int main(){
int n,m;cin>>n>>m;
priority_queue <long long> a;
for(int i=0;i<n;i++){
    long long b;
    cin>>b;
    a.push(b);
}
while(m--){
    long long c=a.top()/2;
    a.pop();
    a.push(c);
}
long long ans=0;
while(!a.empty()){
    ans+=a.top();
    a.pop();
}
cout<<ans;
}

Measured memory_bytes: 1400
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

long long f(long long b,long long n)
{return b>n?n:f(b,n/b)+n%b;}

int main()
{
	long long n,s,m;
	scanf("%lld%lld",&n,&s);
	if(n==s)
	{
		printf("%lld\n",n+1);
		return 0;
	}
	if(n<s)
	{
		puts("-1");
		return 0;
	}
	m=sqrt(n);
	for(long long b=2;b<=m;b++)
		if(f(b,n)==s)
		{
			printf("%lld\n",b);
			return 0;
		}
	for(long long p=m-1,b=(p>=1?(n-s)/p+1:0);p>=1;p--,b=(p>=1?(n-s)/p+1:0))
		if((n-s)%p==0&&n/b==p&&p<=s)
		{
			printf("%lld\n",b);
			return 0;
		}
	if(s==1)
	{
		printf("%lld\n",n);
		return 0;
	}
	puts("-1");
	
	return 0;
}


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

// use example: mergeSort(arr1,0,n-1)
// in the example 0 and n-1 are included in the sorting

void merge(int arr[], int l, int m, int r) { 
    int i, j, k; 
    int n1 = m - l + 1; 
    int n2 =  r - m; 
    /* create temp arrays */
    int L[n1], R[n2]; 
    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++) 
        L[i] = arr[l + i]; 
    for (j = 0; j < n2; j++) 
        R[j] = arr[m + 1+ j]; 
    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray 
    j = 0; // Initial index of second subarray 
    k = l; // Initial index of merged subarray 
    while (i < n1 && j < n2) { 
        if (L[i] <= R[j]) { 
            arr[k] = L[i]; 
            i++; 
        } else {
            arr[k] = R[j]; 
            j++; 
        } 
        k++; 
    } 
    /* Copy the remaining elements of L[], if there 
       are any */
    while (i < n1) { 
        arr[k] = L[i]; 
        i++; 
        k++; 
    } 
    /* Copy the remaining elements of R[], if there 
       are any */
    while (j < n2) {         
        arr[k] = R[j]; 
        j++; 
        k++; 
    } 
} 
/* l is for left index and r is right index of the 
   sub-array of arr to be sorted */
void mergeSort(int arr[], int l, int r) { 
    if (l < r) { 
        // Same as (l+r)/2, but avoids overflow for 
        // large l and h 
        int m = l+(r-l)/2; 
        // Sort first and second halves 
        mergeSort(arr, l, m); 
        mergeSort(arr, m+1, r); 
        merge(arr, l, m, r); 
    } 
} 

int main() {
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    mergeSort(arr,0,n-1);
    
    float res = arr[0];
    for(int i=1;i<n;i++){
        res += arr[i];
        res /=2;
    }
    
    printf("%f",res);
}

Measured memory_bytes: 256
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    AB = [[int(x) for x in input().split()] for _ in range(N)]

    prev = 0
    for i in range(N - 1, -1, -1):
        a, b = AB[i]
        x = -(-(a + prev) // b)
        prev += x * b - (a + prev)

    print(prev)


if __name__ == '__main__':
    main()


Measured memory_bytes: 21108
def forbidden_list():
    # 入力
    X, N = map(int, input().split())
    P = list(map(int, input().split()))
    # 初期処理
    min_abs_list = list()
    # N = 0 の場合
    if N == 0:
        return X
    # X + i
    i = 0
    while True:
        if X + i in P:
            i += 1
        else:
            min_abs_list.append(X + i)
            break
    # X - i
    i = 0
    while True:
        if X + i in P:
            i -= 1
        else:
            min_abs_list.append(X + i)
            break
    # X との絶対値が最小のもの
    if abs(X - min_abs_list[0]) > abs(X - min_abs_list[1]):
        return min_abs_list[1]
    elif abs(X - min_abs_list[0]) == abs(X - min_abs_list[1]):
        return min(min_abs_list)
    elif abs(X - min_abs_list[0]) < abs(X - min_abs_list[1]):
        return min_abs_list[0]

result = forbidden_list()
print(result)

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

#define ll long long 
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) (x).begin(), (x).end()

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	string s; cin >> s;
	int n = s.length();
	bool ok = true;
	for(int i = 0, j = n - 1; i <= j; i++, j--){
		if(s[i] != s[j]){
			ok = false; break;
		}
	}
	for(int i = 0, j = (n - 1) / 2 - 1; i <= j; i++, j--){
		if(s[i] != s[j]){
			ok = false;
			break;
		} 
	}
	if(ok) cout << "Yes\n";
	else cout << "No\n";
	return 0; 	
}

Measured memory_bytes: 256
#include <iostream>
#include <set>
#define llint long long
 
using namespace std;
 
llint n;
llint a[10005], b[10005];
set<llint> S;
 
int main(void)
{
	cin >> n;
	for(llint i = n; i >= 0; i--) cin >> b[i];
	
	llint k = 0;
	for(llint i = 0; i <= n; i++){
		if(b[i]){
			k = i;
			break;
		}
	}
	for(llint i = 0; i <= n-k; i++) a[i] = b[i+k];
	n -= k;
	
	llint m = abs(a[0]);
	for(llint i = 1; i*i <= m; i++){
		if(m%i) continue;
		S.insert(i), S.insert(m/i);
	}
	for(int i = 1; i <= n+1; i++) S.insert(i);
	
	for(auto p : S){
		bool flag = true;
		if(p == 1) flag = false;
		for(llint i = 2; i*i <= p; i++){
			if(p % i == 0){
				flag = false;
				break;
			}
		}
		if(!flag) continue;
		
		for(llint i = 0; i <= n; i++){
			b[i] = a[i];
			if(b[i] < 0) b[i] = p - (-b[i]) % p;
			b[i] %= p;
		}
		for(llint i = n; i >= p-1; i--){
			if(b[i] == 0) continue;
			if(k == 0 && i == p-1) continue;
			b[i-(p-1)] += b[i], b[i-(p-1)] %= p;
			b[i] = 0;
		}
		
		flag = true;
		for(llint i = 0; i <= n; i++){
			if(b[i]) flag = false;
		}
		if(flag) cout << p << endl;
	}
	return 0;
}

Measured memory_bytes: 896
def main():
    x = int(input())
    ans = 1
    for b in range(x, 1, -1):
        p = 2
        ans_temp = -float("inf")
        while True:
            if b**p > x:
                break
            ans_temp = b**p
            p += 1
        ans = max(ans, ans_temp)
    print(ans)


if __name__ == "__main__":
    main()


Measured memory_bytes: 2940
object Main {
  def main(args:Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    var result = "Yes"
    val s = sc.next
    for(i <- 0 to s.length-1) {
      (i%2, s(i)) match {
        case (1, 'R') => result="No"
        case (0, 'L') => result="No"
        case _        => ()
      }
    }
    println(result)
  }
}


Measured memory_bytes: 25552
import bisect

A, B, Q = map(int, input().split())
INF = 10 ** 12
s_list = [-INF] + [int(input()) for _ in range(A)] + [INF]
t_list = [-INF] + [int(input()) for _ in range(B)] + [INF]

for _ in range(Q):
    x = int(input())
    s_right_index = bisect.bisect_right(s_list, x)
    s_right = s_list[s_right_index]
    s_left = s_list[s_right_index - 1]
    t_right_index = bisect.bisect_right(t_list, x)
    t_right = t_list[t_right_index]
    t_left = t_list[t_right_index - 1]

    dist = INF
    for s in [s_right, s_left]:
        for t in [t_right, t_left]:
            dist = min(dist, abs(s - t) + abs(s - x), abs(t - s) + abs(t - x))
    print(dist)


Measured memory_bytes: 12800
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Atcoder
{
    class Atcoderrrrrrr
    {
        static void Main(string[] args)
        {
            string[]s = Console.ReadLine().Split(' ');
            int a = int.Parse(s[0]);int b = int.Parse(s[1]);int c = int.Parse(s[2]);int d = int.Parse(s[3]);
        
            Console.WriteLine(
                a*b>=c*d? a*b:c*d
            );
        }
    }
}


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

namespace CSharpSample01
{


    class Program
    {

        const int INF = 1000000000;

        static int Solve(int N, int K, int[] h)
        {
            int[] dp = new int[N];

            dp[0] = 0;

            for(int i = 1; i < N; i++)
            {
                dp[i] = INF;
                for(int k = 1; k <= K; k++)
                {
                    if (i - k < 0) break;
                    dp[i] = Math.Min(dp[i], dp[i - k] + Math.Abs(h[i] - h[i - k]));
                }
            }

            return dp[N - 1];
        }


        
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            int N = int.Parse(input[0]);
            int K = int.Parse(input[1]);

            int[] h = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

            Console.WriteLine(Solve(N, K, h));
        }
    }
}

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

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define all(x) (x).begin(),(x).end()
#define S(x) (int)(x).size()
#define L(x) (int)(x).length()

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> ordered_set;

const int mod = 998244353;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
/*

.***
....
*...

*/

ll expo(ll x, ll n)
{
	ll res=1;
	while(n)
	{
		if(n&1)
			res =(res*x)%mod;
		x =(x*x)%mod;
		n >>= 1;
	}
	return res;
}

int dp[3009][3009];

void solve()
{
	int a,b,c,d,i,j;
	cin>>a>>b>>c>>d;
	dp[a][b]=1;
	for(i=a;i<=c;i++)
	for(j=b;j<=d;j++)
	{
		if(i==a && j==b) continue;
		dp[i][j]=(0LL+ 1LL*i*dp[i][j-1]+1LL*j*dp[i-1][j]-1LL*(i-1)*(j-1)*dp[i-1][j-1])%mod;
		if(dp[i][j]<0) dp[i][j]+=mod;
	}
	cout<<dp[c][d]<<'\n';
}

int main()
{
	IOS
	int t=1;
	//cin>>t;
	while(t--)
	{
		solve();
	}
}


Measured memory_bytes: 38844
a=int(input())
b=input().split()
c=int(b[0])
d=int(b[1])
if d-c>=a :
  print('OK')
else :
  for i in range(c,d+1) :
    flag=False
    if i%a==0 :
      flag=True
      break
  if flag :
      print('OK')
  else :
      print('NG')

Measured memory_bytes: 61996
def sigmaN(n: int) -> int:
    """sigma{i=1}{n} i = n * (n+1) / 2
    """
    return n * (n+1) // 2


def xor_sum_2(N: int, A: list) -> int:
    """Al + ... + Ar = Al ^ ... ^ Ar
    が成り立つとき、l <= i <= j <= r を満たす全ての
    (i, j) の組について
    Ai + ... + Aj = Ai ^ ... ^ Aj
    が成り立つ。
    また、
    Al + ... + Ar != Al ^ ... ^ Ar
    のとき、
    A{l-1} + Al + ... + Ar != A{l-1} ^ Al ^ ... ^ Ar
    Al + ... + Ar + A{r+1} != Al ^ ... ^ Ar ^ A{r+1}
    が成り立つ。
    したがって、まず l を固定して等式が成り立たなくなるまで r
    を増やしていき、成り立たなくなったら l を一つ進める、という
    ことを繰り返す尺取り法で O(N) になる。
    """
    r = 0              # 右側の上限
    Sadd, Sxor = 0, 0  # +/^ の累積
    count = 0          # (l, r) の組み合わせ数
    overlapped = 0     # 以前に計算済みの部分列との積集合の大きさ
    for l in range(N):
        al = A[l]
        if Sadd != Sxor:
            # l を進めるときは A[l] の削除と重複部分の削除を行う
            Sadd, Sxor = Sadd - al, Sxor ^ al
            overlapped -= 1
            continue

        # 重複して数えてしまう部分の削除
        count -= sigmaN(overlapped-1)

        # r を限界まで伸ばす。
        while r < N and Sadd == Sxor:
            ar = A[r]
            Sadd, Sxor = Sadd + ar, Sxor ^ ar
            overlapped += 1
            r += 1

        if Sadd == Sxor:
            # r を限界まで伸ばして Sadd == Sxor が保たれるということは
            # r == N であり、ここから l を進めても全部重複部分なので
            # 計算する必要がなく、終了
            count += sigmaN(r - l)
            break
        else:
            # r を伸ばすとき、r は Sadd != Sxor になるまで伸ばされていて
            # 結果的に一つ余計なものを含んでいるので、それを削除する
            overlapped -= 1
            count += sigmaN((r-1) - l)

        # l を進めるにあたり、最左の A[l] は削除する必要がある
        Sadd, Sxor = Sadd - al, Sxor ^ al

    return count


if __name__ == "__main__":
    N = int(input())
    A = [int(s) for s in input().split()]
    ans = xor_sum_2(N, A)
    print(ans)


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

// input
int H, W;
vector<string> S;

vector<int> w;
vector<bool> l;


void input() {
    cin >> H >> W;
    S = vector<string>(H);
    rep(i, H) cin >> S[i];
}


int to_id(int x, int y) {
    return W * x + y;
}


void solve() {
    w = vector<int>(H * W, -1);
    l = vector<bool>(H * W, false);
    w[0] = 1;
    l[0] = true;
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        int x = v / W;
        int y = v % W;
        if (x > 0) {
            int i = to_id(x - 1, y);
            if (S[x - 1][y] == '.' && !l[i]) {
                w[i] = w[v] + 1;
                l[i] = true;
                q.push(i);
            }
        }
        if (x < H - 1) {
            int i = to_id(x + 1, y);
            if (S[x + 1][y] == '.' && !l[i]) {
                w[i] = w[v] + 1;
                l[i] = true;
                q.push(i);
            }
        }
        if (y > 0) {
            int i = to_id(x, y - 1);
            if (S[x][y - 1] == '.' && !l[i]) {
                w[i] = w[v] + 1;
                l[i] = true;
                q.push(i);
            }
        }
        if (y < W - 1) {
            int i = to_id(x, y + 1);
            if (S[x][y + 1] == '.' && !l[i]) {
                w[i] = w[v] + 1;
                l[i] = true;
                q.push(i);
            }
        }
    }
}


int main() {
    input();
    solve();
    int s = 0;
    rep(i, H) s += count(S[i].begin(), S[i].end(), '.');
    int ans;
    if (w[H * W - 1] == -1) ans = -1;
    else ans = s - w[H * W - 1];
    cout << ans << endl;
}


Measured memory_bytes: 3564
// Copyright 2020 yaito3014
#include <bits/stdc++.h>
using namespace std;

struct Cor {
  int x, y;
  Cor() = default;
  Cor(int x_, int y_) : x(x_), y(y_) {}
};

bool operator==(const Cor& lhs, const Cor& rhs) {
  return lhs.x == rhs.x && lhs.y == rhs.y;
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int H, W;
  cin >> H >> W;
  vector<string> s(H);
  for (auto& row : s)cin >> row;
  constexpr int INF = INT_MAX;
  vector<vector<int>> costs(H, vector<int>(W, INF));
  queue<Cor> que;
  que.push(Cor{ 0,0 });
  costs[0][0] = (s[0][0] == '#' ? 1 : 0);
  while (!que.empty()) {
    Cor cur = que.front();
    que.pop();
    // cerr << cur.x << "," << cur.y << " : " << costs[cur.y][cur.x] << "\n";
    if (cur == Cor{ W - 1,H - 1 })break;
    if (cur.x + 1 < W) {
      int cost = s[cur.y][cur.x] != '#' && s[cur.y][cur.x + 1] == '#' ? 1 : 0;
      // cerr << "[R] " << s[cur.y][cur.x] << " -> " << s[cur.y + 1][cur.x] << " : " << cost << "\n";
      if (costs[cur.y][cur.x + 1] > costs[cur.y][cur.x] + cost) {
        costs[cur.y][cur.x + 1] = costs[cur.y][cur.x] + cost;
        que.push(Cor{ cur.x + 1, cur.y });
      }
    }
    if (cur.y + 1 < H) {
      int cost = s[cur.y][cur.x] != '#' && s[cur.y + 1][cur.x] == '#' ? 1 : 0;
      // cerr << "[D] " << s[cur.y][cur.x] << " -> " << s[cur.y + 1][cur.x] << " : " << cost << "\n";
      if (costs[cur.y + 1][cur.x] > costs[cur.y][cur.x] + cost) {
        costs[cur.y + 1][cur.x] = costs[cur.y][cur.x] + cost;
        que.push(Cor{ cur.x , cur.y + 1 });
      }
    }
  }
  cout << costs[H - 1][W - 1] << endl;
}


Measured memory_bytes: 384
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdio>
#include<set>

using namespace std;

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


const lint MOD = 1000000007;
lint fib[10];

inline void logger(){ cout << endl; }
template<typename A, typename... B>
void logger(const A& a, const B&... b){
    cout << a << " , ";
    logger(b...);
}


lint dum(lint L){
    if(L>=0) return fib[L] % MOD;
    else return 0;
}
int main(){
    fib[0] = 1; fib[1] = 1;
    repn(i, 8) fib[i+1] = fib[i] + fib[i-1];
    int H, W, K; cin>>H>>W>>K;
    vector<int> rl; int ch = K-1;
    while(ch <= H) {rl.push_back(ch); ch+=2;}

    lint dp[H+1][H+1][H+1];
    rep(i, H+1) rep(j, H+1) dp[i][j][0]=0;
    dp[0][0][0]=1;

    repn(k, H) rep(i, H+1) rep(j, H+1){
        if(i+j>k || (i<j || i-j>=W) ) { dp[i][j][k]=0; continue; }
        lint A = dum(i-j-1)*dum(W-i+j-1) % MOD;
        lint B = dum(i-j)*dum(W-i+j-2) % MOD;
        lint C = dum(i-j)*dum(W-i+j-1) % MOD;

    // 適宜MODを取る?
        if(i == 0 && j==0) dp[i][j][k] = dp[i][j][k-1]*C;
        else if(i==0) dp[i][j][k] = (dp[i][j-1][k-1]*B)%MOD + (dp[i][j][k-1]*C)%MOD;
        else if(j==0) dp[i][j][k] = (dp[i-1][j][k-1]*A)%MOD + (dp[i][j][k-1]*C%MOD);
        else dp[i][j][k] = (dp[i-1][j][k-1]*A%MOD) + (dp[i][j-1][k-1]*B%MOD) + (dp[i][j][k-1]*C%MOD);
 
        dp[i][j][k] %= MOD;
    }


    lint ans = 0;
    for(auto& ch : rl){
        int R = (ch+K-1)/2; int L =(ch-K+1)/2;
        ans += dp[R][L][H];
        ans %= MOD;
    }

    cout<<ans<<endl;
}

Measured memory_bytes: 8320
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <functional>
#include <iostream>
#define INF 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n,m;
ll l;
int k[2001];
ll s[1000001];
ll sum[2002];
ll dp[2002];

int main(void){
	scanf("%d%d%lld",&n,&m,&l);
	for(int i=0;i<n;i++){
		scanf("%d",&k[i]);
	}
	for(int i=0;i<m;i++){
		scanf("%lld",&s[i]);
	}
	for(int i=0;i<m;i++){
		sum[i+1]=sum[i]+s[i];
	}
	sort(k,k+n);
	for(int i=0;i<n;i++){
		dp[i]=INF*20000LL;
	}
	dp[0]=(sum[k[1]]-sum[k[0]-1])/l;
	for(int i=2;i<n;i++){
		for(int j=0;j<i-1;j++){
			dp[i-1]=min(dp[i-1],dp[j]+(sum[k[i]]-sum[k[j]-1])/l);
		}
		for(int j=0;j<i-1;j++){
			dp[j]=dp[j]+(sum[k[i]]-sum[k[i-1]-1])/l;
		}
	}
	ll res=INF*20000LL;
	for(int i=0;i<n-1;i++){
		res=min(res,dp[i]+(sum[k[n-1]]-sum[k[i]-1])/l);
	}
	printf("%lld\n",res);
	return 0;
}

Measured memory_bytes: 4072
s = gets.chop

def count(s, init)
  sum = 0
  c = init
  s.size.times { |i|
    if c != s[i]
      sum += 1
    end
    c = c == "0" ? "1" : "0"
  }
  return sum
end

x = count(s,"0")
y = count(s,"1")
r = x > y ? y : x
p r

Measured memory_bytes: 3964