akhauriyash/Code-Regression huggingface dataset:
import java.util.Scanner

object Main {
//  def getLucas(n: Int): Long = {
//    val l: Long = if (n == 0) {
//      2
//    } else if (n == 1) {
//      1
//    } else {
//      getLucas(n - 1) + getLucas(n - 2)
//    }
//    return l
//  }

  def getLucas(n: Int): Long = {
    var lucasArr: Array[Long] = Array.empty
    lucasArr = lucasArr :+ 2.toLong
    lucasArr = lucasArr :+ 1.toLong
    for (i <- (2 to n)) {
      lucasArr = lucasArr :+ (lucasArr(i - 1) + lucasArr(i - 2)).toLong
    }
    lucasArr(n)
  }

  def main(args: Array[String]): Unit = {
    val input = new Scanner(System.in)
    val n: Int = input.nextInt
    println(getLucas(n))
  }
}

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

int main() {
    int n, a;
    scanf("%d", &n);
    ll ans = 0;    
    for (int i = 0; i < n; i++) {
        scanf("%d", &a);
        ans += a;
    }
    ans -= n;
    printf("%lld", ans);
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int i,j;
int main() {
  string s;
  cin>>s;
  for(i=0;s[i]!='A';i++);
  for(j=s.size();s[j]!='Z';j--);
  cout<<j-i+1;
  return 0;
}


Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>

#define PI 3.14159265359
using namespace std;
class dice{
public:
	int d[6];
	void set(int n, int x){
		d[n] = x;
	}
	int get(int n){
		return d[n];
	}
	void rotate(char c){
		int tmp;
		switch (c){
		case 'W':
			tmp = d[3];
			d[3] = d[0];
			d[0] = d[2];
			d[2] = d[5];
			d[5] = tmp;
			break;
		case 'E':
			tmp = d[5];
			d[5] = d[2];
			d[2] = d[0];
			d[0] = d[3];
			d[3] = tmp;
			break;
		case 'N':
			tmp = d[0];
			d[0] = d[1];
			d[1] = d[5];
			d[5] = d[4];
			d[4] = tmp;
			break;
		case 'S':
			tmp = d[4];
			d[4] = d[5];
			d[5] = d[1];
			d[1] = d[0];
			d[0] = tmp;
			break;
		}
	}
	void espin(){
		rotate('S');
		rotate('E');
		rotate('N');
	}
	bool operator ==(dice e){
		for (int i = 0; i < 6; i++){
			if (d[i] != e.get(i)) return false;
		}
		return true;
	}
};

int main(){
	int tmp;
	dice d, c;
	for (int i = 0; i < 6; i++){
		cin >> tmp;
		d.set(i, tmp);
	}
	for (int i = 0; i < 6; i++){
		cin >> tmp;
		c.set(i, tmp);
	}
	string s = "SESESE";
	for (int i = 0; i < 6; i++){
		c.rotate(s[i]);
		if (d.get(0) == c.get(0)){
			for (int j = 0; j < 4; j++){
				if (d == c){
					cout << "Yes" << endl;
					i = 6;
					j = 3;
				}
				c.espin();
			}
		}
		if (i == 5) cout << "No" << endl;
	}
}

Measured memory_bytes: 3208
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>

#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

class DSummerVacation {
public:
    static constexpr int kStressIterations = 0;

    static void generateTest(std::ostream& test) {
    }

    void solve(std::istream& cin, std::ostream& cout) {
        int N, M;
        cin >> N >> M;
        map<int, vector<int>> m;
        REP(i, N) {
            int a, b;
            cin >> a >> b;
            m[a].push_back(b);
        }

        int res = 0;
        priority_queue<int> q;
        REP(i, M + 1) {
            auto v = m[i];
            REP(j, v.size()) {
                q.push(v[j]);
            }
            if (q.empty()) continue;
            res += q.top();
            q.pop();
        }
        cout << res << endl;
    }
};


signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    DSummerVacation solver;
    solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 10880
#include"bits/stdc++.h"
#include<unordered_set>
#include<unordered_map>
#include<random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; // 998'244'353LL
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
int dx[4]={ 1,0,-1,0 };
int dy[4]={ 0,1,0,-1 };

ll A, B;

signed main(){
    cin >> A >> B;
    cout << A*B << endl;
}

Measured memory_bytes: 3624
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <functional>
#define rep(i,j,n) for(i=j;i<(n);i++)
#define rrep(i,j,n) for(i=j;i>(n);i--)
typedef long long int lli;
#define N 1000000007

using namespace std;

int main(){
    lli x,y,ans;
    lli i,j;
    cin>>x>>y;
    if(x==y){
        ans=0;
    }
    else{
        ans=abs(abs(x)-abs(y));
        if(abs(x)>=abs(y)){
            if((x>=0)&&(y>=0)){
                if(y==0){
                    ans+=1;
                }
                else{
                    ans+=2;
                }
            }
            else{
                if((x<=0)&&(y<=0)){
                }
                else{
                    ans+=1;
                }
            }
        }
        else{
            if((x>=0)&&(y>=0)){
            }
            else{
                if((x<=0)&&(y<=0)){
                    if(x==0){
                        ans+=1;
                    }
                    else{
                        ans+=2;
                    }
                }
                else{
                    ans+=1;
                }
            }
        }
    }
    cout<<ans<<endl;
}


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

int main(){
	int n;
	cin>>n;
	vector<int> x(n), y(n);
	rep(i, n) cin>>x[i]>>y[i];

	int mx=INT_MIN, mn=INT_MAX;
	rep(i, n){
		int c=x[i]+y[i];
		mx=max(mx, c);
		mn=min(mn, c);
	}
	int dmx=mx-mn;

	mx=INT_MIN, mn=INT_MAX;
	rep(i, n){
		int c=x[i]-y[i];
		mx=max(mx, c);
		mn=min(mn, c);
	}
	dmx=max(dmx, mx-mn);

	cout<<dmx<<endl;
	return 0;
}


Measured memory_bytes: 4840
h,w,k = map(int,input().split())
s = []
for i in range(h):
    s0 = list(input())
    s.append(s0)
al = []
wl = [[0 for i in range(h)] for i in range(w)]
for i in range(w):
    count = 0
    for j in range(h):
        if s[j][i] == "1":
            count += 1
        wl[i][j] = count
for i in range(2**(h-1)):
    ans = 0
    b = []
    for j in range(h-1):
        if ((i >> j) & 1):
            b.append(j)
    #print(b)
    b.append(h-1)
    bl = []
    now = -1
    for j in b:
        if now == -1:
            bl.append(wl[0][j])
            now = j
        else:
            bl.append(wl[0][j]-wl[0][now])
            now = j
    ans = len(bl)-1
    key = 1
    key2 = 0
    for j in range(1,w):
        now = -1
        aa = []
        key2 = 0
        for x in  range(len(bl)):
            #print(bl)
            if now == -1:
                if bl[x] > k:
                    key = 0
                    break
                else:
                    bl[x] += wl[j][b[x]]
                    aa.append(wl[j][b[x]])
                    if bl[x] > k:
                        key2 = 1
                    now = b[x]
            else:
                if bl[x] > k:
                    key = 0
                    break
                else:
                    bl[x] += wl[j][b[x]] - wl[j][now]
                    aa.append(wl[j][b[x]]-wl[j][now])
                    now = b[x]
                    if bl[x] > k:
                        key2 = 1
        if key2 == 1 and key == 1:
            for x in range(len(bl)):
                bl[x] = aa[x]
            ans += 1
        elif key == 0:
            break
    if key == 1:
        al.append(ans)
print(min(al))

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

using namespace std;

typedef long long lli;
typedef double lld;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<double> vdl;
typedef vector<vector<lli>> mat;
typedef vector<vdl> mad;
typedef unordered_map<lli,unordered_map<lli,lli>> graph;
typedef complex<double> cmp;
typedef vector<cmp> vcl;

lli n;
string s;
string r,l;
map<vector<string>,lli> m1,m2;
lli ans = 0;

int main(){
  cin >> n >> s;
  r = l = s;
  reverse(l.begin(),l.end());
  for(lli i = 0;i < (1ll << n);i++){
    string a,b;
    for(lli j = 0,k = 1;j < n;j++,k <<= 1){
      if(i&k) a.push_back(r[j]);
      else b.push_back(r[j]);
    }
    m1[vector<string>({a,b})]++;
  }
  for(lli i = 0;i < (1ll << n);i++){
    string a,b;
    for(lli j = 0,k = 1;j < n;j++,k <<= 1){
      if(i&k) a.push_back(l[j]);
      else b.push_back(l[j]);
    }
    m2[vector<string>({a,b})]++;
  }
  for(auto x : m1){
    ans += m1[x.first]*m2[x.first];
  }
  cout << ans << endl;
}


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

set<string> ss;
string s;
int k;

int main()
{
	cin>>s>>k;
	int b=s.size();
	for(int i=1;i<=min(k,b);i++) for(int j=0;j<s.size()-i+1;j++) ss.insert(s.substr(j,i));
	int cnt=0;
	for(set<string>::iterator it=ss.begin();it!=ss.end();it++){
		if(++cnt==k){
			cout<<*it<<endl;
			return 0;
		} 
	}
	return 0;
}

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

namespace ABC
{
    class ABC166A
    {
        const string FILE = "/tmp/data.txt";

        static void Main(string[] args)
        {
#if DEBUG
            Console.SetIn(new StreamReader(FILE));
#endif
            string line;
            line = Console.ReadLine();
            Console.WriteLine(line == "ABC" ? "ARC" : "ABC");

#if DEBUG
            Console.WriteLine();
#endif
        }
    }
}


Measured memory_bytes: 25636
import queue
import sys
input = sys.stdin.readline

N = int(input())
E = {n: set() for n in range(N)}

for _ in range(N - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    E[a].add((b, c))
    E[b].add((a, c))

def m():
    Q, K = map(int, input().split())
    K -= 1
    dist = [-1] * N
    already = set()
    que = queue.Queue()
    dist[K] = 0
    que.put(K)
    while not que.empty():
        now = que.get()
        out = E[now]
        for n, c in out:
            if dist[n] != -1:
                continue
            dist[n] = dist[now] + c        
            que.put(n)
    
    for _ in range(Q):
        x, y = map(int, input().split())
        x -= 1
        y -= 1
        print(dist[x] + dist[y])

if __name__ == "__main__":
    m()


Measured memory_bytes: 65628
#include<bits/stdc++.h>
using namespace std;
const int N=100000;
int a[N];
int main ()
{
string s,str,sr;
cin >> s >> sr >> str;
if (s[s.size()-1]==sr[0]&&sr[sr.size()-1]==str[0])
{
cout << "YES";
return 0;
}
cout << "NO";


return 0;
}




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

using namespace std;

int main(int argc, char* argv[]) {
  int H, W;
  cin >> H >> W;

  char pixels[H][W];
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> pixels[i][j];
    }
  }

  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < W; ++k) {
        cout << pixels[i][k];
      }
      cout << endl;
    }
  }

  return 0;
}

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

using std::pair;
using std::vector;
using std::string;
 
typedef long long ll;
typedef pair<int, int> pii;

#define fst first
#define snd second
#define pb(a) push_back(a)
#define mp(a, b) std::make_pair(a, b)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template <typename T> bool chkmax(T& a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> bool chkmin(T& a, T b) { return a > b ? a = b, 1 : 0; }

const int oo = 0x3f3f3f3f;

string procStatus() {
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}

template <typename T> T read(T& x) {
    int f = 1; x = 0;
    char ch = getchar();
    for(;!isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
    return x *= f;
}

const int N = 100000;

int n;
int a[N + 5];

int main() {
#ifdef Wearry
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
#endif

    for(int i = 1; i <= N; ++i) {
        a[i] = 0;
        int j = i;
        while(j > 0) a[i] += j % 10, j /= 10;
    }

    read(n);
    int ans = oo;
    for(int i = 1; i < n; ++i) chkmin(ans, a[i] + a[n - i]);

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


Measured memory_bytes: 640
io = STDIN
n,a,b=io.gets.split.map(&:to_i)
def tot(i)
  i.to_s.chars.map(&:to_i).inject(&:+)
end
sum=0
(1..n).each do |i|
  tot = tot(i)
  sum+=i if tot>=a && tot<=b
end
puts sum


Measured memory_bytes: 3836
def solve(N,W,knap):
    dp=[[-1]*(W+1) for _ in range(N+1)]
    dp[0][0]=0
    for i in range(1,N+1):
        for j in range(W+1):
            if dp[i-1][j]>-1:
                dp[i][j]=dp[i-1][j]
            if j-knap[i][1]>=0:
                if dp[i-1][j-knap[i][1]]>-1:
                    dp[i][j]=max(dp[i][j],dp[i-1][j-knap[i][1]]+knap[i][0])
    return max(dp[N])

N,W=map(int,input().split())
knap=[(0,0)]+[tuple(map(int,input().split())) for _ in range(N)]
print(solve(N,W,knap))


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

using namespace std;

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
ll n;
ll f[100][5][2];
ll p[100][11];

ll c[100];

int main() {
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 5; j++) {
            std::cin >> f[i][j][0];
            std::cin >> f[i][j][1];
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 11; j++) {
            std::cin >> p[i][j];
        }
    }
    ll result = -1* 1e10;
    for (int i = 1; i < 1024; i++) {
        bitset<10> b(i);
        string bs = b.to_string();
        ll tmp_res = 0;
        for (int j = 0; j < n; j++) {
            ll cnt = 0;
            for (int k = 0; k < 10; k++) {
                if(f[j][k/2][k%2] == 1 && bs[k]=='1') cnt++;
            }
            tmp_res += p[j][cnt];
        }
        result = max(result,tmp_res);
    }
    std::cout << result << std::endl;
}


Measured memory_bytes: 256
#![allow(
  non_snake_case,
  unused_variables,
  unused_assignments,
  unused_mut,
  unused_imports,
  unused_macros,
  dead_code
)]
//use proconio::fastout;
use proconio::{input, marker::*};
use std::cmp::*;
use std::collections::*;
macro_rules! debug {
  ($($a:expr),* $(,)*) => {
      #[cfg(debug_assertions)]
      eprintln!(concat!($("| ", stringify!($a), "={:?} "),*, "|"), $(&$a),*);
  };
}
fn main() {
  input! {
    n:usize, A:[String;n]
  }
  // ip:整数部分, dp:小数部分
  // 1/512で0.001_953_125 で下9桁に届いてしまう。
  let mut cnt = vec![vec![0; 22]; 50];
  let mut p2 = vec![0; n];
  let mut p5 = vec![0; n];
  let A: Vec<String> = A;
  for i in 0..n {
    let s = A[i].clone();
    let mut ip = 0usize;
    let mut dp = 0;
    if let Some(i) = s.find('.') {
      // 小数の場合
      ip = s[0..i].parse().unwrap();
      let ss = s[(i + 1)..].to_string() + "0000000000";
      dp = ss[0..9].parse::<usize>().unwrap()
    } else {
      ip = s.parse::<usize>().unwrap();
    }
    let mut x = ip * 1_000_000_000 + dp;
    while x % 2 == 0 {
      x /= 2;
      p2[i] += 1;
    }
    while x % 5 == 0 {
      x /= 5;
      p5[i] += 1;
    }
    p2[i] = min(p2[i], 18);
    p5[i] = min(p5[i], 18);
    cnt[p2[i]][p5[i]] += 1;
  }

  let mut ans = 0usize;
  for i in 0..n {
    let remain2 = 18 - p2[i];
    let remain5 = 18 - p5[i];
    for j in remain2..=18 {
      for k in remain5..=18 {
        ans += cnt[j][k];
      }
    }
    if p2[i] >= remain2 && p5[i] >= remain5 {
      ans -= 1;
    }
  }
  println!("{}", ans / 2);
}


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

int main() {
  
  int A, B, C, D, E;
  cin >> A >> B >> C >> D >> E;
  
  int res=0;
  if(A%10==0){ res+=A; }
  if(B%10==0){ res+=B; }
  if(C%10==0){ res+=C; }
  if(D%10==0){ res+=D; }
  if(E%10==0){ res+=E; }

  int last=10;
  for(int i=9; i>=1; i--){
    if(A%10==i){ res+=A-i+10; last=i;}
    if(B%10==i){ res+=B-i+10; last=i;}
    if(C%10==i){ res+=C-i+10; last=i;}
    if(D%10==i){ res+=D-i+10; last=i;}
    if(E%10==i){ res+=E-i+10; last=i;}
    
  }
  
  res+=last-10;
  
  cout << res << endl;
  
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

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

int main(){
    int s;
    cin >> s;
    set<int> st;
    int x = s;
    while(st.count(x) == 0){
        st.insert(x);
        if(x % 2 == 1){
            x = 3 * x + 1;
        }else{
            x /= 2;
        }
    }

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

    return 0;
}

Measured memory_bytes: 256
import java.util.*;

class Main{
  public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    int N=sc.nextInt();
    int[] X=new int[N];
    int ANS=100000000;
    int ans=0;
    
    for(int i=0;i<N;i++){
      X[i]=sc.nextInt();
    }
    
    for(int i=0;i<=100;i++){
      for(int j=0;j<N;j++){
        ans+=(X[j]-i)*(X[j]-i);
      }
      if(ANS>ans) ANS=ans;
      ans=0;
    }
    
    System.out.println(ANS);
  }
}

Measured memory_bytes: 22868
! 2020.2.9
!
      program answer
      implicit none
      integer :: N
      integer,allocatable,dimension(:) :: A
      integer :: i
      character(len=3) :: ans
!
      ans = 'YES'
      read(*,*) N
      allocate(A(N))
      read(*,*) (A(i),i=1,N)
!
      call quicksort(N,A,1,N)
      do i=1,N-1
        if(A(i) == A(i+1)) ans='NO'
      end do
!
      write(*,*) ans
!
      end program answer
!
!-------------------------------------------------
      recursive subroutine quicksort(n,x,l,r)
!-------------------------------------------------
! Variables I/O
!-------------------------------------------------
! n     ...  the number of data (IN) : integer
! x(i)  ...  data (INOUT) : integer
! l     ...  the smallest label in a sorted region
!            (compute label)
! r     ...  the largest label in a sorted region
!            (compute label)
!-------------------------------------------------
! Variables (The others)
!-------------------------------------------------
! c     ...  the average of l and r : (l+r)/2
! ls    ...  the smallest label in a sorted region
!            (Input label)
! i     ...  the largest label in a sorted region
!            (Input label)
!-------------------------------------------------
! Contain
!-------------------------------------------------
! Subroutine : swap(a,b) : swaping a for b
!-------------------------------------------------
      implicit none
      integer,intent(in) :: n,l,r
      integer,dimension(n),intent(inout) :: x
      integer :: c,ls,i
!
      if(l < r) then
        c = (l+r) / 2 ! representative value
        call swap(x(l),x(c))
        ls = l
        do i=l+1,r
          if(x(i) < x(l)) then
            ls = ls + 1
            call swap(x(ls),x(i))
          end if
        end do
        call swap(x(ls),x(l))
        call quicksort(n,x,l,ls-1)
        call quicksort(n,x,ls+1,r)
      end if
      contains
!-------------------------------------------------
      subroutine swap(a,b)
!-------------------------------------------------
        integer,intent(inout) :: a,b
        integer :: w
        w=a
        a=b
        b=w
      end subroutine swap
      end subroutine quicksort
!

Measured memory_bytes: 1536
#include<stdio.h>
int main()
{
    int n,r,c=0;
    scanf("%d",&n);
    if(n<1000){
        while(n>0){

    r=n%10;
    if(r==7){
      c++;
      break;
    }
    n=n/10;
    }
    if(c>0)
        printf("Yes\n");
    else
        printf("No\n");

    }

}

Measured memory_bytes: 1616
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cstdio>
#include<cmath>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
int MOD=1000000007;
signed main(){
    int n;cin>>n;
    vector<int> t(n);
    rep(i,n)cin>>t[i];
    vector<int> a(n);
    rep(i,n)cin>>a[i];
    int res=1;
    int max=0;int max2=0;
    rep(i,n){
        if(t[i]==t[n-1]){
            max=i;
            break;
        }
    }
    for(int i=n-1;i>=0;i--){
        if(a[i]==a[0]){
            max2=i;
            break;
        }
    }
    if(t[n-1]!=a[0])res=0;
    int left=1;int right=1;
    int last=t[0];
    rep1(i,max){
        if(t[i]==last){
            left*=last;
            left%=MOD;
        }
        last=t[i];
    }
    last=a[n-1];
    for(int i=n-2;i>=max2;i--){
        if(a[i]==last){
            right*=last;
            right%=MOD;
        }
        last=a[i];
    }
    for(int i=max;i<=max2;i++){
        if(i!=max&&i!=max2){
            res*=t[max];
            res%=MOD;
        }
    }
    res*=left;
    res%=MOD;
    res*=right;
    res%=MOD;
    if(max>max2)res=0;
    cout<<res<<endl;
    return 0;
}

Measured memory_bytes: 1792
#include<iostream>
#include<queue>
using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n,M[N][N];
int d[N];

void bfs( int s ){
  queue<int> q;
  q.push(s);
  for( int i = 0 ; i < n ; i++ ){
    d[i] = INFTY;
  }
  d[s] = 0;int u;
  while( !q.empty() ){
    u = q.front();
    q.pop();
    for( int v = 0 ; v < n ; v++ ){
      if( M[u][v] == 0 )continue;
      if( d[v] != INFTY )continue;
      d[v] = d[u] + 1;
      q.push(v);
    }
  }
  for( int i = 0 ; i < n ; i++ ){
    cout << i + 1 << " " <<((d[i] == INFTY) ? ( -1 ) : d[i]) << endl;
  }
}

int main( void ){
  int u,k,v;
  cin >> n;
  for( int i = 0 ; i < n ; i++ ){
    for( int j = 0 ; j < n ; j++ ){
      M[i][j] = 0;
    }
  }

  for( int i = 0 ; i < n ; i++ ){
    cin >> u >> k;
    u--;
    for( int j = 0 ; j < k ; j++ ){
      cin >>v;
      v--;
      M[u][v] = 1;
    }
  }
  bfs(0);

  return 0;
}

Measured memory_bytes: 1240
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <climits>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    set<int> d_set;
    REP(i, n) {
        int d;
        cin >> d;
        d_set.insert(d);
    }
    cout << d_set.size() << endl;
    
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <set>
#include <vector>
#include <map>
#include <cstdio>
#include <utility>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <cassert>
#include <climits>
using namespace std;
typedef long long ll;
#define DEBUG
#ifdef DEBUG
	#define debug printf
#else
	#define debug(...)
#endif
const int maxn = 1e5 + 5;
int a[maxn];
int main(int argc, char const *argv[])
{
	int n;
	cin>>n;
	int mid = n / 2;
	for(int i = 1;i <= n; ++i)
	{
		scanf("%d",&a[i]);
	}
	sort(a + 1,a + n + 1);
	printf("%d\n",a[mid + 1] - a[mid]);
	return 0;
}

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

struct object{
    int x,y;

    bool operator < (const object &o) const {
        if(x != o.x){
            return x < o.x;
        }
        else {
            return y < o.y;
        }
    }
};

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

    int n;
    cin >> n;
    object o[n];
    
    for(int i = 0;i < n;++i){
        cin >> o[i].x >> o[i].y;
    }

    sort(o,o + n);

    for(int i = 0;i < n;++i){
        cout << o[i].x << " " << o[i].y << endl;
    }
}



Measured memory_bytes: 3996
// Bibary search tree
#include <stdio.h>
#include <string.h>
#define N 500001
 
 typedef struct node {
   int key;
   struct node* left;
   struct node* right;
 } NODE;

void insert(NODE*, NODE*);
void printInorder(NODE*);
void printPreorder(NODE*);

 int main() {
   int i, j;
   char order[10];
   int m, key;
   NODE node[N];
   NODE *z;

   // input
   scanf("%d", &m);
   
   for(i = 0; i < m; i++) {
     scanf("%s", order);
     if(strcmp(order, "insert") == 0) {
       scanf("%d", &key);
       if(i == 0) {
	 node[i].key = key;
	 node[i].left = NULL;
	 node[i].right = NULL;
       }
       else {
	 node[i].key = key;
	 node[i].left = NULL;
	 node[i].right = NULL;
	 insert(node, &node[i]);
       }
     }
     else if (strcmp(order, "print") == 0) {
       printInorder(&node[0]);
       printf("\n");
       printPreorder(&node[0]);
       printf("\n");
     }
     else printf("error\n");
   }

   return 0;
 }

void insert(NODE* tree, NODE* node) {
  int i = 0;
  NODE *x;
  NODE *y;

  y = NULL;
  x = &tree[0];
  while(x != NULL) {
    y = x;
    if(node->key < x->key) x = x->left;
    else x = x->right;
  }
  if(node->key < y->key){
    y->left = node;
  }
  else{
    y->right = node;
  }
}
 
void printInorder(NODE* node) {
  if(node != NULL) {
    printInorder(node->left);
    printf(" %d", node->key);
    printInorder(node->right);
  }
}

void printPreorder(NODE* node) {
  if(node != NULL) {
    printf(" %d", node->key);
    printPreorder(node->left);
    printPreorder(node->right);
  }
}



Measured memory_bytes: 13408
import itertools as it

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

ans = 0

# 初期化
diff = 0

for x1,x2 in list(it.combinations(A,2)):
        if (diff < abs(x1 - x2)):
                diff = abs(x1 - x2)

print(diff)



Measured memory_bytes: 3316
#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
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 (b<a) {a=b; return 1;} return 0;}
const ll INF = 1LL<<60;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    rep(i, n) cin >> a[i];

    ll price_min = INF;
    for (int bit = 0; bit < 1<<n; ++bit) {
        if (__builtin_popcount(bit) < k) continue;
        ll price_cur = 0;
        int now_h = 0;
        rep(i, n) {
            if (bit>>i&1) {
                if (a[i] <= now_h) {
                    price_cur += now_h - a[i] + 1;
                    now_h++;
                }
            }
            chmax(now_h, a[i]);
        }
        chmin(price_min, price_cur);
    }
    cout << price_min << endl;
}

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

// Begin {{{
using namespace std;

#define all(x) x.begin(), x.end()
#define rep(i, n) for (i64 i = 0; i < (n); ++i)
#define reps(i, b, e) for (i64 i = (b); i <= (e); ++i)
#define repr(i, b, e) for (i64 i = (b); i >= (e); --i)
#define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__)

#ifdef DEBUG
#define dump(...) dump_(#__VA_ARGS__, __VA_ARGS__);
#else
#define dump(...)
#endif

using i64 = int_fast64_t;
using pii = pair<i64, i64>;

template <class A, class B>
inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }

template <class A, class B>
inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int MOD = int(1e9) + 7;

// input
void input() {}
template <class Head, class... Tail>
void input(Head&& head, Tail&&... tail) {
  cin >> head;
  input(forward<Tail>(tail)...);
}

// outs
void outs() {}
template <class Head, class... Tail>
void outs(Head&& head, Tail&&... tail) {
  cout << head << " \n"[sizeof...(tail)==0];
  outs(forward<Tail>(tail)...);
}

template <class T>
void outs(vector<T> &vec) { for (auto &e : vec) cout << e << " \n"[&e==&vec.back()]; }

template <class T>
void outs(vector<vector<T>> &df) { for (auto &vec : df) OUTS(vec); }

// outl
void outl() {}
template <class Head, class... Tail>
void outl(Head&& head, Tail&&... tail) {
  cout << head << "\n";
  outl(forward<Tail>(tail)...);
}

template <class T>
void outl(vector<T> &vec) { for (auto &e : vec) cout << e << "\n"; }

// outn
void outn() {}
template <class Head, class... Tail>
void outn(Head&& head, Tail&&... tail) {
  cout << head;
  outn(forward<Tail>(tail)...);
}

template <class T>
void outn(vector<T> &vec) { for (auto &e : vec) cout << e; }

// dump
template <class T>
void dump_(const char *s, T&& x) {
  clog << '{';
  while(*s != '\0') clog << *(s++);
  clog << ": " << x << '}' << "\n";
}

template <class Head, class... Tail>
void dump_(const char *s, Head&& head, Tail&&... tail) {
  clog << '{';
  while(*s != ',') clog << *(s++);
  clog << ": " << head << "}, ";
  for (++s; !isgraph(*s); ++s);
  dump_(s, forward<Tail>(tail)...);
}
// }}} End

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

  var(int, r);

  if (r < 1200) outl("ABC");
  else if (r < 2800) outl("ARC");
  else outl("AGC");

  return 0;
}


Measured memory_bytes: 256
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<map>
#include<iostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<ll> vl;
typedef queue<int> qi;
typedef queue<ll> ql;
typedef pair<ll,ll> pll;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
#define rep(i,n) for (int i=0;i<n;i++)
#define mp make_pair
#define pb push_back
#define pf push_front
#define F first
#define S second
int a,b,c,d,f;
float e;
set<float> wat;
set<int> sug;
double ans=0;
int anst=0,anss=0;
int main()
{
    ios::sync_with_stdio(false);
    cin>>a>>b>>c>>d>>e>>f;
    rep(i,105) rep(j,105){
    	if ((a*i+j*b)*100>f||a*i+b*j==0) continue;
    	wat.insert((a*i+b*j)*100);
    }
    rep(i,2005) rep(j,2005){
    	if (c*i+d*j>f||a*i+b*j==0) continue;
    	sug.insert(c*i+d*j);
    }
    set<int>::iterator it;
    for (it=sug.begin();it!=sug.end();it++){
    	float now=*it;
    	set<float>::iterator fnd=wat.lower_bound(now*100.0/e);
    	if (fnd==wat.end()) continue;
    	if (now+*fnd>f) continue;
    	if (ans< double(now)/(now+*fnd)){
    		ans=double(now)/(now+*fnd);
    		anst=now+*fnd;
    		anss=now;
    	}
    }
    if (anst==0&&anss==0) anst=100*a; 
    cout<<anst<<" "<<anss;
}

Measured memory_bytes: 384
def main():
    N = int(input())
    L = list(map(int,input().split()))
    list.sort(L)
    ans = 0
    
    for i in range(N-2):
        for j in range(i+1,N-1):
            for k in range(j+1,N):
                if L[i] != L[j] and L[k] != L[j] and L[i] != L[k]:
                    if L[i]+L[j]>L[k]:
                        ans += 1
                    
    
    print(ans)
    
if __name__ == '__main__':
    main()

Measured memory_bytes: 9196
#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
    int n,k;
    long a;

    cin >> n >> k;

    a = k;
  
    for (int i = 0; i < n - 1; ++i) {
        a = a * (k - 1);
    }

    cout << a << endl;   
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
    private static final Scanner scn = new Scanner(System.in);
    
    public static void main(String[] args) {
        int n = scn.nextInt();
        insertionSort(createArray(n), n);
    }
    
    private static int[] createArray(int n) {
        int[] ary = new int[n];
        for(int i = 0; i < n; i++) {
            ary[i] = scn.nextInt();
        }
        return ary;
    }
    
    private static void showArray(int[] ary) {
        StringBuilder sb = new StringBuilder();
        for(int elm : ary) {
            sb.append(elm + " ");
        }
        System.out.println(sb.toString().trim());
    }
    
    private static void insertionSort(int[] ary, int n) {
        showArray(ary);
        for(int i = 1; i < n; i++) {
            int v = ary[i];
            int j = i - 1;
            while(j >= 0 && ary[j] > v) {
                ary[j + 1] = ary[j];
                j--;
            }
            ary[j + 1] = v;
            showArray(ary);
        }
    }
}

Measured memory_bytes: 30040
#include <algorithm>
#include <bitset>
#include <iostream>
#include <vector>

#define rep(inc, bgn, end) for (int inc = bgn; inc < end; ++inc)
#define repe(inc, bgn, end) for (int inc = bgn; inc <= end; ++inc)

using ll = long long;
using namespace std;

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

    int offset;
    cin >> offset;  // 1番目だけ保留
    int prev = offset;
    int next = 0;
    int len  = 0;
    rep(i, 1, n) {
        cin >> next;
        len  = max(len, next - prev);
        prev = next;
    }
    len = max(len, offset + k - next);

    cout << k - len << endl;

    return 0;
}


Measured memory_bytes: 256
N,X,Y = map(int,input().split())
X,Y = X-1,Y-1

ans = [0]*N

for i in range(N):
    for j in range(i+1,N):
        if j <= X or Y <= i:
            a = j - i
        elif i <= X and Y <= j:
            a = X-i + 1 + j-Y
        elif i <= X and j <= Y:
            a = min(j-i,X-i + 1 + Y-j)
        elif X <= i and j <= Y:
            a = min(j-i,i-X+1+Y-j)
        else:
            a = min(j-i,i-X+1+j-Y)
        ans[a] += 1

print(*ans[1:],sep='\n')


Measured memory_bytes: 3572
class Die
  def initialize(men)
    @t, @s, @e, @w, @n, @b = men
  end

  def top; @t; end
  def front; @s; end
  def right; @e; end

  def move(d)
    case d
    when 'N'
      @t, @s, @b, @n = @s, @b, @n, @t
    when 'W'
      @w, @b, @e, @t = @t, @w, @b, @e
    when 'E'
      @w, @b, @e, @t = @b, @e, @t, @w
    when 'S'
      @t, @s, @b, @n = @n, @t, @s, @b
    end
  end

  def make_top(num)
    3.times do
      move 'N'
      return if @t == num
      move 'E'
      return if @t == num
    end
    raise "#{num} is not found"
  end

  def rotate
    @n, @w, @s, @e = @w, @s, @e, @n
  end

  def rotate_until(num)
    4.times do
      rotate
      return if @s == num
    end
    raise "#{num} is not found"
  end

  def all
    [@t, @s, @e, @w, @n, @b]
  end


  def ==(other)
    cmp = self.all.sort.uniq
    if cmp.size == 6
      self.make_top(other.top)
      self.rotate_until(other.front)
      return self.all == other.all
    else
      3.times do
        move 'N'
        if @t == other.top
          4.times do
            rotate
            if @s == other.front
              return true if self.all == other.all
            end
          end
        end
        move 'E'
        if @t == other.top
          4.times do
            rotate
            if @s == other.front
              return true if self.all == other.all
            end
          end
        end
      end
    end
    false
  rescue
    false
  end
end

d1 = Die.new(gets.split)
d2 = Die.new(gets.split)
puts (d1 == d2) ? 'Yes' : 'No'



Measured memory_bytes: 6884
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
typedef long long ll;
long long MOD = 1e9 + 7;
const long long INF = 1LL << 60;
using namespace std;

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

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

int main() {
  string s, t;
  cin >> s >> t;
  cout << t + s << endl;
  return 0;
}


Measured memory_bytes: 256
//   _/                _/                 _/_/_/   _/
//_/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/
// _/     _/    _/   _/     _/    _/  _/_/_/     _/
//_/     _/    _/   _/     _/    _/  _/    _/   _/
// _/_/   _/_/       _/_/   _/_/      _/_/     _/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const int inf=1e9;
const ll INF=1e18;
const double pi=3.1415926535897932384626433832;
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  int n,ans=inf,w[200005]={},b[200005]={};
  string s;
  cin >> n >> s;
  for(int i=1; i<=n; i++) {
    if(s[i-1]=='#') {
      b[i]=b[i-1]+1;
    }
    else {
      b[i]=b[i-1];
    }
  }
  for(int i=1; i<=n; i++) {
    if(s[i-1]=='.') {
      w[i]=w[i-1]+1;
    }
    else {
      w[i]=w[i-1];
    }
  }
  for(int i=1; i<=n; i++) {
    ans=min(ans,b[i-1]+w[n]-w[i]);
  }
  cout << ans << endl;
}

Measured memory_bytes: 2384
#include <iostream>
#include <vector>
#include<list>
#include <algorithm>
#include <string>
#include <math.h>
#include<map>
#include <utility>

#include<queue>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
 
#define INF (1e9)
typedef long long unsigned int ll;
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))
 

 
int vector_finder(std::vector<int> vec, int number) {
  size_t index = std::distance( vec.begin(), std::find(vec.begin(), vec.end(), number) );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

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

int N,K;

int main()
{
	cin >> N>>K;
	int A[N];
	int idx;
	REP(i, N)
	{
		cin >> A[i];
		if(A[i]==1){
			idx = i;
		}
	}
	if((N-1)%(K-1)==0){
		cout << (N - 1) / (K - 1)<< endl;
	}else{
		cout << (N - 1) / (K - 1)+1 << endl;
	}
	


	return 0;
}

Measured memory_bytes: 640
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;

typedef long long ll;

const int MAXN = 100005;

int N;
int a[3 * MAXN];
priority_queue <int> mx, mn;
ll maks[3 * MAXN], mini[3 * MAXN];

void load() {
  scanf("%d", &N);
  for (int i = 0; i < 3 * N; i++)
    scanf("%d", a + i);
}

ll solve() {
  ll curr = 0;
  for (int i = 0; i < 3 * N; i++) {
    curr += a[i];
    mn.push(-a[i]);
    if (i >= N) {
      curr += mn.top();
      mn.pop();
    }
    maks[i] = curr;
  }
  
  curr = 0;
  for (int i = 3 * N - 1; i >= 0; i--) {
    curr += a[i];
    mx.push(a[i]);
    if (i < 2 * N) {
      curr -= mx.top();
      mx.pop();
    }
    mini[i] = curr; 
  }
  
  ll sol = -(ll)1e18;
  for (int i = N - 1; i < 2 * N; i++)
    sol = max(sol, maks[i] - mini[i + 1]);
  
  return sol;
}

int main() {
  load();
  printf("%lld\n", solve());
  return 0;
}

Measured memory_bytes: 7032
#include <iostream>
using namespace std;
typedef long long ll;
const int NAX = 3e5 + 5, MOD = 1e9 + 7;
ll a[NAX], ones[62];
//Credit for MOD operations functions add and mul to : LJC00118
inline int add(int x, int y) {
    x += y;
    if(x >= MOD) x -= MOD;
    return x;
}
 

inline int mul(int x, int y) { return 1ll * x * y % MOD; }

int main() {
	int n;
	cin >> n;
	/*
	Our idea : - 
		0 0 1
		0 1 0
		0 1 1
		-----
		0 2 2
		-----
		
	*/
	for(int i=0; i<n; ++i) {
		cin >> a[i];
		for(ll j=0; j<60; ++j) {
			ll p = ((ll)1) << j;
			if(a[i] & p) {
				++ones[j];
			}
		}
	}
	ll res = 0;
	for(int i=0; i<n; ++i) {
		for(int j=0; j<60; ++j) {
			ll p = ((ll)1) << j;
			ll tot_ones = 0; 
			if(a[i] & p) {
				--ones[j];
				tot_ones += n - ones[j] - i - 1;
			} else {
				tot_ones += ones[j];
			}
			p %= MOD;
			// if(tot_ones)
			// 	cout << "tot_ones: " << tot_ones << " a[i]: " << a[i] << " j: " << j << endl;
			res = add(res, mul(p, tot_ones));
		}
	}
	cout << res << endl;
	return 0;
}

Measured memory_bytes: 2560
#include<bits/stdc++.h>
using namespace std;
long long n,ans;
int main(){
    scanf("%lld",&n);
    if(!(n&1)){
        n>>=1;
        while(n){
            ans+=n/5;
            n/=5;
        }
        printf("%lld\n",ans);
    }
    else puts("0");
    return 0;
}

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

int main() {
    int n, m, cnt=0;
    cin >> n >>m;
    vector<int> a(n);
    for(int i=0; i<n; i++) {
        cin >> a[i];
        cnt+=a[i];
    }
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    bool flag=true;
    for(int i=0; i<m; i++) {
        if(a[i]*4*m<cnt) {
            flag=false;
            break;
        }
    }
    if(flag) cout << "Yes";
    else cout << "No";
}

Measured memory_bytes: 256
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for(int i = 0; i < (n); ++i)
typedef long long ll;
const int MOD = 1000000007;
using namespace std;


int main(){
    int n; cin >> n;
    vector<ll> sum(n);
    REP(i, n){
        ll a; cin >> a;
        if(i) sum[i] = sum[i-1] + a;
        else sum[i] = a;
    }
    ll difmin = 9000000000000000000LL;

    REP(i, n-1){
        difmin = min(difmin, llabs(sum[n-1] - 2* sum[i]));
    }
    cout << difmin << "\n";
    return 0;
}


Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
int main(void){
  int N, K;
  cin >> N >> K;
  vector<int> l(N);
  for(auto &itr : l) cin >> itr;
  sort(l.rbegin(), l.rend());
  int ans = 0;
  for(int i = 0; i < K; i++) ans += l[i];
  cout << ans << endl;
  return 0;
}


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

int main() {
	stack<char>s;
	int ans = 0;
	string a;
	cin >> a;
	for (int i = 0; i < a.size(); i++) {
		if (s.size()) {
			if (a[i] == 'T'&&s.top()=='S') {
				ans+=2;
				s.pop();
			}
			else s.push(a[i]);
		}
		else s.push(a[i]);
	}
	cout << a.size()-ans << endl;
}

Measured memory_bytes: 644
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>   
#include<limits.h>
#include<list>
#include <array>
using namespace std;
#define i64  long long
#define int  long long
 
#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807
#define INF I64_MAX
#define MOD 1000000007
#define MEM_SIZE = 1e5+5;

struct mint {
  int x; // typedef long long int;
  mint(int x=0):x((x%MOD+MOD)%MOD){}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += MOD-a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(int t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const {
    return pow(MOD-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};



const int MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}


void solve(void)
{
  int N,K;
  cin>>N>>K;
  vector<int> vec(N);
  for (int i = 0; i < N; i++)
  {
    cin>>vec[i];
  }
  sort(vec.begin(),vec.end());

  COMinit();
  mint ANS;
  //MAX
  int minsuf = 0;
  for (int i = N-1; i >= K-1 ; i--)
  {
    int NUM = i;
    int T = COM(NUM,K-1);
    // cout<<T<<endl;
    ANS += T*vec[i]%MOD;
    ANS -= T*vec[minsuf]%MOD;
    minsuf++;
  }
  cout<<ANS.x<<endl;
  return;
}


int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  // std::cout << std::fixed;
  // std::cout << std::setprecision(8);

  solve();

 
  return 0;
}

Measured memory_bytes: 13056
(function main() {
  const n = Number(require('fs').readFileSync('/dev/stdin', 'utf8'));
  const cache = [1, 1];
  const fib = (n) => cache[n] || (cache[n] = fib(n - 1) + fib(n - 2));
  console.log(fib(n));
})();



Measured memory_bytes: 15136
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
int main()
{
    int n,k;
    cin>>n>>k;
    vector<ll> h(n,0);
    rep(i,n) cin>>h[i];

    if(n<=k)
    {
        cout<<0<<endl;
        return 0;
    }
    sort(h.begin(),h.end(),greater<ll>());
    auto itr=h.begin()+k;
    ll ans=0;
    for (auto i = itr; i!=h.end(); i++)
    {
        ans+=*i;
    }
    cout<<ans<<endl; 

    return 0;
}

Measured memory_bytes: 1792
/* Author: Dhruv Rastogi */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define all(sdk) sdk.begin(),sdk.end()
#define mll map<int, int>
#define pb push_back
#define rep(i,a,b) for(int i=a;i<b;++i)
#define repd(i,a,b) for(int i=a;i>=b;--i)
#define mp make_pair
#define hell 1000000007
#define endl '\n'
#define vvll vector<vector<int> >
#define vll vector<int>
#define mint map<int,int> 
#define sz(x) (int)x.size()
#define sll set<int>
#define pll pair<int,int>
#define F first
#define S second
const long double PI = acos(-1.0);

int a[200005];
vll adjlist[200005];
int ans[200005];
void dfs(int node,int par,int val){
	val+=a[node];
	ans[node]=val;
	for(int i:adjlist[node]){
		if(i!=par){
			dfs(i,node,val);
		}
	}
}	
void solve(){  
	int n,q;
	cin>>n>>q;
	rep(i,0,n-1){
		int x,y;
		cin>>x>>y;
		adjlist[x].pb(y);
		adjlist[y].pb(x);
	}  
	rep(i,0,q){
		int x,v;
		cin>>x>>v;
		a[x]+=v;
	}
	dfs(1,0,0);
	rep(i,1,n+1) cout<<ans[i]<<' ';
	cout<<endl;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

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

const ll LINF = 1e18;
const int INF = 1e9;
const ll MOD = 1000000007;


int main(){

    int x;
    cin >> x;
    int a, b;
    cin >> a >> b;

    x -= a;
    cout << x%b << endl;

    return 0;
}

Measured memory_bytes: 3640
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define LEN 100005

typedef struct pp {
	char name[100];
	int t;
}P;

P Q[LEN];
int head, tail, n;

void enqueue(P x){
	Q[tail] = x;
	tail = (tail + 1) % LEN;
}

P dequeue() {
	P x = Q[head];
	head = (head + 1) % LEN;
	return x;
}

int min(int a, int b) {
	if (a > b) {
		return b;
	}else{
		return a;
	}
}

int main() {

	int time = 0, c;
	int i,q;
	P u;

	scanf("%d %d", &n, &q);
	
	for (i = 1; i <= n; i++) {
		scanf("%s", Q[i].name);
		scanf("%d", &Q[i].t);
	}

	head = 1;
	tail = n + 1;

	while (head != tail) {
		u = dequeue();
		c = min(q, u.t);
		u.t -= c;
		time += c;
		if (u.t > 0) {
			enqueue(u);	
		}
		else{
			printf("%s %d\n", u.name, time);
		}
	}


	return 0;

}

Measured memory_bytes: 11188