akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;

int main() { IO;
  int last;
  cin >> last;

  set<int> s;
  while (s.count(last) == 0) {
    s.insert(last);

    if (last & 1) {
      last = 3 * last + 1;
    } else {
      last = last / 2;
    }
  }

  cout << s.size() + 1 << endl;

  return 0;
}


Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
 
public class Solution
{
    public static void Main()
    {
      int N = int.Parse(Console.ReadLine());
      string str = Console.ReadLine();
      string[] num = str.Split(' ');
      int[] p = new int[N];
      for (int i = 0; i < N; i++)
      {
        p[i] = int.Parse(num[i]);
      }
      // LINQでソート
      var dst = p.OrderBy(a => a).ToList();
      // pとdstで比較してすべて同じか2箇所異なっていればYES
      int cnt = 0;
//      Console.WriteLine($"{p[0]} {p[1]} {p[2]} {p[3]} {p[4]}");
//      Console.WriteLine($"{dst[0]} {dst[1]} {dst[2]} {dst[3]} {dst[4]}");
      for (int i = 0; i < N; i++)
      {
        if (p[i] != dst[i])
        {
          cnt++;
        }
      }
//      Console.WriteLine($"{cnt}");
      if (cnt == 0 | cnt == 2)
      {
        Console.WriteLine("YES");
      }
      else
      {
        Console.WriteLine("NO");
      }
      
    }
}

Measured memory_bytes: 13396
#include <bits/stdc++.h>
#define pb push_back
#define rep(i,n) for(int i = 0;i < (n); ++i)
#define all(v) v.begin(),v.end()
#define sort_1(v) sort(v.begin(),v.end())
#define sort_2(v) sort(v.begin(),v.end(),greater<ll>())
typedef long long ll;
typedef long double la;
using namespace std;

ll choose2(ll n){
    return n*(n-1)/2;
}

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    rep(i,n) a[i]--;
    vector<int> cnt(n);
    rep(i,n) cnt[a[i]]++;
    ll tot = 0;
    rep(i,n){
        tot += choose2(cnt[i]);
    }
    rep(i,n){
        ll ans = tot;
        ans -= choose2(cnt[a[i]]);
        ans += choose2(cnt[a[i]]-1);
        cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

typedef long long ll;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int k,s; cin >> k >> s;
    ll ans = 0;
    for(int i = 0; i <= k; i++)
    for(int j = 0; j <= k; j++)
        if(s - i - j >= 0 && s - i - j <= k) ans++;
    cout << ans << "\n";
}


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

static const int MAX=100;
static const int long long INFTY=(1LL<<32);

int n;
long long d[MAX][MAX];

void floyd(){
    for(int k=0;k<n;k++){
        for(int i=0;i<n;i++){
            if(d[i][k]==INFTY)continue;
            for(int j=0;j<n;j++){
                if(d[k][j]==INFTY)continue;
                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
            }
        }
    }
}

int main(void){
    int e,u,v,c;
    cin>>n>>e;
    
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            d[i][j]=((i==j)? 0: INFTY);
        }
    }
    for(int i=0;i<e;i++){
        cin>>u>>v>>c;
        d[u][v]=c;
    }
    floyd();
    bool negative=false;
    for(int i=0;i<n;i++)if(d[i][i]<0)negative=true;
    if(negative){
        cout<<"NEGATIVE CYCLE"<<endl;
    }else{
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(j)cout<<" ";
                if(d[i][j]==INFTY)cout<<"INF";
                else cout<<d[i][j];
            }
            cout<<endl;
        }
    }
    return 0;
}



Measured memory_bytes: 3160
sx,sy,tx,ty=list(map(int, input().split()))

x = tx-sx
y = ty-sy

answer = 'L'+'U'*(y+1)+'R'*(x+1)+'D' +'L'*x+'D'*y + 'R'*x+'U'*y+  'R'+'D'*(y+1)+'L'*(x+1)+'U'
print(answer)

Measured memory_bytes: 3060
#include<iostream>
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip>//setprecision
#include<math.h>
#include <functional>
#include<climits>
using namespace std;
#define ll  long long 
#define rep(i, n)        for(ll i=0; i<(ll)(n); ++i)
#define mod 1000000007
  vector<ll>to[1000005];
int main(){
  ll a,b,c=0;cin>>a;
  rep(i,a){
    cin>>b;
    if(i%2==0&&b%2==1)c++;
  }
  cout<<c;
}



Measured memory_bytes: 27064
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner();
		String s = sc.next();
		if(s.length() == 2)
			System.out.println(s);
		else {
			for(int i = 2; i > -1; i--)
				System.out.print(s.charAt(i));
			System.out.println();
		}
	}
}

class Scanner {
	private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	private static final int SPACE = (int)' ';
	private static final int LINE_END_0 = 13;
	private static final int LINE_END_1 = 10;
	private static final int BEFORE = -1;
	
	public boolean ready() {
		try {
			return br.ready();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public String next(){
		StringBuilder sb = new StringBuilder();
		
		int length = BEFORE;
		readLoop: while(true) {
			try {
				int value = br.read();
				switch(value) {
				case LINE_END_1:
					if(length == BEFORE)
						break;
					
					if((int)sb.charAt(length) == LINE_END_0)
						sb.deleteCharAt(length);
				case SPACE:
					break readLoop;
				}
				sb.append((char)value);
				length++;

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	
	public int nextInt() {
		return Integer.parseInt(next());
	}
	
	public double nextDouble() {
		return Double.parseDouble(next());
	}
	
	public long nextLong() {
		return Long.parseLong(next());
	}
}


Measured memory_bytes: 22996
#![allow(non_snake_case)]

fn read_line() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read_line()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn read_tuple<T: std::str::FromStr + Copy>() -> (T, T) {
    let v: Vec<T> = read_vec();
    (v[0], v[1])
}

fn main() {
    let (N, K) = read_tuple::<usize>();
    let mut h = vec![0; N];
    for i in 0..N {
        h[i] = read_line().parse().unwrap();
    }
    h.sort();
    let mut ans = i32::max_value();
    for i in 0..N-K+1 {
        ans = std::cmp::min(ans, h[i+K-1]-h[i]);
    }
    println!("{}", ans);
}


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

#define mp make_pair
#define eb emplace_back
#define ff first
#define ss second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#ifdef ONPC
mt19937 rnd(228);
#else
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#endif


const int MAXN = 100;
//#define int long long
const int INF = 1e9;

signed main()
{
    #ifdef FILEIO
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
    #endif

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

    string s;
    cin >> s;
    if (s == "ABC") cout << "ARC\n";
    else cout << "ABC\n";

    return 0;
}


Measured memory_bytes: 3612
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repb(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(),(a).end()
#define PRINT(V) for(auto v:(V))cout<<v<<" "<<endl
inline ll GCD(ll a, ll b){return b ? GCD(b, a % b) : a;}
const int MOD = 1000000007;

map<string, ll> p;
ll best;
ll dp(string s, ll d)
{
    // cout << s << " " << d << " " << s.size() << endl;
    if (s.size() <= 1)
    {
        return d;
    }
    ll res = d;
    auto itr = p.find(s);
    if (itr != p.end())
    {
        return p[s];
    }
    rep(i, s.size() - 1)
    {
        if ((s[i] == '1' && s[i + 1] == '0') || (s[i] == '0' && s[i + 1] == '1'))
        {
            string s2 = s;
            res = max(res, dp(s2.erase(i, 2), d + 2));
            if(res == best)
                break;
        }
    }
    p[s] = res;
    return res;
}
int main()
{
    string s;
    cin >> s;
    best = min(count(ALL(s), '0'), count(ALL(s), '1')) * 2;
    cout << best << endl;
}

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

int main()
{
    int H,W;

    while(1){
        cin>>H>>W;

        if(H==0&&W==0)break;

        for(int i=1;i<=H;i++){
            for(int j=1;j<=W;j++){

                if(i!=1&&i!=H&&j!=1&&j!=W){
                    cout<<".";
                }else{
                    cout<<"#";
                }
            }
            cout<<endl;
        }
        cout<<endl;

    }

    return 0;
}


Measured memory_bytes: 3080
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
//using static System.Globalization.CultureInfo;

class Program
{
    static void Main(string[] args)
    {
        var nx = Add.arL;
        var ar = Add.arL;
        var res = long.MaxValue;
        var min = Enumerable.Repeat(long.MaxValue,(int)nx[0]).ToArray();
        for(var k = 0; k < nx[0]; k++)
        {
            for(var i = 0; i < nx[0]; i++)
            {
                long index;
                if (i - k >= 0)
                    index = i - k;
                else
                    index = nx[0] + (i - k);
                min[i] = Min(min[i], ar[index]);
            }
            res = Min(res, min.Sum() + k * nx[1]);
        }
        WriteLine(res);
    }
}

public class Add
{
    public static string read => ReadLine();
    public  static int[] ar => Array.ConvertAll(read.Split(' '), int.Parse);
    public  static int num => ToInt32(read);
    public  static long[] arL => Array.ConvertAll(read.Split(' '), long.Parse);
    public  static long numL => ToInt64(read);
    public  const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}


Measured memory_bytes: 13280
def calc(N, A):
    x_sum = sum(A) // 2
    for i in range(1, N, 2):
        x_sum -= A[i]
    x = [x_sum]
    for i in range(N - 1):
        x += [A[i] - x[i]]
    print(' '.join([str(2 * y) for y in x]))


N = int(input())
A = [int(s) for s in input().split(' ')]
calc(N, A)

Measured memory_bytes: 19144
S = input()

while "eraser" in S:
  S = S.replace("eraser", "")
while "erase" in S:
  S = S.replace("erase", "")
while "dreamer" in S:
  S = S.replace("dreamer", "")
while "dream" in S:
  S = S.replace("dream", "")
  
if S == "":
  print("YES")
else:
  print("NO")

Measured memory_bytes: 3188
#include <iostream>
using namespace std;
int cnt[2][100005][2];
int main()
{
	for (int i=0;i<2;i++)
	{
		string s;
		cin >> s;
		for (int j=1;j<=s.size();j++)
		{
			cnt[i][j][0]=cnt[i][j-1][0];
			cnt[i][j][1]=cnt[i][j-1][1];
			cnt[i][j][s[j-1]-'A']++;
		}
	}
	int q;
	cin >> q;
	while (q--)
	{
		int a,b,c,d;
		cin >> a >> b >> c >> d;
		a--;
		c--;
		int ca=cnt[0][b][0]-cnt[1][d][0]-cnt[0][a][0]+cnt[1][c][0],cb=cnt[0][b][1]-cnt[1][d][1]-cnt[0][a][1]+cnt[1][c][1];
		if ((ca%3+3)%3==(cb%3+3)%3)
		cout << "YES" << endl;
		else
		cout << "NO" << endl;
	}
}

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

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

  vector<pair<int,int>> res;
  
  if(n&1){
    for (int i = 1; i < n; i++) {
      for (int j = i+1; j <= n; j++) {
	if(j<n && i==n-j) continue;
	res.emplace_back(i,j);
      }
    }
  } else {
    for (int i = 1; i < n; i++) {
      for (int j = i+1; j <= n; j++) {
	if(i+j==n+1) continue;
	res.emplace_back(i,j);
      }
    }
  }

  cout << res.size() << endl;

  for (int i = 0; i < res.size(); i++) {
    cout << res[i].first << " " << res[i].second << endl;
  }

  return 0;
}


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

using namespace std;

#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vll vector<long long>
#define MOD 1000000007

typedef long long ll;

void print(vi v){
    cout<<"Contents of vector:\n";
    for(auto x : v) cout<<x<<" ";
    cout<<endl<<endl;
}

bool check(int a, int b, int c){
    if(a+b <= c || a+c <= b || c+b <= a) return false;

    return true;
}

void solve(){
    int n; cin>>n;
    int ar[n];
    for(int i = 0; i<n; i++) cin>>ar[i];

    int ans = 0;
    for(int i = 0; i<n-2; i++){
        for(int j = i+1; j<n-1; j++){
            for(int k = j+1; k<n; k++){
                if(ar[i] != ar[j] && ar[j] != ar[k] && ar[i] != ar[k] && check(ar[i], ar[j], ar[k])) ans++;
            }
        }
    }
    cout<<ans<<endl;

}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    //ifstream cin("saul1.in"); ofstream cout("output.txt");///cia failai

    //int T; cin>>T;
    int T = 1;

    for(int it = 1; it<=T; it++){
        solve();


    }
}


Measured memory_bytes: 3692
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

void dfs(vector<vector<int>> &tree, vector<int> &d, int p){
  for(auto i : tree.at(p)){
    if(d.at(i) != -1) continue;
    d.at(i) = d.at(p) + 1;
    dfs(tree, d, i);
  }
}

int main(){
  int n, ta, ao;
  cin >> n >> ta >> ao;
  ta--; ao--;
  vector<vector<int>> tree(n, vector<int>());
  rep(i, n-1){
    int a, b;
    cin >> a >> b;
    a--; b--;
    tree.at(a).push_back(b);
    tree.at(b).push_back(a);
  }

  vector<int> dta(n, -1), dao(n, -1);
  dta.at(ta) = 0; dao.at(ao) = 0;
  dfs(tree, dta, ta);
  dfs(tree, dao, ao);
  // rep(i, n){
  //   cerr << dta.at(i) << " ";
  // }
  // cerr << endl;
  // rep(i, n){
  //   cerr << dao.at(i) << " ";
  // }
  // cerr << endl;

  int ans = 0;
  rep(i, n){
    if(dao.at(i) > dta.at(i)){
      ans = max(ans, dao.at(i)-1);
    }
  }
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 11648
# -*- coding: utf-8 -*-

def calcSumOfEachDigit(num):
    charts = list(str(num))
    return sum(int(i) for i in charts)

[N_str, A_str, B_str] = input().split()
N = int(N_str)
A = int(A_str)
B = int(B_str)

matches = []
for i in range(1, N+1):
    total = calcSumOfEachDigit(i)
    if total >= A and total <= B:
        matches.append(i)

print(sum(matches))

Measured memory_bytes: 3296
n=int(input())
l=[]
for i in range(n):
    s=input()
    s=sorted(s)
    s="".join(s)
    l.append(s)

l.sort()
cnt=0
tmp=0

for i in range(1,len(l)):
    if l[i]==l[i-1]:
        tmp += 1
    else:
        cnt += (tmp+1)*tmp//2
        tmp=0
cnt += (tmp+1)*tmp//2
print(cnt)
    

Measured memory_bytes: 10640
N = int(input())
A = list(map(int, input().split()))

all_price = 0
now = 0
for i in range(N + 1):
    if (i == N):
        all_price += abs(A[i - 1])
        break
    all_price += abs(A[i] - now)
    now = A[i]


for i in range(N):
    if (i == 0):
        print(all_price + abs(A[i + 1] - 0) -
              (abs(A[i] - 0) + abs(A[i + 1] - A[i])))

    elif (i == N - 1):
        print(all_price + abs(A[i - 1] - 0) -
              (abs(A[i] - A[i - 1]) + abs(abs(A[i] - 0))))

    else:
        print(all_price + abs(A[i - 1] - A[i + 1]) -
              (abs(A[i - 1] - A[i]) + abs(A[i] - A[i + 1])))


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

using namespace std;

int main()
{
  int n, l;
  cin >> n >> l;
  string line;
  getline(cin, line);

  vector<string> strs;
  for(int i = 0; i < n; i++)
  {
	getline(cin, line);
	strs.push_back(line);
  }
  sort(strs.begin(), strs.end());
  for(auto v : strs)
  {
	cout << v;
  }
  cout << endl;
}


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

using namespace std;

typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;
typedef vector<unordered_map<lli,lli>> graph;

lli n,h;
vll a,b;
vll c,d;
lli ans;

int main(){
  cin >> n >> h;
  a = vll(n);
  b = vll(n);
  for(lli i = 0;i < n;i++) cin >> a[i] >> b[i];
  d = b;
  sort(b.begin(),b.end());
  c = b;
  reverse(c.begin(),c.end());
  for(lli i = 0;i < n-1;i++)c[i+1] += c[i];
  ans = lower_bound(c.begin(),c.end(),h) != c.end() ?  lower_bound(c.begin(),c.end(),h)-c.begin()+1 : LONG_MAX;
  for(lli i = 0;i < n;i++){
    lli idx = b.end()-lower_bound(b.begin(),b.end(),a[i]);
    if(h-c[idx-1] <= 0) ans = min(ans,idx);
    else{
      lli ans_ = ((h-c[idx-1])+a[i]-1)/a[i];
      ans = min(ans,ans_+idx);
    }
    // cout << ans << endl;
  }
  cout << ans << endl;
  return 0;

}


Measured memory_bytes: 3328
#include<bits/stdc++.h>
using namespace std;
#define int int64_t
#define PB push_back
#define loop(i,x,y) for(int i=x;i<y;i++)
#define rloop(i,x,y) for(int i=x;i>y;i--)
#define NL cout<<"\n"
#define ans(x) cout<<x<<"\n"
#define out(x) cout<<x<<" "
#define F first
#define S second
#define ansf(x,y) cout<<fixed<<setprecision(y)<<x<<endl;
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
void solve()
{
  int n;
  string s;
  cin>>n>>s;
  loop(i,0,s.length())
    s[i]=((s[i]-'A')+n)%26+'A';
  ans(s);
}
signed main()
{
  // FAST;
  int T=1;
  // cin>>T;
  while(T--) solve();
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define set_bits(a) __builtin_popcount(a)
#define pb push_back
#define pf push_front
#define mod 1000000007
#define M 998244353
#define fi first
#define se second
#define endl '\n'
#define INF 1e18
#define PI 3.14159265358979323846
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);


int main()
{
    string s;
    cin>>s;
    if(s[1]=='B')
    {
        cout<<"ARC"<<endl;
    }
    else
    {
        cout<<"ABC"<<endl;
    }
}


Measured memory_bytes: 3596
import sys
sys.setrecursionlimit(500000)
K = int(input())

ans_list = []

def dfs(l):
    if len(l)==1 and l[0]!="0":
        ans_list.append(int("".join(l)))
    if l!=[] and l[0]=="0":
        return
    if l!=[] and int("".join(l))>3234566667:
        return
    for i in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
        l.append(i)
        #ここで絶対値1以下になるものだけdfs
        if len(l)==1:
            dfs(l)
            l.pop()
        elif abs(int(l[len(l)-2])-int(l[len(l)-1]))<=1:
            dfs(l)
            ans_list.append(int("".join(l)))
            l.pop()
        else:
            l.pop()

    return

dfs([])
ans_list.sort()
print(ans_list[K-1])



Measured memory_bytes: 21760
#include"bits/stdc++.h"
#include<boost/multi_array.hpp>
#include<boost/optional.hpp>
#include<boost/range/irange.hpp>
#include<boost/range/algorithm.hpp>
#include<boost/range/adaptors.hpp>
using namespace std;
namespace adaptor = boost::adaptors;

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
typedef long long ll;

void Main(){
 int a,b,c;
   cin >> a >> b >> c;
   
  if((a+b-c)*(b+c-a)*(c+a-b)==0)
  cout << "Yes" << endl;
  else
  cout << "No" << endl;
 return;
 }
  
int main(){
 cin.tie(nullptr);
 ios_base::sync_with_stdio(false);
 cout << fixed << setprecision(15);
 Main();
 return 0;
 }


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

using namespace std;

#define countof(a) (sizeof(a)/sizeof(*a))

#define vi vector<int>
#define vvi vector<vector<int> >
#define vpi vector<pi >
#define pi pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define all(n) n.begin(), n.end()

#define FROMTO(var, from, to) for (register int var = (from), var##down = ((int)(to)) < ((int)(from));var##down ? (var >= (int)(to)) : (var <= (int)(to));var##down ? var-- : var++)
#define UPTO(var, from, to)   for (register int var = (from); var <= ((int)to); var++)
#define DOWNTO(var, from, to) for (register int var = (from); var >= ((int)to); var--)
#define FOR(var, to)          UPTO(var, 0, (to)-1)
#define DOWN(var, from)       DOWNTO(var, (from)-1, 0) 

#define INIT(var, val) FOR(i,countof(var)) var[i] = val
#define INPUT(var) FOR(i,countof(var)) cin >> var[i]
#define INPUT1(var) FOR(i,countof(var)) cin >> var[i], var[i]--

#define SORT(v) qsort(v,countof(v),sizeof(*v),int_less)
#define SORTT(v) qsort(v,countof(v),sizeof(*v),int_greater)
#define QSORT(v,b) qsort(v,countof(v),sizeof(*v),b)

#define MOD 1000000007
#define INF ((1 << 30)-1)
#define LINF ((1LL << 62)-1)

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;

struct Comb {
    vector<vector<s64> > data;
    Comb(int n) { // O(n^2)
        data = vector<vector<s64> >(n+1,vector<s64>(n+1,1));
        UPTO(i,1,n) {
            FOR(j,i+1) {
                if (!j || j == i) data[i][j] = 1;
                else data[i][j] = data[i-1][j-1] + data[i-1][j];
            }
        }
    }
    
    s64 ncr(int n, int r) {
        return data[n][r];
    }
};

int int_less(const void *a, const void *b) {
  return (*((const int*)a) - *((const int*)b));
}
int int_greater(const void *a, const void *b) {
  return (*((const int*)b) - *((const int*)a));
}

#define mygc(c) (c)=getchar_unlocked()
int ri() {
	int k,m=0;int x=0;
	for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;x=x*10+k-'0';}if(m)x=-x;
	return x;
}

int main() {
	int n = ri();
	int k = ri();
 	int a[n];
	int b[n];
	FOR(i,n) a[i] = ri(), b[i] = ri();
	
	s64 res = 0;
	FOR(i,n) if ((k&a[i]) == a[i]) res += b[i];
	FOR(i,30) {
		if (k&(1<<i)) {
			int kk = k;
			kk&=~(1<<i);
			DOWN(j,i) kk |= (1<<j);
			s64 res_tmp = 0;
			FOR(j,n) {
				if ((kk&a[j]) == a[j]) {
					res_tmp += b[j];
				}
			}
			res = max(res, res_tmp);
		}
	}
	cout << res << endl;
	
    return 0;
}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define rep(i, n) repl(i, 0, n)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;

int sum(vector<vector<int>> A, int N, int x) {
    int summation = 0;
    rep(i, x + 1) {
        summation += A.at(0).at(i);
    }
    repl(i, x, N) {
        summation += A.at(1).at(i);
    }
    return summation;
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(false);
    
    int N;
    cin >> N;
    vector<vector<int>> A(2, vector<int>(N));
    rep(i, 2) {rep(j, N) cin >> A.at(i).at(j);}

    int Candies = 0, temp = 0;
    rep(x, N) Candies = max(Candies, sum(A, N, x));
    cout << Candies << endl;
    return 0;
}


Measured memory_bytes: 3656
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second

const int mx=100010;
const ll mod=1e9+7;

struct UnionFind {  
  vector<int> par,sizes;                     // par[i]:iの親の番号(ex)par[3]=2 : 3の親が2
  UnionFind(int n) : par(n),sizes(n,1)  {    //最初は全てが根であるとして初期化
    for(int i=0; i<n; i++){ par[i]=i; }  
  }
  int root(int x) {                       //xが属する木の根を再帰で得る:root(x)={xの木の根}
    if(par[x]==x){ return x; }
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {                 // xとyの木を併合
    int rx = root(x);                        //xの根をrx
    int ry = root(y);                        //yの根をry
    if(rx == ry){ return; }                  //xとyの根が同じ(=同じ木にある)時はそのまま
    if(sizes[rx]<sizes[ry]){ swap(rx, ry); } //xの木をおおきくする
    par[ry] = rx; 
    sizes[rx] += sizes[ry];                  //sizes[ry]はもう使わない
  }

  bool check(int x, int y) {                 // x,yが属する木が同じならtrueを返す
    return root(x) == root(y);
  }
  
  int size(int x){                           //xが含まれる木の大きさを返す
    return sizes[root(x)];
  }
};

int main() {
  int n,m; cin >> n >> m;
  UnionFind tree(n);
  rep(i,m){
    int x,y,c;
    cin >> x >> y >> c; x--; y--;
    tree.unite(x,y);
  }
  
  set<int> st;
  rep(i,n){
    st.insert(tree.root(i));
  }

  cout << st.size() << endl;
  return 0;
}


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

int main()
{
    constexpr size_t MOD = 1e9 + 7;
    size_t N, K;
    cin >> N >> K;

    vector<size_t> negative;
    vector<size_t> non_negative;
    for (size_t i = 0; i < N; i++)
    {
        long long A;
        cin >> A;
        if (A < 0)
        {
            negative.push_back(-A);
        }
        else
        {
            non_negative.push_back(A);
        }
    }
    size_t ans = 1;
    if (N == K)
    {
        for (size_t n : negative)
        {
            ans *= n;
            ans %= MOD;
        }
        for (size_t n : non_negative)
        {
            ans *= n;
            ans %= MOD;
        }
        if (negative.size() % 2)
        {
            ans = (MOD - ans) % MOD;
        }
    }
    else if (non_negative.empty() && K % 2 == 1)
    {
        // The answer is negative
        sort(negative.begin(), negative.end());
        for (size_t i = 0; i < K; i++)
        {
            ans *= negative[i];
            ans %= MOD;
        }
        ans = (MOD - ans) % MOD;
    }
    else
    {
        // The answer is non-negative
        sort(negative.rbegin(), negative.rend());
        sort(non_negative.rbegin(), non_negative.rend());
        non_negative.push_back(1);

        size_t i = 0;
        size_t j = 0;
        size_t count = 0;

        while (count < K)
        {

            if (i + 1 < non_negative.size() && ((j + 1) >= negative.size() || count + 2 > K || non_negative[i] * non_negative[i + 1] >= negative[j] * negative[j + 1]))
            {
                assert(i + 1 < non_negative.size());
                ans *= non_negative[i];
                ans %= MOD;
                i++;
                count++;
            }
            else
            {
                assert(j + 1 < negative.size());
                ans *= negative[j];
                ans %= MOD;
                ans *= negative[j + 1];
                ans %= MOD;
                j += 2;
                count += 2;
            }
        }
    }
    cout << ans << endl;
}


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

using namespace std;

int main()
{   int count=0,maxm=0;
    string s;
    cin>>s;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='R') count++;
        else count=0;
        maxm=max(maxm,count);
    }
    cout<<maxm;
    return 0;
}


Measured memory_bytes: 3576
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll v[100002];
int main () {
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);cout.tie(NULL);
	ll n,m,a,b;
	map<int,int> ma;
	cin>>n>>m;
	int i=1;
	while (n--){
		cin>>v[i];
		ma[i]=0;
		i++;
	}
	while (m--){
		cin>>a>>b;
		if (v[a]>v[b]) 
		{
			ma[b]++;
		}
		if (v[a]<v[b]) {
			ma[a]++;
		}
		if (v[a]==v[b]) {
			ma[a]++;ma[b]++;
		}
	}
	int c=0;
	for (i=1;i<=ma.size();i++){
		if (ma[i]==0) c++;
	}
	cout<<c;
	return 0;
}


Measured memory_bytes: 9096
#include<bits/stdc++.h>
using namespace std;
char a[100010][12];
bool camp(char x[],char y[]){
	for(int i=0;i<10;i++)if(x[i]!=y[i])return x[i]<y[i];
	return 0;
}
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%s",a[i]),sort(a[i],a[i]+10);
	char **re=new char * [100010];
	for(int i=1;i<=n;i++)re[i]=a[i];
	sort((char **)(re+1),(char **)(re+n+1),camp);
	//for(int i=1;i<=n;i++,cout<<endl)for(int j=1;j<=10;j++)cout<<re[i][j];
	for(int i=1;i<n;i++)a[i][11]=camp(re[i],re[i+1]); 
	long long cnt=0;
	for(int i=1,j=1;i<n;i=j+1,j=i){
		while(j<n&&!a[j][11])j++;
		cnt+=1LL*(j-i+1)*(j-i)/2;
	}
	cout<<cnt<<endl;
}

Measured memory_bytes: 2176
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

int main() {
  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  if (A + B < C + D) {
    cout << "Right" << endl;
  } else if (A + B > C + D) {
    cout << "Left" << endl;
  } else {
    cout << "Balanced" << endl;
  }
  return 0;
}

Measured memory_bytes: 3608
x,k,d = gets.split.map(&:to_i)
x = x.abs
if x > k*d
    puts x.abs-k*d
    exit 0
end

# x%d or (x%d-d).abs

if ((x-x%d)/d)%2 == k%2
    puts x%d
else
    puts (x%d-d).abs
end

Measured memory_bytes: 14316
#include <iostream>
#include <bits/stdc++.h>
#define ll long long int 
#define pb push_back
#define mp make_pair
#define P pair
#define M 998244353
#define fr(i,n) for(int i=0;i<n;i++)
#define M2 1000000007
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define CASES  ll t;cin>>t;while(t--)
#define Take_n ll n; cin>>n;
using namespace std;
 
int main()
{
    int n;
    cin>>n;
    vector<int> h(n);
    for(int i=0; i<n; i++) cin>>h[i];
    vector<int> dp(n,INT_MAX);
    dp[0]=0;
    for(int i=1; i<n; i++){
        if(i>=2) dp[i]=min(dp[i],dp[i-2]+abs(h[i]-h[i-2]));
        dp[i]=min(dp[i],dp[i-1]+abs(h[i]-h[i-1]));
    }
    cout<<dp[n-1];
    
    return 0;
}

Measured memory_bytes: 3972
debug = False
if debug:
    a = [2, 7, 1, 8, 2, 8, 1, 8]
    a = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5]
    N = len(a)
else:
    N = int(input())
    a = list(map(int, input().split()))

assert len(a) == N
d = {}
for ai in a:
    if ai in d:
        d[ai] += 1
    else:
        d[ai] = 1
s = 0
for k, v in d.items():
    t = min(abs(k - v), v)
    t = 0
    if k > v:
        t = v
    elif k == v:
        pass
    else:
        t = v - k
    s += t

print(s)


Measured memory_bytes: 18156
#include <algorithm>
#include <cstdint>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <cfloat>
#include <bitset>

using namespace std;

using lint=int64_t;
using uint=uint32_t;
using ulint=uint64_t;

template<class T>
using vector2d=vector<vector<T>>;

template<class T>
bool UpdateMax(T &a, const T &b){
  if(a<b){
    a=b;
    return true;
  }else{
    return false;
  }
}

template<class T>
bool UpdateMin(T &a, const T &b){
  if(a>b){
    a=b;
    return true;
  }else{
    return false;
  }
}

template <class T>
void OutVec(const vector<T>& vec){
  for(int i = 0; i < vec.size() - 1; ++i){
    cout<<vec[i]<<" ";
  }
  cout<<vec.back()<<endl;
}

template <class T>
void OutVec2d(const vector2d<T>& vec){
  for(auto v:vec){
    OutVec(v);
  }
}

int main(){
  cout << std::fixed << std::setprecision(16);
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int h,w,k;
  cin>>h>>w>>k;
  int res=0;
  vector2d<char> field(h, vector<char>(w));
  for(int r=0;r<h;r++){
    for(int c=0;c<w;c++){
      cin>>field[r][c];
    }
  }

  for(int i=0;i<(1<<h);i++){
    for(int j=0;j<(1<<w);j++){
      bitset<6> bsr(i),bsc(j);
      int cnt=0;
      for(int r=0;r<h;r++){
        for(int c=0;c<w;c++){
          if(bsr[r]&&bsc[c]&&field[r][c]=='#'){
            cnt++;
          }
        }
      }
      if(cnt==k){
        res++;
      }
    }
  }
  cout<<res<<endl;


  return 0;
}




Measured memory_bytes: 3636
#include "bits/stdc++.h"

using namespace std;

using pii = pair<int, int>;

int main() {
    int n; cin >> n;
    vector<pii> jobs;
    for (int i = 0; i < n; ++i) {
        int a, b; cin >> a >> b;
        jobs.emplace_back(b, a);
    }
    
    sort(jobs.begin(), jobs.end(), [](const pii& l, const pii& r) {
        if (l.first == r.first) return l.second > r.second;
        return l.first > r.first;
    });
    
    int next_start = jobs.front().first;
    for (pii& x : jobs) {
        if (next_start > x.first) {
            next_start = x.first;
        }
        next_start -= x.second;
        if (next_start < 0) {
            cout << "No" << endl;
            return 0;
        }
        
        // cout << x.first << ", " << x.second << endl;
    }
    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 2420
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) for(int i=(n)-1;0<=i;--i)
#define each(e,v) for(auto&& e:(v))
#define all(v) begin(v),end(v)
#define dump(x) cerr<<#x<<": "<<(x)<<endl
#define INF 100000000
#define ll long long
#define v std::vector
using std::cin;
using std::cout;
using std::endl;
using std::ios;

ll int N, M, ans = -INF;

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

    cin >> N >> M;
    v<ll int> x(N), y(N), z(N);
    rep(i, N) cin >> x[i] >> y[i] >> z[i];
    if(M == 0) {cout << 0 << endl; return 0;}
    rep(bitO, 8) {
        v<ll> temp(N, 0);
        rep(i, N) {
            ll int a = x[i] * (bitO & 1 ? 1 : -1);
            ll int b = y[i] * (bitO & 2 ? 1 : -1);
            ll int c = z[i] * (bitO & 4 ? 1 : -1);
            temp[i] = a + b + c;
        }
        std::sort(temp.begin(), temp.end() , std::greater<ll int>());
        rep(i, M - 1) temp[i + 1] += temp[i];
        ans = std::max(ans, temp[M - 1]);
    }

    cout << ans << endl;
}


Measured memory_bytes: 256
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=map(int,input().split())
    B=[a-i for i,a in enumerate(A)]
    if n==1:
        print(0)
        return
    B.sort()
    f=lambda x:sum(abs(x-b) for b in B)
    ans=min(f(B[n//2]),f(B[n//2+1]))
    print(ans)
resolve()

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

using namespace std;

const int mod = 1e9 + 7;

int main()
{
  string S;
  cin >> S;
  if(stoi(S) % (accumulate(begin(S), end(S), 0) - '0' * S.size()) == 0) puts("Yes");
  else puts("No");

}



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

int main(){
    int n; cin >> n;
    int p[n]; for(int i=0; i<n; i++) cin >> p[i];
    int count = 0;
    for(int i=0; i<n-2; i++){
        if(p[i]<p[i+1] && p[i+1]<p[i+2]) count++;
        if(p[i]>p[i+1] && p[i+1]>p[i+2]) count++;
    }
    cout << count << endl;
    return 0;
}

Measured memory_bytes: 256
//Volume1-0140
import java.util.ArrayList;
import java.util.Scanner;

class Main {
	public static void main(String[] args){

		int     start,end,p;
		boolean flg;
		ArrayList<Integer> ans  = new ArrayList<Integer>();
		int[]              stop = {6,7,8,9,5,4,3,2,1,0,1,2,3,4,5,6,7,8};

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		while(n-- > 0){
			ans.clear();
			start = sc.nextInt();
			end   = sc.nextInt();

			if   (start > end){p = -1;}
			else              {p =  1;}

			if(0 <= start && start <= 5){
				for(int i=start;i!=end;i+=p){
					ans.add(i);
				}
				ans.add(end);
			} else {
				flg = false;
				for(int i=0;i<stop.length;i++){
					if(start==stop[i]){
						flg = true;
					}
					if(flg){
						ans.add(stop[i]);
						if(stop[i]==end){
							break;
						}
					}
				}
			}
			print(ans);
		}
	}

	private static void print(ArrayList<Integer> ans){
		for(int i=0;i<ans.size();i++){
			System.out.print(ans.get(i));
			if(i<ans.size()-1){
				System.out.print(" ");
			}
		}
		System.out.println();
	}
}

Measured memory_bytes: 17188
#include <stdio.h>

int min(int a, int b, int c, int d)
{
	int r;
	
	if (a < b){
		r = a;
	}
	else {
		r = b;
	}
	if (c < r){
		r = c;
	}
	if (d < r){
		r = d;
	}
	
	return (r);
}

int main(void)
{
	int N;
	int i, K;
	int x, y;
	
	scanf("%d %d", &N, &K);
	
	for (i = 0; i < K; i++){
		scanf("%d %d", &x, &y);
		
		printf("%d\n", min(x - 1, y - 1, N - x, N - y) % 3 + 1);
	}
	
	return (0);
}

Measured memory_bytes: 596
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    x,y = LI()
    if x >= y and x % y == 0:
        return -1

    return x*y-x


print(main())




Measured memory_bytes: 7884
#include<iostream>
#include<string>
#include<vector>
using namespace std;
 
int main(){
    string S;
    cin >> S;
 
    vector<string> tokens = { "dreamer", "dream","erase", "eraser"};
 
    while (S.size() > 0) {
        bool hit = false;
        for(int i=0; i<tokens.size(); i++) {
            string t = tokens[i];
            if (S.size() >= t.size() && S.substr(S.size() - t.size(), t.size()) == t) {
                S = S.substr(0, S.size() - t.size());
                hit = true;
                break;
            }
        }
        if (!hit) {
            cout << "NO" << endl;;
            return 0;
        }
    }
 
    cout << "YES" << endl;;
    return 0;
}


Measured memory_bytes: 3700
N  = gets.to_i
AB = N.times.map{ gets.split.map(&:to_i) }

AB.sort!{ |a, b| a[1] <=> b[1] }

lapse = 0
AB.each do | a, b |
  lapse += a
  if lapse > b
    puts 'No'
    exit
  end
end
puts 'Yes'

Measured memory_bytes: 18524
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define pii pair <int, int>
#define fi first
#define sc second
#define ALL(x) x.begin(), x.end()
#define RALL(X) x.begin(), x.end()
#define FOR(i, n, k) for(i=0; i<n; i+=k)
#define FO(i, n, k) for(i=1; i<=n; i+=k)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define N 100005
#define mid ((l+r)/2)
#define dbg(x) (cerr << #x << " : " << x << endl)
#define endl "\n"
#define MOD 1000000007

using namespace std;

typedef long long int lli;
int broken[100005];
lli dp[100005];

int main(){
	ios_base::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	for(int i=0; i<m; i++){
		int x;
		cin >> x;
		broken[x] = 1;
	}

	dp[0] = 1;
	if(!broken[1])
		dp[1] = 1;
	for(int i=2; i<=n; i++){
		if(!broken[i]){
			dp[i] += (dp[i-1] + dp[i-2]) % MOD;
			dp[i] %= MOD;
		}
		
	}

	cout << dp[n] % MOD;

	return 0;
}

Measured memory_bytes: 1408
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++) 
#define rrep(i,n) for(int i = 1; i <= (int)(n); i++) 
#define pb push_back
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<char> vc;
typedef long long ll;
typedef unsigned long long ull;

//const ll mod = 1000000007;
//const ll INF = mod * mod;
//const double pi = 3.14159265358979;

int main() {
    int n,k;
    cin >> n >> k;
    vi a(n,0);
    rep(i,k) {
        int d;
        cin >> d;
        rep(j,d) {
            int x;
            cin >> x;
            x--;
            a[x]++;
        }
    }
    int ans = 0;
    rep(i,n) {
        if(a[i] == 0) ans++;
    }
    cout << ans<< endl;
}

Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
#define N 200005
#define L first
#define R second
#define mk make_pair
#define pa pair<int,int>
int a[N],lg[N];
pa v[2][20][N];
priority_queue<pair<int,pa > > q;
int n;
int rmq(int tp,int l,int r){
	int k=lg[r-l+1];
	return min(v[tp][k][l],v[tp][k][r-(1<<k)+1]).R;
}
void push(int l,int r){
	if (r<l) return;
	q.push(mk(-a[rmq((l&1)^1,l,r)],pa(l,r)));
}
int main(){
	scanf("%d",&n);
	a[0]=1e9;
	for (int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for (int i=2;i<=n;i++)
		lg[i]=lg[i>>1]+1;
	for (int i=1;i<=n;i++)
		v[0][0][i]=pa((i&1)?a[i]:1e9,i);
		
	for (int i=1;i<=n;i++)
		v[1][0][i]=pa((i&1)?1e9:a[i],i);
	for (int j=1;j<=17;j++)
		for (int k=1;k+(1<<j)-1<=n;k++){
			v[0][j][k]=min(v[0][j-1][k],v[0][j-1][k+(1<<(j-1))]);
			v[1][j][k]=min(v[1][j-1][k],v[1][j-1][k+(1<<(j-1))]);
		}
	push(1,n);
	while (!q.empty()){
		pa x=q.top().R; q.pop();
		int md1=rmq((x.L&1)^1,x.L,x.R);
		int md2=rmq((x.L&1)^0,md1,x.R);
		push(x.L,md1-1);
		push(md1+1,md2-1);
		push(md2+1,x.R);
		printf("%d %d ",a[md1],a[md2]);
	}
}

Measured memory_bytes: 62072
#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 si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#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 vc = vector<char>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
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;
    int K;
    cin >> K;
    
    vi P(N);
    in(N,P);
    rep(i,N) P[i]--;
    vl C(N);
    in(N,C);
    
    vvl D(N,vl(N+1,-infl));
    rep(i,N) D[i][0] = 0;
    
    vi AA(N,1);
    vl BB(N,-infi);
    vl CC(N);
    
    rep(i,N) {
        int a = i;
        rep(j,N){
            D[i][j+1] = D[i][j] + C[P[a]];
            chmax(BB[i],D[i][j+1]);
            a = P[a];
            if(a == i){
                CC[i] = D[i][j+1];
                break;
            }
            AA[i]++;
        }
    }
    
    //outmat(D);
    //outve(AA);
    //outve(BB);
    //outve(CC);
    
    ll ans = -infl;
    
    rep(i,min(N,K)){
        rep(j,N) chmax(ans,D[j][i+1]);
    }
    
    rep(i,N){
        if(CC[i] <= 0) continue;
        if(K > AA[i]){
            int bb = K/AA[i];
            chmax(ans,CC[i]*bb);
            chmax(ans,(bb-1)*CC[i]+BB[i]);
            rep(k,K-bb*AA[i]){
                chmax(ans,CC[i]*bb+D[i][k+1]);
            }
        }
    }

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


Measured memory_bytes: 199068
using static System.Math;
using System;

public class hello
{
    public static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var a = Array.ConvertAll(line, int.Parse);
        Array.Sort(a);
        Console.WriteLine(Abs(a[0] + a[3] - a[1] - a[2]));
    }
}



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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
typedef pair<int, ll> il;
typedef vector<ii> vii;
typedef vector<il> vil;
typedef vector<li> vli;

#define F first
#define S second
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
#define sz size()
#define all(A)    A.begin(), A.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define f0(i,b)   for(int i=0;i<(b);i++)
#define f1(i,b)   for(int i=1;i<=(b);i++)
#define f2(i,a,b) for(int i=(a);i<=(b);i++)
#define fr(i,b,a) for(int i=(b);i>=(a);i--)
#define rep(i,a,b,c) for(int i=(a);i!=(b);i+=(c))
#define mod 1000000007

#define forr(i,n)    for(i=0; i<n; i++)
#define whilee(i,t)  while(i!=t)
#define  sl(a)  scanf("%lld",&a)
#define  pl(a)  printf("%lld",a)

//for(auto it =A.begin(); it!=A.end(); it++)
//sort(A.begin(),A.end());
//binary_search(A.begin(),A.end(),x);
//reverse(A.begin(), A.end());
//cout << *max_element(A.begin(), A.end());
//cout << *min_element(A.begin(), A.end());
//count(A.begin(), A.end(), X); //counts the occurrences of X
//distance(A.begin(),A.end());  //distance between first to last element
//accumulate(A.begin(), A.end(), 0); //add
//next_permutation(A.begin(), A.end());
//prev_permutation(A.begin(), A.end());
//swap(S[0], T[0]);
//scanf("%d",&x);
//printf("%d\n",x);

//vector <int> A;
//deque <int> A;
//queue <int> A;
//stack <int> A;
//set <int> A;
//map <int,int> A;
//pair <int,int> A ;
//vector <pair <int,int>> A ;
//char ch;
//string S, T, U;
ll i, j, k, a, b, c, d, f, g, h, l, m, n, p, q, r, s, t, w, x, y, z, maxx=INT_MIN, minn=INT_MAX, ans=1, sum=0, cnt=0;

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

    cin>>n>>p;
    if(n==1)
    {
        cout<<p<<endl;
        return 0;
    }
    for(i=2; i*i<=p; i++)
    {
        cnt=0;
        while(p%i==0)
        {
            cnt++;
            p/=i;
            if(cnt==n)
            {
                ans*=i;
                cnt=0;
            }
        }
    }
    cout<<ans<<endl;

    return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
#define deb(x) cout<<#x<<" "<<x<<endl;
using namespace std;



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

	int n; cin >> n;
	int a[n];
	int c = 0, mn = (int)2e9;
	for (int i = 0; i < n; i++) cin >> a[i];
	for (int i = 0; i < n; i++) if (mn > a[i]) {mn = a[i]; c++;}
	cout << c;

}



Measured memory_bytes: 4300
#include <iostream>
#include <vector>

#define MOD 1000000007

using namespace std;

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

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

    vector<long long int> s(n + 1);
    s[0] = 0;
    for (int i = 0; i < n; i++) {
        s[i + 1] = s[i] + a[i];
    }

    long long int result = 0;
    long long int temp = 0;

    for (int i = 0; i < n - 1; i++) {
        temp = (s[n] - s[i + 1]) % MOD;
        temp = (a[i] * temp) % MOD;
        result = (temp + result) % MOD;
    }

    cout << result << endl;

    return 0;
}

Measured memory_bytes: 6372
# AとBの値を取得
A, B = map(int, input().split())
# Cの値を設定
C = 'Yes'
# AとBを掛け合わせた結果が奇数かどうか判断
if (A * B) % 2 == 0:
    C = 'No'
# 出力
print(C)


Measured memory_bytes: 2940