akhauriyash/Code-Regression huggingface dataset:
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <random>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res * a % mod; a = a * a % mod; }return res; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

int t;

int main()
{
	ios::sync_with_stdio(false);
#ifdef RG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int k, n;
	cin >> k >> n;
	vi a(n);
	rep(i, 0, n) cin >> a[i];
	vi d(n);
	int maxn = 0;
	rep(i, 0, n - 1) d[i] = a[i + 1] - a[i];
	d[n - 1] = a[0] + k - a[n - 1];
	rep(i, 0, n) maxn = max(maxn, d[i]);
	cout << k - maxn << endl;
	return 0;
}

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

void convert(int x, int b) {
  int p;
  int t;
  if(x == 0) return;
  t = x % 10;
  if(t < 0) t = -t;
  if(t == 0) {
    p = 0;
  } else if(b*x < 0) {
    p = 10-t;
    x -= b*10;
  } else {
    p = t;
  }
  convert(x/10, b*(-1));
  cout << p;
}

int main() {
  while(1) {
    int x;
    cin >> x;
    if(x == 0) break;
    convert(x, 1);
    cout << endl;
  }
  return 0;
}

Measured memory_bytes: 0
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>

#define CH(N,A,B) (A<=N&&N<B)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define RREP(i,a,b) for(int i=(b-1);a<=i;i--)

using namespace std;

long long dp[2005][2005];
int n, a[2005];

long long dfs(int sta, int end, int left, bool joi){
  if(dp[sta][end] != -1) return dp[sta][end];

  if(left == 1){
    if(!joi) {
      return dp[sta][end] = 0;
    }else{
      return dp[sta][end] = a[sta];
    }
  }

  if(!joi){
    if(a[sta] < a[end]){
      return dp[sta][end] = dfs(sta, (end+1)%n, left-1, true);
    }else{
      return dp[sta][end] = dfs((sta-1+n)%n, end, left-1, true);
    }
  }else{
    return dp[sta][end] = max(dfs((sta-1+n)%n, end, left-1, false) + a[sta], dfs(sta, (end+1)%n, left-1, false) + a[end]);
  }

}

int main() {
  cin>>n;
  REP(i,0,n) cin>>a[i];
  REP(i,0,n)REP(j,0,n) dp[i][j] = -1;
  vector<long long> v;
  REP(i,0,n) v.push_back( dfs(i,(i+1)%n,n,true) );
  cout<<*max_element(v.begin(), v.end())<<endl;
  return 0;
}

Measured memory_bytes: 34584
# coding: utf-8
import math
n = int(input())
t = 0
x = 0
y = 0
# txy = []
# for i in range(n):
#     ti, xi, yi = list(map(int, input().split()))
#     txy.append((ti, xi, yi))
for i in range(n):
    ti, xi, yi = map(int, input().split())
    if abs(xi - x) + abs(yi - y) > abs(ti - t):
        print("No")
        exit()
    elif (abs(xi - x) + abs(yi - y) - abs(ti - t)) % 2 != 0:
        print("No")
        exit()
    else:
        t = ti
        x = xi
        y = yi
print("Yes")


Measured memory_bytes: 9128
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mod 1000000007
#define f(i,a,b) for(ll i=a;i<b;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ii pair<ll,ll>
#define vii vector<ii>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define MAX 1e9
 
ll modpow(ll base, ll exp, ll modulus) {base %= modulus;ll result = 1;while (exp > 0) {if (exp & 1) result = (result * base) % modulus;base = (base * base) % modulus;exp >>= 1;}return result;}
const ld pi=acos(-1);


int main(){
    IOS;
    ll n;cin>>n;
    set<string>st;
    f(i,0,n){string s;cin>>s;st.insert(s);}
    cout<<st.size();
    return 0;  
} 





Measured memory_bytes: 19244
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
int dp[200][5][5][5];
/*
0-初期
1-A
2-G
3-C
4-T
*/
int main() {
  int N;
  cin >> N;
  memset(dp, 0, sizeof(dp));
  dp[0][0][0][0] = 1;
  REP(n, N) {
    REP(i, 5) {
      REP(j, 5) {
        REP(k, 5) {
          for (int l = 1; l < 5; l++) {
            if (j == 1 && k == 2 && l == 3) continue;
            if (j == 2 && k == 1 && l == 3) continue;
            if (i == 1 && k == 2 && l == 3) continue;
            if (i == 1 && j == 2 && l == 3) continue;
            if (j == 1 && k == 3 && l == 2) continue;
            dp[n + 1][j][k][l] += dp[n][i][j][k];
            dp[n + 1][j][k][l] %= MOD;
          }
        }
      }
    }
  }
  int ans = 0;
  for (int i = 1; i < 5; i++)
    for (int j = 1; j < 5; j++)
      for (int k = 1; k < 5; k++) {
        ans += dp[N][i][j][k];
        ans %= MOD;
      }
  cout << ans << endl;
}

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

using namespace std;

int main()
{
  int w, h, n;

  scanf("%d %d %d", &w, &h, &n);

  int x, y, a, o[2] = {0, 0}, ans[2] = {w, h};
  for (int i = 0; i < n; i++)
  {
    scanf("%d %d %d", &x, &y, &a);

    if (a == 1 && o[0] < x)
    {
      o[0] = x;
    }
    else if (a == 2 && ans[0] > x)
    {
      ans[0] = x;
    }
    else if (a == 3 && o[1] < y)
    {
      o[1] = y;
    }
    else if (a == 4 && ans[1] > y)
    {
      ans[1] = y;
    }
  }

  if (ans[0] - o[0] < 0)
  {
    x = 0;
  }
  else
  {
    x = ans[0] - o[0];
  }

  if (ans[1] - o[1] < 0)
  {
    y = 0;
  }
  else
  {
    y = ans[1] - o[1];
  }

  printf("%d", x * y);
}

Measured memory_bytes: 256
n,k=map(int,input().split())
A = list(map(int, input().split()))
r = max(A)
l = 0
while l+1<r:
  mid=(l+r)//2
  if sum([(a-1)//mid for a in A])>k:
    l=mid
  else:
    r = mid
print(r)

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

using namespace std;

int main(void){
    int N;
    cin >> N;
    vector<int> a(N); for(auto& x:a) cin >> x;
    vector<int> b(N); for(auto& x:b) cin >> x;
    
    vector<int> acca(N+1);
    acca[0] = 0; for(int i=1;i<=N;i++) acca[i] = a[i-1]+acca[i-1];
    
    vector<int> accb(N+1);
    accb[0] = 0; for(int i=1;i<=N;i++) accb[i] = b[i-1]+accb[i-1];


    int ans = 0;
    for(int i=0;i<N;i++){
        ans = max(ans, acca[i+1] + accb[N] - accb[i]);
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 384
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(void){
    int N, tmp;
    cin >> N;
    priority_queue<double, vector<double>, greater<double> > q;
 
    for(int i = 0; i < N; i++){
        cin >> tmp;
        q.push(tmp);
    }
 
    while(q.size() > 1){
        double first = q.top();
        q.pop();
        double second = q.top();
        q.pop();
        double avg = (first + second) / 2;
        q.push(avg);
    }
 
    cout << q.top() << endl;
 
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
struct pro{string x;int y;};
int i = 0,teil;
int main(){
	int n,time = 0,rim;
	cin>>n>>rim;
	teil = n;
	pro aho[n+1] ={};
	for(int i = 0;i<n;i++){
		cin>>aho[i].x>>aho[i].y;
	}
	int i = 0;
	while(i != teil){
		if(aho[i].y >rim){
			time += rim;
			aho[i].y -= rim;
			aho[teil] = aho[i];
			if(teil == n){
				teil = 0;
				i++;
			}
			else if(i == n){
				teil++;
				i = 0;
			}
			else{
				i++;
				teil++;
			}
			
		}
		else if(aho[i].y<=rim){
			time += aho[i].y;
			aho[i].y = time;
			cout<<aho[i].x<<" "<<aho[i].y<<endl;
			if(i == n){
				i = 0;
			}
			else{
				i++;
			}
		}
		
	}
}	


Measured memory_bytes: 6300
#include <bits/stdc++.h>
#define int int64_t
#define fi first
#define se second
#define mod 1000000007
using namespace std;
signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int x,y,p=0;
  int fx=0,fy=0;
  int p1=1,p2=1,p3=0,p4=0,t=0;
  int s[100000];
  s[0]=1,s[1]=1;
  cin>>x>>y;
  while(true){
    if(p3<=x&&x<p1&&p4<=y&&y<p2){
      cout<<p%3+1<<"\n";
      //cout<<p1<<" "<<p2<<" "<<p3<<" "<<p4<<"\n";
      break;
    }
    t++;
    //cout<<p1<<" "<<p2<<" "<<p3<<" "<<p4<<"\n";
    if(p%4==0) p1+=s[t];
    else if(p%4==1) p2+=s[t];
    else if(p%4==2) p3-=s[t];
    else p4-=s[t];
    s[t+1]=s[t-1]+s[t];
    p++;
  }
  return 0;
}


Measured memory_bytes: 3192
n = int(input())
a = len(str(n))
ans = 0
x = 9
x2 = 4
for i in range(3, a):
    x *= 3
    x2 *= 2
    ans += x - x2 * 3 + 3
b = 0
l = ['3', '5', '7']
while(True):
    c = 3 ** (a - 1)
    s = ''
    bb = b
    for i in range(a):
        s += l[bb // c]
        bb = bb % c
        c = c // 3
    if int(s) > n or s.count('7') == a:
        break
    elif s.count('3') and s.count('5') and s.count('7'):
        ans += 1
    b += 1
print(ans)

Measured memory_bytes: 3188
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for(ll i=0; i<n; i++)
#define repr(i, s, e) for(ll i=s; i>=e; i--)
#define reps(i, s, e) for(ll i=s; i<=e; i++)
#define inf 1e18
#define all(v) v.begin(),v.end()
#define ok cout << "ok" << endl;
#define sp << " " <<
template<typename T> inline bool chmax(T &a, T b){ if(a<b) a=b; return a<b; }
template<typename T> inline bool chmin(T &a, T b){ if(b<a) a=b; return b<a; }
template<typename T> void vdeb(T v){
    cout << "#vector set debug" << endl;
    for(auto vv : v) cout << vv << " ";
    cout << endl;
}
template<typename T1, typename T2> void adeb(T1 arr[], T2 n){
    cout << "#adebug" << endl;
    for(int i=0; i<=n; i++) cout << arr[i] << " ";
    cout << endl;
}
template<typename T1> void mdeb(T1 mp){
    cout << "#map pair debug" << endl;
    for(auto const& m : mp) cout << m.first sp m.second << endl;
}
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){ fill((T*)array, (T*)(array+N), val); }
void ans(bool b){ cout << (b ? "Yes" : "No") << endl; }
void ans2(bool b){ cout << (b ? "YES" : "NO") << endl; }
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};


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

    int n;
    cin >> n;
    vector<int> g[100010];
    vector<pair<int, int>> vp;
    int a, b;
    rep(i, n-1){
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
        vp.push_back(make_pair(a, b));
    }

    int k=0;
    int colors[100010];
    map<pair<int, int>, int> mp;
    queue<int> q;
    q.push(1);
    int visit[100010];
    visit[1] = 1;
    while(!q.empty()){
        int v = q.front();
        q.pop();
        if(k < g[v].size()) k = g[v].size();
        int color = 1;
        for(auto const& e : g[v]){
            if(visit[e]) continue;
            if(colors[v] == color) color++;
            colors[e] = color;
            mp[make_pair(e, v)] = mp[make_pair(v, e)] = color++;
            visit[e] = 1;
            q.push(e);
        }
    }

    cout << k << endl;
    rep(i, n-1){
        cout << mp[vp[i]] << endl;

    }

    return 0;
}


Measured memory_bytes: 20088
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;

int main() {
	int N;
	cin >> N;
	cout << (N + 1) / 2 << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
 
void Main()
{
  int s, w;
  cin >> s >> w;
  
  
  if(s <= w){
    printf("unsafe");
  }
  else
    printf("safe");
}
 
int main()
{
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

Measured memory_bytes: 3656
n,a,b = map(int,input().split())
ans = 0
for i in range(n+1):
    list = [int(c) for c in str(i)]
    d = sum(list)
    if a<=d<=b:
      list = [int(a) for a in list]
      list = map(str, list)
      str_num = "".join(list) 
      int_num = int(str_num)
      num_list=[]
      num_list.append(int_num)
      ans = ans + int_num
print(ans)  

Measured memory_bytes: 3064
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define N 200010
ll n;
namespace uf{
  ll p[N],siz[N],id[N];
  ll to[N],len[N],m;
  void init(){
      for(int i=1;i<=n;i++)p[i]=i,siz[i]=1,id[i]=i;
      m=n;
  }
  ll par(ll x){return p[x]=(x==p[x]?x:par(p[x]));}
  
  void unite(ll a,ll b,ll e){
      a=par(a); b=par(b);
      to[id[a]]=to[id[b]]=++m;
      len[id[a]]=siz[b]*e,len[id[b]]=siz[a]*e;
      p[a]=b,siz[b]+=siz[a],id[b]=m;
  }
  
  vector<ll> g[N];
  ll ans[N];
  void dfs(ll x,ll d){
      ans[x]=d;
      for(auto y:g[x])dfs(y,d+len[y]);
  }
  void main(){
      for(int i=1;i<m;i++){
	  g[to[i]].push_back(i);
      }
      dfs(m,0);
      for(int i=1;i<=n;i++)cout<<ans[i]<<endl;
  }
};
struct edge{
  ll a,b,c;
  bool operator<(const edge&key)const{
      return this->c>key.c;
  } 
};
edge e[N];
int main(){
    cin>>n;
    for(int i=0;i<n-1;i++)cin>>e[i].a>>e[i].b>>e[i].c;
    sort(e,e+n-1); uf::init();
    for(int i=0;i<n-1;i++){
	uf::unite(e[i].a,e[i].b,e[i].c);
    }
    uf::main();
    return 0;
}



Measured memory_bytes: 26368
# AOJ ALDS1_14_B "String Search"
# returns i s.t. S[i+j] = T[j] for 0 <= j < len(T)

def RollingHash(S, T):
    if len(S) < len(T):
        return []
    # gcd(h, b) = 1
    h = 10**11+7
    b = 10**7+7
    L = len(T)

    bL = 1
    for i in range(L):
        bL = bL * b % h

    hashS = 0
    for i in range(L):
        hashS = (hashS * b + ord(S[i])) % h

    hashT = 0
    for i in range(L):
        hashT = (hashT * b + ord(T[i])) % h
    
    correctIndexes = []
    if hashS == hashT:
        correctIndexes.append(0)
    for j in range(len(S)-L):
        hashS = (hashS * b - ord(S[j])*bL + ord(S[L+j])) % h
        if hashS == hashT:
            correctIndexes.append(j+1)
    return correctIndexes


if __name__ == "__main__":
    import sys
    input = sys.stdin.readline

    S = input().rstrip()
    T = input().rstrip()

    ans = RollingHash(S, T)
    for a in ans:
        print(a)


Measured memory_bytes: 47912
'use strict';
process.stdin.on('data', function(chunk) {
    var inputs = chunk.toString().trim().split('\n');
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i] === '0') {
            break;
        }
        console.log('Case ' + (i + 1) + ': ' + inputs[i]);
    }
});
process.stdin.resume();
process.stdin.setEncoding('utf8');

Measured memory_bytes: 20588
import java.util.Scanner;
import java.util.Arrays;
 
class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
 
        int n = sc.nextInt();
        int[][] a = new int[n][2];
        int[][] b = new int[n][2];
 
        for(int i = 0; i < n; i++){
            a[i][0] = sc.nextInt();
            a[i][1] = sc.nextInt();
        }
 
        for(int i = 0; i < n; i++){
            b[i][0] = sc.nextInt();
            b[i][1] = sc.nextInt();
        }
 
        sc.close();
        
        Arrays.sort(a, (s, t) -> Integer.compare(t[1],s[1]));
 
        Arrays.sort(b, (s, t) -> Integer.compare(s[0],t[0]));
 
        int result = 0;
 
        loop:
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if((a[i][0] < b[j][0]) && (a[i][1] < b[j][1])){
                    result++;
                    b[j][0] = -1;
                    b[j][1] = -1;
                    continue loop;
                }
            }
        }
        System.out.println(result);
    }
}

Measured memory_bytes: 27220
#include <bits/stdc++.h>
#define vsort(v) sort(v.begin(), v.end())
#define rvsort(v) sort(v.begin(), v.end(), greater<ll>())
#define vfind(v, target) find(v.begin(), v.end(), target)
#define notFound(v, target) (vfind(v, target) == v.end())
#define ll unsigned long long
#define rep(i, s, g) for (ll i = (s); i < (g); i++)
#define mod 1000000007
using namespace std;

class mint {
    long long x;
public:
    mint(long long x=0) : x((x%mod+mod)%mod) {}
    mint operator-() const { 
      return mint(-x);
    }
    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(ll 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;
    }

    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
};

mint f(int n)
{
	if (n == 0) return 1;
  mint x = f(n / 2);
  x *= x;
  if (n % 2 == 1) x *= 2;
  return x;
}

mint choose(int n, int a) 
{
	mint x = 1, y = 1;
  rep(i, 0, a) {
  	x *= n - i;
    y *= i + 1;
  }
  return x / y;
}

int main()
{
  int n, a, b; cin >> n >> a >> b;
  mint ans = f(n);
  ans -= 1;
  ans -= choose(n, a);
  ans -= choose(n, b);
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
	long long a,b;
	int k,i,ans=0;
	scanf("%lld %lld %d",&a,&b,&k);
	while(1)
	{
		if(a%2!=0)
		{
			b+=(a-1)/2;
			a=(a-1)/2;
			ans++;
			if(ans==k)
			{
				printf("%lld %lld\n",a,b);
				break;
			}
			if(b%2!=0)
			{
				a+=(b-1)/2;
				b=(b-1)/2;
				ans++;
				if(ans==k)
				{
					printf("%lld %lld\n",a,b);
					break;
				}
			}
			else
			{
				a+=b/2;
				b=b/2;
				ans++;
				if(ans==k)
				{
					printf("%lld %lld\n",a,b);
					break;
				}
			}
		}
		else
		{
			b+=a/2;
			a=a/2;
			ans++;
			if(ans==k)
			{
				printf("%lld %lld\n",a,b);
				break;
			}
			if(b%2!=0)
			{
				a+=(b-1)/2;
				b=(b-1)/2;
				ans++;
				if(ans==k)
				{
					printf("%lld %lld\n",a,b);
					break;
				}
			}
			else
			{
				a+=b/2;
				b=b/2;
				ans++;
				if(ans==k)
				{
					printf("%lld %lld\n",a,b);
					break;
				}
			}
		}
	}
	return 0;
}

Measured memory_bytes: 128
#include <stdio.h>
int main(){
  int P[5],J[5];
  int i;
  for(i=0; i<5; i++){
    scanf("%d",&P[i]);
  }
  for(i=0; i<5; i++){
    J[i]=P[i]%10;
    if(J[i]==0)
      J[i]=10;
  }
  int min=10;
  int memo=-1;
  for(i=0; i<5; i++){
    if (min>J[i]){
      min=J[i];
      memo=i;
    }
  }
  
  int sum=0;
  if(memo==-1){
  	for(i=0; i<5; i++){
      sum+=P[i];}
  }
  else{
    for(i=0; i<5; i++){
      if(i==memo)
        sum+=P[i];
      else{
        sum+=(P[i]+10-J[i]);}
    }
  }
  printf("%d\n",sum);
  return 0;
}

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

#define ll long long
#define pb push_back
#define N 1000000007
#define nmax 200001
#define pll pair<ll,ll>
#define fill_nums(A,n) for(ll i=0;i<n;i++) scanf("%lld",&A[i]);
#define fill_edges(adj,m) for(ll i=0;i<m;i++) {ll a, b; scanf("%lld %lld",&a,&b); adj[a-1].pb(b-1); adj[b-1].pb(a-1);}
#define fill_direct(adj,m) for(ll i=0;i<m;i++) {ll a, b; scanf("%lld %lld",&a,&b); adj[a-1].pb(b-1);}

ll extended_gcd(ll a, ll b, ll &x, ll &y) {
	if(a==0) {
		x = 0;
		y = 1;
		return b;
	}

	ll x1,y1;
	ll gcd = extended_gcd(b%a,a,x1,y1);

	x = y1 - (b/a)*x1;
	y = x1;

	return gcd;
}

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

    vector<ll> A(n);
    for(ll i=0;i<n;i++){
    	ll a; scanf("%lld",&a);
    	A[a-1]=i;
    }

    multiset<ll> se;
    se.insert(-1);
    se.insert(-1);
    se.insert(n);
    se.insert(n);

    ll res = 0;
    for(ll i=n-1;i>=0;i--) {
    	se.insert(A[i]);
    	multiset<ll>::iterator it1 = se.find(A[i]);
    	multiset<ll>::iterator it2 = it1, it3 = it1, it4 = it1, it5 = it1;

    	ll c = 0;
    	it2++;
    	it3++; it3++;
    	it4--;
    	it5--;it5--;

        if((*it2)!=n) {
            c += ((*it3)-(*it2))*((*it1)-(*it4));
        }

        if((*it4)!=-1) {
            c += ((*it4)-(*it5))*((*it2)-(*it1));
        }

     	res += (i+1)*c;
    }

    cout<<res<<endl;
	return 0;
}



Measured memory_bytes: 5760
char buf[5000];
sz,k;
S,N,A,K,i,T;
main()
{
	read(0,buf,5000);
	for(;;)
	{
		k=buf[sz++];
		if(k<'0')break;
		N=N*10+k-'0';
	}
	for(i=0;i<N;i++)
	{
		A=K=0;
		for(;;)
		{
			k=buf[sz++];
			if(k<'0')break;
			A=A*10+k-'0';
		}
		for(;;)
		{
			k=buf[sz++];
			if(k<'0')break;
			K=K*10+k-'0';
		}
		while(T=A/K+1,A%K%T)A=A/K*K-T+A%K%T;
		S^=T-1;
	}
	write(1,S?"Takahashi":"Aoki",S?9:4);
}

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

const double eps=1e-8;

int sgn(double d)
{
   if (d>eps) return 1;
   if (d<-eps) return -1;
   return 0;
}

int main()
{
   double d;
   while(scanf("%lf",&d)!=EOF)
   {
      if (sgn(d)==0) break;
	  double x,y,vx,vy;
	  scanf("%lf%lf%lf%lf",&x,&y,&vx,&vy);
	  double b1=x*vy-y*vx;
	  if (sgn(b1)==0)
	  {
		 if (sgn(vx*x)<0||sgn(vy*y)<0)
		 {
			 double dis=sqrt(x*x+y*y);
			 if (sgn(dis-d)>0) printf("impossible\n");
			 else printf("%.6lf\n",dis);
		 }
		 else
		 {
		     double dis=sqrt(x*x+y*y);
			 dis=2.0-dis;
			 if (sgn(dis-d)>0) printf("impossible\n");
			 else printf("%.6lf\n",dis);
		 }
		 continue;
	  }
	  printf("impossible\n");
   }
   return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  
  int s = (1 << m) - 1;
  for (int i = 0; i < n; ++i) {
    int k;
    cin >> k;
    int p = 0;
    for (int j = 0; j < k; ++j) {
      int a;
      cin >> a;
      --a;
      p |= 1 << a;
    }
    s &= p;
  }
  cout << __builtin_popcount(s) << endl;
}

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

namespace ABC111C_cs
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = int.Parse(Console.ReadLine());
            var sq = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            var x = new Sequence(n, sq);
            Console.WriteLine(x.MininumChange());
        }
    }
    public class Sequence
    {
        private readonly int n;
        private readonly int[] num;
        private int[] oddnum;
        private int[] evennum;
        public Sequence(int n, int[] num)
        {
            this.n = n;
            this.num = num;
            oddnum = num.Where((x, index) => index % 2 == 1).ToArray();
            evennum = num.Where((x, index) => index % 2 == 0).ToArray();
        }

        public int MininumChange()
        {
            var tmpOdd = oddnum.GroupBy(x => x).Select(x => new { Val = x.Key, Cnt = x.Count() }).OrderByDescending(x => x.Cnt);
            var oddnumFirstMode = tmpOdd.First();
            var tmpEven = evennum.GroupBy(x => x).Select(x => new { Val = x.Key, Cnt = x.Count() }).OrderByDescending(x => x.Cnt);
            var evennumFirstMode = tmpEven.First();
            if (oddnumFirstMode.Val != evennumFirstMode.Val)
            {
                return oddnum.Length - oddnumFirstMode.Cnt + evennum.Length - evennumFirstMode.Cnt;
            }
            else
            {
                int result1 = int.MaxValue;
                int result2 = int.MaxValue;
                if (tmpEven.Count() == 1 && tmpOdd.Count() == 1)
                {
                    return num.Length / 2;
                }
                if (tmpOdd.Count() > 1)
                {
                    var oddnumSecondModeCnt = tmpOdd.Skip(1).First().Cnt;
                    result1 = oddnum.Length - oddnumSecondModeCnt + evennum.Length - evennumFirstMode.Cnt;
                }
                if (tmpEven.Count() > 1)
                {
                    var evennumSecondModeCnt = tmpEven.Skip(1).First().Cnt;
                    result2 = oddnum.Length - oddnumFirstMode.Cnt + evennum.Length - evennumSecondModeCnt;
                }
                return Math.Min(result1, result2);
            }
        }
    }
}


Measured memory_bytes: 51600
#include<iostream>
using namespace std;
#define MAX 500000
#define SENTINEL 2000000000

int L[MAX/2+2], R[MAX/2+2];
int cnt;

void merge(int A[], int n, int left, int mid, int right){
  int n1 = mid - left;
  int n2 = right - mid;
  for ( int i = 0; i < n1; i++ ) L[i] = A[left + i];
  for ( int i = 0; i < n2; i++ ) R[i] = A[mid + i];
  L[n1] = R[n2] = SENTINEL;
  int i = 0, j = 0;
  for ( int k = left; k < right; k++ ){
    cnt++;
    if ( L[i] <= R[j] ){
      A[k] = L[i++];
    } else {
      A[k] = R[j++];
    }
  }
}

void mergeSort(int A[], int n, int left, int right ){
  if ( left+1 < right ){
    int mid = (left + right)/2;
    mergeSort(A, n, left, mid);
    mergeSort(A, n, mid, right);
    merge(A, n, left, mid, right);
  }
}

int main(){
  int A[MAX], n, i;
  cnt = 0;

  cin >> n;
  for ( i = 0; i < n; i++ ) cin >> A[i];

  mergeSort(A, n, 0, n);

  for ( i = 0; i < n; i++ ) {
    if ( i ) cout << " ";
    cout << A[i];
  }
  cout << endl;

  cout << cnt << endl;

  return 0;
}





Measured memory_bytes: 6896
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long ll;
ll n, a[200010], ruiseki[200010];
ll sum, ans = 2000000000000000000;
int main() {
	cin >> n;
	cin >> a[0];
	sum = a[0];
	ruiseki[0] = a[0];
	for (int i = 1; i < n; i++) {
		cin >> a[i];
		sum += a[i];
		ruiseki[i] = ruiseki[i - 1] + a[i];
	}
	for (int i = 0; i < n - 1; i++)if (abs(ruiseki[i] - (sum - ruiseki[i])) < ans)ans = abs(ruiseki[i] - (sum - ruiseki[i]));
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3328
#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
const int MOD = (int)1e9 + 7;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()


int main(int argc, char const *argv[]) {
int N;
cin>>N;
vector<int> a(N);
ll ans=0;
REP(i,N){
  cin>>a[i];
  ans+=a[i];
}
cout<<ans-N<<endl;

return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<queue>
#define REP(i,a) for (int i = 0;i < (a);++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(obj) (obj).begin(),(obj).end()
#define SORT(list) sort(ALL((list)));
#define MOD 1000000007
using namespace std;
using ll = long long;
int main(){
    int a,b;
    cin >> a >> b;
    if(a/10 != 0 || b/10 != 0){
        cout << -1 << endl;
    }else {
        cout << a*b << 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;
typedef long long ll;
typedef pair<int,int> P;
const int MAX_N = 10005;
const int MAX_A = 1000005;
const int MOD = (1e9)+7;
ll A[MAX_N],inv[MAX_A],C[MAX_A];

ll gcd(ll a, ll b)
{
	if(b == 0) return a;
	return gcd(b,a%b);
}

ll modpow(ll a, ll n)
{
	if (n == 0)
	{
		return 1;
	}
	if (n == 1)
	{
		return a;
	}
	else
	{
		if (n % 2 == 0)
		{
			ll b = modpow(a,n/2);
			return b * b % MOD;
		}
		else
		{
			return a * modpow(a,n-1) % MOD;
		}
	}
}

void init()
{
	for (int i = 1; i < MAX_A; ++i)
	{
		inv[i] = modpow(i,MOD-2);
		//cout << "A";
	}
}

int main()
{
	init();
	//cout << "a";
	int N;
	cin >> N;
	rep(i,N)
	{
		cin >> A[i];
	}
	if (N == 1)
	{
		cout << "1" << endl;
		return 0;
	}
	/*
	ll g = A[0];
	ll p = A[0];
	rep(i,N-1)
	{
		g = gcd(g,A[i+1]);
		p = (p*A[i+1]) % MOD;
	}
	*/

	ll cnt;
	rep(j,N)
	{
		ll now = A[j];
		for (int i = 2; i*i <= A[j]; ++i)
		{
			cnt = 0;
			while(now % i == 0)
			{
				cnt++;
				now /= i;
			}
			C[i] = max(C[i],cnt);
		}
		if (now != 1) C[now] = max(C[now],ll(1));
	}

	ll ans = 0;
	ll l = 1;
	for (int i = 2; i < MAX_A; ++i)
	{
		//cout << C[i] << endl;
		l = (l * modpow(i,C[i])) % MOD;
	}
	//cout << l << endl;
	rep(i,N)
	{
		ans = (ans + (l * inv[A[i]]) % MOD) % MOD;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 15744
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;

int a, b, c;

int main()
{
	cin >> a >> b >> c;
	if (a != b && a != c) printf("%d\n", a);
	else if (b != a && b != c) printf("%d\n", b);
	else printf("%d\n", c);
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD=1e9+7;

int main() {
    int n,k;
    cin>>n>>k;
    long long ans=0;
    
    int i=n/k;//n以下のkの倍数の個数
    ans+=1*i;//a=b=c i
    if(i>=2) ans+=(long long)3*i*(i-1);//a=b!=c iP2
    if(i>=3) ans+=(long long)i*(i-1)*(i-2);//a!=b!=c!=a iP3
    
    //k%2!=0ではa%k=0,b%k=0,c%k=0のみ
    if(k%2==0){//a%2=k/2,b%2=k/2,c%2=k/2の場合
        int j=n/(k/2)-n/k;//n以下のk/2の倍数かつkの倍数でない個数
        ans+=1*j;//a=b=c j
        if(j>=2) ans+=(long long)3*j*(j-1);//a=b!=c jP2
        if(j>=3) ans+=(long long)j*(j-1)*(j-2);//a!=b!=c!=a jP3
    }
    
    printf("%lld\n",ans);
    return 0;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            long[] ABC = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
            long K = long.Parse(Console.ReadLine());
            long result = 0;
            long max = 0;
            for(int j = 0; j < K; j++)
            {
                if (max == 0)
                {
                    max = ABC.Max() * 2;
                }
                else
                {
                    max = max * 2;
                }
            }
            Array.Clear(ABC,Array.IndexOf(ABC,ABC.Max()),1);
            Console.WriteLine(result + ABC.Sum() + max);
        }
    }
}
// 文字列の入力
//string s = Console.ReadLine();

// 整数の入力
//long n = long.Parse(Console.ReadLine());

// 文字列配列の入力
//string[] inputStrArray = Console.ReadLine().Split(' ');

// 整数配列の入力
//long[] inputLongArray = Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();


Measured memory_bytes: 28132
#include <bits/stdc++.h>
using namespace std;
int INF = 1000000007;

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

  int64_t N;
  cin >> N;
  string S;
  cin >> S;
  int64_t ans = 0;
  int64_t dp[N+1][N+1] = {};
  for(int64_t i=N-1;i>=0;i--){
    for(int64_t j=N-1;j>i;j--){
      if(S[i] == S[j]){
        dp[i][j] = dp[i+1][j+1] + 1;
        ans = max(ans, min(dp[i][j], j-i));
      }
      else{
        dp[i][j] = 0;
      }
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 195712
def main():
    N=int(input())
    c=list(input())
    
    #x...WR
    #o...RR, RW, WW
    #Wが来たらそれ以降はW
    #R(0個以上)+W(0個以上)
    
    res=0
    cR=c.count('R')
    cW=c.count('W')
    
    l,r=0,N-1
    while l!=r:
        if c[l]=='W':
            if c[r]=='R':
                c[l],c[r]='R','W'
                res+=1
            else:
                r-=1
        else:
            l+=1
        
    print(min(cR,cW,res))
    
 
if __name__ == '__main__':
    main()

Measured memory_bytes: 10748
import java.util.*;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        int old = sc.nextInt();
        if(old == 1){
            System.out.println("Hello World");
        }else{
            int a, b;
            a = sc.nextInt();
            b = sc.nextInt();
            System.out.println(a + b);
        }
    }
}

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


int main() {
    string a, b;
    cin >> a >> b;
    int x = stoi(a+b);
    for (int i = 0; i <= 1000; i++) {
        if (i*i == x) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
}


Measured memory_bytes: 256
/* 
  Krishnakant Tiwari
  Problem Link:
  
 */
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _cin                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);
#define endl "\n"
#define pb push_back
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define repd(i, a, b) for (ll i = a; i >= b; --i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define mll map<ll, ll>
#define sz(x) (ll) x.size()
#define sll set<ll>
#define pll pair<ll, ll>
#define F first
#define S second
#define eb emplace_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define precise(x) cout << fixed << setprecision(x)
const ll MAX = 1000004;
const ll INF = 1e18L + 5;
template <typename Arg1>
void pn(Arg1 &&arg1)
{
    cout << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void pn(Arg1 &&arg1, Args &&... args)
{
    cout << arg1 << "\n";
    pn(args...);
}
template <typename Arg1>
void ps(Arg1 &&arg1)
{
    cout << arg1 << " ";
}
template <typename Arg1, typename... Args>
void ps(Arg1 &&arg1, Args &&... args)
{
    cout << arg1 << " ";
    ps(args...);
}
template <typename Arg1>
void read(Arg1 &&arg1)
{
    cin >> arg1;
}
template <typename Arg1, typename... Args>
void read(Arg1 &&arg1, Args &&... args)
{
    cin >> arg1;
    read(args...);
}

ll g[MAX];
ll a[MAX];

void solve()
{
    ll n;
    read(n);
    rep(i, 0, n)
    {
        read(a[i]);
    }
    if (n < 3)
    {
        pn(0);
    }
    else
    {
        ll ans = 0;
        ll ch=0;
        rep(i, 0, n)
        {
            ch=0;
            rep(j, i + 1, n)
            {
                rep(k, j + 1, n)
                {
                    if ((a[i] + a[j] > a[k]) && (a[i]+a[k]>a[j]) && (a[k]+a[j]>a[i]) &&(a[i]!=a[j] && a[j]!=a[k] && a[i]!=a[k])){
                        ans++;
                    
                    }
                }
                
            }
        }
        pn(ans);
    }
}

int main()
{

    _cin
    // ll tc;
    // cin>>tc;
    // while(tc--){
    solve();
    // }
}

Measured memory_bytes: 3648
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <tuple>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>
#include <random>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod  1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
bool debug=false;

/*---------------------------------------------------*/

int main(){
  vector<int> a(4);
  rep(i, 4)cin >> a[i];
  sort(all(a));
  //cerr << a[0] << a[1] << a[2] << a[3] << endl;
  if(a[0] == 1 && a[1] == 4 && a[2] == 7 && a[3] == 9) cout << "YES" << endl;
  else cout << "NO" << endl;
  return 0;
}


Measured memory_bytes: 256
import sys
input = sys.stdin.readline
a, b, c, d, e, k = (int(input()) for i in range(6))
list, ans = [a, b, c, d, e], 'Yay!'
for i in range(4):
    for j in range(i, 5):
        if list[j] - list[i] > k:
            ans = ':('
            break
print(ans)


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

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr <<">>>>>>"<< #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

int main()
{
	
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)cin>>a[i];
sort(a,a+n,greater<int>());
int an=0;
for(int i=0;i<k;i++)an+=a[i];
cout<<an<<endl;

}



Measured memory_bytes: 3600
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <random>
#include <chrono>
#include <queue>
#include <math.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 10e+6
#define DEKA 1000000007

using namespace std;

typedef pair<long,long> pairs;

int main(){
    long n;
    cin >> n;
    vector<pairs> vec(n);
    for (int i=0;i<n;i++){
        long a,b;
        cin >> a >> b;
        vec.at(i)=make_pair(b,a);
    }
    sort(vec.begin(),vec.end());
    long real=0;
    bool can=true;
    for (int i=0;i<n;i++){
        if(vec.at(i).first>=real){
            real+=vec.at(i).second;
            if(vec.at(i).first<real){
                can=false;
                break;
            }
        }
        else{
            can=false;
            break;
        }
    }
    if(can) cout <<"Yes"<< endl;
    else cout <<"No"<< endl;
}




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

#define REP(i, x) REPI(i, 0, x)
#define REPI(i, a, b) for (int i = int(a); i < int(b); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
using namespace std;

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

  int A, B, C, X, Y;
  cin >> A >> B >> C >> X >> Y;
  int ans1, ans2, ans3, ans4;
  ans1 = A * X + B * Y;
  ans2 = C * 2 * max(X, Y);
  ans3 = C * 2 * X + max((Y - X), 0) * B;
  ans4 = C * 2 * Y + max((X - Y), 0) * A;

  // cout << ans1 << ":" << ans2 << ":" << ans3 << ":" << ans4 << endl;

  cout << min(min(min(ans1, ans2), ans3), ans4) << endl;

  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL_DEBUG
  #include "LOCAL_DEBUG.hpp"
#endif
#define int long long

signed main(){

  string s[7] = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
  string t; cin >> t;
  for(int i = 0; i < 7; i++){
    if(t == s[i]){
      cout << (7-i) % 8 << endl;
      return 0;
    }
  }

  return 0;
}


Measured memory_bytes: 764
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <time.h>
#include <list>

using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define reps(x,c) for(auto x:c)
#define all(x) begin(x), end(x)
#define pb(a) emplace_back(a) 
#define mp(a,b) make_pair((a),(b))
#define pl pair<ll,ll>
#define INF 2147483600
#define MOD 1000000007
#define fi first
#define se second
#define dbg(x) cout << #x"="<< (x) << endl
#define dbgv(x) cout<<#x<<endl;reps(y,x){cout<<y<<" ";}cout<<endl

int main(){
  ll n,m;cin>>n>>m;
  vector<ll> x(n),y(n),z(n);
  rep(i,n){
    cin>>x[i]>>y[i]>>z[i];
  }
  
  vector<ll> ans;
  vector<ll> cand(n);
  ll temp1,temp2;
  rep(i,n){
  	temp1=0;
  	temp2=0;
  	cand[i]=x[i]+y[i]+z[i];
  }
  sort(all(cand),greater<ll>());
	rep(i,m){
		temp1+=cand[i];
		temp2+=cand[n-1-i];
	}
	ans.pb(temp1);
	ans.pb(temp2);
	temp1=0;
	temp2=0;
	rep(i,n){
  	cand[i]=x[i]-y[i]+z[i];
  }
  sort(all(cand),greater<ll>());
	rep(i,m){
		temp1+=cand[i];
		temp2+=cand[n-1-i];
	}
	ans.pb(temp1);
	ans.pb(temp2);
	temp1=0;
  temp2=0;
  rep(i,n){
  	cand[i]=x[i]+y[i]-z[i];
  }
  sort(all(cand),greater<ll>());
  rep(i,m){
		temp1+=cand[i];
		temp2+=cand[n-1-i];
  }
  ans.pb(temp1);
  ans.pb(temp2);
  temp1=0;
  temp2=0;
  rep(i,n){
  	cand[i]=-x[i]+y[i]+z[i];
  }
  sort(all(cand),greater<ll>());
  rep(i,m){
  	temp1+=cand[i];
  	temp2+=cand[n-1-i];
  }
  ans.pb(temp1);
  ans.pb(temp2);

  rep(i,ans.size()){
  	ans[i]=abs(ans[i]);
  }
  sort(all(ans),greater<ll>());
	cout<<ans[0]<<endl;      
  return 0;
}

Measured memory_bytes: 256
function main(input) {
  const formatted_input = input.split('\n')[0];
  const b = formatted_input;
  var result = '';
  if(b=='A'){result='T'}
  if(b=='T'){result='A'}
  if(b=='G'){result='C'}
  if(b=='C'){result='G'}
  console.log(result);
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));



Measured memory_bytes: 7372
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
int N, Q, a, b;
pair<int, int> calc(int id) {
	int des = 0; long long mul = 1; id--;
	for (int i = 0; id >= mul; i++) {
		id -= mul; mul *= N; des++;
	}
	return make_pair(des, id);
}
int solve(int des, int id) {
	int mul = 1;
	for (int i = 0; i < des; i++) {
		id += mul;
		mul *= N;
	}
	return id + 1;
}
int main() {
	cin >> N >> Q;
	while (Q--) {
		cin >> a >> b;
		if (N == 1) cout << min(a, b) << endl;
		else {
			pair<int, int> pa = calc(a);
			pair<int, int> pb = calc(b);
			if (pa.first < pb.first) swap(pa, pb);
			while (pa.first > pb.first) pa.second /= N, pa.first--;
			while (pa.second != pb.second) {
				pa.second /= N; pa.first--;
				pb.second /= N; pb.first--;
			}
			cout << solve(pa.first, pa.second) << endl;
		}
	}
	return 0;
}

Measured memory_bytes: 1280
// Author -- Frame

#include<bits/stdc++.h>

#define lowbit(x) ((x)&(-x))
#define Finline __inline__ __attribute__ ((always_inline))
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int,int> pi;
typedef std::pair<int,int> pl;

const int inf=0x3f3f3f3f,Inf=0x7fffffff;
const ll INF=0x7fffffffffffffff;

Finline uint rnd()
{
	static uint seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
template <typename _Tp>_Tp gcd(const _Tp &a,const _Tp &b){return (!b)?a:gcd(b,a%b);}
template <typename _Tp>Finline _Tp abs(const _Tp &a){return a>=0?a:-a;}
template <typename _Tp>Finline _Tp max(const _Tp &a,const _Tp &b){return a<b?b:a;}
template <typename _Tp>Finline _Tp min(const _Tp &a,const _Tp &b){return a<b?a:b;}
template <typename _Tp>Finline void chmax(_Tp &a,const _Tp &b){(a<b)&&(a=b);}
template <typename _Tp>Finline void chmin(_Tp &a,const _Tp &b){(b<a)&&(a=b);}
template <typename _Tp>Finline void read(_Tp &x)
{
	char ch(getchar());
	bool f(false);
	while(ch<48||ch>57) f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();
	while(ch>=48&&ch<=57) x=(((x<<2)+x)<<1)+(ch&15),ch=getchar();
	if(f) x=-x;
}
template <typename _Tp,typename... Args>Finline void read(_Tp &t,Args &...args)
{
	read(t);read(args...);
}
Finline int read_str(char *s)
{
	char ch(getchar());
	while(ch==' '||ch=='\r'||ch=='\n') ch=getchar();
	char *tar=s;
	*tar=ch,ch=getchar();
	while(ch!=' '&&ch!='\r'&&ch!='\n'&&ch!=EOF) *(++tar)=ch,ch=getchar();
	return tar-s+1;
}

const int N=1000005;
const int mod=1000000007;
template<typename _Tp1,typename _Tp2>Finline void add(_Tp1 &a,_Tp2 b){(a+=b)>=mod&&(a-=mod);}
template<typename _Tp1,typename _Tp2>Finline void sub(_Tp1 &a,_Tp2 b){(a-=b)<0&&(a+=mod);}
int dp[N];
int sum[N];
int main()
{
	int n;
	read(n);
	dp[1]=n;
	dp[2]=1ll*n*n%mod;
	sum[1]=n;
	sum[2]=1ll*n*(n+1)%mod;
	for(int i=3;i<=n;++i)
	{
		dp[i]=(1ll*(n-1)*(n-1)+sum[i-3]+dp[i-1]+n-i+2)%mod;
		sum[i]=(sum[i-1]+dp[i])%mod;
	}
	printf("%d\n",dp[n]);
	return 0;
}


Measured memory_bytes: 8064
S = input().rstrip()
Q = int(input().rstrip())


q = []
for n in range(Q):
  q.append(list(input().rstrip().split()))

#print(q)
def rev(olist):
  nlist  = olist[::-1]
  return nlist

def addhead(olist,ele):
  nlist = olist.insert(0,ele)
  return nlist
  
def addtail(olist,ele):
  nlist = olist.append(ele)
  return nlist

def rev2(flg):
  if flg == 0:
    flg = 1
  else:
    flg = 0
  return flg

def add(olist,ele,j,flg,head,tail):
  if j == '1' and flg == 0:
    head.insert(0,ele)
  elif j == '1' and flg == 1:
    tail.append(ele)
  elif j == '2' and flg == 0:
    tail.append(ele)
  elif j == '2' and flg == 1:
    head.insert(0,ele)
  
def add2(olist,ele,j,flg,head,tail):
  if j == '1' and flg == 0:
    head = ele + head
  elif j == '1' and flg == 1:
    tail = tail + ele
  elif j == '2' and flg == 0:
    tail = tail + ele
  elif j == '2' and flg == 1:
    head = ele + head
  return head,tail
flg = 0
#head = []
#tail = []
head=''
tail = ''
for n in range(Q):
  
  if q[n][0] == '1':
    flg = rev2(flg)
  
  else:
    head,tail = add2(S,q[n][2],q[n][1],flg,head,tail)


ans = head + S + tail

if flg == 1:
  ans1 = rev(list(ans))
  print(''.join(ans1))
else:
  print(ans)



Measured memory_bytes: 34580
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(I, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  
  vector<int> a(n);
  REP(i,n) cin >> a[i];
  
  sort(a.begin(), a.end());
  
  cout << a[n-1] - a[0] << endl;

}

Measured memory_bytes: 3656
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n, k;
  cin >> n >> k;
  vector<int> l(n);
  for (int i = 0; i < n;i++){
    cin >> l[i];
  }
  sort(l.rbegin(), l.rend());
  int sum = 0;
  for (int i = 0; i < k;i++){
    sum += l[i];
  }
  cout << sum << endl;
  return 0;
}

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

int main(){
  int a, b;
  
  cin >> a >> b;
  
  if(a == b){
      cout << "Draw";
      return 0;
  }
  else if(a == 1){
      cout << "Alice";
      return 0;
  }
  else if(b == 1){
      cout << "Bob";
      return 0;
  }
  else if(a > b){
      cout << "Alice";
      return 0;
  }
  
  else{
      cout << "Bob";
      return 0;
  }
  return 0;
}


Measured memory_bytes: 3540
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include<cmath>
#define rep(i,n) for(int i = 0;i < (n);i++)
using namespace std;

int main(){
    int N;
    cin >> N;
    
    int a[21];
    int b[21];
    int c[21];
   
    rep(i,N) cin >> a[i];
  
    rep(i,N) cin >> b[i];
  
    rep(i,N - 1) cin >> c[i];
    
    int ans =0;
    rep(i,N){
      ans += b[a[i] - 1];
      if(i > 0){
      if(a[i] - a[i - 1] == 1){
        ans += c[a[i - 1] - 1];
      }
      }
    }
  
  cout << ans << endl;
  
    
}

Measured memory_bytes: 256