akhauriyash/Code-Regression huggingface dataset:
// Math = ♥
// Sometimes it is people that no one imagines anything of who do things that no one can imagine
// After all this time? Always

#include <bits/stdc++.h>                        // uncomment before submission
//#include <ext/pb_ds/assoc_container.hpp>		// uncomment before submission
//#include <ext/pb_ds/tree_policy.hpp>			// uncomment before submission
//using namespace __gnu_pbds;					// uncomment before submission
using namespace std;
//<---------------------------------------------------Template----------------------------------------------------------->
#define int long long
#define ll long long
#define ld long double
const int INF = 1e15 + 7;
const int MAX = 1e5 + 7;
const int MOD = 1e9 + 7;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;                  // Vector of long long
typedef vector<vi> vvi;                 // Vector of vi
typedef vector<ii> vii;                 // Vector of pairs
typedef vector<vii> vvii;               // Vector of Vector of pairs
typedef vector<bool> vb;                // Vector of bool
#define pq priority_queue               // Max heap (To convert to min heap, use negative sign before every value)
#define ff first                        // For pairs
#define ss second                       // For pairs
#define pb push_back                    // Pushback to vector
#define mp make_pair                    // Makes pairs to be stored as pair
#define all(c) (c).begin(), (c).end()   // Mainly used by me in sorting
// ordered_set adds two new functions to set - (set).find_by_order([kth element based on zero indexing]) and order_of_key()
// order_of_key returns number of elements less that parameter. If element exists, that order is its index
#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//<----------------------------------------------------------------------------------------------------------------------->

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    int ans = (n * (n - 1)) / 2 + (m * (m - 1)) / 2;
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 384
# abc125_a.py
# https://atcoder.jp/contests/abc125/tasks/abc125_a

# A - Biscuit Generator /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 100点

# 問題文
# ビスケット生産装置を起動すると、起動してから A秒後、2A 秒後、3A 秒後、... (A の倍数秒後) に B枚ずつビスケットを生産します。
# ビスケット生産装置を起動してから T+0.5秒後までに生産されるビスケットの合計枚数を求めてください。

# 制約
#     入力は全て整数である。
#     1≤A,B,T≤20

# 入力
# 入力は以下の形式で標準入力から与えられる。
# A B T

# 出力
# ビスケット生産装置を起動してから T+0.5秒後までに生産されるビスケットの合計枚数を出力せよ。

# 入力例 1
# 3 5 7

# 出力例 1
# 10

#     ビスケット生産装置を起動してから 3秒後に 5枚のビスケットが生産されます。
#     ビスケット生産装置を起動してから 6秒後に 5枚のビスケットが生産されます。
#     したがって、ビスケット生産装置を起動してから 7.5秒後までに合計 10枚のビスケットが生産されます。

# 入力例 2
# 3 2 9

# 出力例 2
# 6

# 入力例 3
# 20 20 19

# 出力例 3
# 0


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # line = lines[0]
    # N = int(lines[0])
    A, B, T = list(map(int, lines[0].split()))
    # values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[1].split()))
    # values = list()
    # for i in range(6):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(Q):
    #     valueses.append(list(map(int, lines[i+1].split())))

    result = 0

    result = (T//A) * B

    return [result]


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['3 5 7']
        lines_export = [10]
    if pattern == 2:
        lines_input = ['3 2 9']
        lines_export = [6]
    if pattern == 3:
        lines_input = ['20 20 19']
        lines_export = [0]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(1)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()


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

int main() {
  long long H, W;
  cin >> H >> W;
  if (H == 1 || W == 1) {
    cout << 1;
  }
  else if (H % 2 == 1 && W % 2 == 1) {
    cout << H * W / 2 + 1;
  }
  else {
    cout << H * W / 2;
  }
}

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

Measured memory_bytes: 256
import sys
input = lambda: sys.stdin.readline().rstrip()
yes = lambda boolean: print('Yes') if boolean else print('No')
input_ints = lambda: list(map(int, input().split()))
 
N, M = input_ints()
A = input_ints()
A.sort(reverse=1)
yes(A[M-1]*4*M>=sum(A))

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using pii = pair<int, int>;

//const ll INF = 1e18L + 5;
//const int INF = 1e9 + 5;
//const double pi = 3.14159265358979323846;

#include <bits/stdc++.h>

using namespace std;

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

	int n;
	cin >> n;
	map<int, int> m;
	rep(i, n) m[i] = 0;
	while (--n) {
		int i;
		cin >> i;
		m[--i]++;
	}
	for (auto p : m) {
		cout << p.second << endl;
	}
}


Measured memory_bytes: 12980
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(k,i,n) for(ll i=k;i<n;++i)
int main(void){
    ll n,m,sum=0;
    cin>>n>>m;
    rep(0,i,m){
        ll a;
        cin>>a;
        sum+=a;
    }
    ll ans=n-sum;
    if(ans<0){
        cout<<-1;
    }else{
        cout<<ans;
    }
}


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

int main() {
	string str;
	int res = 0;
	cin >> str;
	for (int i = 0; i < str.size(); i++) {
		if (str[i] == '1')res++;
	}
	cout << res << endl;
	return 0;
}

Measured memory_bytes: 256
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N = int(readline())
H = np.array(read().split(),dtype=np.int32)

x = np.arange(N)
x[:-1][H[:-1] >= H[1:]] += 1

# 最終着地地点
for _ in range(20):
    x = x[x]

answer = (x - np.arange(N)).max()
print(answer)

Measured memory_bytes: 18572
#include <bits/stdc++.h>
    using namespace std;
    int main() {
        unsigned long long sum=0;
        int n,m,k;
        string s;
        cin>>n>>m;
        for(int i=0;i<m;i++){
            cin>>k;
            n-=k;
        }
        if(n<0)n=-1;
        printf("%d\n",n);
    	return 0;
    }

Measured memory_bytes: 3724
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <utility>
#include <fstream>
#include <random>
#include <map>
#include <unordered_map>
#include <cstdlib>
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
#define p pair<int,int>
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
using namespace std;
int POW(int x,int y){return int(pow(double(x),double(y)));}


int main(){
	int n;
	cin>>n;
	rep(i,n){
		char a;
		cin>>a;
		if(a=='Y'){
			cout<<"Four"<<endl;
			return 0;
		}
	}
	cout<<"Three"<<endl;
			return 0;
}





Measured memory_bytes: 256
# Author: S Mahesh Raju
# Username: maheshraju2020
# Date: 31/07/2020

from sys import stdin,stdout
from math import gcd, ceil, sqrt
from collections import Counter
from bisect import bisect_left, bisect_right
ii1 = lambda: int(stdin.readline().strip())
is1 = lambda: stdin.readline().strip()
iia = lambda: list(map(int, stdin.readline().strip().split()))
isa = lambda: stdin.readline().strip().split()
mod = 1000000007

n, w = iia()
wt, val = [], []
for i in range(n):
    a, b = iia()
    wt.append(a)
    val.append(b)
tot = sum(val)
dp = [[float('inf') for i in range(tot + 1)] for j in range(n + 1)]

dp[0][0] = 0
for i in range(1, n + 1):
    for j in range(tot + 1):
        if j < val[i - 1]:
            dp[i][j] = dp[i - 1][j]
        else:
            dp[i][j] = min(dp[i - 1][j], wt[i - 1] + dp[i - 1][j - val[i - 1]])

ans = 0
for i in range(1, tot + 1):
    if dp[-1][i] <= w:
        ans = max(ans, i)
print(ans)    

Measured memory_bytes: 268284
import java.util.*;
import java.math.*;

public class Main{
  public static void main(String args[]){
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
	Integer[] data=new Integer[n];
	BigInteger t;
    BigInteger sum=BigInteger.valueOf(0);
	BigInteger doubled=BigInteger.valueOf(0);
    for(int i=0;i<data.length;i++){
      data[i]=sc.nextInt();
      t=BigInteger.valueOf(data[i]);
      sum=sum.add(t);
      doubled=doubled.add(t.multiply(t));
    }
    BigInteger multied=sum.multiply(sum);
    multied=multied.subtract(doubled).divide(BigInteger.valueOf(2)).mod(BigInteger.valueOf(1000000007));
    
    System.out.println(multied.intValue());
    
    
  }
}

Measured memory_bytes: 64052
#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>
#include <string>
#include <vector>
#include <iomanip>
#include <map>

using namespace std;

#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

typedef long long ll;
typedef long double ld;

const ll SIZE = 1e6 + 10, S2 = 1e7 * 2, MOD = 1e9 + 7, INF = 1e9 + 10;

vector<ll> vec;
vector<bool> graph[2];

int main() {
    fastInp;

    ll n, m;
    cin >> n >> m;

    graph[0].resize(n);
    graph[1].resize(n);
    for (int i = 0; i < m; i++) {
        ll u, v;
        cin >> u >> v;

        if (u > v) swap(u, v);
        if (u == 1) {
            graph[0][v] = 1;
        }
        if (v == n) {
            graph[1][u] = 1;
        }
    }

    bool flag = false;
    for (int i = 0; i < n; i++) {
        if (graph[0][i] && graph[1][i]) flag = true;
    }

    if (flag) {
        cout << "POSSIBLE\n";
    }
    else {
        cout << "IMPOSSIBLE\n";
    }
    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
const static int MAX = 100000;
long long T[MAX];
int n, k;

int check(const long long &p) {
    int i = 0;
    for (int j = 0; j < k; j++) {
        long long s = 0;
        while (s + T[i] <= p) {
            s += T[i];
            i++;
            if (i == n) return n;
        }
    }
    return i;
}

int solve() {
    long long left = 0;
    long long right = 100000 * 100000;
    long long mid;
    while (right - left > 1) {
        mid = (left + right) / 2;
        int v = check(mid);
        if (v >= n) right = mid;
        else left = mid;
    }
    return right;
}

int main()
{
    std::cin >> n >> k;
    for (int i = 0; i < n; i++) std::cin >> T[i];
    long long ans = solve();
    std::cout << ans << std::endl;

    return 0;
}



Measured memory_bytes: 3884
from fractions import Fraction

def f(x, suf):
    return int(str(x)+'9'*suf)

def s(n):
    return sum(int(c) for c in str(n))

def g(x, suf):
    n = f(x, suf)
    return Fraction(n,s(n))


res = []
cur=1
x,suf=1,0
while res==[] or res[-1]<=10**15:
    y=x+1
    nxt=g(y,suf)
    if cur<=nxt:
        res.append(f(x,suf))
        x=y
        cur=nxt
    else:
        x=(x-1)//10+1
        suf+=1
        cur=g(x,suf)


k=int(input())
for i in range(k):
    print(res[i])


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

int main()
{
  int N, A;
  cin >> N >> A;
  if (N % 500 <= A)
  {
    cout << "Yes\n"
         << endl;
  }
  else
  {
    cout << "No\n"
         << endl;
  }
}


Measured memory_bytes: 256
#include<cstdio>
using namespace std;
int main(){
  int a,b;
  scanf("%d %d",&a,&b);
  if(a > b){
    printf("a > b\n");
  }else if(a < b){
    printf("a < b\n");
  }else if(a == b){
    printf("a == b\n");
  }
  return 0;
}

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

#define rep(i, n) for(int i = 0; i<(n); i++)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef long long ll;

const int MOD = 1000000007;

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

    int ans = 0;
    rep(L, N + 1) {
        rep(R, N + 1) {
            if (N < L + R) continue;
            int rest = K - (L + R);
            if (rest < 0) continue;

            vector<int> has;
            rep(i, L) has.push_back(v[i]);
            rep(i, R) has.push_back(v[N - 1 - i]);
            sort(has.begin(), has.end());

            int n = has.size();
            int sum = 0;
            rep(i, n) sum += has[i];
            rep(i, min(n, rest)) {
                if (0 < has[i]) break;
                sum -= has[i];
            }
            chmax(ans, sum);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
using namespace std;
using edge = pair<int, int>;
using graph = vector<vector<edge>>;
vector<int> color;

graph g;
int n;

//pはvの親
void dfs(int v, int p, int clr){
    color[v]= clr;
    for(auto e: g[v]){
        if(e.first == p){continue;}
        if(e.second & 1){dfs(e.first, v, 1-clr);}
        else{dfs(e.first, v, clr);}
    }
}

int main(){
    cin >> n;
    g.assign(n, vector<edge>());
    for(int i=0; i<n-1; ++i){
        int u, v, w; cin >> u >> v >> w;
        --u; --v;
        g[u].push_back(edge(v, w));
        g[v].push_back(edge(u, w));
    }
    color.assign(n, 0);
    dfs(0, -1, 1);
    for(auto v: color){cout << v << endl;}
}

Measured memory_bytes: 11636
#include <stdio.h>
int main(void){
  int n,i,j,a[50][26]={},ans[26]={},min;
  char s[51];
  scanf("%d\n",&n);
  for (i=0;i<n;i++){
    scanf("%s",s);
    for (j=0;s[j];j++) a[i][(int)s[j]-97]++;
  }
  for (i=0;i<26;i++){
    min=50;
    for (j=0;j<n;j++){
      if (a[j][i]==0){
        ans[i]=0;
        break;
      }
      if (min>a[j][i]) min=a[j][i];
    }
    if (j==n) ans[i]=min;
  }
  for (i=0;i<26;i++){
    if (ans[i]){
      for (;ans[i]>0;ans[i]--) printf("%c",(char)(i+97));
    }
  }
  printf("\n");
  return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<string, string>;
ll MOD = 1e9+7;
int main()
{
    int n;
    string s;
    cin >> n >> s;
    string t = s.substr(n, n);
    reverse(t.begin(), t.end());

    map<P, int> m;
    for(int i = 0; i < (1 << n); i++)
    {
        string red, blue;
        for(int j = 0; j < n; j++)
        {
            if((i >> j) & 1)
            {
                red.append(1, s[j]);
            }
            else
            {
                blue.append(1, s[j]);
            }
        }
        m[P(red, blue)]++;
    }
    ll ans = 0;
    for(int i = 0; i < (1 << n); i++)
    {
        string red, blue;
        for(int j = 0; j < n; j++)
        {
            if((i >> j) & 1)
            {
                red.append(1, t[j]);
            }
            else
            {
                blue.append(1, t[j]);
            }
        }
        P p = P(red, blue);
        if(m.find(p) != m.end())
        {
            ans += m[p];
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 46080
#include <iostream>
#include <vector>
#include <cmath>
#include <sstream>
#include <string>
#include <stack>
#include <algorithm>
#include <queue>
#include <functional>
#include <map>
#include <iomanip>
#include <unordered_map>
#include <time.h>
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL)

using namespace std;
typedef long long ll;


int main() {
	int n;
  	cin>>n;
  	float ans = 0.0;
  if(n&1)
    ans += ((n+1)/2.0)/(n*1.0);
  else
    ans += (n/2.0)/(n*1.0);
  cout<<ans;
	return 0;
}



Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <queue>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
using namespace std;
typedef long long ll;

ll N,K;

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

    ll ans=(N/K)*(N/K)*(N/K);
    if(K%2==0){
        ll x=N%K>=K/2?N/K+1:N/K;
        ans+=x*x*x;
    }

    cout<<ans<<endl;
}

Measured memory_bytes: 256
#input
X = int(input())

#output
import math
M = int(math.sqrt(X))

def is_prime(n):
    if n == 1:
        return False
    for p in range(2, int(math.sqrt(n))+1):
        if n % p == 0:
            return False
    return True

while not is_prime(X):
    X += 1

print(X)

Measured memory_bytes: 3068
import java.io.BufferedReader;
import java.io.*;
import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;

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

        int n = sc.nextInt();

        List<Integer> list = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();

        for(int i = 1 ; i < n ; i++){
            list.add(sc.nextInt());
        }

        for(int i = 0 ; i < n ; i++){
            list2.add(0);
        }

        for(int i = 0 ; i < n-1 ; i++){
            list2.set(list.get(i)-1, list2.get(list.get(i)-1)+1);
            //System.out.println(list2);
        }

        for(int i = 0 ; i < n ; i++){
            System.out.println(list2.get(i));
        }


  
                


    }
}

Measured memory_bytes: 67484
import sys
sys.setrecursionlimit(10**7)

def run():
  V, E = map(int, input().split())
  if V == 1: exit()
  g = {}
  for e in range(E):
    A, B = map(int, input().split())
    g.setdefault(B, []).append(A)
    g.setdefault(A, []).append(B)
  order = [0]*V
  count = 0
  art = []
  def dfs(now, prev):
    nonlocal count
    fcount = artflag = 0
    count += 1
    low_p = order[now-1] = count
    for nxt in g[now]:
      if nxt == prev:
        continue
      if order[nxt-1] == 0:
        nxt_low_p = dfs(nxt, now)
        artflag |= nxt_low_p >= order[now-1]
        low_p = min(low_p, nxt_low_p)
        fcount += 1
      else:
        low_p = min(low_p, order[nxt-1])
    if (prev == -1 and fcount > 1) or (prev != -1 and artflag):
      art.append(now)
    return low_p
  dfs(1, -1)
  for a in sorted(art):
    print(a)
  
if __name__ == '__main__':
  run()


Measured memory_bytes: 21760
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <numeric>

using namespace std;

using ll = long long;
using ull = unsigned long long;

const ll inf = (ll)1e9;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s, f) cout << fixed << setprecision(f) << s << endl;


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

	vector<int> a(n);
	for (int i = 0; i < n; i++)
	{
		cin >> a.at(i);
	}

	sort(all(a));
	int res = 0;
	for (int i = 0; i < n ; i++)
	{
		if ((i != n - 1 && x >= a.at(i)) || (i == n - 1 && x == a.at(i)))
		{
			res++;
			x -= a.at(i);
			if (x <= 0)
			{
				break;
			}
		}
	}

	println(res);
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#define N 100005
using namespace std;

typedef long long ll;
ll n, m, s;
int main()
{
	ll i, j;
	cin >> n >> m;
	for (i = 1,j = 1; i <= m / n; i *= 2, j++);
	cout << j-1;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>

using namespace std;

int H, W, K;
char map[1005][1005];
int sumJ[1005][1005], sumO[1005][1005], sumI[1005][1005];

int main(void)
{
	cin >> H >> W >> K;
	for(int y = 1; y <= H; y++){
		for(int x = 1; x <= W; x++){
			cin >> map[x][y];
		}
	}
	
	for(int y = 1; y <= H; y++){
		for(int x = 1; x <= W; x++){
			sumJ[x][y] = sumJ[x-1][y] - sumJ[x-1][y-1] + sumJ[x][y-1];
			sumO[x][y] = sumO[x-1][y] - sumO[x-1][y-1] + sumO[x][y-1];
			sumI[x][y] = sumI[x-1][y] - sumI[x-1][y-1] + sumI[x][y-1];
			if(map[x][y] == 'J') sumJ[x][y]++;
			if(map[x][y] == 'O') sumO[x][y]++;
			if(map[x][y] == 'I') sumI[x][y]++;
		}
	}
	
	int x1, y1, x2, y2;
	for(int k = 0; k < K; k++){
		cin >> y1 >> x1 >> y2 >> x2;
		cout << sumJ[x2][y2] + sumJ[x1-1][y1-1] - sumJ[x2][y1-1] - sumJ[x1-1][y2] << " ";
		cout << sumO[x2][y2] + sumO[x1-1][y1-1] - sumO[x2][y1-1] - sumO[x1-1][y2] << " ";
		cout << sumI[x2][y2] + sumI[x1-1][y1-1] - sumI[x2][y1-1] - sumI[x1-1][y2] << endl;
	}
	
	return 0;
}


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

int main(){
	
	int N,X,sum=0;
	vector<int> M;
	cin>>N>>X;
	for(int i=0;i<N;i++){
		
		int tmp;
		cin>>tmp;
		M.push_back(tmp);
		sum+=tmp;
		
	}
	sort(M.begin(),M.end());
	cout<<N+(X-sum)/M[0]<<endl;
	
	return 0;
}

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

int main(){
  int n,k;
  cin >> n >> k;
  
  int h[n];
  for(int i = 0; i < n; ++i){
    cin >> h[i];
  }
  sort(h,h+n);
  
  long long int res = 10000000000;
  for(int i = 0; i < n-k+1; ++i){
    long long int c = h[i+k-1] - h[i];
    res = min({res, c});
  }
  
  cout << res << endl;
}

Measured memory_bytes: 3968
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int (i)=0;i<(n);i++)
#define REP(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define MOD 1000000007
#define fcout cout << fixed << setprecision(15)
#define int long long
#define endl "\n"
#define yorn(f) puts((f)?"Yes":"No")
#define YORN(f) puts((f)?"YES":"NO")
typedef long long ll;
typedef pair<int, int> P;
int gcd(int a,int b){return b?gcd(b,a%b):a;};
int lcm(int a,int b){return a/gcd(a,b)*b;};
int mod(int a,int b){return (a+b-1)/b;};
template<typename A, size_t N, typename T>
void Fill(A(&array)[N],const T &val){std::fill((T*)array,(T*)(array+N),val);}
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;};
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;};

#define cost first
#define to second
#define INF LLONG_MAX

const int MAX_V = 100010;

vector<P> G[MAX_V];
int dist[MAX_V];
int V; // 頂点数

void dijikstra(int s) {
	priority_queue< P, vector<P>, greater<P> > que;
	fill(dist, dist + V, INF);
	dist[s] = 0;
	que.push(P(0, s));

	while(!que.empty()) {
		P p = que.top(); que.pop();
		int next = p.to;
		if(dist[next] < p.cost) continue;
		for(P e : G[next]) {
			if(dist[e.to] > dist[next] + e.cost) {
				dist[e.to] = dist[next] + e.cost;
				que.push(P(dist[e.to], e.to));
			}
		}
	}
}

signed main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    
    cin >> V;
    rep(i, V-1) {
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        G[a].push_back(P(c, b));
        G[b].push_back(P(c, a));
    }
    int q, k;
    cin >> q >> k;
    dijikstra(--k);
    rep(i, q) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        cout << dist[x] + dist[y] << endl;
    }
    return 0;
}

Measured memory_bytes: 12144
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            System.out.println(String.format("%d %d", a * b, 2 * (a + b)));
        }
    }
}



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

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

using namespace std;



int main(){
    ll n;
    cin >> n;
    vector<ll> d(n);
    rep(i, n)   cin >> d[i];
    ll m;
    cin >> m;
    vector<ll> t(m);
    rep(i, m)   cin >> t[i];

    map<ll, ll> md;
    map<ll, ll> mt;
    rep(i, n)   md[d[i]]++;
    rep(i, m)   mt[t[i]]++;

    bool ok = true;
    rep(i, m){
        if(mt[t[i]] <= md[t[i]])    continue;
        else{
            ok = false;
            break;
        }
    }

    cout << ((ok)? "YES":"NO") << endl;
    

    return 0;
}



Measured memory_bytes: 28416
// https://atcoder.jp/contests/agc029/tasks/agc029_b

#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "map"
#include "numeric"
#include "queue"
#include "set"
#include "string"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define rep1(i, to) for (ll i = 1; i <= (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
#define repr(i, from) for (ll i = from - 1; i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define unless(cond) if (!(cond))
#define fi first
#define se second
using namespace std;
typedef long long ll;
template <typename T>
using V = vector<T>;
using VL = V<ll>;
using VVL = V<VL>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T>
inline bool chmax(T& a, T b);
template <typename T>
inline bool chmin(T& a, T b);
void print_ints(vector<ll> v);
template <typename T>
void drop(T a);

const ll INF = 1e18;

void solve() {
  ll N;
  cin >> N;

  multiset<ll> as;
  rep(i, N) {
    ll a;
    cin >> a;
    as.insert(a);
  }

  ll res = 0;

  while (!as.empty()) {
    auto num_it = --as.end();
    ll num = *num_it;

    ll sum = 1;
    while (sum <= num) sum *= 2;

    ll pair = sum - num;
    auto pair_it = as.find(pair);

    if (pair_it != as.end() && !(num_it == pair_it && as.count(num) == 1)) {
      res++;
      as.erase(pair_it);
    }

    as.erase(num_it);
  }

  cout << res << endl;
}

void solve2() {
  ll N;
  cin >> N;

  VL as(N);
  rep(i, N) cin >> as[i];

  ll res = 0;

  map<ll, ll> m;
  rep(i, N) m[-as[i]]++;

  auto dec = [&m](ll val) {
    m[val]--;
    if (m[val] == 0) m.erase(val);
  };

  while (!m.empty()) {
    ll num = -m.begin()->first;
    dec(-num);

    ll sum = 1;
    while (sum <= num) sum *= 2;

    ll p = sum - num;

    if (m.count(-p)) {
      res++;
      dec(-p);
    }
  }

  cout << res << endl;
}

void solve3() {
  ll N;
  cin >> N;

  VL as(N);
  rep(i, N) cin >> as[i];

  ll res = 0;

  map<ll, ll> m;
  rep(i, N) m[as[i]]++;

  auto dec = [&m](ll val) {
    m[val]--;
    if (m[val] == 0) m.erase(val);
  };

  while (!m.empty()) {
    ll num = m.rbegin()->first;
    dec(num);

    ll sum = 1;
    while (sum <= num) sum *= 2;

    ll p = sum - num;

    if (m.count(p)) {
      res++;
      dec(p);
    }
  }

  cout << res << endl;
}

struct exit_exception : public std::exception {
  const char* what() const throw() { return "Exited"; }
};

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

  try {
    solve3();
  } catch (exit_exception& e) {
  }

  return 0;
}
#endif

template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void print_ints(vector<ll> v) {
  rep(i, v.size()) {
    if (i > 0) {
      cout << " ";
    }

    cout << v[i];
  }

  cout << endl;
}

template <typename T>
void drop(T res) {
  cout << res << endl;
  throw exit_exception();
}


Measured memory_bytes: 14336
#include <vector>
#include <iostream>
using namespace std;
int n, s; vector<int> a; vector<vector<bool> > vis, dp;
bool solve(int x, int t) {
	if (vis[x][t]) return dp[x][t];
	bool ret = false;
	for (int i = 1; i <= a[t] && x - i > 0; i++) {
		if (!solve(x - i, (t + 1) % (2 * n))) ret = true;
	}
	vis[x][t] = true;
	return dp[x][t] = ret;
}
int main() {
	while (cin >> n, n) {
		cin >> s; a.resize(2 * n);
		for (int i = 0; i < 2 * n; i++) cin >> a[i];
		vis = vector<vector<bool> >(s + 1, vector<bool>(2 * n, false));
		dp = vector<vector<bool> >(s + 1, vector<bool>(2 * n));
		cout << solve(s, 0) << endl;
	}
	return 0;
}

Measured memory_bytes: 4420
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define ub upper_bound
#define lb lower_bound
#define all(v) ((v).begin()),((v).end())
#define allr(v) ((v).rbegin()),((v).rend())
#define ff first
#define ss second
#define mp make_pair
#define pll pair<ll,ll> 
#define fo(i,n) for(ll i=0;i<n;i++)
#define foa(i,s,e) for(ll i=(s);i<=e;i++)
#define fod(i,s,e) for(ll i= (s);i>=(e);i--)
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
#define sz(x) (ll)(x.size())
#define ANS cout<<ans<<'\n'
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
const ld pi = 3.14159265358979323846;
ll MOD = 1e9 + 7;
//ll MOD = 998244353;
const char nl = '\n';
const ll inf = 1e18;
#define fill(a,b) memset(a, b, sizeof(a))
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) fo(i, n){fo(j, m)cout<<dp[i][j]<<" ";cout<<"\n";}
ll nxt(){ll x; cin >> x; return x;}
ll mul(ll x,ll y){
    return (1ll* (x%MOD)*(y%MOD));
}
ll modpow(ll x,ll y){ll z=1;while(y>0){if(y%2)z=mul(z,x);x =mul(x,x) ;y/=2;}return z;}
ll power(ll x,ll y){ll z=1;while(y>0){if(y%2)z=z*x;x =x*x ;y/=2;}return z;}
ll gcd(ll a,ll b){if(a<b) return gcd(b,a);if(b==0) return a;return gcd(b,a%b);} 
ll min(ll a,ll b){if(a<b)return a;return b;}
ll max(ll a,ll b){if(a>b)return a;return b;}
ll sq(ll a){
    ll ans = (1ll*a*a);
    return ans;
}
void solve(){
    ll a,b,c,d;
    cin >> a >> b >> c >> d;
    ll ans = -1e18 - 2;
    if((0 <= b && 0 >= a) || (0 <=d && 0 >= c)){
        ans = 0;
    }
    if(b <= 0 && d <= 0){
        ans = max(ans, a * c);
    }
    if(b > 0 && d > 0){
        ans = max(ans, max(a * c, b * d));
    }
    else if(b <= 0){
        if(c > 0)ans = max(ans, b * c);
        else  ans = max(ans, a * c);
    }
    else if(d <= 0){
        if(a > 0)ans = max(ans, a * d);
        else  ans = max(ans, a * c);
    }

    ANS;
}
int main(){ 
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("inputf.txt" , "r" , stdin) ;
    freopen("outputf.txt" , "w" , stdout) ;
    freopen("error.txt" , "w" , stderr) ;
    #endif
    ll TC = 1;
    //cin>>TC;
    fo(TT, TC){
        // Google,FB cout << "Case #"<< TT + 1 << ": ";
        solve();
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}


Measured memory_bytes: 3760
"use strict";
function Main(input) {
    let lines = input.split("\n");
    let N = lines.shift() - 0;
    let h = lines.shift().split(/\s/).map((v) => v - 0);
    let ans = 0;
    let act = 0;
    for (let n = 0; n < N; n++) {
        if (act >= h[n]) act = h[n];
        else {
            ans += h[n] - act;
            act = h[n];
        }
    }
    console.log(ans);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));


Measured memory_bytes: 8392
x1, y1, x2, y2 = [int(x) for x in input().split()]

x3 = x2 + y1 - y2
y3 = x2 - x1 + y2
x4 = x1 + y1 - y2
y4 = x2 - x1 + y1

print(x3, y3, x4, y4)


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

using namespace std;

int main(){
    int n,m;cin >> n;
    vector<string> u(n);
    for(int i = 0; i < n; i++)cin >> u[i];
    sort(u.begin(), u.end());
    bool isopen = false;
    cin >> m;
    for(int i = 0; i < m; i++){
        string m;cin >> m;
        bool find = false;
        for(int j = 0; j < n; j++) if(u[j] == m){
            find = true;
            break;
        }
        if(find){
            if(isopen){
                cout << "Closed by " << m << endl;
            }
            else{
                cout << "Opened by " << m << endl;
            }
            isopen = !isopen;
        }
        else{
            cout << "Unknown " << m << endl;
        }
    }
	return 0;
}




Measured memory_bytes: 3236
#include <cstdio>
#include <bitset>
using namespace std;

#define MAX 1000001
bitset<MAX> bs;
int mul[3] = { 2,3,5 };

int main(){
	bs.set(1);
	int p;
	for (int i = 0; i < MAX; i++) {
		if (bs[i]) {
			for (int j = 0; j < 3; j++) {
				p = i*mul[j];
				if (p < MAX) {
					bs.set(p);
				}
			}
		}
	}
	int m, n, c;
	while (scanf("%d", &m), m != 0) {
		scanf("%d", &n);
		c = 0;
		for (int i = m; i <= n; i++) {
			if (bs[i])c++;
		}
		
		printf("%d\n", c);
	}
	
	return 0;
}

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

using namespace std;
 
#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl;
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}
typedef long long ll;
#define int ll
 
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(int)(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl;
// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
// vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
const ll MOD = 1000000007;
// const ll MOD = 998244353;
// #define mp make_pair
//#define endl '\n'



signed main() {
    fio();
    map<char, int> mp;
    string s;
    cin >> s;
    for (char c: s) {
        mp[c]++;
    }

    if (mp.size() % 2 == 1) {
        cout << "No" << endl;
    }
    else if (mp.size() == 4) {
        cout << "Yes" << endl;
    }
    else {
        if (mp['E'] && mp['W'] || mp['N'] && mp['S']) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
}

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

using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        //freopen("input.txt", "r", stdin);
    #endif // ONLINE_JUDGE

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

    int a, b, c;
    cin >> a >> b >> c;
    if((a==b && a!=c) || (a==c && a!=b) || (b==c && b!=a))
        cout << "Yes\n";
    else
        cout << "No\n";
    

    return 0;
}


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

void write(){
    cout << "Yes"<<endl;
}

int main(){
    int a ,b,c;
    cin >> a >>b>>c;

    if(a==b&&a!=c){
        write();
    }
    else if(b==c&&b!=a){write();}
    else if(c==a&&c!=b){write();}
    else {cout << "No"<<endl;}
}

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

int main()
{
    int k, s;
    cin >> k >> s;
    int counter = 0;
    for (int x = 0; x <= k; x++) {
        for (int y = x; y <= k; y++) {
            for (int z = y; z <= k; z++) {
                if (x + y + z == s) {
                    if (x != y && y != z && x != z) {
                        counter += 6;
                    } else if (x == y && y == z) {
                        counter++;
                    } else {
                        counter += 3;
                    }
                }
            }
        }
    }
    cout << counter << endl;
    return 0;
}

Measured memory_bytes: 764
#include <bits/stdc++.h>
using namespace std;
int main(){
    int  i=0, ans=0;
    string s;
    cin >>s;
    while(s[i]!='\0'){
        if(s[i]=='+') ans++;
        else ans--;
        i++;
    }
    cout << ans <<endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){
  long long n,m;cin>>n>>m;
  long long N=n,M=m;
  long long a = 0;
  while((N==n || n%4) && n <= m){
    a ^= n;
    n++;
  }
  while((M==m||(m+1)%4) && n <= m){
    a ^= m;
    m--;
  }
  cout << a << endl;
}
    


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const LL mod=1e9+7;

int main(){
	int N;
	cin >> N;
	string S[2];
	cin >> S[0] >> S[1];
	S[0]+='0', S[1]+='1';
	bool ver=false;
	LL ans=1;
	rep(i,N){
		if(S[0][i]==S[0][i+1] && S[1][i]==S[1][i+1]){
			if(ver) ans*=3;
			else{
				if(i==0) ans*=6;
				else ans*=2;
			}
			i++, ver=true;
		}
		else if(S[0][i]==S[1][i]){
			if(ver) ver=false;
			else{
				ver=false;
				if(i==0) ans*=3;
				else ans*=2;
			}
		}
		ans%=mod;
	}
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256
#n = int(input())
#l = list(map(int, input().split()))

'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''

import sys
input = sys.stdin.readline

s = input()
a,b,c,d=[int(s[i]) for i in range(4)]
op = -1
ans = []
for i in range(2):
    for j in range(2):
        for m in range(2):
            if (a+b*op**i+c*op**j+d*op**m)==7:
                ans.append(i)
                ans.append(j)
                ans.append(m)
for i in range(3):
    if ans[i]==0:
        ans[i]="+"
    else:
        ans[i]="-"
print(s[0]+ans[0]+s[1]+ans[1]+s[2]+ans[2]+s[3]+"=7")

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

using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define REP1(i,n) for(int i=1;i<(n);i++)
#define ALL(a)  (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using Pint = pair<int, int>;
const ll mod = 1e9+7;

int main(int argc, char const* argv[])
{
  int n;
  cin>>n;
  map<int, int> m;
  REP(i,n) {
    int a;
    cin>>a;
    m[a]++;
  }
  if(m.size()==n) cout << "YES" << endl;
  else cout << "NO" << endl;

  return 0;
}

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

int main(){
    int N, K; cin >> N >> K;
    vector<int> x(N);
    for(int i = 0; i < N; i++) cin >> x[i];
    int MIN = 1 << 30;
    for(int i = 0; i < N - K + 1; i++){
        if(x[i] >= 0) MIN = min(MIN, x[i + K - 1]);
        else if(x[i+K-1] <= 0) MIN = min(MIN, -x[i]);
        else MIN = min(MIN, x[i + K - 1] - x[i] + min(-x[i], x[i + K - 1]));
    }
    cout << MIN << endl;
}

Measured memory_bytes: 640
#include<iostream>
#include<string>
using namespace std;
int main(void){
  int N,K = 0;
  int Hi;
  int cnt=0;
  cin >> N >> K;
  for (int i=0;i<N;i++){
    cin >> Hi;
    cnt+=int(K<=Hi);
  }
  cout << cnt;
}

Measured memory_bytes: 256
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Globalization;
using System.Diagnostics;
using System.Threading.Tasks;



class Myon
{
    public Myon() { }
    public static int Main()
    {
        new Myon().calc();
        return 0;
    }

    Scanner cin;

    void calc()
    {
        cin = new Scanner();
        int N = cin.nextInt();
        int M = cin.nextInt();
        string S = cin.next();
        string T = cin.next();

        long ans = long.MaxValue;
        for (int t = 1; t <= M; t++)
        {
            long L = (long)N * t;
            if (L % N != 0) continue;
            if (L % M != 0) continue;
            long stepA = L / N;
            long stepB = L / M;

            int i = 0;
            int j = 0;

            bool flag = true;
            while (i < N && j < M)
            {
                if (i * stepA == j * stepB)
                {
                    if (S[i] != T[j])
                    {
                        flag = false;
                        break;
                    }
                    i++; j++;
                }
                else if (i * stepA < j * stepB) i++;
                else j++;
            }
            if (flag)
            {
                ans = L;
                break;
            }
        }

        if (ans == long.MaxValue) ans = -1;
        Console.WriteLine(ans);
    }
}



class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}



Measured memory_bytes: 12256
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime> 
#include <memory.h>
#include <assert.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;
const int INF = 1e9 + 1;
const ll LLINF = 1e18 + 1;

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

    int tmp = N;
    int s = 0;
    while(tmp > 0) {
        s += tmp % 10;
        tmp /= 10;
    }

    if(N % s == 0) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

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

#define ll long long

int main(void){
    string s,t;
    cin >> s >> t;
    int end = s.length()-1;
    for(int i=0;i<s.length();i++){
        if(s == t){
            cout << "Yes" << endl;
            return 0;
        }
        char a = s[end];
        for(int j=end;1<=j;j--){
            s[j] = s[j-1];
        }
        s[0] = a;
    }
    cout << "No"<<endl;
    return 0;
}

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

using namespace std;

int main(){
    int n;cin>>n;
    vector<int> A(n),B(n);
    for(int i=0;i<n;i++){
        cin>>A[i];
        B[i] = A[i];
    }
    sort(A.begin(),A.end());
    int M1 = A[n-1],M2 = A[n-2];
    for(int i=0;i<n;i++){
        if(B[i]==M1)cout<<M2<<endl;
        else cout<<M1<<endl;
    }
}

Measured memory_bytes: 3200
n, m = map(int, input().split())
all_cand = [tuple(map(int, input().split())) for _ in range(m)]

start_path_end = set([path[1] for path in all_cand if path[0]==1])
end_path_start = set([path[0] for path in all_cand if path[1]==n])

print('POSSIBLE' if start_path_end&end_path_start else 'IMPOSSIBLE')

Measured memory_bytes: 38460
package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	for i := range a {
		fmt.Scan(&a[i])
	}

	d := make([]int, n)
	sum := 0
	for i := range a {
		sum += a[i]
	}
	sum2 := 0
	for i := 1; i <= n-1; i += 2 {
		sum2 += a[i]
	}
	d[0] = sum - 2*sum2
	for i := 1; i < n; i++ {
		d[i] = 2*a[i-1] - d[i-1]
	}

	for i := range d {
		fmt.Print(d[i])
		if i != n-1 {
			fmt.Print(" ")
		} else {
			fmt.Println()
		}
	}
}


Measured memory_bytes: 6528
input =   readlines.map(&:chomp)
N = input[0]

@index = 0
@arr =  input[1].split(' ').map(&:to_i)

def culc (array)
    if array.all? {|s| s.even?}
        @arr = array.map{|n| n/2}
        @index += 1
        culc(@arr)
    else
        p  @index 
    end
end

culc(@arr)

Measured memory_bytes: 14308
#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
 
#define pb push_back
#define mp make_pair

typedef long long ll; 

#define fi first
#define se second
 
#define all(x) (x).begin(), (x).end()
 
using namespace std;             
//using namespace __gnu_pbds;
 
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int INF = (int)1e10;

void solve() {               
    int a, b, k;
    cin >> a >> b >> k;

    vector<int> v;
    for (int i = 1; i <= max(a, b); i++) {
        if (b % i == 0 && a % i == 0) {
            v.pb(i);
        }
    }

    sort(all(v));
    reverse(all(v));

    cout << v[k - 1];
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int tt = 1;
 
    while (tt--) {
        solve();
    }
 
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

const ll mod = 1000000007;

int main() {
    int n;
    cin >> n;
    double ans = 0;
    rep(i,n){
        int a;
        cin >> a;
        ans += (double)1 / a;
    }
    cout << fixed << setprecision(10) << 1 / ans << endl;

    
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 2e5+33;
#define  LL long long

LL a[maxn],b[maxn];

void f(LL a,LL b,LL &Max,LL &Min)
{
	if(a==0||b==0) return;
	Max=max(Max,a);
	Max=max(Max,b);
	Min=min(Min,a);
	Min=min(Min,b);
}

LL ff(LL x,LL be,LL ed)
{
	LL sum=b[ed]-b[be-1];
	LL aim=sum/2+b[be-1];
	LL k=x;
	LL ju=abs(aim-b[x]);
	if(x-1>=be&&ju>abs(aim-b[x-1]))
	{
		ju=abs(aim-b[x-1]);
		k=x-1;
	}
	if(x+1<ed&&ju>abs(aim-b[x+1])){
		k=x+1;
	}
	return k;
}

int main()
{
	LL n;
	scanf("%lld",&n);
	for(LL i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		b[i]=b[i-1]+a[i];
	}
	LL ans=1e18;
	for(LL i=2;i<=n-2;i++){
		LL Max=0;
		LL Min=1e18;
		LL aim=b[i]/2;
		LL to=lower_bound(b+1,b+1+i,aim) - b;
		if(to==i) to--;
		to=ff(to,1,i);
		f(b[to],b[i]-b[to],Max,Min);
		aim=(b[n]-b[i])/2+b[i];
		to=lower_bound(b+i+1,b+n+1,aim) - b;
		if(to==n) to--;
		to=ff(to,i+1,n);
		f(b[to]-b[i],b[n]-b[to],Max,Min);
		ans=min(ans,Max-Min);
	}
	printf("%lld\n",ans);



}


Measured memory_bytes: 3328