akhauriyash/Code-Regression huggingface dataset:
fun main(args: Array<String>) {
    fun toPair(L: List<Double>) = Pair(L[0], L[1])
    val N = nextInt()
    val E = Array<Pair<Double, Double>>(N, { toPair(listOfDouble()) })
    E.sortBy { Math.atan2(it.first, it.second) }
    var ans = 0.0
    for (i in 0 until N) {
        var x = 0.0
        var y = 0.0
        for (j in 0 until N) {
            x += E[(i + j) % N].first
            y += E[(i + j) % N].second
            ans = Math.max(ans, Math.sqrt(x * x + y * y))
        }
    }
    println(ans)
}
fun next() = readLine()!!
fun nextInt() = Integer.parseInt(next())
fun listOfString() = next().split(" ")
fun listOfDouble() = listOfString().map { java.lang.Double.parseDouble(it) }

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

int main() {
    int seat = 0;
    int N;
    cin >> N;
    
    int l,r;
    
    for (int i=0; i<N; i++){
        cin >> l >> r;
        seat += r-l+1;
    }
    cout << seat << endl;
}


Measured memory_bytes: 256
#include<cstring>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
long long a[200010];
long long ans[200010];
long long sum=0;
long long x,y;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        sum+=a[i];
    }
    x=a[0];
    y=sum-a[0];
    ans[0]=abs(x-y);
    for (int i=1;i<n-1;i++)
    {
        x+=a[i];
        y-=a[i];
        ans[i]=abs(x-y);
     }
    sort(ans,ans+n-1);
    cout<<ans[0]<<endl;
    return 0;
}





Measured memory_bytes: 3328
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct UnionFind {
  vector< ll > data;
 
  UnionFind(ll sz) {
    data.assign(sz, -1);
  }
 
  bool unite(ll x, ll y) {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }
 
  ll find(ll k) {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }
 
  ll size(ll k) {
    return (-data[find(k)]);
  }
};
int main(){
    ll n,m;
    cin >> n >> m;
    ll a[1000050],b[1000050];
    for(ll i = 0;i < m;i++)cin >> a[i] >> b[i],a[i]--,b[i]--;
    UnionFind uf(n);
    vector<ll> ans;
    ans.push_back(n*(n-1)/2);
    ll x = 0;
    for(ll i = m-1;i > 0;i--){
        ll u = uf.size(a[i]),v = uf.size(b[i]);
        if(uf.unite(a[i],b[i]))x += (u * v);
        ans.push_back(n * (n-1)/2 - x);
    }
    for(ll i = ans.size()-1;i >= 0;i--){
        cout << ans[i] << endl;
    }

}

Measured memory_bytes: 9332
def ans_dp(H,W,M):
    dp = [[0 for i in range(W)] for j in range(H)]
    flag = [[0 for i in range(W)] for j in range(H)]
    if M[0][0] == "#":
        dp[0][0] = 1
        flag[0][0] = 1
    else:
        dp[0][0] == 0
    for i in range(1,W):
        if M[0][i] == "#":
            flag[0][i] = 1
            if flag[0][i-1] == 1:
                dp[0][i] = dp[0][i-1]
            else:
                dp[0][i] = dp[0][i-1] + 1 
        else:##M[0][i] = "."
            dp[0][i] = dp[0][i-1]
    for j in range(1,H):
        if M[j][0] == "#":
            flag[j][0] = 1
            if flag[j-1][0] == 1:
                dp[j][0] = dp[j-1][0]
            else:
                dp[j][0] = dp[j-1][0] + 1 
        else:##M[0][i] = "."
            dp[j][0] = dp[j-1][0]
    
    for i in range(1,H):
        for j in range(1,W):
            if M[i][j] == "#":
                flag[i][j] = 1
                dp[i][j] = min(dp[i-1][j]+int(not flag[i-1][j]),dp[i][j-1]+int(not flag[i][j-1]))
            else:#M[i][j] == "."
                dp[i][j] = min(dp[i-1][j],dp[i][j-1])
    """
    for i in range(H):
        print(dp[i])
    for i in range(H):
        print(flag[i])
    """
    return dp[H-1][W-1]

def main():
    H,W = map(int,input().split())
    M = []
    for i in range(H):
        M.append(list(input()))
    print(ans_dp(H,W,M))
 
if __name__ == "__main__":
    main()

Measured memory_bytes: 3444
def read_input():
    n = int(input())

    flist = []
    for i in range(n):
        flist.append(list(map(int, input().split())))

    plist = []
    for j in range(n):
        plist.append(list(map(int, input().split())))

    return n, flist, plist


# aとbの1が重複している数を数える
def check_open(a, b):
    c = 0
    for i,j in zip(a, b):
        if i == j == 1:
            c += 1

    return c

# 整数を受け取り、2進数のリストにして返す
def get_pattern(p):
    binary_p = bin(p)[2:]
    list_p = [int(c) for c in binary_p]
    list_p = [0]*(10 - len(list_p)) + list_p
    return list_p


def submit():
    n, flist, plist = read_input()
    max_pattern = 2048
    max_profit = -float('inf')
    for p in range(1, 2048):
        myshop = get_pattern(p)
        profit = 0
        for i in range(n):
            target_shop = flist[i]
            c = check_open(myshop, target_shop)
            profit += plist[i][c]

        if max_profit < profit:
            max_profit = profit

    print(max_profit)

if __name__ == '__main__':
    submit()

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

int main(void){
    long long A;
    string B;
    cin >> A >> B;
    long long Bdash = 0;
    for(int i=0; i<B.size(); i++){
        if(i==1)continue;
        Bdash *= 10;
        Bdash += B[i]-'0';
    }
    cout << (long long)(A*Bdash/100) << endl;
    return 0;
}

Measured memory_bytes: 3620
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll=long long;
using P = pair<int,int>;
using LP=pair<ll,int>;
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
const ll MOD=1000000007,MOD2=998244353;

string num(ll a,ll b){
    string x="";
    string y=x;
    while(a){
        x.pb('0'+a%10);
        a/=10;
    }
    while(sz(x)<6){x.pb('0');}
    reverse(x.begin(),x.end());
    while(b){
        y.pb('0'+b%10);
        b/=10;
    }
    while(sz(y)<6){y.pb('0');}
    reverse(y.begin(),y.end());
    return x+y;
}

int main() {
    int N,M;
    cin>>N>>M;
    vector<map<ll,int>> city(N+1);
    rep(i,M){
        int p;ll y;
        cin>>p>>y;
        city[p][y]=i;
    }
    vector<string> ans(M);
    rep(i,N+1){
        if(city.at(i).size()==0)continue;
        int cnt=0;
        for(LP C:city.at(i)){
            cnt++;
            ans.at(C.second)=num(i,cnt);
        }
    }
    rep(i,M){
        cout<<ans.at(i)<<endl;
    }
}

Measured memory_bytes: 17248
s = input()
k = int(input())
A = []
b = 1
if s[0] != s[-1]:
  for i in range(len(s)-1):
    if s[i] == s[i+1]:
      b+= 1
    else:
      A.append(b)
      b = 1
  A.append(b)
  print(k*sum(a//2 for a in A))
elif all(a  == s[0] for a in s):
  print((k*(len(s))//2))
else:
  t = s[0]
  ahead = 0
  back = 0
  ans = 0
  for i in range(len(s)):
    if s[i] == t:
      ahead += 1
    else:
      break
  for i in reversed(range(len(s))):
    if s[i] == t:
      back += 1
    else:
      break
  ans += ahead//2 + back//2 + (k-1)*((ahead+back)//2)
  l = s[ahead:-back]
  for i in range(len(l)-1):
    if l[i] == l[i+1]:
      b+= 1
    else:
      A.append(b)
      b = 1
  A.append(b)
  ans += (k*sum(a//2 for a in A))
  print(ans)

Measured memory_bytes: 3064
#include <iostream>
#include <string>
using namespace	std;
int main(){

  int n;
  string s1,s2;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>s1>>s2;
    int dp[1001][1001]={};
    for(int j=1;j<=s2.size();j++){
      for(int k=1;k<=s1.size();k++){
        if(s1[k-1]==s2[j-1]) dp[j][k]=dp[j-1][k-1]+1;
        else dp[j][k]=max(dp[j-1][k],dp[j][k-1]);
      }
    }
    cout<<dp[s2.size()][s1.size()]<<endl;
  }
  return 0;

}

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

using namespace std;

#define ll long long
#define PI acos(-1.0)

ll dp[100105];
int n, k,arr[100005];

ll func(int indx)
{
	if(indx == n) return 0;
	if(dp[indx] != -1) return dp[indx];
	int len = min(indx+k, n);
	ll total = INT_MAX;
	for(int i = indx+1; i <= len;i++)
	{
		ll ans = abs(arr[indx]-arr[i]) + func(i);
		total = min(total,ans);
	}
	return dp[indx] = total;
}

int main()
{   
    scanf("%d %d", &n, &k);
    for(int i = 1;i <= n; i++)
    {
		scanf("%d", &arr[i]);
	}
	memset(dp,-1,sizeof(dp));
	ll ans = func(1);
	printf("%lld\n",ans);
	    	
	return 0;
}


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

using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;
	int x;
	vector<vector<int>>matrix(n, vector<int>(m,0));
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			
			cin >> x;
			matrix[i][j] = x;
		}
	}
	vector<int>v(m);
	for (int i = 0; i < m; i++)

	{
		
		cin >> x;
		v[i] = x;
	}
	vector<int>answer(n, 0);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m;j++)
		{
			
			answer[i] += matrix[i][j] * v[j];
		}
		
	}
	for (int i = 0; i < n; i++)
	{
		cout << answer[i] << endl;
	}

		
}


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

int main(){
    int a,b;
    char op;
    while(1){
        cin >> a >> op >> b;
        if(op=='?')break;
        if(op=='+')cout << a+b << endl;
        if(op=='-')cout << a-b << endl;
        if(op=='*')cout << a*b << endl;
        if(op=='/')cout << a/b << endl;
    }
    return 0;
}


Measured memory_bytes: 3084
def printSoroban(data)
  for line in 0...data.size
    for raw in 0...data[0].size
      print data[line][raw]
    end
    puts
    puts "=====" if  line == 1
  end
end

def createData(value)
  data =  [[],[],[],[],[]],
          [[],[],[],[],[]],
          [[],[],[],[],[]],
          [[],[],[],[],[]],
          [[],[],[],[],[]],
          [[],[],[],[],[]],
          [[],[],[],[],[]]
            
  for digit in 0...value.size
    #そろばんの上
    if  value[digit] /5 == 1
      data[0][digit] = " "
      data[1][digit] = "*"
    else
      data[0][digit] = "*"
      data[1][digit] = " "
    end
    #そろばんの下
    for raw in 2...data.size
      if  raw == value[digit] % 5 + 2
        data[raw][digit] = " "
      else
        data[raw][digit] = "*"
      end
    end
  end
  
  return data
end

c = 0
while input = gets
  puts  if c != 0
  n = input.to_i
  value = []
  value << n / 10000
  value << (n % 10000) / 1000
  value << (n % 1000) / 100
  value << (n % 100) / 10
  value << n % 10
  printSoroban(createData(value))
  c += 1
end

Measured memory_bytes: 6120
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <algorithm>

#include <cstdio>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define print_array(ary, size) cout << ary[0]; rep1(i, 1, size - 1) cout << ' ' << ary[i]; cout << endl;
typedef long long ll;
typedef unsigned long long ull;

int M[100][100];
int MinCost[100];
int done[100];

int main()
{
	int n, u, k, v, c;
	int next;
	int cost, sum = 0;

	cin >> n;

	rep(i, n) {
		MinCost[i] = 10000001;
		done[i] = 0;
		rep(j, n) M[i][j] = -1;
	}

	rep(i, n) {
		cin >> u >> k;
		rep(j, k) {
			cin >> v >> c;
			M[u][v] = c;
		}
	}

	MinCost[0] = 0;
	next = 0;
	done[0] = 1;
	sum = 0;
	
	rep(loop, n - 1) {
		rep(i, n) {
			if (M[next][i] != -1) {
				MinCost[i] = min(MinCost[i], MinCost[next] + M[next][i]);
			}
		}

		cost = 10000001;
		rep(i, n) {
			if (done[i] == 0 && cost > MinCost[i]) {
				cost = MinCost[i];
				next = i;
			}
		}

		done[next] = 1;
		sum += cost;
	}

	rep(i, n) {
		cout << i << ' ' << MinCost[i] << endl;
	}

	return 0;
}


Measured memory_bytes: 3120
package main

import "fmt"

var h []int
var dp []int

func main() {
	var N int
	fmt.Scan(&N)
	h = make([]int, N+10)
	for i := 0; i < N; i++ {
		fmt.Scan(&h[i])
	}

	dp = make([]int, N+10)
	for i := 0; i < N+10; i++ {
		dp[i] = 999999999
	}

	ans := dfs(N - 1)
	fmt.Println(ans)
}

func dfs(i int) int {
	if i <= 0 {
		return 0
	}

	if dp[i] < 999999999 {
		return dp[i]
	}

	res := 999999999
	res = min(res, dfs(i-1)+abs(h[i]-h[i-1]))
	if i-2 >= 0 {
		res = min(res, dfs(i-2)+abs(h[i]-h[i-2]))
	}

	dp[i] = res
	return res
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}


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

int main()
{
  int N, A;
  int max_A = 1;
  int min_A = 1000000000;

  cin >> N;
  for (int i = 0; i < N; i++)
  {
    cin >> A;
    if (max_A < A)
      max_A = A;
    if (A < min_A)
      min_A = A;
  }
  cout << max_A - min_A << endl;
}

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

public class Main {
    static PrintWriter out = new PrintWriter(System.out);
    static Scanner sc = new Scanner(System.in);
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    long mod = 1000000000+7;
    List<Integer>[] tree;
    int n;
    public static void main(String[] args) throws IOException {
        Main main = new Main();
//        main.solve();
        out.println(main.solve());
        out.flush();
    }
    long solve() throws IOException {
        n = sc.nextInt();
        tree = new List[n];
        for(int i=0;i<n;i++) tree[i] = new ArrayList<>(25);
        for(int i=0;i<n-1;i++){
            int a = sc.nextInt()-1, b = sc.nextInt()-1;
            tree[a].add(b); tree[b].add(a);
        }
        int m = sc.nextInt();
        int[][] restri = new int[m][];
        for(int i=0;i<m;i++){
            int u = sc.nextInt()-1, v = sc.nextInt()-1;
            restri[i] = new int[]{u,v};
        }
        Set<Integer>[] white = new Set[m];
        for(int i=0;i<m;i++){
            int u = restri[i][0], v = restri[i][1];
            white[i] = getEdges(u,v);
        }
        long sub = 0; // how many illegal choices are there
        for(int state=1; state<(1<<m); state++){
            // then the relevant paths are all white
            int cnt = 0;
            Set<Integer> tot_white = new HashSet<>(50);
            for(int j=0; j<m; j++){
                int mask = 1<<j;
                if((mask&state)>0){
                    // we let this restriction unfulfilled
                    tot_white.addAll(white[j]);
                    cnt++;
                }
            }
            int remain = n-1-tot_white.size();
            if(cnt%2==1) sub += 1L<<remain;
            else sub -= 1L<<remain;
        }
        long ans = (1L<<(n-1))-sub;
        return ans;
    }
    Set<Integer> getEdges(int u, int v){
        int[] prev = new int[n];
        dfs(u,u,prev,v);
        Set<Integer> ans = new HashSet<>();
        // since prev[u] = u;
        while(prev[v]!=v){
            ans.add(getKey(v, prev[v]));
            v = prev[v];
        }
        return ans;
    }
    void dfs(int cur, int from, int[] prev, int target){
        prev[cur] = from;
        if(cur==target) return;
        for(int to:tree[cur]){
            if(to==from) continue;
            dfs(to, cur, prev, target);
        }
    }
    int getKey(int a, int b){
        return Math.min(a,b)*n+Math.max(a,b);
    }

}

Measured memory_bytes: 343188
#include<stdio.h>
int main(void)
{
    int a,b,c;
    scanf("%d %d %d",&a, &b, &c);
    if(a<b){
        if(b<c){
        printf("Yes\n");
    }else{
        printf("No\n");
    }
    }else{
        printf("No\n");
    }
    return 0;
}   

Measured memory_bytes: 580
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<math.h>
#include<utility>
#include<iomanip>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int n, r;
	cin >> n >> r;
	if (n < 10) cout << r + 100 * (10 - n);
	else cout << r;
}



Measured memory_bytes: 256
#include<stdio.h>
int main(){
  int k,a;
  while(scanf("%d",&k)!=EOF){
    if(k<=39){
      a=k;
    }
    if(k>39){
      a=k%39;
      if(a==0){
	a=39;
      }
    }
    if(a<10){
      printf("3C0%d\n",a);
    }
    else{
      printf("3C%d\n",a);
    }
  }
  return 0;
}

Measured memory_bytes: 596
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
               Scanner sc = new Scanner(System.in);		
               int a = sc.nextInt();
               int b = sc.nextInt();
		
		String odd = "Odd";
		String even = "Even";
		String answer = "";
		
		int result = a * b;
		if (result % 2 == 1 ) {
			answer = odd;
		}else{
			answer = even;
		}
		
		System.out.println(answer);

	}

}

Measured memory_bytes: 23764
n, m = map(int, input().split())
k = []
for i in range(m):
    tmp = list(map(int, input().split()))
    k.append(tmp[1:])
p = list(map(int, input().split()))

res = 0
for i in range(2 ** n):
    l = []
    for j in range(n):
            l.append(int((i & (2 ** j)) == 0))
    flag = True
    
    for ll, j in enumerate(k):
        cnt = 0
        for q in j:
            cnt += l[q - 1]
        if cnt % 2 != p[ll]:
            flag = False

    if flag:
        res += 1
print(res)


Measured memory_bytes: 3064
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
static const int MAX_N = 500;

int N;
double r[MAX_N];

double memo[MAX_N];

int main(){
	scanf("%d", &N);
	for(int i = 0; i < N; i++) scanf("%lf", &r[i]);
	for(int i = 0; i < N; i++){
		memo[i] = r[i];
		for(int j = 0; j < i; j++){
			memo[i] = max(memo[i], memo[j] + sqrt(pow(r[i] + r[j], 2.0) - pow(fabs(r[i] - r[j]), 2.0)));
		}
	}
	double res = 0.0;
	for(int i = 0; i < N; i++) res = max(res, memo[i] + r[i]);
	printf("%.8lf\n", res);
	return 0;
}



Measured memory_bytes: 2760
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n][3];
for(int i=0;i<n;i++)
{
for(int j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
int num;	
int dp[n][3]={0};
for(int i=0;i<3;i++)
dp[0][i]=arr[0][i];
for(int i=1;i<n;i++)
{
for(int j=0;j<3;j++)
{
num=dp[i-1][j];
for(int k=0;k<3;k++)
{
if(j!=k)
dp[i][k]=max(dp[i][k],num+arr[i][k]);
}
}
}
int maxer=0;
for(int i=0;i<3;i++)
{
if(dp[n-1][i]>maxer)
maxer=dp[n-1][i];
}
cout<<maxer<<endl;
return 0;
}


Measured memory_bytes: 2560
#include <bits/stdc++.h>
#define rep(i,n) for (int (i) = 0; (i) < (n); i++)
#define ll long long
using namespace std;
int main() {
  ll N = 0,check = 0,check4 = 0;
  cin >> N;
  vector<ll>A(N);
  rep(i,N){
    cin >> A[i];
    if(A[i] % 4 == 0){
      check += 2;
      check4 += 2;
    }
    else if(A[i] % 2 == 0){
      check++;
    }
  }
  if(N % 2 == 0){
    if(N <= check){
      cout << "Yes";
    }
    else{
      cout << "No";
    }
  }
  else{
    if(check4+1 >= N){
      cout << "Yes";
    }
    else if(check >= N){
      cout << "Yes";
    }
    else{
      cout << "No";
    }
  }
}

Measured memory_bytes: 3948
void main(){
	int[] input;
	foreach(i; 0..3)input ~= _scan();
	( (input[0]-input[1])%input[2] ).writeln();
}

import std.stdio, std.conv, std.algorithm, std.numeric, std.string, std.math;

// 1要素のみの入力
T _scan(T= int)(){
	return to!(T)( readln().chomp() );
}

// 1行に同一型の複数入力
T[] _scanln(T = int)(){
	T[] ln;
	foreach(string elm; readln().chomp().split()){
		ln ~= elm.to!T();
	}
	return ln;
}


Measured memory_bytes: 2732
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;

int eratos(int n){
	vector<int> v;
	for(int i=0; i<=n; i++) v.push_back(i);
	v[1]=0;
	for(int i=2; i<=(int)sqrt((double)n); i++){
		if(v[i]!=0){
			for(int j=2; i*j<=n; j++){
			v[i*j]=0;
			}
		}
	}
	int count=0;
	for(int i=2; i<=n; i++)
		if(v[i]!=0) count++;
	return count;
	return 0;
}

int main(void){
	int n;
	while(cin >> n)
		cout << eratos(n) << endl;
	return 0;
}

Measured memory_bytes: 5032
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

// 

public class Program_TTB // ←Class名変更！！
{
    public static void Main()
    {
        var na = Array.ConvertAll(ReadLine().Split(' '), int.Parse);
        var t = na[0];
        var a = na[1];
        var maxK = na[2];

        for (int k = 0; k < maxK; k++)
        {
            if (k%2==0)
            {
                t = t / 2;
                a += t;

            }
            else
            {
                a = a / 2;
                t += a;

            }
        }

        WriteLine($"{t} {a}");
        return;

    } // Main

}     // Class


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

const int mxn = 2e5;

#define Max(a,b) a=max(a,b)
#define Min(a,b) a=min(a,b)
#define f(i,a,b) for (int i = a; i < b; i++)

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	clock_t start = clock();
#endif

	int n;
	cin>>n;
	int a[n];
	f(i,0,n) cin>>a[i];
	f(i,0,n) a[i]--;
	int ans = n-1;
	int pos[n];
	f(i,0,n) pos[a[i]]=i;
	f(i,0,n) {
		int j = i;
		while (j<n-1&&pos[j+1]>pos[j])
			j++;
		Min(ans,n-(j-i+1));
		i = j;
	}
	cout << ans << endl;

	

#ifdef LOCAL
	cout << setprecision(12) << (long double)(clock()-start) / CLOCKS_PER_SEC << endl;
#endif
	
	return 0;
}



Measured memory_bytes: 1792
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

class Main{
    public static void main(String[] Args) throws IOException{
        final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        final int n = Integer.parseInt(br.readLine());
        final vec[][] mat = new vec[n][];
        for(int i = 0; i<n; i++){
            final StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            final int u = Integer.parseInt(st.nextToken());
            final int k = Integer.parseInt(st.nextToken());
            mat[u] = new vec[k];
            for(int j = 0; j<k; j++){
                mat[u][j] = new vec(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
            }
        }
        final int[] cost = new int[n];
        for(int i = 0; i<n; i++)
            cost[i] = Integer.MAX_VALUE;
        final PriorityQueue<vec> pq = new PriorityQueue<vec>();
        pq.add(new vec(0, 0));
        while(!pq.isEmpty()){
            final int t = pq.peek().t;
            final int c = pq.poll().c;
            if(c>=cost[t]) continue;
            cost[t] = c;
            for(final vec v:mat[t]){
                pq.add(new vec(v.t, c+v.c));
            }
        }
        final StringBuilder sb = new StringBuilder();
        for(int i = 0; i<n; i++){
            sb.append(i).append(' ').append(cost[i]).append('\n');
        }
        System.out.print(sb);
    }
}

class vec implements Comparable<vec>{
    final int t;
    final int c;
    vec(int to, int cost){
        t = to;
        c = cost;
    }
    @Override
    public int compareTo(vec other){
        return c-other.c;
    }

}

Measured memory_bytes: 86776
def f(x):
  out=[]
  for j in range(2, int(x**(1/2))+1, 1):
    while x%j==0:
      out.append(j)
      x//=j
      if x==1:
        break
  if x!=1:
    out.append(x)
  return out

N=int(input())
mod=10**9+7
nums=[1]*(N+1)
for i in range(2, N+1):
  a=f(i)
  for j in a:
    nums[j]+=1
    
for i in range(3, N+1):
  nums[i]=(nums[i-1]*nums[i])%mod
  
print(nums[-1])

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

class Main {
    static Scanner scanner = new Scanner();
    static long mod = 1000000007;
    static int inf = 0x7ffffff;

    public static void main(String[]$) throws Exception {
        int n = scanner.nextInt();
        int[] d = scanner.nextIntArray(n);
        long sum = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                sum += d[i] * d[j];
            }
        }
        System.out.println(sum);
    }

    static class System {
        static PrintWriter out = new PrintWriter(java.lang.System.out);

        static {
            Runtime.getRuntime().addShutdownHook(new Thread(System.out::flush));
        }
    }

    static class Scanner {
        BufferedReader reader = new BufferedReader(new InputStreamReader(java.lang.System.in), 1 << 15);
        StringTokenizer tokenizer;

        String next() {
            try {
                while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                    tokenizer = new StringTokenizer(reader.readLine());
                }
            } catch (IOException ignored) {
            }
            return tokenizer.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        char[][] nextCharArray(int n, int m) {
            char[][] a = new char[n][m];
            for (int i = 0; i < n; i++) {
                a[i] = next().toCharArray();
            }
            return a;
        }

        int[] nextIntArray(int n, java.util.function.IntUnaryOperator op) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = op.applyAsInt(nextInt());
            }
            return a;
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextInt();
            }
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextLong();
            }
            return a;
        }

        long[] nextLongArray(int n, java.util.function.LongUnaryOperator op) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = op.applyAsLong(nextLong());
            }
            return a;
        }
    }
}


Measured memory_bytes: 28244
n = int(input())
fn = [0] * (n+1)
def f(x, y, z):
    return x**2 + y**2 + z**2 + x*y + y*z + z*x 

for x in range(1, 101):
    for y in range(1, 101):
        for z in range(1, 101):
            idx = f(x, y, z)
            if idx <= n:
                fn[idx] += 1
for i in range(1, n+1):
    print(fn[i])


Measured memory_bytes: 9340
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <math.h>
#include <vector>
#include <map>
#include <queue>
#include <utility>
#include <algorithm>
#include <string>
#include <set>
#include <random>
using namespace std;
class UnionFind {
private:
  vector <long long> par; // 親ノード
  vector <long long> rank; // 各ノードのランク
  long long n; // 全体のサイズ
public:
  // コンストラクタ
  UnionFind(long long sz_) : n(sz_), par(sz_), rank(sz_, 1) {
	for (long long i = 0; i < sz_; i++) {
		par[i] = i;
	  }
  }

  // メンバ関数
long long root(long long x) {
  if (par[x] == x) {
	  return x;
	}
  else {
	  par[x] = root(par[x]);
	  return par[x];
	}
}

void unite(long long x, long long y) {
  x = root(x);
  y = root(y);
  if (x == y) {
	  return;
	}
  if (rank[x] < rank[y]) {
	  swap(x, y);
	}
  rank[x] += rank[y];
  par[y]   = x;
}

bool isSame(long long x, long long y) {
  return root(x) == root(y);
}

long long siz(long long x) {
  return rank[root(x)];
}
};

int main() {
  long long n, m;
  cin >> n >> m;
  UnionFind uf(n);
  vector <long long> a(m);
  vector <long long> b(m);
  for (long long i = 0; i < m; i++) {
	  cin >> a[i] >> b[i];
	  a[i]--;
	  b[i]--;
	}
  vector <long long> ans(m);
  ans[m - 1] = n * (n - 1) / 2;
  for (long long i = m - 1; i > 0; i--) {
	  if (uf.isSame(a[i], b[i])) {
		  ans[i - 1] = ans[i];
		} else {
		  ans[i - 1] = ans[i] - (long long) uf.siz(a[i]) * uf.siz(b[i]);
		  uf.unite(a[i], b[i]);
		}
	}
  for (long long i = 0; i < m; i++) {
	  cout << ans[i] << endl;
	}
  return 0;
}


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


using namespace std;

string l,s;
int n,m,dp[3001][3001];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //getline(cin,l);
    //getline(cin,s);
    cin >> l;
    cin >> s;
    int sL=l.length(),sS=s.length();
    //int dp[sL][sS];
    for (int i = 0; i <= sL; i ++)
    {
        dp[i][0]=0;
    }
    for (int i = 0; i <= sS; i++)
    {
        dp[0][i]=0;
    }

    for (int i = 1; i <= sL ; i++)
        for (int j = 1; j <= sS ; j++)
        {
            if (l[i-1] == s[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
            else
            dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
        }

    int x = sL, y = sS;
    string kq="";
    while (x > 0 && y > 0)
    {
        if (l[x-1]==s[y-1])
        {
            kq=s[y-1]+kq;
            x--;
            y--;
        }
        else
        if (dp[x][y] == dp[x-1][y])
        {
            x--;
        }
        else

        {
            y--;
        }
    }
    cout << kq ;
    return 0;
}


Measured memory_bytes: 35456
#HEX
X, Y = input().split()
list = ['A', "B", "C", "D", "E", "F"]
for i in range (0,6):
    if X == list[i]:
        NX = i
    if Y == list[i]:
        NY = i
if NX > NY:
    print(">")
elif NX < NY:
    print("<")
else:
    print("=")

Measured memory_bytes: 3060
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <math.h>
#include <tuple>

#include <set>
#include <unordered_map>
#include <stack>
#include <map>

using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define all(x) (x).begin(),(x).end()
using ll = long long;
template<class T>bool chmaxj(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
void YesNo(bool y) { cout << (y ? "Yes" : "No") << endl; } //true==yes


#define INF 21474836

//////////////////////////////////////////////////////////////



int main() {

	int N, M, Q;
	cin >> N >> M >> Q;
	vector<int> a(Q), b(Q), c(Q), d(Q);
	
	rep(i, Q) {
		cin >> a[i] >> b[i] >> c[i] >> d[i];
		a[i] --;
		b[i] --;
	}

	vector<int> A(N,1);
	A[N-1] = 0;

	long long max = 0;
	

	while (true) {
		rep(i, N) {
			A[N - 1 - i]++;
			if (A[N - 1 - i] <= M)break;
			A[N - 1 - i] = 1;
			if (N - 2 - i < 0) {
				cout << max << endl;
				return 0;
			}
		}
		rep(i, N-1) {
			if (A[i] > A[i + 1])A[i + 1] = A[i];
		}

		long long now = 0;

		rep(i, Q) {
			if (A[b[i]] - A[a[i]] == c[i])now += d[i];
		}

		if (now > max)max = now;
	}


	

}


Measured memory_bytes: 3600
#include <bits/stdc++.h>
#define ll  long long

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, "\e[31;1m"); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\e[0m")
#else
    #define eprintf(...) 42
#endif

struct Point
{
    int X;
    int Y;
    int A;
};

ll solve(const int W, const int H, const std::vector<Point>& P)
{
    std::vector<std::vector<int>> A(H, std::vector<int>(W, 0));
    for (auto itr = P.begin(); itr != P.end(); ++itr)
    {
        switch (itr->A)
        {
        case 1: // X < Xi
            for (int x = 0; x < itr->X; x++)
            {
                for (int y = 0; y < H; y++)
                {
                    A[y][x] = 1;
                }
            }
            break;

        case 2: // X > Xi
            for (int x = itr->X; itr->X - 1 < x && x < W; x++)
            {
                for (int y = 0; y < H; y++)
                {
                    A[y][x] = 1;
                }
            }
            break;

        case 3: // Y < Yi
            for (int x = 0; x < W; x++)
            {
                for (int y = 0; y < itr->Y; y++)
                {
                    A[y][x] = 1;
                }
            }
            break;

        case 4: // Y > Yi
            for (int x = 0; x < W; x++)
            {
                for (int y = itr->Y; itr->Y - 1 < y && y < H; y++)
                {
                    A[y][x] = 1;
                }
            }
            break;
        }
    }
#if DEBUG1
    for (auto itr1 = A.begin(); itr1 != A.end(); ++itr1)
    {
        std::cout << "DEBUG1: ";
        for (auto itr2 = itr1->begin(); itr2 != itr1->end(); ++itr2)
        {
            std::cout << *itr2;
        }
        std::cout << std::endl;
    }
#endif

    return std::accumulate(A.begin(), A.end(), 0, [](const int a, const std::vector<int>& v){
        return std::accumulate(v.begin(), v.end(), a, [](const int a, const int v){
            if (0 == v)
            {
                return a + 1;
            }
            return a;
        });
    });
}

int main(void)
{
    int W, H, N;
    std::cin >> W >> H >> N;

    std::vector<Point> P(N);
    for (int i = 0; i < N; i++)
    {
        std::cin >> P[i].X >> P[i].Y >> P[i].A;
    }
    std::cout << solve(W, H, P) << std::endl;

    return 0;
}

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

#define ll long long

int dp[500 + 1][1000];
const int MOD = 998244353;

signed main(){
   int n, x;
   cin >> n >> x;
   
   dp[0][0] = 1;
   int p = 1;

   while (x >= p) p <<= 1;
   for (int i = 0; i <= x; i += 1){
      for (int j = 0; j < n; j += 1){
         for (int k = 0; k < p; k += 1){
            dp[j + 1][k ^ i] += dp[j][k];
            dp[j + 1][k ^ i] %= MOD;
         }
      }
   }

   cout << dp[n][x] << endl;
}


Measured memory_bytes: 5048
#include <bits/stdc++.h>
#define maxn 2005
using namespace std;
typedef long long ll;
ll inv[maxn],F[maxn],Finv[maxn];
ll mod=1e9+7;
void init(){
    inv[1]=1;
    for(int i=2;i<maxn;i++){
        inv[i]=(mod-mod/i)*inv[mod%i]%mod;
    }
    F[0]=1;
    Finv[0]=1;
    for(int i=1;i<=maxn;i++){
        F[i]=F[i-1]*i%mod;
        Finv[i]=Finv[i-1]*inv[i]%mod;
    }
}
ll C(ll n,ll m){
    if(n<0||m<0||m>n) return 0;
    return F[n]*Finv[m]%mod*Finv[n-m]%mod;
}
int main()
{
    int n,k;
    init();
    scanf("%d%d",&n,&k);
    for(int i=1;i<=k;i++){
        ll a=C(n-k+1,i);
        ll b=C(k-1,i-1);
        printf("%lld\n",a*b%mod);
    }
    return 0;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
        int ans = 0;
        for (int i = 1; i < n + 1; i++) {
            if (String.valueOf(i).length() % 2 == 1) {
                ans++;
            }
        }
        System.out.println(ans);
    }
}

Measured memory_bytes: 27348
m=10**9
n=int(input())
a='abcdefghijklmnopqrstuvwxyz'
pue=[]
for i in range(len(a)):
    pue.append([a[i],m])
for i in range(n):
    s=input()
    for j in range(26):
        pue[j][1]=min(s.count(a[j]),pue[j][1])

ans=''
for i in range(26):
    for j in range(pue[i][1]):
        ans+=a[i]
print(ans)

Measured memory_bytes: 9204
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;
  cin >> N;
  vector<int> x(N);
  vector<int> y(N);
  for (int i = 0; i < N; i++) cin >> x.at(i) >> y.at(i);
  
  int mi = 50;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (j == i) continue;
      int X = x.at(i) - x.at(j);
      int Y = y.at(i) - y.at(j);
      int cnt = 0;
      for (int k = 0; k < N; k++) {
        for (int l = 0; l < N; l++) {
          if (l == k) continue;
          if (x.at(k) - x.at(l) == X && y.at(k) - y.at(l) == Y) {
            cnt++;
          }
        }
      }
      mi = min(N - cnt, mi);
    }
  }
  if (N > 1)
    cout << mi << endl;
  else
    cout << 1 << endl;
}

Measured memory_bytes: 384
r, c = map(int, input().split())
a = []

for i in range(r):
    a.append(list(map(int, input().split())))
    a[i].append(sum(a[i]))

for i in range(r):
    print(" ".join(list(map(str, a[i]))))

csum = []
for j in range(c+1):
    su = 0
    for i in range(r):
        su += a[i][j]
    csum.append(su)

print(" ".join(list(map(str, csum))))



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

int main(){
  int n;
  cin >> n;
  map<string, int> mp;
  rep(i,n){
      string s;
      cin >> s;
      mp[s]++;
  }
  int mx = 0;
  for(auto p : mp) mx = max(mx, p.second); //mx(現時点でのキーの最頻値とp.secondを比較誌大きいほうをとる)
  for(auto p : mp) {
      if(p.second !=mx)continue;
      cout << p.first << endl;
  }  
  
  return 0;
}

Measured memory_bytes: 25216
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<sstream>
#include<string>
using namespace std;
typedef long long ll;
ll mod = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)

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

    vector<pair<int, int>> a(n);

    rep(i, n) {
        int tmp_a, tmp_b;
        cin >> tmp_a >> tmp_b;
        a[i] = make_pair(tmp_a, tmp_b);
    }

    stable_sort(a.begin(), a.end());

    ll sum = 0;
    rep(i, n) {
        sum += a[i].second;
        if ( sum >= k ) {
            cout << a[i].first << endl;
            return 0;
        }
    }

    return 0;
}

Measured memory_bytes: 1792
import java.util.*;

public class Main{

    static Scanner sc = new Scanner(System.in);
    static int n = sc.nextInt();
    static int m = sc.nextInt();
    static List<Node>[] g = new ArrayList[n];
    static long[] d = new long[n];

    public static void main(String[] args){
        d[0] = 0;
        for (int i=0; i<n; i++){
            g[i] = new ArrayList<>();
        }

        for (int i=0; i<n; i++) {
            g[i] = new ArrayList<Node>();
            if (i!=0) g[i].add(new Node(i-1, 0));

        }
        for (int i=0; i<m; i++){
            int l = sc.nextInt()-1;
            int r = sc.nextInt()-1;
            int c = sc.nextInt();
            g[l].add(new Node(r, c));
        }

        System.out.println(dijkstra());

    }

    public static long dijkstra(){
        Arrays.fill(d, Long.MAX_VALUE);
        d[0] = 0;
        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(new Node(0, 0));

        while (!pq.isEmpty()){
            Node nd = pq.poll();
            if (nd.node==n-1) return nd.cost;
            if (nd.cost > d[nd.node]) continue;

            for (Node next: g[nd.node]){
                if (d[next.node] > nd.cost+next.cost){
                    d[next.node] = nd.cost+next.cost;
                    pq.add(new Node(next.node, nd.cost+next.cost));
                }
            }

        }
        return -1;
    }

    public static class Node implements Comparable<Node>{
        int node;
        long cost;
        public Node(int n, long c){
            this.node = n;
            this.cost = c;
        }
        public int compareTo(Node nd){
            if (this.cost<nd.cost) return -1;
            if (this.cost>nd.cost) return 1;
            return 0;
        }
    }

}

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

#include <iomanip>
void _130() {
    int w, h, x, y;
    cin >> w >> h >> x >> y;

    cout << fixed << setprecision(10);
    cout << (double)w * h / 2 << " " << (2 * x == w && 2 * y == h) << endl;
}

void _131() {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;

    long long count = 0;
    for (long long i = a; i <= b; ++i) {
        if (i % c != 0 && i % d != 0){
            count++;
        }
    }
    cout << count << endl;
}

#include <cmath>
void _131_() {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;

    long long min_baisuu;
    for (long long i = min(c, d); i <= c * d; ++i) {
        if (i % c == 0 && i % d == 0) {
            min_baisuu = i;
            cout << min_baisuu << endl;
            break;
        }
    }

    long long c_baisuu_num = 0;
    long long d_baisuu_num = 0;
    long long cd_baisuu_num = 0;

    c_baisuu_num = abs(a / c - b / c);
    d_baisuu_num = abs(a / d - b / d);
    cd_baisuu_num = abs(a / min_baisuu - b / min_baisuu);

    cout << abs(a - b) + 1 - (c_baisuu_num + d_baisuu_num - cd_baisuu_num) << endl;
}

void _131__() {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;

    long long min_baisuu = 0;
    long long max_yakusuu = 0;
    
    long long amari = 0;
    long long bigger = max(c, d);
    long long smaller = min(c, d);
    while (true) {
        amari = bigger % smaller;
        if (amari == 0) {
            max_yakusuu = smaller;
            break;
        }
        bigger = smaller;
        smaller = amari;
    }
    // cout << max_yakusuu << endl;
    min_baisuu = c * d / max_yakusuu;

    long long b_num = b - b / c  - b / d + b / min_baisuu;
    long long a_num = a - 1 - (a - 1) / c - (a - 1) / d + (a - 1) / min_baisuu;
    // cout << b_num << " " << a_num << endl;
    cout << b_num - a_num << endl;
}

#include <vector>
#include <algorithm>
void _132() {
    int n;
    cin >> n;
    vector<int> d;
    int tmp;
    for (int i = 0; i < n; ++i)  {
        cin >> tmp; 
        d.push_back(tmp);
    }
    sort(d.begin(), d.end());
    
    if (d.size() % 2 == 1) {
        cout << 0 <<endl;
        return;
    }
    int half_index = d.size() / 2 - 1;
    cout << d[half_index + 1] - d[half_index] << endl;

    

}

void _133() {
    int l, r;
    cin >> l >> r;

    if (r - l > 2019) {
        cout << 0 << endl;
        return;
    }
    int min_mod = 2019;
    long long multi;
    long long mod;
    for (int i = l; i <=r; ++i) {
        for (int j = i + 1; j  <= r; ++j) {
            multi = (long long)i * j;
            mod = multi % 2019;
            // cout << i << " " << j << " " << mod <<endl;
            if (mod < min_mod) {
                min_mod = mod;
                // cout << min_mod << endl;
            }
        }
    }
    cout << min_mod << endl;
}

int main()
{
    _133();
    return 0;
}



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

  int main() {
    vector<string> A(7);
    A={"SUN","MON","TUE","WED","THU","FRI","SAT"};
    string S;
    cin >> S;

    for(int i=0; i<7; i++){
      if(A.at(i)==S){
        cout << 7-i << endl;
        break;
    }
    }
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
using namespace std;
int main(){
	string s;
	cin>>s;
	if(s=="++--"||s=="+-+-"||s=="+--+"||s=="-++-"||s=="--++"||s=="-+-+")cout<<0<<endl;
	else if(s=="+++-"||s=="++-+"||s=="+-++"||s=="-+++")cout<<2<<endl;
	else if(s=="++++")cout<<4<<endl;
	else if(s=="----")cout<<-4<<endl;
	else cout<<-2<<endl;
} 


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define IN(x,c) (find(c.begin(),c.end(),x) != (c).end())
#define REP(i,n) for (int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define INIT(a,v) memset(a,v,sizeof(a))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }

#define SPC << " " <<
#define DEBUG(x) cerr << #x << " = "; cerr << x << endl;
#define DEBUG_ITER(x) cerr << #x << " = "; for (auto _ : x) cerr << _ << ' '; cerr << endl;

typedef pair<int,int> PII;
typedef long long int64;
typedef vector<int> VI;

int main() {
	//freopen("test.in","r",stdin);
	int tests;
    scanf("%d",&tests);
    REP (test,tests) {
    	int n;
		scanf("%d",&n);
		VI k(n), l(n), r(n);
		vector<VI> f,b;
		int64 sc=0;
		REP (i,n) {
			scanf("%d %d %d",&k[i],&l[i],&r[i]);
			sc+=min(l[i],r[i]);
			if (l[i]>r[i]) f.PB({k[i],l[i]-r[i]});
			if (l[i]<r[i]) b.PB({k[i],r[i]-l[i]});
		}
		int j;
		sort(ALL(f)); reverse(ALL(f));
		priority_queue<int> pqf;
		j=0;
		for (int i=f.size();i>=1;i--) {
			while (j<f.size() && f[j][0]>=i) {
				pqf.push(f[j][1]);
				j++;
			}
			if (!pqf.empty()) {
				sc+=pqf.top();
				pqf.pop();
			}
		}
		sort(ALL(b));
		priority_queue<int> pqb;
		j=0;
		for (int i=f.size()+1;i<=n;i++) {
			while (j<b.size() && b[j][0]<i) {
				pqb.push(b[j][1]);
				j++;
			}
			if (!pqb.empty()) {
				sc+=pqb.top();
				pqb.pop();
			}
		}
		cout << sc << '\n';
    }
	return 0;
}


Measured memory_bytes: 17448
a1=list(map(int,input().split()))
a2=list(map(int,input().split()))
a3=list(map(int,input().split()))

n=int(input())
for i in range (n):
  num=int(input())
  if num in a1:
  	a1[a1.index(num)]='O'
  if num in a2:
  	a2[a2.index(num)]='O'
  if num in a3:
  	a3[a3.index(num)]='O'
if a1[0] == 'O' and a1[1] == 'O' and a1[2] == 'O':
  print('Yes')
elif a2[0] == 'O' and a2[1] == 'O' and a2[2] == 'O':
  print('Yes')
elif a3[0] == 'O' and a3[1] == 'O' and a3[2] == 'O':
  print('Yes')
elif a1[0] == 'O' and a2[0] == 'O' and a3[0] == 'O':
  print('Yes')
elif a1[1] == 'O' and a2[1] == 'O' and a3[1] == 'O':
  print('Yes')
elif a1[2] == 'O' and a2[2] == 'O' and a3[2] == 'O':
  print('Yes')
elif a1[0] == 'O' and a2[1] == 'O' and a3[2] == 'O':
  print('Yes')
elif a1[2] == 'O' and a2[1] == 'O' and a3[0] == 'O':
  print('Yes')
else:
  print('No')

Measured memory_bytes: 61836
#include <bits/stdc++.h>
#include <numeric>
typedef long long Int;
#define rep(i,a,b) for(Int i=a;i<b;++i)
#define rrep(i,a,b) for(Int i=a;i>=b;--i)
using namespace std;

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

    Int N, K; cin >> N >> K;
    vector<Int> h(N);
    rep(i, 0, N) cin >> h[i];
    
    vector<Int> dp(N, 0);

    rep(i, 1, N) {
        dp[i] = dp[i-1] + abs(h[i]-h[i-1]);
        if (K == 1) continue;

        for(int k = 1; i-k >= 0 and k <= K; ++k) {
            dp[i] = min(dp[i], dp[i-k] + abs(h[i]-h[i-k]));
        }
    }

    cout << dp[N-1] << endl;
}

Measured memory_bytes: 1792
#include <iostream>

int h, w, h1, w1; 
int kekka;
int Calculation(int h, int w, int h1, int w1);
int main()
{
	std::cin >> h >> w;
	std::cin >> h1 >> w1;
	Calculation(h, w, h1, w1);
}
int Calculation(int h, int w, int h1, int w1)
{
	if(h == h1 && w == w1)
	{
		std::cout << 0;
	}else
	{
		kekka = h * w - w * h1 - ((h - h1)*w1);
		std::cout << kekka;
	}
}

Measured memory_bytes: 256
using namespace std;
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 200010
#define ll long long
int n;
int a[N];
ll k[N],b[N];
bool eql(ll d,ll k,ll b){
	ll s=1;
	for (;k && d<=b && s*d<=b;k>>=1,d*=d)
		if (k&1)
			s*=d;
	return k==0 && s==b;
}
bool judge(int d){
	int tp=0;
	k[0]=b[0]=0;
	k[tp=1]=a[1],b[1]=0;
	for (int i=2;i<=n;++i)
		if (a[i-1]<a[i]){
			if (b[tp]==0)
				k[tp]+=a[i]-a[i-1];
			else
				k[++tp]=a[i]-a[i-1],b[tp]=0;
		}
		else{
			int t=a[i-1]-a[i];
			while (t){
				if (t>=k[tp])
					t-=k[tp--];
				else{
					k[tp]-=t;
					for (;b[tp] && t;--t)
						b[tp]/=d;
					t=0;
				}
			}
			b[tp]++;
			ll s=0;
			for (;tp>=1 && eql(d,k[tp],b[tp]);--tp){
				b[tp-1]++;
				b[tp]=0;
				s+=k[tp];
			}
			if (tp==0)
				return 0;
			if (s){
				k[++tp]=s;
				b[tp]=0;
			}
		}
	return 1;
}
int main(){
	scanf("%d",&n);
	bool ok1=1;
	for (int i=1;i<=n;++i)
		scanf("%d",&a[i]),ok1&=(a[i-1]<a[i]);
	if (ok1){
		printf("1\n");
		return 0;
	}
	int l=2,r=1000000000,res=r;
	while (l<=r){
		int mid=l+r>>1;
		if (judge(mid))	
			r=(res=mid)-1;
		else
			l=mid+1;
	}
	printf("%d\n",res);
	return 0;
}

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

int main() {
    int N;
    cin >> N;
    
    vector<int> vec(N);
    
    for (int i = 0; i < N; i++) {
        cin >> vec.at(i);
    }
    
    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());
    
    int sum = 1;
    for (int i = 1; i < N; i++) {
        if (vec.at(i-1) != vec.at(i)) sum++;
    }
    
    cout << sum << endl;
}


Measured memory_bytes: 256
#include<stdio.h>

int main(void)
{
	int m=1,f=1,r=1,N;
	
	for(;m!=-1||f!=-1||r!=-1;){
		
		scanf("%d %d %d",&m,&f,&r);
		N=0;
		
		if(m==-1||f==-1){
			
			if(m==-1&&f==-1&&r==-1){}
			
			else printf("F");
			
		}
		N=m+f;
		if(m!=-1&&f!=-1){
			
			if(N>=80){
				
				printf("A");
			}
			else if(N>=65&&N<=80){
				
				printf("B");
				
			}
			else if(N>=50&&N<65){
				
				printf("C");
				
			}
			else if(N>=30&&N<50){
				
				if(r>=50){
					
					printf("C");
					
				}
				else printf("D");
				
			}
			else if(N<30){
				
				printf("F");
				
			}
		}
		if(m!=-1||f!=-1||r!=-1){
			printf("\n");
		}
		
	}
	return 0;
}

Measured memory_bytes: 2680
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=0;(i)<(n);i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define MOD 1000000007
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define ALL(v) v.begin(), v.end()
typedef long long ll;

int main(){
    int a,b,c;
    cin >> a >> b >> c;
    
    if(a==b and b==c)cout << "No" << endl;
    else if (a!=b and b!=c and c!=a)cout << "No" << endl;
    else cout << "Yes" << endl;


    
    return 0;
}

Measured memory_bytes: 3640