akhauriyash/Code-Regression huggingface dataset:
import Data.List.Split

data Rect = Rect {
  len :: Int,
  wid :: Int
} deriving (Show)

readRect :: String -> Rect
readRect line =
  let l:w:_ = splitOn " " line
      len = read l :: Int
      wid = read w :: Int
  in Rect len wid

getArea :: Rect -> Int
getArea Rect { len=l, wid=w } = l * w

getPerimeter :: Rect -> Int
getPerimeter Rect { len=l, wid=w } = l * 2 + w * 2

main = do
  line <- getLine
  let r = readRect line
      rarea = getArea r
      rperi = getPerimeter r
  putStrLn $ show rarea ++ " " ++ show rperi
  return ()

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

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T>
void printv(const vector<T> &v)
{
    int sz = v.size();
    for (int i = 0; i < sz; i++)
    {
        cout << v[i] << " \n"[i == sz - 1];
    }
}

set<P> ret;

void rec(int cur, int par, const vector<vector<int>> &g, vector<int> &sum)
{
    for (auto v : g[cur])
    {
        if (v == par)
            continue;
        rec(v, cur, g, sum);
        if (sum[v] % 2)
        {
            ret.emplace(v, cur);
        }
        else
        {
            ret.emplace(cur, v);
        }
        sum[cur] += sum[v];
    }
}

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

    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n);
    for (int i = 0; i < m; i++)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    if (m % 2)
    {
        cout << -1 << endl;
        return 0;
    }
    vector<vector<int>> tr(n);
    queue<P> que;
    vector<int> used(n, 0), cnt(n, 0);
    que.push(P(0, -1));
    used[0] = 1;
    while (!que.empty())
    {
        int cur = que.front().first;
        int par = que.front().second;
        que.pop();
        for (auto v : g[cur])
        {
            if (v == par)
                continue;
            if (used[v])
            {
                if (ret.find(P(v, cur)) != ret.end())
                    continue;
                ret.emplace(cur, v);
                cnt[cur]++;
                continue;
            }
            tr[cur].push_back(v);
            cnt[cur]++;
            used[v] = 1;
            que.emplace(v, cur);
        }
    }
    vector<int> sum(n, 0);
    for (int i = 0; i < n; i++)
        if (cnt[i] % 2)
            sum[i]++;
    rec(0, -1, tr, sum);
    assert(ret.size() == m);
    vector<int> aa(n, 0);
    for (auto p : ret)
    {
        aa[p.first]++;
        cout << p.first + 1 << " " << p.second + 1 << endl;
    }
    for (int i = 0; i < n; i++)
        assert(aa[i] % 2 == 0);
}


Measured memory_bytes: 22528
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
const ll mod = 1e9 + 7;
int main(){
    int x;
    cin >> x;
    if(x == 1)
        cout << 0 << endl;
    else 
        cout << 1 << endl;
    return 0;
}

Measured memory_bytes: 3628
n = int(input())
xl_lis = []
for i in range(n):
    xl_lis.append(list(map(int, input().split())))
fe_lis = []

for x, l in xl_lis:
    fe_lis.append([x - l, x + l])
fe_lis.sort(key = lambda x:x[1])        
curr = float("-inf")
cnt = 0
for f, e in fe_lis:
    if curr <= f:
        cnt += 1
        curr = e
print(cnt)       

Measured memory_bytes: 43080
N,M = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N)]
import heapq
AB.sort()
heap = []
heapq.heapify(heap)
ans = 0
i = 0
day = 1
while day <= M:
    while i < N and AB[i][0] <= day:
        heapq.heappush(heap,-AB[i][1])
        i += 1
    day += 1
    if not(heap):continue
    ans -= heapq.heappop(heap)

print(ans)

Measured memory_bytes: 31672
s = input()
s1 = ''
s2 = ''
s1s = '0'
s2s = '1'
dems1 = 0
dems2 = 0
for i in range(len(s)):
    s1+=s1s
    if s1s != s[i]:
        dems1+=1
    tam = s1s
    s1s = s2s
    s2+=s2s
    if s2s != s[i]:
        dems2+=1
    s2s = tam
print(min(dems1,dems2))


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

#define endl "\n"
#define INF 9e18
#define HELL (int) (1e9 + 7)
#define int long long
#define double long double
#define uint unsigned long long
#define deb(x) cerr << #x << " => " << x << "\n"
#define deba(x) cerr << #x << "\n"; for (auto a : x) cerr << a << " "; cerr << "\n";
#define debm(x) cerr << #x << "\n"; for (auto a : x){for(auto b : a) cerr << b << " "; cerr << "\n";}
#define getMat(x, n, m, val) vector<vector<int>> x(n, vector<int> (m, val))
#define fastio ios_base :: sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define pout cout << fixed << setprecision(10)

int fastpow(int a, int b, int m) {
    int res = 1; a %= m; while (b > 0) {
        if (b & 1) res = (res * a) % m;
        a = (a * a) % m; b >>= 1;
    } return res;
}

int now() {
    return chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now()
    .time_since_epoch()).count();
}

int32_t main() { fastio;
    time_t start = now();

    int n; cin >> n;
    cout << (n + 1) / 2 << endl;

    cerr << "TIME => " << now() - start << endl;
    return 0;
}

Measured memory_bytes: 256
package main

import (
    "fmt"
    "os"
    "bufio"
    "sort"
    "math"
    "strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {

    sc.Split(bufio.ScanWords)
    n := nextInt()
    var p []int

    for i := 0; i < n; i++ {
        p = append(p, nextInt())
    }

    min_val := 1001001001

    count := 0
    for i := 0; i < n; i++ {
        min_val = min(min_val, p[i])
        if min_val == p[i] {
            count++
        }
    }

    fmt.Println(count)

}



//===========================
//
// Library
//
//===========================

//===========================
//  round(12.54, 0) -> 13.0
//  round(12.54, -1) -> 12.5
//===========================

func round(num float64, digit uint) float64 {
    d := float64(digit)
    return float64(int(num * math.Pow(10, d) + 0.5)) / math.Pow(10, d)
}

func max(a, b int) int {
    return int(math.Max(float64(a), float64(b)));
}

func min(a, b int) int {
    return int(math.Min(float64(a), float64(b)));
}

func gcd(x, y int) int {
    if y == 0 {
        return x
    }

    return gcd(y, x%y)
}

//===========================
//  var q Queue
//  q.Enqueue(n)
//  n := q.Dequeue()
//===========================
type Queue struct {
    data []int
}

func (q *Queue) Enqueue(n int) {
    q.data = append(q.data, n)
}

func (q *Queue) Dequeue() int {
    tmp := q.data[0]
    q.data = q.data[1:]
    return tmp
}

// 数値を桁区切りのスライスにする
func digit(n int, list []int) []int {
   if n > 0 {
       return digit(n/10, append(list, n%10))
   }
   return list
}

func sortIntsRev(list []int) {
    sort.Sort(sort.Reverse(sort.IntSlice(list)))
}

func strReverse(s string) string {
    runes := []rune(s)
    for i := 0; i < len(s)/2; i++ {
        runes[i], runes[len(s)-1-i] = runes[len(s)-1-i], runes[i]
    }
    return string(runes)
}

func nextInt() int {
    sc.Scan()
    i, _ := strconv.Atoi(sc.Text())
    return i
}

func nextStr() string {
    sc.Scan()
    return sc.Text()
}



Measured memory_bytes: 8448
#include<bits/stdc++.h>
using namespace std;
int main(){
    int w,a,b;
    cin >> w >> a >> b;
    if(a<=b){
        if(b>(a+w)){
        cout << abs(b-(a+w)) << endl;
        }
        else{
            cout << 0 << endl;
        }
    }
    else{
        if(a>(b+w)){
        cout << abs(a-(b+w)) << endl;
        }
        else{
            cout << 0 << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cmath>
#include<string>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
        int n;
        scanf("%d",&n);
        vector<int>ar(n);
        for(auto&e:ar){
                scanf("%d",&e);
        }
        for(int i=n-1;i>-1;i--){
                if(i>0){
                        printf("%d ",ar[i]);
                }else{
                        printf("%d\n",ar[i]);
                }
        }
        return 0;
}

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

typedef pair<int, int> P;

int main() {
    int n, m;
    cin >> n >> m;
    int a[n];
    P p[m];
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a, a + n);
    for (int i = 0; i < m; i++) {
        int b, c;
         cin >> b >> c;
        p[i] = P(c, b);
    }
    sort(p, p + m, greater<P>());
    long ans = 0;
    int j = 0;
    for (int i = 0; i < n; i++) {
        if (p[j].first <= a[i] || j == m)
            ans += a[i];
        else {
            ans += p[j].first;
            p[j].second--;
            if (p[j].second == 0)
                j++;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1408
/*
  AOJ 1609
  Title:Look for the Winner!
  @kankichi573
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int N,table[26],table2[26],n1,n2,winner;
int mikakutei,turn;
char vote;

int compare(const void *a, const void *b)
{
  if( *(int *)a > *(int *)b)
    return(-1);
  if( *(int *)a < *(int *)b)
    return(1);
  if( *(int *)a == *(int *)b)
    return(0);
}


main()
{
int i,j,ret;
  
  while(scanf("%d ",&N) && N)
    {
      memset(table, 0,sizeof(table));

      winner=-1;
      mikakutei = -1;

      for(i=0;i<N;i++)
	{
	  scanf("%c ",&vote);
	  //printf("%d: %c\n",i,vote);
	  table[vote-'A']++;

	  //for(j=0;j<26;j++)
	  //	if(table[j])
	  //	  printf("%c [%d]\n",'A'+j,table[j]);



	  if(mikakutei)
	    {
	      
	      memcpy(table2,table,sizeof(table));
               qsort(table2,26,sizeof(int),compare);
               n1=table2[0],n2=table2[1];
	       //printf("n1n2zan=%d %d [%d]\n",n1,n2,N-i-1);
               if(n1 > n2 + (N-i-1))
		{
		  for(j=0;j<26;j++)
		    if(n1==table[j])
		      {
			winner = j;
			turn   = i;
			mikakutei=0;
			break;
		      }
		}
	    }
	}
    
      if(winner!=-1)
	{
	  printf("%c %d\n",winner+'A',turn+1);
	  // printf("%d %d\n",n1,n2);
	}
      else
	printf("TIE\n");
    }
  return(0);
}

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

#define NIL NULL

int parent(int i){
  return floor(i / 2);
}

int left(int i){
  return i * 2;
}

int right(int i){
  return (i * 2) + 1;
}

int main(){
  int h,i;
  int *k,*pk,*lk,*rk;

  scanf("%d",&h);

  k = malloc(sizeof(int)*(h+1));
  pk = malloc(sizeof(int)*(h+1));
  lk = malloc(sizeof(int)*(h+1));
  rk = malloc(sizeof(int)*(h+1));

  for(i = 1;i < h+1;i++){
    scanf("%d",&k[i]);
  }

  for(i = 1;i < h+1;i++){
    if(i != 1){
      pk[i] = k[parent(i)];
    }
    lk[i] = k[left(i)];
    rk[i] = k[right(i)];
  }

  for(i = 1;i < h+1;i++){
    printf("node %d: key = %d, ",i,k[i]);
    if(i != 1){
      printf("parent key = %d, ",pk[i]);
    }
    if(lk[i] != NIL && left(i) <= h){
      printf("left key = %d, ",lk[i]);
    }
    if(rk[i] != NIL && right(i) <= h){
      printf("right key = %d, ",rk[i]);
    }
    printf("\n");
  }

  return 0;
}

Measured memory_bytes: 604
#include<bits/stdc++.h>
using namespace std;
int dp[3010][3010];

string s1, s2;
int f(int p1, int p2){
     if(p1<0 or p2<0) return 0;
     int&ans=dp[p1][p2];
     if(ans!=-1) return ans;

     ans=0;
     if(s1[p1]==s2[p2]) ans=1+f(p1-1, p2-1);
     else{
          ans=max(ans, f(p1-1, p2));
          ans=max(ans, f(p1, p2-1));
     }
     return ans;
}
string ans="";
void rec(int p1, int p2){
     if(p1==0){
          for(int i=p2;i>=0;i--){
               if(s1[p1]==s2[i]){
                    ans=s1[p1]+ans;
                    break;
               }
          }
           cout<<ans;
           exit(0);
     }
     if(p2==0){
          for(int i=p1;i>=0;i--){
               if(s1[i]==s2[p2]){
                    ans=s2[p2]+ans;
                   break;
               }
          }
         cout<<ans;
           exit(0);
     }
     
    if(s1[p1]==s2[p2]){
         ans=s1[p1]+ans;
         rec(p1-1, p2-1);
    }
    else{
         int temp1, temp2;
         temp1=dp[p1-1][p2];
         temp2=dp[p1][p2-1];
         if(temp1>temp2){
              rec(p1-1, p2);
         }
         else rec(p1, p2-1);
    }
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    memset(dp, -1, sizeof(dp));
     cin>>s1>>s2;
     int temp=f(s1.size()-1, s2.size()-1);
    rec(s1.size()-1, s2.size()-1);
}




Measured memory_bytes: 36096
#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    string b;
    cin>>b;
    
    if(b=="A"){
      cout<<"T"<<endl;
    }
    if(b=="T"){
      cout<<"A"<<endl;
    }
    if(b=="C"){
      cout<<"G"<<endl;
    }
    if(b=="G"){
      cout<<"C"<<endl;
      }
}

Measured memory_bytes: 256
A, B, K = map(int, input().split())

k = 0
while True:

    if A % 2 != 0:
            
        A -= 1

    #print('a-1',A, B)

    B += round(A/2)
    A -= round(A/2)

    #print('a-2', A, B)
    k += 1
    if k == K:
        break

    if B % 2 != 0:
        
        B -= 1

    #print('b-1', A, B)

    A += round(B/2)
    B -= round(B/2)

    #print('b-2', A, B)
    k += 1
    if k == K:
        break

print(A, B)

Measured memory_bytes: 3064
#include<bits/stdc++.h>
 const int N=1e5+5;
 int a[N];
 int main()
 {
 	int n,ai;
 	scanf("%d",&n);
 	while(n--)
 	{
 		scanf("%d",&ai);
 		a[ai]++;
 		a[ai-1]++;
 		a[ai+1]++;
	}
	int max=0;
	for(int i=0;i<N-1;i++)
	{
		if(max<a[i])
		{
			max=a[i];
		}
	}
	printf("%d",max);
 } 

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

using namespace std;
#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++) 
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                                                                                                                                                Red

inline void Input_Output () {
	//freopen(".in", "r", stdin);
   //freopen(".out", "w", stdout);
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;                                          
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
 
int n, m, k;
string s;
int a[N];
vector < int > g[N];
int lp[mxn];

int cnt[mxn];

int bpow (ll a, int b) {
	ll res = 1;
	while (b > 0) {
		if (b & 1) (res *= a) %= mod;
		(a *= a) %= mod;
		b /= 2;
	}
	return res;
}


int main () {
	SpeedForce;
	for (int i = 2; i < mxn; ++i) {
		if (!lp[i]) {
			for (int j = i; j < mxn; j += i)
				lp[j] = i;
		}
	}

	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		int x = a[i];
		while (x > 1) {
			int y = lp[x];
			int cur = 0;
			while (x % y == 0) {
				x /= y;
				++cur;
			}
			cnt[y] = max(cnt[y], cur);
		}
	}

	int lcm = 1;
	for (int i = 2; i < mxn; ++i) if (lp[i] == i) {
		for (int j = 0; j < cnt[i]; ++ j) {
			lcm = (lcm * (ll)i) % mod;
		}
		
	}

	//cout << lcm << '\n';
	int ans = 0;
	for (int i = 1; i <= n; i ++) {
		ans = (ans + lcm * (ll)bpow(a[i], mod - 2)) % mod;
	}
	cout << ans << '\n';



   	return Accepted;
}

// B...a

Measured memory_bytes: 22528
#include <iostream>
using namespace std;
int main(void){
    int N,M,a=1;
    cin>>N>>M;
    for(int i=1;i*i<=M&&i<=M/N;i++){
        if(M%i==0){
            a=max(a,i);
            if(M/i<=M/N){
                a=max(a,M/i);
            }
        }
    }
    cout<<a<<endl;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace AtCoder.B
{
    public class Program
    {
        public static void Main() { var r = GetResult(); Debug.WriteLine(r); Console.Write(r); }

        private static object GetResult()
        {
            var N = ReadLong();
            var d = ReadLongs();

            var sum = 0L;
            for (int i = 0; i < N - 1; i++)
            {
                for (int j = i + 1; j < N; j++)
                {
                    sum += d[i] * d[j];
                }
            }

            return sum;
        }

        #region Console

        public static string ReadText() { return Console.ReadLine(); }
        public static List<string> ReadTexts() { return Console.ReadLine().Split(' ').ToList(); }
        public static long ReadLong() { return long.Parse(Console.ReadLine()); }
        public static List<long> ReadLongs() { return Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList(); }

        #endregion
    }
}


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

int main(){
  
  int A,B; cin >> A >> B;
  
  if(A > 8 || B > 8){
    cout << ":(" << endl;
  }
              
  else{
    cout << "Yay!" << endl;
  }
  
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,b) for(int i=0; i<int(b);i++)
#define eb emplace_back

typedef int Weight;
struct Edge {
    int src, dst;
    Weight weight;
    Edge(int src_, int dst_, Weight weight_) :
        src(src_), dst(dst_), weight(weight_) { }
};
bool operator < (const Edge &e, const Edge &f) {
    return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
        e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

int const MAX_V = 10010;
void stronglyConnectedComponents(Graph const & g, int cmp[MAX_V]){
    int n = g.size();
    Graph rg(n);
    rep(i,n) for(auto & e : g[i]) rg[e.dst].eb(e.dst, e.src, e.weight);
    static int added[MAX_V], visited[MAX_V], ord[MAX_V], S[MAX_V];
    rep(i,n) added[i] = visited[i] = cmp[i] = 0;
    int sz = 0;
    rep(i,n){
        if(visited[i]) continue;
        int s = 0;
        S[s++] = i;
        while(s){
            int v = S[s-1];
            visited[v] = true;
            bool pushed = false;
            for(auto & e : g[v]){
                int dst = e.dst;
                if(!visited[dst]){
                    S[s++] = dst;
                    pushed = true;
                }
            }
            if(!pushed){
                int t = S[s-1];
                if(!added[t]){
                    added[t] = true;
                    ord[sz++] = t;
                }
                s--;
            }
        }
    }
    int k = 1;
    rep(i,n){
        int u = ord[n-i-1];
        if(cmp[u]) continue;
        int s = 0;
        S[s++] = u;
        while(s){
            int v = S[--s];
            cmp[v] = k;
            for(auto & e : rg[v]){
                int d = e.dst;
                if(!cmp[d]) S[s++] = d;
            }
        }
        k++;
    }
}

int main(){
    int n,m;
    scanf("%d%d", &n,&m);
    Graph g(n);
    rep(i,m){
        int a,b;
        scanf("%d%d", &a,&b);
        g[a].eb(a,b,0);
    }
    int c[MAX_V];
    stronglyConnectedComponents(g,c);
    int q; cin >> q;
    rep(i,q){
        int a,b;
        scanf("%d%d", &a,&b);
        printf("%d\n", c[a]==c[b]);
    }
}

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

int main() {
  int n;
  cin >> n;
  int a[n], b[n];
  for ( int i = 0; i < n; i++ ) {
    cin >> a[i] >> b[i];
  }
  sort(a, a+n);
  sort(b, b+n);

  int amid;
  int bmid;
  if ( n%2 == 1 ) {
    amid = a[n/2];
    bmid = b[n/2];
  } else {
    amid = a[(n/2)-1]+a[n/2];
    bmid = b[(n/2)-1]+b[n/2];
  }
  cout << (bmid-amid) + 1 << endl;
  return 0;
}


Measured memory_bytes: 5200
#include"bits/stdc++.h"
#include<boost/multi_array.hpp>
#include<boost/optional.hpp>
#include<boost/range/irange.hpp>
#include<boost/range/algorithm.hpp>
#include<boost/range/adaptors.hpp>
#include<boost/operators.hpp>
#include<boost/dynamic_bitset.hpp>

namespace adaptor = boost::adaptors;
using index_t = std::size_t;

void Main()
{
	int N;
	std::string str;
	std::cin >> N >> str;
	if (N % 2 != 0)
	{
		std::cout << "No" << std::endl;
		return;
	}
	auto n = N / 2;
	for (auto i : boost::irange(0, n))
	{
		if (str[i] != str[i + n])
		{
			std::cout << "No" << std::endl;
			return;
		}
	}
	std::cout << "Yes" << std::endl;
}

int main()
{
	std::cin.tie(nullptr);
	std::ios_base::sync_with_stdio(false);
	std::cout << std::fixed << std::setprecision(15);
	Main();
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i) = 0; (i) <(n); (i)++) 
#define so(v) sort(v.begin(),v.end())
using ll = long long;
using vi=vector<int>;
using vvi=vector<vi>;
const int MAX = 510000;
const int MOD = 1000000007;
ll gcd(ll a, ll b)
{
   if (a%b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a%b));
   }
}
ll lcm(ll a, ll b)
{
   return a * b / gcd(a, b);
}
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;
}
int main() {
    int h,w;cin>>h>>w;
    char s[h][w];
    rep(i,h){
      rep(j,w){
        cin>>s[i][j];
      }
    }
    int M=-1;

    rep(i,h){
      rep(j,w){
        if(s[i][j]=='#')continue; 
        vector<vector<int>> sikaku(h,vector<int>(w,-1));
        queue<pair<int,int>> que;
        sikaku[i][j]=0;
        que.push(make_pair(i,j));
        int saidai=0;
        while(!que.empty()){
          int fi=(que.front()).first;
          int se=que.front().second;
          que.pop();
          if((fi-1>=0)&&(s[fi-1][se]=='.')&&(sikaku[fi-1][se]==-1)){
            sikaku[fi-1][se]=sikaku[fi][se]+1;
            saidai=max(saidai,sikaku[fi-1][se]);
            que.push(make_pair(fi-1,se));//cout<<fi<<" "<<se<<1<<endl;
          }
          if((fi+1<=h-1)&&(s[fi+1][se]=='.')&&(sikaku[fi+1][se]==-1)){
            sikaku[fi+1][se]=sikaku[fi][se]+1;
            saidai=max(saidai,sikaku[fi+1][se]);
            que.push(make_pair(fi+1,se));//cout<<fi<<" "<<se<<2<<endl;
          }
          if((se-1>=0)&&(s[fi][se-1]=='.')&&(sikaku[fi][se-1]==-1)){
            sikaku[fi][se-1]=sikaku[fi][se]+1;
            saidai=max(saidai,sikaku[fi][se-1]);
            que.push(make_pair(fi,se-1));//cout<<fi<<" "<<se<<3<<endl;
          }
          if((se+1<=w-1)&&(s[fi][se+1]=='.')&&(sikaku[fi][se+1]==-1)){
            sikaku[fi][se+1]=sikaku[fi][se]+1;
            saidai=max(saidai,sikaku[fi][se+1]);
            que.push(make_pair(fi,se+1));//cout<<fi<<" "<<se<<4<<endl;
          }
        }
        M=max(M,saidai);
      }
    }
    cout<<M<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxx = 1e5 + 7;
LL n, k;

int main() {
	scanf("%lld %lld", &n, &k);
	if(n == k) puts("0");
	else {
		n = n - (n / k) * k;
		LL ans = min(abs(n - k), n);
		printf("%lld\n", ans);
	}
	return 0;
}

Measured memory_bytes: 256
// Atcoder Grand Contest 023 A ver 3.0
#include <iostream>
using namespace std;
int main()
{
  // データ入力
  int n = 0;
  cin >> n;
  int array[n];
  for (int i = 0; i < n; i++) {
    cin >> array[i];
  }
  // 累積和を作成
  long long int csum[n+1];
  csum[0] = 0;
  for (int i = 0; i < n; i++) {
    csum[i+1] = csum[i] + array[i];
  }
  // 累積和をソート
  sort(csum, csum+n+1);
  // 累積和の同じ値をカウント
  long long int count = 0, flag = 0;
  long long int before = csum[0];
  for (int i = 1; i < n + 1; i++) {
    if (before == csum[i]) {
      count++;
      if (flag != 0) {
        count += flag;
      }
      flag++;
    } else {
      flag = 0;
    }
    before = csum[i];
  }
  // 結果を表示
  cout << count << endl;
  return 0;
}

Measured memory_bytes: 2560
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, k;
    cin >> n >> k;

    unordered_set<int> N;

    for (int i = 0; i < k; i++)
    {
        int d;
        cin >> d;
        for (int j = 0; j < d; j++)
        {
            int id;
            cin >> id;
            N.insert(id);
            /* code */
        }        /* code */
    }

    cout << n - (int)N.size() << endl;
    
    return 0;
}

Measured memory_bytes: 3624
#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

struct task
{
	string name;
	int time;
};

int main()
{

	int n, m;
	cin >> n >> m;
	task t;
	int total = 0;
	queue<task>qt;
	for (int i = 0; i < n; i++) {
		cin >> t.name >> t.time;
		qt.push(t);
	}
		
	while (!qt.empty()) {
		t = qt.front();
		qt.pop();
		int t1 = min(t.time, m);
		t.time -= t1;
		total += t1;
		if (t.time > 0)
			qt.push(t);
		else
		{
			cout << t.name << " " << total << endl;
		}
	}
	return 0;
}



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

int main(){
	int N,A=0;
	cin >>  N;
	string S;
	for(int i=0;i<N;i++){
		cin >>  S;
		if(S=="P")
			A=A|0b0001;
		else if(S=="W")
			A=A|0b0010;
		else if(S=="G")
			A=A|0b0100;
		else if(S=="Y")
			A=A|0b1000;
	}
	if(A==0b0111)
		cout << "Three" << endl;
	else
		cout << "Four" << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<int>topo;
vector<bool> visited;
void dfs(int v)
{
	visited[v] = true;
	for(int u :adj[v])
		if(!visited[u])
			dfs(u);
	topo.push_back(v);
}

int main()
{
	int n,m;
	cin>>n>>m;
	adj.assign(n,vector<int>());
	visited.assign(n,0);
	for(int i = 0;i<m;i++)
	{
		int x,y;
		cin>>x>>y;
		adj[x-1].push_back(y-1);
	}
	for(int i = 0;i<n;i++)
	{
		if(!visited[i])
			dfs(i);
	}
	reverse(topo.begin(),topo.end());
	vector<int> dp(n,0);
	for(int i = 0;i<n;i++)
	{
		int x = topo[i];
		for(int u :adj[x])
		{
			dp[u] = max(dp[u],dp[x]+1);
		}
	}
	cout<<*max_element(dp.begin(),dp.end())<<'\n';
}


Measured memory_bytes: 8696
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <iomanip>
#include <queue>
#include <functional>
#include <cassert>
#include <cmath>
#include <set>

#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define NL << endl

using namespace std;
using ll = long long;
using ull = unsigned long long;


int main() {

	int n;

	cin >> n;

	vector<int> v(n + 2);

	v[0] = 5000;
	REP(i, n) {
		cin >> v[i + 1];
		v[i + 1] += 5000;
	}
	v[n + 1] = 5000;

	int dis = 0;
	REP(i, n + 1) {
		dis += abs(v[i + 1] - v[i]);
	}
	FOR(i, 1, n + 1) {
		cout << dis - (abs(v[i + 1] - v[i]) + abs(v[i - 1] - v[i]) - abs(v[i + 1] - v[i - 1])) NL;
	}

	return 0;

}

Measured memory_bytes: 1664
#include <iostream>
#include <map>
#include <vector>
#define int long long
typedef long long ll;
using namespace std;

signed main(){
    ll n, a, b, flag=0;

    cin >> n >> a >> b;
    for(int i=a;i<=b;i++){
        if(i%n==0) flag = 1;
    }
    if(flag) cout << "OK" << endl;
    else cout << "NG" << endl;
    return 0;
}

Measured memory_bytes: 3636
#include<bits/stdc++.h>
const long long INF = INT_MAX / 2;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) x.begin(),x.end()
#define ll long long int
using namespace std;
ll ans = 0, sum = 0, n, k, cnt = 0, w, h;
string s;
vector<ll> v;
vector < pair<ll, ll > > vp;
vector<vector<ll> > vv(50, vector<ll>(50, INF));
vector<string> vs;
vector<char> vc;
set<ll> st;
map<char, ll> mp;

/*--------------------template--------------------*/


int main()
{
	cin >> n;
	cout << n / 3600 << ':' << (n % 3600) / 60 << ':' << (n % 60) << endl;
}

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

int n;
int f[9][9];
int v[9][9];

bool ok() {
	for (int i = 0; i < n; i++) {
		int cnt = 0;
		for (int j = 0; j < n; j++) {
			cnt += v[i][j];
		}
		if (cnt > (n >> 1)) return false;
	}
	for (int j = 0; j < n; j++) {
		int cnt = 0;
		for (int i = 0; i < n; i++) {
			cnt += v[i][j];
		}
		if (cnt > (n >> 1)) return false;
	}
	return true;
}

int dfs(int x, int y) {
	if (x == n) return 1;
	if (y == n) return dfs(x + 1, x + 2);
	if (f[x][y]) {
		return ok() ? dfs(x, y + 1) : 0;
	}
	int res = 0;
	v[y][x] = 1;
	if (ok()) res += dfs(x, y + 1);
	v[x][y] = 1; v[y][x] = 0;
	if (ok()) res += dfs(x, y + 1);
	v[x][y] = v[y][x] = 0;
	return res;
}

int main()
{
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				f[i][j] = v[i][j] = 0;
			}
		}
		int m;
		cin >> m;
		for (int i = 0; i < m; i++) {
			int x, y;
			cin >> x >> y; --x, --y;
			f[x][y] = f[y][x] = 1;
			v[x][y] = 1;
		}
		cout << dfs(0, 1) << endl;
	}
	return 0;
}



Measured memory_bytes: 3092
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
const int maxn=100000+5;
typedef long long ll;
int a[maxn],n,k;
inline int calc(int len){
	if (len<=k) return 1;
	if (len==1) return 0;
	int tp=(len-1)/(k-1);
	if ((len-1)%(k-1)!=0) tp++;
	return tp;
}
int main(){
	cin>>n>>k;
	int id;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		if (a[i]==1){
			id=i;
		}
	}
	int ans=calc(n);
	cout<<ans<<endl;
}

Measured memory_bytes: 4072
N, M = map(int, input().split())
A = []
for i in range(N):
  A.append(list(map(int, input().split())))
L = [0 for i in range(M)]
for a in A:
  for i in range(1, len(a)):
    L[a[i]-1]+=1
print(len([i for i in L if i == N]))

Measured memory_bytes: 3064
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <cassert>
#include <queue>
#include <random>
#include <stack>
#include <iomanip>

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

using Int = unsigned int;
using llong = long long;
using Llong = unsigned long long;
using ldouble = long double;
using intV = vector<int>;
using llongV = vector<long long>;
using llongVV = vector<vector<long long>>;

template<typename T=llong>
using Graph = vector<vector<T>>;

template<typename T=llong>
struct Edge {
    T from;
    T to;
    T cost;
};

template<typename T=llong>
using asc = less<T>();
template<typename T=llong>
using desc = greater<T>();

const llong MOD = 1e9 + 7;
const llong INF = 1e17;

#define FOR(i, n) for (llong i = 0; i < n; i++)
#define FORS(i, a, b) for (llong i = a; i < b; i++)
#define FORR(i, n) for (llong i = n; i > 0; i++)

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

    llong N;
    cin >> N;

    llongVV A(N, llongV(N, 0));
    FOR(i, N) {
        FOR(j, N) {
            cin >> A[i][j];
        }
    }

    llongV grpScore(1 << N, 0);
    FOR(mask, 1 << N) {
        llong score = 0;
        FOR(i, N) {
            FORS(j, i + 1, N) {
                if(((mask & (1 << i)) > 0) && ((mask & (1 << j)) > 0)) {
                    score += A[i][j];
                }
            }
        }
        grpScore[mask] = score;
    }

    llongV dp(1 << N, 0);

    FOR(mask, 1 << N) {
        llong canUse = mask ^ ((1 << N) - 1);
        for(llong newGrp = canUse; newGrp > 0; newGrp = ((newGrp - 1) & canUse)) {
            if(newGrp <= mask) break;
            chmax(dp[mask | newGrp], dp[mask] + grpScore[newGrp]);
        }
    }

    cout << dp[(1 << N) - 1] << endl;

    return 0;
}

Measured memory_bytes: 1280
import functools
from math import gcd
k = int(input())
cnt = 0
for i in range(1,k+1):
    for j in range(i,k+1):
        for l in range(j,k+1):
            if i == j and j == l:
                cnt += i
            elif i == j and j != l:
                li = [i,l]
                cnt += functools.reduce(gcd,li)*3
            elif i != j and j == l:
                li = [i,j]
                cnt += functools.reduce(gcd,li)*3
            else:
                li = [i,j,l]
                cnt += functools.reduce(gcd,li)*6
print(cnt)

Measured memory_bytes: 69788
#include <bits/stdc++.h>
#define MODNum 1000000007
#define REP(type, i, a, b) for( type i = a; i < b; i++ )
typedef long long ll;

using namespace std;

int main (void) {
    ll a,b,c;
    cin >> a >> b >> c;
    ll d = c - a - b;
    if ( 4 * a * b < d * d && d > 0 ){
            cout << "Yes" << endl;
    }
    else cout << "No" << endl;
    return 0;
}

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(){
    vector<int> a(9);
    rep(i,9) cin >> a[i];
    int n;
    cin >> n;
    rep(i,n){
        int b;
        cin >> b;
        rep(j,9){
            if(a[j]==b) a[j] = 0;
        }
    }
    if(a[0]+a[1]+a[2]==0 || a[3]+a[4]+a[5]==0 || a[6]+a[7]+a[8]==0 ||
       a[0]+a[3]+a[6]==0 || a[1]+a[4]+a[7]==0 || a[2]+a[5]+a[8]==0 ||
       a[0]+a[4]+a[8]==0 || a[2]+a[4]+a[6]==0){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
        String k = sc.next();
        if(k.charAt(k.length()-1)=='1' || k.charAt(k.length()-1)=='3'|| k.charAt(k.length()-1)=='5' || k.charAt(k.length()-1)=='7' || k.charAt(k.length()-1)=='9') {
            System.out.println(b-a);
        } else {
            System.out.println(a-b);
        }
    }
}

Measured memory_bytes: 21204
#include<bits/stdc++.h>
using namespace std;
int main()
{
  long long a,b,c,k;
  cin>>a>>b>>c>>k;
  if(a>=k)cout<<k<<endl;
  else if(a+b>=k)cout<<a<<endl;
  else 
    cout<<a+a+b-k<<endl;
  }
    
  
  
  
  


Measured memory_bytes: 3636
n=int(input())
a=([int(n) for n in input().split()])
b=([int(n) for n in input().split()])
b.append(0)
r=0
t=0
x=0
for i in range(n+1):
  t=b[i]+x-a[i]
  if t>=0:
    r=r+a[i]
    x=min(b[i],t)
  else:
    r=r+b[i]+x
    x=0
print(r)

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

using namespace std;
using ll = long long;

template<class T>
vector<T> divisor(T n){
    vector<T> res;
    for(T i = 1; i * i <= n; i++){
        if(n % i == 0){
            res.push_back(i);
            T j = n / i;
            if(i != j)res.push_back(j);
        }
    }
    sort(res.begin(),res.end());
    return res;
}

const int MAX = 1000000;

int main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> in(MAX + 1);
    for(int i = 0; i < n; i++)cin >> a[i], in[a[i]]++;
    int sum = 0;

    for(int i = 0; i < n; i++) {
        vector<int> num = divisor(a[i]);
        int res = 0;
        for(int j = 0; j < num.size(); j++) {
            res += in[num[j]];
        }
        if(res == 1)sum++;
    }
    cout << sum << '\n';
}

Measured memory_bytes: 7888
#include<cstdio>
#include<cmath>
#include<climits>
#include<algorithm>
#include<sstream>
#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<list>
#include<map>
#include<vector>

using namespace std;


#define rep(i,n) for(int i=0; i<n; i++)
#define rept(i,a,b) for(int i=a; i<b; i++)
#define ll long long
#define MOD 1000000007

#define IMIN INT_MIN;
#define IMAX INT_MAX;


int main(){
	int N;
	cin >> N;
	cout << N*N*N << endl;


	return 0;
}



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() {
    string N;
    cin >> N;
    bool a=false;
    rep(i,3){
        if(N.at(i)=='7') a=true;
    }    
    if(a) cout << "Yes" << endl;
    else cout <<  "No" << endl;
}

Measured memory_bytes: 3588
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define sz(a) (int)a.size()
using namespace std;
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
string a[15];
int n,m,k,ans=1e9+1;
int main(){
    cin>>n>>m>>k;
    rep(i,0,n){
        cin>>a[i];
    }
    rep(mask,0,(1<<(n-1))){
        int tot=__builtin_popcount(mask);
        vector<int>cur(tot+1,0);
        rep(j,0,m){
            int p=0;
            rep(i,0,n) {
                cur[p]+=(a[i][j]=='1');
                if((mask>>i)&1){
                    p++;
                }
            }
            bool bad = false;
            rep(i, 0, sz(cur)) {
                if (cur[i] > k) {
                    bad = true;
                }
            }
            if (bad) {
                tot++;
                fill(cur.begin(), cur.end(), 0);
                int p = 0;
                rep(i, 0, n) {
                    cur[p] += (a[i][j] == '1');
                    if ((mask >> i) & 1) {
                        p++;
                    }
                }
                bool bad = false;
                rep(i, 0, sz(cur)) {
                    if (cur[i] > k) {
                        bad = true;
                    }
                }
                if (bad) {
                    tot = 1e9+1;
                }
            }
        }
        setmin(ans, tot);
    }
    cout << ans << "\n";
    return 0;
}

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

int main()
{
	int N;
	cin >> N;
	int a[100];
	for (int i = 0; i < N; i++) {
		cin >> a[i];
	}
	set<int> vals;
	for (int i = 0; i < N; i++) {
		vals.insert(a[i]);
	}
	cout << vals.size() << endl;
}

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() {
  ll n, total = 0;
  cin >> n;
  vector<ll> s(n);
  rep(i, n) {
    cin >> s[i];
    total += s[i];
  }
  sort(s.begin(), s.end());
  if (total % 10 != 0) {
    cout << total << endl;
    return 0;
  }
  rep(i, n) {
    if (s[i] % 10 != 0) {
      cout << total - s[i] << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cmath>

using namespace std;

int main() {
  int N;
  cin >> N;
  int a[10010];
  for(int i = 0; i < N; i++){
    cin >> a[i];
  }

  int count = 0;
  for(int i = 0; i < N; i++){
    while(a[i] % 2 == 0){
      count++;
      a[i] /= 2;
    }
  }

  cout << count << endl;

  return 0;
}




Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
typedef vector<int> vint;
typedef vector<string> vstr;




int main()
{
	int a, b;
	cin>>a>>b;
	if (a > 0 && b > 0)cout << "Positive" << endl;
	else if ((a > 0 && b < 0) || (a<0 && b>0) || a == 0 || b == 0)cout << "Zero" << endl;
	else if ((a - b + 1) % 2 == 0)cout<<"Positive" << endl;
	else cout << "Negative"<<endl;
}

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

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	
	int a, b; cin >> a >> b;
	
	cout << ((a%3==0 || b%3==0 || (a+b)%3==0) ? "Possible" : "Impossible");
}


Measured memory_bytes: 256
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<bitset>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#define rep(i,j,k) for(register int i = j; i <= k; i++)
#define dow(i,j,k) for(register int i = j; i >= k; i--)
#define ui unsigned int 
#define ll long long
#define eps 1e-8
using namespace std;

inline int read() {
	int s = 0, t = 1; char c = getchar();
	while( !isdigit(c) ) { if( c == '-' ) t = -1; c = getchar(); }
	while( isdigit(c) ) s = s * 10 + c - 48, c = getchar();
	return s * t;
}

const int N = 1e5+5;
int v[N*3];

int main() {
	int n = read();
	rep(i,1,3*n) v[i] = read();
	sort(v+1,v+3*n+1);
	ll ans = 0;
	rep(i,1,n) ans += v[3*n-2*i+1];
	cout<<ans<<endl;
	return 0; 
}

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

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  int a = A + B, b = C + D;

  if (a > b) {
    cout << "Left" << endl;
  }
  else if (a == b) {
    cout << "Balanced" << endl;
  }
  else {
    cout << "Right" << endl;
  }
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <bitset>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>

using namespace std;
typedef long long ll;

const ll MOD=1000000007;

int main(){
  int N,K;
  cin >> N >> K;
  vector<ll> A(N+2);
  A[0]=-1000000100;
  A[N+1]=1000000100;
  for(int i=1;i<=N;i++) cin >> A[i];

  vector<ll> fac(N+1);
  vector<ll> inv(N+1);
  vector<ll> facinv(N+1);

  fac[0]=fac[1]=1;
  facinv[0]=facinv[1]=1;
  inv[1]=1;

  for(int i=2;i<=N;i++){
    fac[i]=fac[i-1]*i%MOD;
    inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
    facinv[i]=facinv[i-1]*inv[i]%MOD;
  }

  ll ans=0;
  sort(A.begin(),A.end());
  for(int i=1;i<=N+1-K;i++){
    ans-=A[i]*fac[N-i]%MOD*facinv[K-1]%MOD*facinv[N-i-K+1]%MOD;
    ans%=MOD;
  }

  sort(A.begin(),A.end(),greater<ll>());
  for(int i=1;i<=N+1-K;i++){
    ans+=A[i]*fac[N-i]%MOD*facinv[K-1]%MOD*facinv[N-i-K+1]%MOD;
    ans%=MOD;
  }

  cout << (ans+MOD)%MOD << endl;

  return 0;
}

Measured memory_bytes: 3328
#include <bits/stdc++.h>
using namespace std;
using PP = pair<long, long>;
const int INF = 1e9;
template <class T> T Next() { T buf; cin >> buf; return buf; }

long input() {
  long ret = 0;
  char c;
  while (c = getchar(), isdigit(c)) {
    ret *= 10;
    ret += c - '0';
  }
  return ret;
}

const long INFLL = 1e18;
int main() {
  int a = input();
  int b = input();
  int c = input();
  int z = input();
  
  vector<long> s, t, u;
  s.reserve(a + 1);
  t.reserve(b + 1);
  u.reserve(c + 1);
  for (int i = 0; i < a; ++i) s.push_back(input());
  for (int i = 0; i < b; ++i) t.push_back(input());
  for (int i = 0; i < c; ++i) u.push_back(input());
  
  sort(s.rbegin(), s.rend());
  sort(t.rbegin(), t.rend());
  sort(u.rbegin(), u.rend());
  s.push_back(-INFLL);
  t.push_back(-INFLL);
  u.push_back(-INFLL);
  
  unordered_set<int> st;
  priority_queue<pair<long, int>> Q;
  Q.push(make_pair(s[0] + t[0] + u[0], 0));
  
  int cnt = 0;
  while (cnt < z) {
    auto q = Q.top(); Q.pop();
    long sc = q.first;
    int pos = q.second;
    if (st.count(pos)) continue;
    st.insert(pos);
    
    int i =  pos        & 0x3ff;
    int j = (pos >> 10) & 0x3ff;
    int k = (pos >> 20) & 0x3ff;
    
    cout << sc << endl;
    Q.push(make_pair(s[i + 1] + t[j] + u[k], pos +  1       ));
    Q.push(make_pair(s[i] + t[j + 1] + u[k], pos + (1 << 10)));
    Q.push(make_pair(s[i] + t[j] + u[k + 1], pos + (1 << 20)));
    ++cnt;
  }
}


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

int main(){
  int n,a;
  cin >> n >> a;
  cout << n*n - a << endl;
  return 0;
}


Measured memory_bytes: 256
/*
 * 科赫曲线
 * 输出科赫曲线的顶点坐标.
 */


#include <cstdio>
#include <cmath>

struct Point{ double x,y; };

void koch(int n, Point a, Point b){
    if(n==0) return;

    Point s,t,u;
    double th = M_PI * 60.0 / 180.0;//将单位从度变为弧度

    s.x = (2.0 * a.x + 1.0 * b.x) / 3.0;
    s.y = (2.0 * a.y + 1.0 * b.y) / 3.0;
    t.x = (1.0 * a.x + 2.0 * b.x) / 3.0;
    t.y = (1.0 * a.y + 2.0 * b.y) / 3.0;
    u.x = (t.x - s.x) * cos(th) - (t.y - s.y) * sin(th) + s.x;
    u.y = (t.x - s.x) * sin(th) + (t.y - s.y) * cos(th) + s.y;

    koch(n-1, a, s);
    printf("%.8f %.8f\n",s.x, s.y);
    koch(n-1, s, u);
    printf("%.8f %.8f\n",u.x, u.y);
    koch(n-1, u, t);
    printf("%.8f %.8f\n",t.x, t.y);
    koch(n-1, t, b);
}


int main(){
    Point a,b;
    int n;
    scanf("%d",&n);

    a.x = 0;
    a.y = 0;
    b.x = 100;
    b.y = 0;

    printf("%.8f %.8f\n",a.x, a.y);
    koch(n,a,b);
    printf("%.8f %.8f\n",b.x, b.y);

    return 0;
}


Measured memory_bytes: 2740
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
#define FOR(i, n) for(int i = 1; i<=n; i++)
#define F0R(i, n) for(int i = 0; i<n; i++)
#define mp make_pair
#define pb push_back
#define f first
#define s second


//var 
LL n, ans, a[200005];

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

	cin >> n;
	FOR(i, n)
		cin >> a[i];

	FOR(i, n){
		if(a[i] >= a[i-1]) continue;
		ans += a[i-1] - a[i];
		a[i] = a[i-1];
	}

	cout << ans << endl;

	cout.flush();
	return 0;
}

Measured memory_bytes: 5200
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
    int a,b;
    cin >> a >> b;
    int ans = 0;
    for(int i = a; i < b+1; i++){
        string s = to_string(i);
        string t = s;
        reverse(t.begin(), t.end());
        if(s == t) ans++;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\n"

constexpr int MOD = 1000000007;
const int INF = 1 << 30;

int main() {
    int n, m;
    cin >> n >> m;
    vec g[100010];
    rep(i, m) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    vec res(n, -1);
    res[0] = 0;

    queue<int> que;
    que.push(0);

    while (!que.empty()) {
        int v = que.front();
        que.pop();

        for (auto&& c : g[v]) {
            if (res[c] == -1) {
                res[c] = v+1;
                que.push(c);
            }
        }
    }

    cout << "Yes" << endl;
    for (int i = 1; i < n; i++) cout << res[i] << endl;
    return 0;
}

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

#define PI 3.1415926535897932
#define MOD 1000000007
#define INF 1e9

#define REP(i, n) for(int i = 0; i < (n); i++)
#define REP1(i, n) for(int i = 1; i <= (n); i++)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<bool> vb;


void solve()
{
    int N, M; cin >> N >> M;
    vector<pii> p(M); REP(i, M) cin >> p[i].first >> p[i].second;
    sort(ALL(p));

    int l = 1, r = N;
    int ans = 1;
    REP(i, M)
    {
        r = min(r, p[i].second);
        if(p[i].first < r) l = max(l, p[i].first);
        else
        {
            l = r;
            ans++;
            r = p[i].second;        
        }
    }
    cout << ans << endl;
}

int main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    solve();

    return 0;
}

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

string s, t;
vector<int> idx[28];

signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> s >> t;
    rep(i, s.size()) idx[s[i]-'a'].pb(i);
    int ans = 0;
    int now = -1;
    rep(i, t.size()) {
        int ti = t[i]-'a';
        if (idx[ti].size()==0) {
            cout << -1 << endl;
            exit(0);
        }
        auto it = upper_bound(idx[ti].begin(), idx[ti].end(), now);
        if (it!=idx[ti].end()) now = *it;
        else {
            ans += s.size();
            now = idx[ti][0];
        }
    }
    cout << ans+now+1 << endl;
}

Measured memory_bytes: 1736