akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <numeric>
#include <random>
#include <algorithm>
#include <functional>

using namespace std;
typedef long long ll;

const int INF = (1 << 30) - 1;
const ll INFLL= (1LL << 61) - 1;
const int MOD = 1000000007;
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll N;
  cin>>N;
  ll ans=INFLL;
  FOR(i,1,4000000){
    if(N%i==0){
      ans=min(ans,i+N/i);
    }
  }
  cout<<ans-2<<endl;
}


Measured memory_bytes: 384
using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
namespace AtCoder
{
    class Program
    {
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }

        //static long Mod = (long)1e9 + 7;
        static void Main(string[] args)
        {
            //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            var N = Sarray()[0];
            var both = 0L;
            var left = 0L;
            var right = 0L;
            var ab = 0L;
            for (var i = 0; i < N; ++i)
            {
                var s = ReadLine();
                for (var j = 0; j < s.Length - 1; ++j)
                    if (s[j] == 'A' && s[j + 1] == 'B')
                        ++ab;
                if (s.First() == 'B' && s.Last() == 'A')
                    ++both;
                else if (s.First() == 'B')
                    ++left;
                else if (s.Last() == 'A')
                    ++right;
            }
            var bab = Max(0, both - 1);
            var lrab = Min(left, right);
            var blrab = (left + right != 0 && both != 0) ? 1 : 0;
            WriteLine(ab + bab + lrab + blrab);
        }
    }
}

Measured memory_bytes: 13396
import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();
        long l = sc.nextLong();
        long r = sc.nextLong();
        long x = l % 2019;
        if (x + (r - l) >= 2019) {
            System.out.println(0);
            return;
        }
        long ans = Long.MAX_VALUE;
        for (long i = l; i <= l + Math.min(2019, r - l); i++) {
            for (long j = l; j <= l + Math.min(2019, r - l); j++) {
                if (i == j) {
                    continue;
                }
                long res = (i * j) % 2019;
                ans = Math.min(ans, res);
            }
        }
        System.out.println(ans);

    }
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        } else {
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() {
        if (hasNextByte())
            return buffer[ptr++];
        else
            return -1;
    }

    private static boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }

    public boolean hasNext() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr]))
            ptr++;
        return hasNextByte();
    }

    public String next() {
        if (!hasNext())
            throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext())
            throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while (true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if (b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
            throw new NumberFormatException();
        return (int) nl;
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }
}


Measured memory_bytes: 23124
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::cpp_int;
using namespace std;

#if __has_include("print.hpp")
  #include "print.hpp"
#endif

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007

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

typedef long long ll;
typedef pair<ll, ll> p;


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

  ll res = 0;
  for (ll i = 2; i*i <= n; ++i) {
    ll tmp = n;
    while(tmp % i == 0){
      tmp /= i;
    }
    if(tmp % i == 1) res++;

    if(i * i != n && n / i != i){
      tmp = n;
      ll x = tmp / i;
      while(tmp % x == 0){
        tmp /= x;
      }
      if(tmp % x == 1) res++;
    }
  }
  res++;
  if(n > 2) res++;
  cout << res << endl;
}


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

typedef long long ll;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const ll INF = 1<<21;
// static const ll MOD = 1e9 + 7;

bool compPair(const pair<int, int>& arg1, const pair<int, int>& arg2) {
    return arg1.first > arg2.first;
}

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

    vector<int> a(3, -1);

    int s, c;
    for(int i=0;i<m;i++) {
        cin >> s >> c; s--;
        if(a.at(s)!=-1&&a.at(s)!=c) { 
            cout << -1 << endl;
            return 0;
        }
        else a.at(s) = c;
    }

    for(int i=0;i<n;i++) {
        if(n!=1&&a.at(0)==0) { 
            cout << -1 << endl;
            return 0;
        }
        if(n==1&&a.at(0)==0) { 
            cout << 0 << endl;
            return 0;
        }

        if(n!=1&&i==0&&a.at(i)==-1) {
            cout << 1;
            continue;
        }
        if(n==1&&i==0&&a.at(i)==-1) {
            cout << 0 << endl;
            return 0;
        }

        if(i!=0&&a.at(i)==-1) {
            cout << 0;
            continue;
        }

        cout << a.at(i);
    }

    cout << endl;
    return 0;
}


Measured memory_bytes: 256
import java.util.*;
public class Main {
    public static void main(String[] args) throws java.io.IOException{
        Scanner scan = new Scanner(System.in);
        HashMap<Character,String> ep =new HashMap<Character,String>();
        HashMap<String,Character> dp =new HashMap<String,Character>();

		ep.put(' ', "101");
		ep.put('\'', "000000");
		ep.put(',', "000011");
		ep.put('-', "10010001");
		ep.put('.', "010001");
		ep.put('?', "000001");
		ep.put('A', "100101");
		ep.put('B', "10011010");
		ep.put('C', "0101");
		ep.put('D', "0001");
		ep.put('E', "110");
		ep.put('F', "01001");
		ep.put('G', "10011011");
		ep.put('H', "010000");
		ep.put('I', "0111");
		ep.put('J', "10011000");
		ep.put('K', "0110");
		ep.put('L', "00100");
		ep.put('M', "10011001");
		ep.put('N', "10011110");
		ep.put('O', "00101");
		ep.put('P', "111");
		ep.put('Q', "10011111");
		ep.put('R', "1000");
		ep.put('S', "00110");
		ep.put('T', "00111");
		ep.put('U', "10011100");
		ep.put('V', "10011101");
		ep.put('W', "000010");
		ep.put('X', "10010010");
		ep.put('Y', "10010011");
		ep.put('Z', "10010000");
        
		dp.put("00000", 'A');
		dp.put("00001", 'B');
		dp.put("00010", 'C');
		dp.put("00011", 'D');
		dp.put("00100", 'E');
		dp.put("00101", 'F');
		dp.put("00110", 'G');
		dp.put("00111", 'H');
		dp.put("01000", 'I');
		dp.put("01001", 'J');
		dp.put("01010", 'K');
		dp.put("01011", 'L');
		dp.put("01100", 'M');
		dp.put("01101", 'N');
		dp.put("01110", 'O');
		dp.put("01111", 'P');
		dp.put("10000", 'Q');
		dp.put("10001", 'R');
		dp.put("10010", 'S');
		dp.put("10011", 'T');
		dp.put("10100", 'U');
		dp.put("10101", 'V');
		dp.put("10110", 'W');
		dp.put("10111", 'X');
		dp.put("11000", 'Y');
		dp.put("11001", 'Z');
		dp.put("11010", ' ');
		dp.put("11011", '.');
		dp.put("11100", ',');
		dp.put("11101", '-');
		dp.put("11110", '\'');
		dp.put("11111", '?');
        
        while(scan.hasNext()){
        	char [] line= scan.nextLine().toCharArray();
        	StringBuilder tmp=new StringBuilder();
        	for(char c:line)
        		tmp.append(ep.get(c));
        //	System.out.println(tmp);
        	while(tmp.length()%5!=0)tmp.append('0');
     
        	String splitedCode =tmp.toString();
        	StringBuilder sb = new StringBuilder();
        	for(int i=0;i<splitedCode.length();i+=5)
        		sb.append(dp.get(splitedCode.substring(i,i+5)));
        	System.out.println(sb);
        }
    }
}

Measured memory_bytes: 12000
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()
 
using namespace std;
using ll = long long;
using pii = pair<int, int>;
 
const int oo = 1e9 + 7;
const int mod = 1e9 + 7, maxn = 100100;
const long double PI = acos(-1);
 
int main (){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m, ac = 0, wa = 0;
    cin >> n >> m;

    vector<int> v(n+1, 0);
    string s;

    for (int i=0; i<m; i++){
        int p;
        cin >> p >> s;
        if (s == "WA" and v[p] != -1) v[p]++;
        if (s == "AC" and v[p] != -1){
            ac++;
            wa+=v[p];
            v[p] = -1;
        }
    }

    cout << ac << " " << wa << endl;
    return 0;
}

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

using namespace std;
int n;
pair<int,int> ps[100005];
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
    return a.first > b.first;
}

int main()
{
    set<int> ss;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%d",&ps[i].first);
        ps[i].second = i+1;
    }
    sort(ps,ps+n,cmp);
    ss.insert(ps[0].second);
    int l,r;
    long long ans = 0;
    for(int i=1;i<n;++i)
    {
        auto pos = ss.upper_bound(ps[i].second);
        if(pos!=ss.end()){
            auto pos2 = pos;++pos2;
            if(pos2==ss.end()) r=n+1;
            else r = *pos2;
            if(pos!=ss.begin()){
                pos2 = pos;--pos2;
                l = *pos2;
            }else l=0;
            ans += 1LL*(ps[i].second-l)*(r-*pos)*ps[i].first;
        }
        auto pos3 = pos;

        if(pos!=ss.begin()){
            --pos;
            auto pos2 = pos;
            if(pos2==ss.begin()) l = 0;
            else {
                --pos2;l=*pos2;
            }
            if(pos3==ss.end()) r= n+1;
            else {
                r = *pos3;
            }
            ans += 1LL*(*pos-l)*(r-ps[i].second)*ps[i].first;
        }
        ss.insert(ps[i].second);
        //cout << ps[i].first << " "<<ans << endl;
    }

    printf("%lld\n",ans);
    return 0;
}


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

int main(void){
	
	int a, b, c, d;

	std::cin >> a >> b >> c >> d;

	bool flag = false;

	if (std::abs(a - c) <= d || (std::abs(a - b) <= d && std::abs(b - c) <= d)) {
		std::cout << "Yes" << std::endl;
	}
	else {
		std::cout << "No" << std::endl;
	}

	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
//251103FD
//4008802802
using namespace std;
#define PB pop_back
#define PF pop_front
#define np next_permutation
#define eb emplace_back
#define pb push_back
#define bf push_front
#define mp make_pair
#define s second
#define f first
#define foreach(it, a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)
#define Foreach(it, a) for(__typeof((a).rbegin()) it=(a).rbegin(); it!=(a).rend(); it++)
#define forn(i, a) for(register int i=0; i<a; i++)
#define Forn(i, a) for(register int i=1; i<=a; i++)
#define memINF 127
#define all(x) x.begin(), x.end()
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<pi,int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef unsigned int uint;
typedef vector<vector<ll> > matrix;

inline int in() 
{
    int x=0, c;
    while((uint)((c=getchar())-48)>=10) 
		if(c=='-') 
			return -in();
    do 
	{ x=(x<<3)+(x<<1)+(c^48); }
	while((uint)((c=getchar())^48)<10);
    return x;
}
inline int lcm(int a, int b){return a*b/__gcd(a, b);}

const int MAX=1e5+5;
const int MAX_LOG=17;

int N, a, res=INT_MAX;
map<int,int>MP1, MP0;
vector<pi> odd, even;

inline int cal(pi a1, pi a2)
{
    if(a1.s==a2.s)
        return N-max(a1.f, a2.f);
    return N-a1.f-a2.f;
}

int main()
{
    N=in();
    MP0.clear();
    MP1.clear();
    Forn(i, N)
    {
        a=in();
        if(i&1)
           MP1[a]++;
        else
           MP0[a]++;
    }
    foreach(it, MP0)
    {
        odd.eb(mp(it->s, it->f));
        sort(odd.rbegin(), odd.rend());
        while(odd.size()>10)
            odd.pop_back();
    }
    foreach(it, MP1)
    {
        even.eb(mp(it->s, it->f));
        sort(even.rbegin(), even.rend());
        while(even.size()>10)
            even.pop_back();
    }
    foreach(it, odd)
        foreach(ii, even)
            res=min(res, cal(*it, *ii));
    printf("%d\n", res);
    return 0;
}

Measured memory_bytes: 4992
import java.io.*;

public class Main{

    public static void main(String[]args)throws IOException{

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        String keyword = br.readLine();
        keyword = keyword.toLowerCase();
        int count = 0;
        
        while(!(str = br.readLine()).equals("END_OF_TEXT")){
            str = str.toLowerCase();
            String[] word = str.split(" ");

            for(int i=0; i<word.length; i++){
                if(word[i].equals(keyword)){
                    count++;
                }
            }      
        }
        System.out.println(count);
    }
}

Measured memory_bytes: 24332
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;

int main(){
	int n,m;
	cin >> n >> m;
	vector<int> number(n+m);
	int count = 0;
	for( int i=0; i<n; i++ ) number[i] = 2;
	for( int j=n; j<n+m; j++ ) number[j] = 1;
	for( int i=0; i<n+m; i++ ){
		for( int j=0; j<n+m; j++ ){
			if( i==j ) continue;
			if(fmod( number[i]+number[j] , 2 )==0 ) count++;
		}
	}
	cout << count/2 << endl;
	return 0;
}


Measured memory_bytes: 512
<?php
define("MOD", 10**9+7);

//$x = (int)trim(fgets(STDIN));
//$T = trim(fgets(STDIN));
//list($a,$b,$c,$d) = array_map('intval',explode(" ",trim(fgets(STDIN))));
$A = array_map('intval',explode(" ",trim(fgets(STDIN))));

$m = 10**9*10**9*-10;
for($i=0;$i<2;$i++) {
  for($j=0;$j<2;$j++) {
   $m = max($m,$A[$i]*$A[$j+2]);
  }
}
printf("%d\n",$m);


Measured memory_bytes: 16156
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# rstrip().decode('utf-8')
# INF=float("inf")
# MOD=10**9+7
# sys.setrecursionlimit(2147483647)
# import math
# import numpy as np
# import operator
# import bisect
# from heapq import heapify,heappop,heappush
# from math import gcd
# from fractions import gcd
# from collections import deque
# from collections import defaultdict
# from collections import Counter
# from itertools import accumulate
# from itertools import groupby
# from itertools import permutations
# from itertools import combinations
# from scipy.sparse import csr_matrix
# from scipy.sparse.csgraph import floyd_warshall
# from scipy.sparse.csgraph import csgraph_from_dense
# from scipy.sparse.csgraph import dijkstra
# map(int,input().split())

def knapsack(N, W, weight, value):
	# 初期化
	inf = float("inf")
	dp = [[-inf for i in range(W + 1)] for j in range(N + 1)]
	for i in range(W + 1): dp[0][i] = 0
	
	# DP
	for i in range(N):
		for w in range(W + 1):
			if weight[i] <= w:  # dp[i][w-weight[i]の状態にi番目の荷物が入る可能性がある
				dp[i + 1][w] = max(dp[i][w - weight[i]] + value[i], dp[i][w])
			else:  # 入る可能性はない
				dp[i + 1][w] = dp[i][w]
	return dp


def main():
	n, t = map(int, input().split())
	AB = []
	A = []
	B = []
	for _ in range(n):
		a, b = map(int, input().split())
		AB.append((a, b))
	AB.sort()
	for a,b in AB:
		A.append(a)
		B.append(b)
	dp= knapsack(n, t, A, B)
	
	ans=0
	for i in range(n):
		ans=max(ans,dp[i][t-1] + AB[i][1])
	print(ans)


if __name__ == '__main__':
	main()


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

// def
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define repeat(i, a, b) for (int i = (a); i < (b); i++)
#define revrepeat(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
const int M = 1e9 + 7;
const int INF = 1e9;
const int MAX_V = 1000 + 5;
const int MAX_N = 1e5 + 5;

int main() {
    int n, d;
    cin >> n >> d;
    int a = n;
    int b = d * 2 + 1;
    cout << (a + b - 1) / b << endl;
}


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

int main(void){
    int n;
    cin >> n;
    vector<vector<int>> a(2, vector<int>(n));
    for (int i=0; i<2; i++){
        for (int j=0; j<n; j++) cin >> a[i][j];
    }
    for (int i=1; i<n; i++){
        a[0][i] += a[0][i-1];
        a[1][n-1-i] += a[1][n-i];
    }
    int ans = 0;
    for (int i=0; i<n; i++){
        ans = max(ans, a[0][i]+a[1][i]);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3612
import java.util.Scanner;
public class Main{
	public static void main(String[]args){
	Scanner scan=new Scanner(System.in);
	for(;;) {
		String i=scan.next();
		if(i.equals("0")) {
			break;
		}
		int a=0;
		for(int j=0;j<i.length();j++) {
			char ch=(i.charAt(j));
			String str=""+ch;
			a+=Integer.valueOf(str);
		}
		System.out.println(a);
	}
	}
}


Measured memory_bytes: 26744
#include<bits/stdc++.h>
#include <math.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define lper(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9;
const ll INF=4e18;
const ll dy[4]={1,0,-1,0};
const ll dx[4]={0,-1,0,1};
int main() {
  int a;
  cin >>a;
  int y=3*a*a;
  cout << y<< endl;
}

Measured memory_bytes: 256
h,w=map(int,input().split())
s=[]
for i in range(h):s.append(list(input()))
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
for i in range(h):
  for j in range(w):
    if s[i][j]=='#':
      flag=False
      for k in range(4):
        x = j+dx[k];y=i+dy[k]
        if 0 <= x and x <= w-1 and 0 <= y and y <= h-1:
          if s[y][x]=="#":flag=True
      if not flag:
        print('No')
        exit()
 
print('Yes')

Measured memory_bytes: 3064
S = input()
ch = ["dream", "dreamer", "erase", "eraser"]
while S:
  N = len(S)
  if S[N-5:] == ch[0] or S[N-5:] == ch[2]:
    S = S[:N-5]
  elif S[N-6:] == ch[3]:
    S = S[:N-6]
  elif S[N-7:] == ch[1]:
    S = S[:N-7]
  else:
    print("NO")
    exit()
print("YES")

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

using namespace std;


int main(){
    int n;
    cin >> n;
    vector<vector<int>> dp(n,vector<int>(3,0));
    vector<int> a(n),b(n),c(n);
    for(int i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i];
    
    dp[0][0] = a[0]; dp[0][1] = b[0]; dp[0][2] = c[0];
    for(int i =1; i < n; i++){
        dp[i][0] = max(dp[i-1][1]+a[i],dp[i-1][2]+a[i]);
        dp[i][1] = max(dp[i-1][0]+b[i],dp[i-1][2]+b[i]);
        dp[i][2] = max(dp[i-1][0]+c[i],dp[i-1][1]+c[i]);
    }
    cout << max(dp[n-1][0],max(dp[n-1][1],dp[n-1][2]));
    
}

Measured memory_bytes: 6912
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author baito
 */
public class Main {

    /**
     * @param args the command line arguments
     * @throws java.lang.Exception
     */
    static int n,k,total,max;
    static int[] w  ;
    public static void main(String[] args)throws Exception {
        // TODO code application logic here
        ContestScanner sc = new ContestScanner();
        n = sc.nextInt();
        k = sc.nextInt();
        w = new int[n];
        for (int i = 0; i < n; i++) {
            w[i] = sc.nextInt();
            total += w[i];
            max = Math.max(max, w[i]);
        }
        int answer = solve();
        System.out.println(answer);
    }
    public static int check(int max) {
        int sum = 0,i=0,j;
        for (j = 0; j < k;j++) {
            while(sum + w[i] <= max){
                sum += w[i];
                if(i == n - 1 )return n ;
                i++;
            }
            sum = 0;
            
        }
        return i;
    }
    public static int solve() {
        int first = max;
        int last = total  ,ans;
        while(- first + last > 1){
            int mid = (first + last)/2;
            ans = check(mid);
           // if(ans == n) return ans;
            if(ans >=  n ) last = mid ;
            else first = mid ;
        }
        if(check(first) == n)return first;
        return last;
    }

    
    
}

//????????????????????????
class ContestScanner {
    
    private final BufferedReader reader;
    private String[] line;
    private int idx;
    
    public ContestScanner() throws FileNotFoundException {
        reader = new BufferedReader(new InputStreamReader(System.in));
    }
    
    public ContestScanner(String filename) throws FileNotFoundException {
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
    }
    //???????????? 

    public String readLine() throws Exception {
        return reader.readLine();
    }
    
    public String nextToken() throws IOException {
        if (line == null || line.length <= idx) {
            line = reader.readLine().trim().split(" ");
            idx = 0;
        }
        return line[idx++];
    }
    
    public long nextLong() throws IOException, NumberFormatException {
        return Long.parseLong(nextToken());
    }
    
    public int nextInt() throws NumberFormatException, IOException {
        return (int) nextLong();
    }
    
    public double nextDouble() throws NumberFormatException, IOException {
        return Double.parseDouble(nextToken());
    }
}

Measured memory_bytes: 40508
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    n = II()
    k = II()
    x = II()
    y = II()
    ans = 0
    for i in range(n):
        if i >= k:
            ans += y
        else:
            ans += x
    print(ans)
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    A()


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

void testCase(){
    int n, m;
    long long tot = 0;
    cin>>n>>m;
    vector<int> v(n);
    for(int i = 0; i < n; i++){
        cin>>v[i];
        tot += v[i];
    }
    sort(v.rbegin(), v.rend());
    if((v[m-1]*1.0) < (tot*1.0)/(4*m)) cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
}

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



Measured memory_bytes: 256
import sys

lines = 2
inp = list()

for _ in range(lines):
    inp.append(sys.stdin.readline().strip())


k = int(inp[0])
ab = inp[1].split()
a = int(ab[0])
b = int(ab[1])

na1, na2 = divmod(a, k)
nb1, nb2 = divmod(b, k)

if na1 < nb1 or na2 == 0 or nb2 == 0:
    print("OK")
else:
    print("NG")


Measured memory_bytes: 9072
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define PI acos(-1)
typedef  long long ll;
ll MOD=1000000007;
ll gcd(ll x,ll y){
   if(y==0) return x;
   else return gcd(y,x%y);
}
ll lcm(ll x,ll y){
   return x/gcd(x,y)*y;
}

int main(){
   
   int n;
   cin>>n;
   vector<int> a(n);

   rep(i,n){
      cin>>a[i];
   }

   int ans=10000000;
   int cnt=0;
   for(int i=-100;i<=100;i++){
      rep(j,n){
         cnt+=pow((i-a[j]),2);
      }
      ans=min(ans,cnt);
      cnt=0;
   }

   cout<<ans;
   return 0;
}


Measured memory_bytes: 3608
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef vector<pair<int,int>> vpi;
typedef vector<pair<long long, long long>> vpl;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
#define INF (int)(1e9)
#define MAXX 1.1529215e+18
#define inf 999999
#define EPS (1e-7)
#define rep(i,n) for(int i=0; i<(int)(n);i++)
#define REP(i,n) for(int i=1;i<=(int)(n);i++)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);i++)
#define ALL(a)  a.begin(),a.end()
#define RALL(a) a.begin(),a.end(),greater<int>()
#define ROT(a) a.begin(),a.begin()+1,a.end()
#define RROT(a) a.begin(),a.end()-1,a.end()
#define PB push_back
#define MP make_pair
#define PI acos(-1.0)
#define sz(a) a.size()
const ll MOD = 1e9+7;
const int MAX = 10000000;
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
//cout << 'H' << endl;
/*--------------------------------------------*/

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

    int H,A;
    cin >> H >> A;

    cout << (H+A-1)/A << endl; 


}

Measured memory_bytes: 256
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll MOD = 1e9+7;

#define vec vector<int>
#define vecll vector<ll>
#define vpair vector<pair<int, int>>
#define vpairll vector<pair<ll, ll>>

#define rep(i,n)         for(int i=(int)0; i<(int)n; i++)
#define REP(i,m,n)       for(int i=(int)m; i<(int)n; i++)
#define arr(var, n)      vecll var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n)    vecll var(n); rep(i,n){cin >> var[i];}
#define all(var)         (var).begin(), (var).end()
#define sortall(var)     sort(all(var))
#define prt(var)         cout << var << endl
#define prtd(n, var)     cout << fixed << setprecision(n) << var << endl
#define prt2(var1, var2) cout << var1 << " " << var2 << endl
#define prseq(var, n)    rep(i,n){cout << var[i] << " ";} cout << endl
#define prseql(var, n)   rep(i,n){cout << var[i] << " " << endl;}

//----------------------------------------------------------------

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

  arr(l,n);

  ll p=0;
  int ans=1;
  rep(i,n){
    p = p + l[i];
    if(p<=x){
      ans++;
    }
    //prt(p);
  }

  prt(ans);

}


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

int main() {
  int n;
  cin >> n;
  n %= 10;
  if (n == 3) {
    cout << "bon" << endl;
  } else if (n == 0 || n == 1 || n == 6 || n == 8) {
    cout << "pon" << endl;
  } else {
    cout << "hon" << endl;
  }
  return 0;
}

Measured memory_bytes: 3604
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
int main() {
  cin.tie(0);
  cout << fixed << setprecision(10);
  int h, w; cin >> h >> w;
  vector<vector<bool>> a(h, vector<bool>(w));
  for(int i=0;i<h;++i) {
    for(int j=0;j<w;++j) {
      char tmp; cin >> tmp;
      a[i][j] = (tmp == '#' ? true : false);
    }
  }
  vvll dp(h, vll(w, 0));
  dp[0][0] = 1;
  for(int i=0;i<h-1;++i) {
    if(a[i+1][0]) break;
    else dp[i+1][0] = 1;
  }
  for(int i=0;i<w-1;++i) {
    if(a[0][i+1]) break;
    else dp[0][i+1] = 1;
  }
  for(int i=1;i<h;++i) {
    for(int j=1;j<w;++j) {
      if(a[i][j]) continue;
      ll x = (a[i-1][j] ? 0 : dp[i-1][j]), y = (a[i][j-1] ? 0 : dp[i][j-1]);
      if(x == 0 && y == 0) {
        continue;
      } else if(x == 0) {
        dp[i][j] = y;
      } else if(y == 0) {
        dp[i][j] = x;
      } else {
        dp[i][j] = (x + y) % MOD;
      }
    }
  }
  cout << dp[h-1][w-1] << endl;
}


Measured memory_bytes: 8320
#include<stdio.h>

int main(void){
	char str[21],ans[21];
	int i,j=0;
	int n;
	
	scanf("%s",str);
	for(i=0;i<21;i++){
		if(str[i]=='\0') break;
	}
	n=i-1;
	for(i=n;i>=0;i--){
		ans[j]=str[i];
		j++;
	}
	ans[j]='\0';
	printf("%s\n",ans);
	
	return 0;
}

Measured memory_bytes: 0
#include<cstdio>
  #include<cstdlib>
  #include<algorithm>
  #include<iostream>
  #include<queue>
  #include<vector>
  #include <bitset>
  #include<math.h>
  #include<map>
  #include<set>
  using namespace std;
  #define INF 11000000000
  #define MOD 1000000007
  typedef long long ll;
  typedef pair<ll,ll> P;

  //計算量はO(V*E)

  struct edge{
    int from,to,cost;
  };

  edge es[110000]; //辺

  ll d[110000],pre; //最短距離
  bool flag=true;

  int V,E,C[3000]; //Vは頂点数，Eは辺数

  //s番目の頂点から各頂点への最短距離を求める
  void shortest_path(int s){
    int count=0;
    for(int i=1;i<=V;i++) d[i]=INF;
    d[s]=0;
    while(true){
      bool update=false;
      for(int i=0;i<E;i++){
        edge e=es[i];
        if(C[e.from]) C[e.to]++;
        if(d[e.from]!=INF && d[e.to]>d[e.from]+e.cost){
          d[e.to]=d[e.from]+e.cost;
          update=true;
          if(count>=V-1) C[e.to]++; 
        }
      }
      if(!update) break;
      if(count==2*(V-1)) return;
      count++;
    }

  }

  int main(){
    cin>>V>>E;
    int a,b,c,p;
    cin>>p;
    ll A[110000],ans=0;

    for(int i=0;i<E;i++){
      cin>>a>>b>>c;
      es[i].from=a; es[i].to=b; es[i].cost=-c+p;
    }

    shortest_path(1);
    if(d[V]>0) d[V]=0;
    
    if(C[V]) cout<<-1<<endl;
    else cout<<d[V]*(-1)<<endl;
  }


Measured memory_bytes: 384
import java.lang.reflect.Array;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int H = Integer.parseInt(scan.next());
        int W = Integer.parseInt(scan.next());
        char[][] s = new char[H][W];

        String h;
        for(int i = 0; i < H; i++){
            h = scan.next();
            for(int j = 0; j < W; j++){
                s[i][j] = h.charAt(j);
            }
        }

        if(H == 1 && W == 1){
            if(s[0][0] == '#'){
                System.out.println("Yes");
            }else{
                System.out.println("No");
            }
        }
        
        for(int i = 0; i < H; i++){
            for(int j = 0; j < W; j++) {
                
                if(s[i][j] == '#'){
                    if (0   <= i-1 && s[i-1][j] == '#') continue;
                    if (i+1 <= W-1 && s[i+1][j] == '#') continue;
                    if (0   <= j-1 && s[i][j-1] == '#') continue;
                    if (j+1 <= H-1 && s[i][j+1] == '#') continue;
                    System.out.println("No");
                    return;
                }
            }
        }

        System.out.println("Yes");
        return;
    }
}

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


int main()
{

        int i;
        int n;

        scanf("%d", &n);


        for (i=0; i<65535; i++) {
                if (n >= pow(2, i) && n < pow(2, i+1)) {
                        break;
                }
        }

        printf("%d\n", (int)pow(2, i));


        return 0;
}

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

template<class T> void vin(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
}

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

    double ans = 0;
    rep(i, N) {
        double x;
        string s;
        cin >> x >> s;
        if (s == "JPY") ans += x;
        else {
            ans += 380000.0 * x;
        }
    }

    printf("%.10f\n", ans);
}


Measured memory_bytes: 256
#include<stdio.h>
#include<math.h>
#include<stdlib.h>

int main(void){
  int a[3][3];
  int my[3][3];
  int i,j,k;

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

  int n;

  scanf("%d",&n);
int b[n];
  for(i=0;i<n;i++){
    scanf("%d",&b[i]);
    //printf("koko");
  }

  for(i=0;i<3;i++){
    for(j=0;j<3;j++){
      for(k=0;k<n;k++){
        if(a[i][j]==b[k]){
          my[i][j]=1;
          break;
        }
        else{
          my[i][j]=0;
        }
      }
    }
  }

  /*for(i=0;i<3;i++){
    for(j=0;j<3;j++){
      printf("%d",my[i][j]);
    }
  }
  */


  for(i=0;i<3;i++){
    if(my[i][0]==1&&my[i][1]==1&&my[i][2]==1){
      printf("Yes");
      return 0;
    }
    if(my[0][i]==1&&my[1][i]==1&&my[2][i]==1){
      printf("Yes");
      return 0;
    }
  //  printf("kk");
  }

  if(my[0][0]==1&&my[1][1]==1&&my[2][2]==1){
    printf("Yes");
    return 0;
  }

  if(my[0][2]==1&&my[1][1]==1&&my[2][0]==1){
    printf("Yes");
    return 0;
  }

  printf("No");


  return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll gcd(ll a, ll b) {
	if (a<b) { return gcd(b, a); }
	else if (a%b) { return gcd(b, a%b); }
	else { return b; }
}
ll lcm(ll a,ll b){
    return (a/gcd(a,b))*b;
}
int main(){
    int D;
    cin>>D;
    if(D==22)cout<<"Christmas Eve Eve Eve"<<endl;
    if(D==23)cout<<"Christmas Eve Eve"<<endl;
    if(D==24)cout<<"Christmas Eve"<<endl;
    if(D==25)cout<<"Christmas"<<endl;


}


Measured memory_bytes: 256
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int hours, minutes, seconds;
    int time = Integer.parseInt(sc.next());
    hours = time / 3600;
    minutes = (time % 3600) / 60;
    seconds = time % 60;
    System.out.println(hours + ":" + minutes + ":" + seconds);
  }
}


Measured memory_bytes: 26268
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int main()
{
	long long i,n,k,f=1;
	cin>>n>>k;
	long long qzh[n+2];//3	2		0123
	memset(qzh,0,sizeof(qzh));
	for(i=2;i<=n+1;i++)
	{
		qzh[i]=qzh[i-1]+f;
		f++;
		//cout<<i<<" "<<qzh[i]<<endl;
	}
	long long ans=0,fst,lst;
//	set<long long> q;
	for(i=k;i<=n+1;i++)
	{
		fst=qzh[i],lst=qzh[n+1]-qzh[n+1-i];
		//x=(long long)()%(long long)(1000000000+7);
		//q.insert(x);
		//cout<<i<<" "<<fst<<" "<<lst<<" "<<ans<<endl;
		ans = ( ans + ( lst - fst + 1 ) ) % ( long long ) ( mod ) ;
	}
	//cout<<q.size()<<endl;
	cout<<ans%(long long)(mod)<<endl;
    return 0;
}	

Measured memory_bytes: 5104
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

void solve(ll n){
    ll ansa,ansb;
    for(ll b=1;b*b/2<=n;b++){
        ll rem=n-b*(b+1)/2;
        if(rem%b) continue;
        rem/=b;
        ansa=rem+1;ansb=b;
    }
    cout<<ansa<<' '<<ansb<<endl;
}

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

    while(true){
        ll n;cin>>n;
        if(n==0) return 0;
        solve(n);
    }
    return 0;
}



Measured memory_bytes: 3160
#include <vector>
#include <iostream>
#include <cmath>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <fstream>
#include <unistd.h>
#include <string.h>
#include <string>
#include <numeric>
#include <queue>
#include <deque>
#include <sstream>
#include <iomanip>
#include <set>
#include <stack>
#include <cassert>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
typedef pair<ll, ll> PL;
typedef vector<PL> vpl;
typedef vector<vector<PL>> vvpl;
const int INF = 1001001001;
const ll LINF = 1e17;
const double pi = 3.1415926535897932;
const string endstr = "\n";
#define FOR(i, a, b) for(ll i = (a); i < b; i++)
#define RFOR(i, a, b) for(ll i = (a); i > b; i--)
#define rep(i, n) for(ll i = 0; i < n; i++)
#define rrep(i, n) for(ll i = n-1; i > -1; i--)
#define FORMAP(it, m) for(auto it = m.begin(); it != m.end(); it++)
#define ff first
#define ss second
#define pb push_back
#define epb emplace_back
#define ALL(X) (X).begin(),(X).end()

template <typename T>
T gcd(T a, T b) {
    return (a == 0) ? b : gcd(b%a, a);
}
template <typename T>
T lcm(T a, T b) {
    return a / gcd(a, b) * b;
}

bool p_comp_fs(const PL p1, const PL p2){ return p1.first < p2.first;};
bool p_comp_fg(const PL p1, const PL p2){ return p1.first > p2.first;};
bool p_comp_ss(const PL p1, const PL p2){ return p1.second < p2.second;};
bool p_comp_sg(const PL p1, const PL p2){ return p1.second > p2.second;};
template <typename T>
vector<T> uniquen(vector<T> vec){
    sort(ALL(vec));
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    return vec;
}

inline ll popcnt(ll x){return __builtin_popcountll((unsigned long long)x);};
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
//friend bool operator<(const stc& lhs, const stc& rhs){
//    return std::tie(lhs.l, lhs.r) < std::tie(rhs.l, rhs.r);
//}

vvl G;
ll N;

vl dp;

ll dfs(ll now, ll par){
    if(dp[now] >= 0) return dp[now];
    vl cs;
    for(ll c : G[now]) if(c != par){
        cs.pb(dfs(c, now));
    }
    sort(ALL(cs), greater<ll>());
    ll res = 0;
    rep(i, cs.size()){
        chmax(res, cs[i]+i+1);
    }
    return dp[now] = res;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N; G = vvl(N); dp = vl(N, -1);
    FOR(i, 1, N){
        ll a; cin >> a; a--;
        G[i].pb(a); G[a].pb(i);
    }
    cout << dfs(0, -1) << endstr;
    return 0;
}


Measured memory_bytes: 15872
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys

sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): #startline--------------------------------------------
    n,m = map(int,input().split())
    xyz = [list(map(int,input().split())) for i in range(n)]
    ans = []
    for i in range(8):
        sub = []
        for j in range(n):
            tmp = 0
            for k in range(3):
                if ((i>>k)&1):
                    tmp -= xyz[j][k]
                else:
                    tmp += xyz[j][k]
            sub.append(tmp)
        sub.sort(reverse = True)
        ans.append(sum(sub[:m]))
    print(max(ans))
if __name__ == "__main__":
    main() #endline===============================================
    

Measured memory_bytes: 5428
TURTLE_LEG_COUNT = 2
CRANE_LEG_COUNT = 4

animal_count, leg_count = gets.chomp.split(' ').map{|i| i.to_i}

if leg_count.odd?
  puts "No"
  exit
end

if (animal_count * TURTLE_LEG_COUNT) > leg_count
  puts "No"
  exit
end

animal_counts = []
(0..animal_count).each do |i|
  temp = {turtle: i, crane: animal_count - i}
  animal_counts.push(temp)
end

flug = false
animal_counts.each do |animal|
  temp = (animal[:turtle] * TURTLE_LEG_COUNT) + (animal[:crane] * CRANE_LEG_COUNT)
  
  if leg_count == temp
    flug = true
    break
  end
end
  
if flug
  puts "Yes"
else
  puts "No"
end


Measured memory_bytes: 14260
#include<iostream> 
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <vector>

using namespace std;
#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++)

int main() {
	int n, i = 0, j = 1;
	cin >> n;
	string s;
	cin >> s;
	rep2(i, 1, n) {
		if (s[i] != s[i - 1]) j++;
	}
	cout <<j << endl;
	return 0;

}


Measured memory_bytes: 3740
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>
#include <stack>
#include <numeric>


using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)

int main(){
    int h,w,n;
    cin >> h >> w >> n;
    int a,b;
    cin >> a >> b;
    string s,t;
    cin >> s >> t;
    vector<pair<int,int> > p,q;
    rep(i,n){
        if(s[i]=='R'){
            q.push_back(MP(1,0));
        }else if(s[i]=='L'){
            q.push_back(MP(-1,0));
        }else if(s[i]=='U'){
            p.push_back(MP(-1,0));
        }else{
            p.push_back(MP(1,0));
        }
        if(t[i]=='R'){
            q.push_back(MP(1,1));
        }else if(t[i]=='L'){
            q.push_back(MP(-1,1));
        }else if(t[i]=='U'){
            p.push_back(MP(-1,1));
        }else{
            p.push_back(MP(1,1));
        }
    }
    reverse(q.begin(),q.end());
    reverse(p.begin(),p.end());
    int ng1,ng2;
    ng1 = 0;
    ng2 = h+1;
    bool flag =0;
    rep(i,p.size()){
        int x = p[i].first;
        int y = p[i].second;
        if(y==0){
            if(x==1){
                ng2--;
                
            }else{
                ng1++;
            }
        }else{
            if(x==1){
                ng1--;
                ng1 = max(ng1,0);
                
            }else{
                ng2++;
                ng2 = min(ng2,h+1);
            }
        }
        if(ng1+1>=ng2){
            flag = 1;
        }
        //cerr <<ng1 << " " << ng2 << endl;
    }
   
    if(flag||ng1>=a||ng2<=a){
        cout << "NO" << endl;
        return 0;
    }
    ng1 = 0;
    ng2 = w+1;
    flag =0;
    rep(i,q.size()){
        int x = q[i].first;
        int y = q[i].second;
        //cerr << x << "  " << y << endl;
        if(y==0){
            if(x==1){
                ng2--;
            }else{
                ng1++;
            }
        }else{
            if(x==1){
                ng1--;
                ng1 = max(ng1,0);
                
            }else{
                ng2++;
                ng2 = min(ng2,w+1);
            }
        }
        if(ng1+1>=ng2){
            flag = 1;
        }
        //cerr << ng1 << " " << ng2 << endl;
    }
    //cerr << ng1 << " " << ng2 << endl;
    if(flag||ng1>=b||ng2<=b){
        cout << "NO" << endl;
        return 0;
    }
    cout << "YES" << endl;
    return 0;
}

Measured memory_bytes: 3956
/*
 * atcoder/abc129/d.cpp
 */

// C++ 14
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring> // memset
#include <cassert>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for(int __x = __start; __x < __end; __x++)
template <class T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template <class T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }

const int MAX = 2010;
int H, W;
bool G[MAX][MAX];
int L[MAX][MAX] = {0};
int R[MAX][MAX] = {0};
int D[MAX][MAX] = {0};
int U[MAX][MAX] = {0};

void solve() {
  cin >> H >> W;
  char c;
  loop(y,0,H) {
    loop(x,0,W) {
      cin >> c;
      G[y][x] = c == '.';
    }
  }

  loop(y,0,H) {
    loop(x,0,W) {
      if (x==0) { L[y][x] = G[y][x]; continue; }
      L[y][x] = G[y][x] ? 1 + L[y][x-1] : 0;
    }
    for (int x=W-1; x>=0; x--) {
      if (x==W-1) { R[y][x] = G[y][x]; continue; }
      R[y][x] = G[y][x] ? 1 + R[y][x+1] : 0;
    }
  }

  loop(x,0,W) {
    loop(y,0,H) {
      if (y==0) { U[y][x] = G[y][x]; continue; }
      U[y][x] = G[y][x] ? 1 + U[y-1][x] : 0;
    }
    for (int y=H-1; y>=0; y--) {
      if (y==H-1) { D[y][x] = G[y][x]; continue; }
      D[y][x] = G[y][x] ? 1 + D[y+1][x] : 0;
    }
  }

  int max_ = 0;
  loop(y,0,H) {
    loop(x,0,W) {
      if (!G[y][x]) continue;
      chmax(max_, L[y][x] + R[y][x] + U[y][x] + D[y][x] - 3);
    }
  }
  cout << max_ << endl;
}
int main() {
  // cout.precision(15); cout << fixed;

  solve();

  return 0;
}


Measured memory_bytes: 67328