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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long A[] = new long[N];
        
        for(int i = 0; i < N; i++) {
            A[i] = sc.nextLong();
        }
        
        long ans = 0;
        int r = 0;
        long sum = 0;
        for( int l = 0; l < N; l++ ) {
            while( r < N && (sum + A[r]) == (sum ^ A[r]) ) {
                sum += A[r];
                r++;
            }
            
            ans += r - l;
            sum -= A[l];
        }
        
        System.out.println( ans );
    }
}


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

int main(void){
    ll K,L;
    cin >> K;
    if((K%2) == 0 || (K%5) == 0) {
        cout << "-1" << endl;
        return 0;
    }else L = 9*K;

    ll seven=0;
    for(int cnt=1; cnt <= K; ++cnt){
        seven = seven * 10 + 7;
        seven %= K;
        if(seven == 0){
            cout << cnt << endl;
            return 0;
        }
    }
    cout << "-1" << endl;
    return 0;
}

Measured memory_bytes: 3624
import java.util.*;
public class Main{
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                int a = sc.nextInt();
                if(Math.abs(a - sc.nextInt()) > Math.abs(a - sc.nextInt())) System.out.println("B");
                else System.out.println("A");
        }
}//0=48
 //a=97 a -> +26 = 123
//鶏になれ

Measured memory_bytes: 35652
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<string.h>
#include<algorithm>
#include <cmath>

using namespace std;

int K,i,j,k,a,b,c,ans;

char S[10];

int gcd(int n,int m)
{
  int a;
  if(n<m)
  {
    a=m;
    m=n;
    n=a;
  }
  
  a=n%m;
  while(a!=0)
  {
    n=m;
    m=a;
    a=n%m;
  }
  
  return m;
  
}


int main()
{
  cin >>K;
  
  ans=0;
  
  for(a=1;a<=K;a++)
  {
    for(b=1;b<=K;b++)
    {
      for(c=1;c<=K;c++)
      {
        ans+=gcd(gcd(a,b),c);
        
      }
    }
  }
  
  cout <<ans;
  
}

Measured memory_bytes: 3644
#include<stdio.h>
int main(){
	int n,k,q,i,j;
	scanf("%d %d %d",&n,&k,&q);
	int a[n],b[q];
	for(i=0;i<n;i++){
		a[i]=k;
	}
	for(i=0;i<q;i++){
		scanf("%d",&b[i]);
	}
	for(i=0;i<q;i++){
		a[b[i]-1]=a[b[i]-1]+1;
	}
	for(j=0;j<n;j++){
		a[j]=a[j]-q;
		if(a[j]>0)
		printf("Yes\n");
		else
		printf("No\n");
	}
	return 0;
} 

Measured memory_bytes: 1280
#include<iostream>
using namespace std;
 
int n,m,s[2][100];
 
int calc(int x, int y)
{
int s1,s2,i;
s1=s2=0;
for(i=0;i<n;i++)
 if(i==x)s1+=s[1][y];
 else s1+=s[0][i];
for(i=0;i<m;i++)
 if(i==y)s2+=s[0][x];
 else s2+=s[1][i];
if(s1==s2)return s[0][x]+s[1][y];
else return -1;
}
 
int main()
{
while(cin>>n>>m,n|m){
int i,j,t,x,y;
bool f;
for(i=0;i<n;i++)cin>>s[0][i];
for(j=0;j<m;j++)cin>>s[1][j];
f=1;
for(i=0;i<n;i++)for(j=0;j<m;j++){
 t=calc(i,j);
 if(t!=-1){
  if(f){x=i;y=j;f=0;}
  else if(t<s[0][x]+s[1][y]){x=i;y=j;}
 }
}
if(f)cout<<"-1"<<endl;
else cout<<s[0][x]<<" "<<s[1][y]<<endl;
}
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
int n, k, q;
int a[100005];
int main()
{
    scanf("%d%d%d", &n, &k, &q);
    for (int i = 1; i <= q; i++)
    {
        int x;
        scanf("%d", &x);
        a[x]++;
    }
    for (int i = 1; i <= n; i++)
        if (q - a[i] >= k)
            puts("No");
        else
            puts("Yes");
    return 0;
}

Measured memory_bytes: 1024
program sample
    implicit none
  
    integer(8) :: i,j,m,n,k
    integer(8),allocatable :: x(:),y(:)
  
    read(*,*) n,k
    allocate(x(n))
    read(*,*)x
    do i=1,n-k
        if(x(i)<x(k+i))then
            write(*,*)'Yes'
        else
            write(*,*)'No'
        end if
    end do
    
    stop
end program sample
  



Measured memory_bytes: 4652
/*input 
4
1
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
const ld PI = 4*atan((ld)1);

ll N, S, d;
vl fac;

ll digsum(ll n, ll b) {
	ll ret = 0;
	ll m = n; 
	while(m) {
		ret += (m % b);
		m /= b;
	}
	return ret;
}

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

	cin >> N >> S;

	ll d = N - S;

	if (d == 0) {
		cout << (N + 1) << '\n';
		return 0;
	}

	if (d < 0) {
		cout << -1 << '\n';
		return 0;
	}

	for (ll i = 1;; i++) {
		if (d % i == 0) fac.pb(i);
		if (i * i > d) break;
	}

	F0Rd(i, sz(fac)) fac.pb(d / fac[i]);

	F0R(i, sz(fac)) {
		if (digsum(N, fac[i] + 1) == S) {
			cout << (fac[i] + 1) << '\n';
			return 0;
		}
	}
	cout << -1 << '\n';
}


Measured memory_bytes: 384
let rec gcd a b = if b = 0 then a else gcd b (a mod b)
let lcm a b = a / gcd a b * b
let n = Scanf.scanf "%d" (fun x -> x)
let xs = Array.init n (fun _ -> Scanf.scanf " %d" (fun x -> x))
let () = Array.fold_left lcm 1 xs |> Printf.printf "%d\n"

Measured memory_bytes: 4480
#include <stdio.h>
int main(void){
    // Your code here!
long long int a, b, c, k;
scanf("%lld %lld %lld %lld", &a, &b, &c, &k);
if (k <= a )
printf("%lld", k);
else if (k <= a + b)
printf("%lld", a);
else
    printf("%lld", a - (k - a - b));



    return (0);
}


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

int main(){
    int K;
    string s1;
     cin >> K >> s1;
    int slength;
    slength = s1.size(); 
    string s2;
    s2 = "...";
    if(slength <= K){
        cout << s1 << endl;
    }else{
        cout << s1.substr(0,K) + s2 << endl;
    }
    return 0;
}

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

int main() {
	int n;
	cin >> n;
	vector<string> s(n);
	for(int i = 0; i < n; i++) {
		cin >> s.at(i);
	}
	sort(s.begin(), s.end());
	int count = 1;
	for(int i = 0; i < n -1; i++) {
		if(s.at(i) != s.at(i + 1)) {
			count++;
		}
	}
	cout << count << endl;
}

Measured memory_bytes: 9600
#include<stdio.h>
int main(){
	
	int K,S,X,Y,Z,count=0;
	scanf("%d%d",&K,&S);
	
	for(Z=0;Z<=K;Z++){
		
		for(Y=0;Y<=K&&Y<=S-Z;Y++){
		
		
			if(0<=S-Z-Y&&S-Z-Y<=K){
		count++;
			}
		
		}
	}
	
	printf("\n%d\n\n",count);
	
	return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
int main() {
    int V, E;
    cin >> V >> E;
    vector<vector<int>> G(V);
    for (int i = 0; i < E; i++) {
        int a, b;
        cin >> a >> b;
        G[a - 1].push_back({b - 1});
        G[b - 1].push_back({a - 1});
    }
    vector<bool> seen(V, false);  // 既に見たことがある頂点か記録する
    vector<int> c(V);   //道しるべ
    queue<int> que;
    que.emplace(0);  // 0から探索する
    seen[0] = true;
    while (que.size() != 0) {     // 幅優先探索
        int state = que.front();  // 現在の状態
        que.pop();
        for (auto next : G[state]) {
            if (!seen[next]) {  // 未探索の時のみ行う
                seen[next] = true;
                c[next] = state;
                que.emplace(next);  //次の状態をqueueへ格納
            }
        }
    }
    cout << "Yes" << endl;
    for (int i = 1; i < V; i++) {
        cout << c[i] + 1 << endl;
    }
    return 0;
}


Measured memory_bytes: 10072
fun readString() = readLine()!!
fun readInt() = readLine()!!.toInt()
fun readLong() = readLine()!!.toLong()
fun readIntList() = readLine()!!.split(" ").map(String::toInt)
fun readLongList() = readLine()!!.split(" ").map(String::toLong)
fun <T> Array<Array<T>>.print() {
    this.forEach {
        println(it.joinToString(" "))
    }
}

fun main() {
    val (n, k) = readIntList()
    var res = 0
    var num = n
    while (num > 0) {
        res++
        num /= k
    }
    println(res)
}

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

int main() {
    string s;
    int q;
    cin >> s >> q;

    bool r = false;
    rep(i, q) {
        int t;
        cin >> t;
        if (t == 1) {
            r = !r;
        } else {
            int f;
            char c;
            cin >> f >> c;
            if (f == 1) {
                r ? s += c : s = c + s;
            } else {
                r ? s = c + s : s += c;
            }
        }
    }

    if (r) reverse(s.begin(), s.end());
    cout << s << endl;
    return 0;
}


Measured memory_bytes: 4676
#include"bits/stdc++.h"
#include<cassert>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
const int mod = 1000000007;
const int inf = 1ll << 61;
const int logN = 18;
typedef pair<int, int> P;
typedef pair<int, P>PP;
struct edge { int to, cost, color,id; };
struct Q { int id,color,cost; };
vector<edge>G[100006];
vector<int>clr[100006]; int sclr[100006],zclr[100006];
vector<Q>query1[100006],query2[100006];
int id[200006], vs[200006], depth[100006], es[200006];
int esum[200006];
int ans1[100006],ans2[100006],vst[100006];
int A[100006], B[100006], C[100006], D[100006];
int QX[100006], QY[100006], QU[100006], QV[100006];
int parent[20][100006];

void dfs(int v, int par, int d, int &k) {
	id[v] = k;
	vs[k] = v;
	k++;
	for(edge e: G[v]) {
		if (e.to != par) {
			esum[k] = e.cost;
			es[e.id * 2] = k;
			dfs(e.to, v, d + 1, k);
			vs[k] = v;
			esum[k] = -e.cost;
			es[e.id * 2 + 1] = k;
			k++;
		}
	}
}

void dfs1(int v, int p, int d) {
	parent[0][v] = p;
	depth[v] = d;
	for (edge e : G[v]) {
		if (e.to != p)dfs1(e.to, v, d + 1);
	}
}
void init(int V) {
	dfs1(0, -1, 0);
	for (int k = 0; k + 1 < logN; k++) {
		rep(v, V) {
			if (parent[k][v] < 0)parent[k + 1][v] = -1;
			else parent[k + 1][v] = parent[k][parent[k][v]];
		}
	}
}
int lca(int u, int v) {
	if (depth[u] > depth[v])swap(u, v);
	rep(k, logN) {
		if ((depth[v] - depth[u]) >> k & 1) {
			v = parent[k][v];
		}
	}
	if (u == v)return u;

		for (int k = logN-1; k >= 0; k--) {
			if (parent[k][u] != parent[k][v]) { 
				u = parent[k][u]; v = parent[k][v];
			}
		}
		return parent[0][u];
}

signed main() {
	int n, q; cin >> n >> q;
	rep(i, n - 1) {
		int a, b, c, d; cin >> a >> b >> c >> d;
		a--; b--;
		G[a].push_back({ b,d,c,i });
		G[b].push_back({ a,d,c,i });
		A[i] = a; B[i] = b; C[i] = c; D[i] = d;
	}
	init(n);
	int start = 0; dfs(0, -1, 0, start);
	rep(i, 2 * n)esum[i + 1] += esum[i];
	rep(i, q) {
		int x, y, u, v; cin >> x >> y >> u >> v;
		u--; v--;
		query1[u].push_back({ i,x,y });
		query1[v].push_back({ i,x,y });
		query2[lca(u, v)].push_back({ i,x,y });
		QX[i] = x; QY[i] = y; QU[i] = u; QV[i] = v;
	}
	vector<P>V;
	rep(i, n + n - 2)V.push_back(P(es[i], i));
	sort(V.begin(), V.end());
	rep(i, 2 * n) {
		int now = vs[i];
		if (vst[now] == 0) {//入った
			for (Q j : query1[now]) {
				int ID = j.id;
				ans1[ID] += esum[i];
				ans1[ID] -= sclr[j.color];
				ans1[ID] += zclr[j.color] * j.cost;
			}
			for (Q j : query2[now]) {
				int ID = j.id;
				ans2[ID] += esum[i];
				ans2[ID] -= sclr[j.color];
				ans2[ID] += zclr[j.color]*j.cost;
			}
			vst[now]++;
		}
		if (i < 2 * n - 2) {
			int S = V[i].second;
			if (S % 2==0) {
				zclr[C[S / 2]]++;
				sclr[C[S / 2]] += D[S / 2];
			}
			else {
				zclr[C[S / 2]]--;
				sclr[C[S / 2]] -= D[S / 2];
			}
		}
	}

	rep(i, q)cout << ans1[i]-2*ans2[i]<<endl;

}

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

#define rep(i,n) for (int i = 0; i< (n); ++i)
#define vvec(m,n) vector<vector<int>> (int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

using ll = long long;

int main(){
    string a ,b, c;
    cin >> a >> b >> c;
    if(a.at(a.size()-1) == b.at(0) && b.at(b.size()-1) == c.at(0)){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
}

Measured memory_bytes: 256
#!/usr/bin/env python3
def g(n):
    acc = 0
    if (n % 4) >= 2:
        acc ^= 1
    for k in range(1, 60):
        q = n % (2 ** (k + 1))
        if q > 2 ** k and q % 2 == 1:
            acc ^= 2 ** k
    return acc

def main():
    a, b = map(int, input().split())
    print(g(b + 1) ^ g(a))

if __name__ == '__main__':
    main()


Measured memory_bytes: 3060
using System;
using System.Linq;
using System.Collections.Generic;

using E = System.Linq.Enumerable;

public class Program
{
    public static void Main()
    {
        var n = int.Parse(Console.ReadLine());
        var rs = new int[n];
        var ls = new int[rs.Length];
        for (int i = 0; i < rs.Length; i++)
        {
            var ab = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
            rs[i] = ab[0];
            ls[i] = ab[1];
        }

        Array.Sort(ls, rs);
        var t = 0L;
        for (int i = 0; i < rs.Length; i++)
        {
            t += rs[i];
            if (t > ls[i])
            {
                Console.WriteLine("No");
                return;
            }
        }
        Console.WriteLine("Yes");
    }
}


Measured memory_bytes: 18784
#include<iostream>
#include<algorithm>
 
using namespace std;
 
int main(){
	long long n, a[100009];
  	cin >> n;
  	for(int i=0;i<n;i++)cin >> a[i];
  	sort(a, a+n);
  	int cnt=1;
  	long long sm=a[0];
  	for(int i=1;i<n;i++){
    	if(sm*2<a[i])cnt=0;
      	cnt++;
      	sm+=a[i];
    }
  	cout << cnt << endl;
}

Measured memory_bytes: 1024
def main():
    N = int(input())
    S = list(str(input()))
    sum = S.count("R") * S.count("G") * S.count("B")
    for i in range(0, N-2):
        si = S[i]
        for j in range(i+1, N-1):
            sj = S[j]
            k = 2*j -i  # Nを超える
            if k < N:
                sk = S[k]
                #if j - i != k -j and si != sj and si != sk and sj != sk:
                if si != sj and si != sk and sj != sk:
                    sum -= 1
    print(sum)

    # 提出 #11871188


main()

Measured memory_bytes: 67972
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
#include "cstdlib"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define st first
#define nd second
#define exp 1e-8
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int MAXN = 1e5 + 5;
int n, a, b, c = 0, pass= 0;
char s[MAXN];
int main(int argc, char const *argv[])
{
	scanf("%d%d%d%s", &n, &a, &b, s);
	for(int i = 0; i < n; ++i) {
		if(s[i] == 'c') printf("No\n");
		else if(s[i] == 'a') {
			if(pass < a + b) printf("Yes\n"), pass++;
			else printf("No\n");
		}
		else {
			c++;
			if(pass < a + b && c <= b) printf("Yes\n"), pass++;
			else printf("No\n");
		}
	}
	return 0;
}

Measured memory_bytes: 768
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring> 
#include <cmath> 
#include <algorithm> 
using namespace std; 
const int Mod = 1e9 + 7; 
const int MAX_N = 2e5 + 5; 
int fpow(int x, int y) { 
	int res = 1; 
	while (y) {
		if (y & 1) res = 1ll * res * x % Mod; 
		x = 1ll * x * x % Mod; 
		y >>= 1; 
	} 
	return res; 
} 
int N = 2e5, M, A, B; 
int fac[MAX_N], ifc[MAX_N]; 
int C(int n, int m) { 
	if (n < m || n < 0 || m < 0) return 0; 
	return 1ll * fac[n] * ifc[m] % Mod * ifc[n - m] % Mod; 
} 
int solve(int x, int y, int u, int v) { 
	return C(u - x + v - y, u - x); 
} 
int main () { 
	fac[0] = 1; for (int i = 1; i <= N; i++) fac[i] = 1ll * fac[i - 1] * i % Mod; 
	ifc[N] = fpow(fac[N], Mod - 2); 
	for (int i = N - 1; ~i; i--) ifc[i] = 1ll * ifc[i + 1] * (i + 1) % Mod; 
	cin >> N >> M >> A >> B;
	int ans = 0; 
	for (int i = N - A, j = B + 1; i >= 0 && j <= M; i--, j++) { 
		ans = (ans + 1ll * solve(1, 1, i, j) * solve(i, j, N, M)) % Mod; 
	} 
	printf("%d\n", ans); 
    return 0; 
} 

Measured memory_bytes: 1792
#include <stdio.h>

int main(int argc, char **argv) {
  
  int a,b;
  
  scanf("%d %d", &a, &b);
  
  if(((a&0b1) == 1) && (b&0b1 == 1))
    printf("Odd");
  else
    printf("Even");
  
  return 0;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
int main(){long long a,b,c,d,p=0;
      cin>>a>>b>>c>>d;
      if(a>=0&&d<=0)p=a*d;
        else if(b<=0&&c>=0)p=b*c;
      else if(a*c<b*d)p=b*d;
     else p=a*c;
cout<<p<<endl;}

Measured memory_bytes: 3640
x, y, a, b, c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p = sorted(p, reverse = True)[:x]
q = sorted(q, reverse = True)[:y]
r.sort(reverse = True)

# print(p)
# print(q)
# print(r)

almost_ans = sorted(p + q)
# print("almost_ans", almost_ans)

colored_ind = 0
uncol_ind = 0
while colored_ind < len(almost_ans) and uncol_ind < len(r) and almost_ans[colored_ind] < r[uncol_ind]:
	almost_ans[colored_ind] = r[uncol_ind]
	colored_ind += 1
	uncol_ind += 1

print(sum(almost_ans))


Measured memory_bytes: 107884
n,Ma,Mb=map(int,input().split())
inf=float("inf")
dp=[[[inf]*401 for _ in range(401)] for _ in range(n+1)]
dp[0][0][0]=0

l=[tuple(map(int,input().split())) for _ in range(n)]

for i in range(1,n+1):
    for j in range(401):
        for k in range(401):
            a,b,c=l[i-1]
            dp[i][j][k]=min(dp[i][j][k],dp[i-1][j][k])
            if j-a>=0 and k-b>=0:
                dp[i][j][k]=min(dp[i][j][k],dp[i-1][j-a][k-b]+c)

ans=inf
for i in range(1,1+400//max(Ma,Mb)):
    ans=min(ans,dp[n][Ma*i][Mb*i])

print(ans if ans!=inf else -1)

Measured memory_bytes: 133596
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define inf1 INT_MAX
#define inf2 LLONG_MAX
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define sbit __builtin_popcount
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)
#define rit(it,x) for(auto it=x.begin();it!=x.end();it++)
#define cases(t) int t; cin>>t; rep(casn,1,t+1)
#define fill(a,b) memset(a,b,sizeof(a))
#define dbg(x) cerr<<#x<<" "<<x<<endl;
#define dbga(arr,n) cerr<<#arr<<": "; rep(tt,0,n)cerr<<arr[tt]<<" "; cerr<<endl;
#define infile ifstream fin; fin.open("input.txt")
#define outfile ofstream fout; fout.open("output.txt")
#define vec vector<int>
#define pii pair<int,int>
#define plii pair<ll,int>
#define pll pair<ll,ll>
#define p_queue priority_queue< plii,vector< plii >,greater< plii > > 
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define sp(x) setprecision(x)
#define si(x) scanf("%d",&x)
#define sc(x) scanf("%c",&x)
#define sl(x) scanf("%lld",&x)
#define sst(x) scanf("%s",x)
#define pl(x) printf("%lld\n",x)
#define pi(x) printf("%d\n",x)
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search

inline ll binex(ll a,ll b)
{
    ll ans=1,temp=a%mod;
    while(b!=0){
        if(b&1) ans=(ans*temp)%mod;
        temp=(temp*temp)%mod;
        b=b>>1;
    }
    return ans;
}

inline ll solve(int n,int k,int arr[])
{
    ll dp[k+1][n];
    rep(i,0,n) dp[0][i]=1;
    rep(i,0,k+1){
        if(i<=arr[0]) dp[i][0]=i+1;
        else dp[i][0]=arr[0]+1;
    }
    rep(j,1,n){
        rep(i,1,k+1){
            if(i>arr[j]) dp[i][j]=dp[i][j-1]-dp[i-arr[j]-1][j-1];
            else dp[i][j]=dp[i][j-1];
            dp[i][j]=(dp[i][j]+2*mod+dp[i-1][j])%mod;
        }
    }
    
    /*rep(i,0,k+1){
        rep(j,0,n) cerr<<dp[i][j]<<" "; cerr<<endl;
    }*/
    
    if(k!=0) return (dp[k][n-1]-dp[k-1][n-1]+mod)%mod;
    else return dp[k][n-1];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,k; cin>>n>>k;
    int arr[n];
    rep(i,0,n) cin>>arr[i];
    cout<<solve(n,k,arr)<<endl;
}

Measured memory_bytes: 78336
import sys
def input():
    return sys.stdin.readline()[:-1]
N=int(input())
pr=10**9+7
a=0
l=[0]*(2*10**5)
C=[int(input())-1 for i in range(N)]
for i in range(N-1):
    if C[i]==C[i+1]:
        continue
    a,l[C[i]]=(l[C[i]]+a)%pr,(l[C[i]]+a+1)%pr
a+=l[C[-1]]
print((a+1)%pr)

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

int n,k;
long long a[200002];
priority_queue<pair<int,pair<int,int> > >pq;

bool ck(long long x){
	int tmp = 0;
	for(int i=0;i<n;i++){
		tmp += ceil((double)a[i]/x);
		tmp--;
	}

	if(tmp<=k)return(true);
	return(false);
}

int main(){

	/*
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>a[i];
		pq.push(make_pair(a[i],make_pair(a[i],1)));
	}

	while(cnt<k){
		pair<int,pair<int,int> >p = pq.top();pq.pop();
		pair<int,pair<int,int> >np = pq.top();
		if(p.first == 1)break;

		cnt = cnt - (p.second.second-1);
		int tmp = (p.second.first/np.first)+1;
		while(k < cnt+(tmp-1))tmp=k-cnt+1;	
		cnt = cnt+(tmp-1);

		p.second.second = tmp;
		p.first = ceil((double)p.second.first/p.second.second);
		pq.push(p);

	}

	cout<<pq.top().first<<endl;
	*/

	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}

	long long left = 1,right = 1000000000;
	long long ans = right;
	while(left <= right){
		long long mid = (left+right)/2;
		if(ck(mid)==true){
			ans = mid;
			right = mid-1;
		}
		else{
			left = mid+1;
		}
	}

	cout<<ans<<endl;
	return(0);
}

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

using namespace std;

int main() {
  string s;
  cin >> s;
  int length = s.size();
  int odd_0 = 0, odd_1 = 0, even_0 = 0, even_1 = 0;
  for ( int i = 0; i < length; i++ ) {
    if ( i % 2 == 0 ) {
      if ( s[i] == '0' )
        even_0++;
      else
        even_1++;
    }
    else
    {
      if ( s[i] == '0' )
        odd_0++;
      else
        odd_1++;
    }
  }
  cout << min( even_0 + odd_1, even_1 + odd_0 ) << endl;
  return 0;
}

Measured memory_bytes: 512
/*Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.

My little horse must think it queer
To stop without a farmhouse near
Between the woods and frozen lake
The darkest evening of the year.

He gives his harness bells a shake
To ask if there is some mistake.
The only other sound’s the sweep
Of easy wind and downy flake.

The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.*/

#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
const int maxn= 100010;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout<<setprecision(20);
    cout<<fixed;
    int w=0,h=0,x=0,y=0;
    cin>>w>>h>>x>>y;
    cout<<((double)(w)*(double)(h))/2<<" ";
    if(2*x==w&&2*y==h)
        cout<<"1";
    else
        cout<<"0";
    cout<<endl;
}


Measured memory_bytes: 256
N,M,X,Y = gets.strip.split.map(&:to_i)
xrow = gets.strip.split.map(&:to_i).sort.reverse
yrow = gets.strip.split.map(&:to_i).sort

if xrow[0] < yrow[0]
  if yrow[0] > X + 1
    if xrow[0] < Y
      puts 'No War'
    else puts 'War'
    end
  else puts 'War'
  end
else
  puts 'War'
end

Measured memory_bytes: 1788
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
using namespace std;
int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0' || c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0' && c<='9'){
		x=(x<<3)+(x<<1)+c-48;
		c=getchar();
	}
	return x*f;
}
int main(){
	string s;
	int sum=0,ans=0;
	cin>>s;
	for(register int i=0;i<s.size();i++){
		if(s[i]=='A' || s[i]=='C' || s[i]=='G' || s[i]=='T')sum++;
		else sum=0;
		ans=max(ans,sum);
	}
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;


int main()
{
	int a[15],n,m;
	for(int i=1;i<=7;i++)
	{
		if(i==2)a[i]=3;	
		else if(i%2)a[i]=1;
		else a[i]=2;
	}
	for(int i=8;i<=12;i++)
	{
		if(i%2)a[i]=2;
		else a[i]=1;
	}
	cin>>n>>m;
	if(a[n]==a[m])cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	return 0;
}

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

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

		Scanner sc= new Scanner(System.in);

		while(true) {
			int a = sc.nextInt();
			String op = sc.next();
			int b = sc.nextInt();

			if(op.equals("?")) break;
			if(op.equals("+")) System.out.println(a + b);
			if(op.equals("-")) System.out.println(a - b);
			if(op.equals("*")) System.out.println(a * b);
			if(op.equals("/")) System.out.println(a / b);


		}

	}
}


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

#define int long long

typedef long long ll;
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}

int L,N;

signed main(){
    int p=0,q=0;
    cin>>L>>N;
    rep(i,N){
        int x,w;
        cin>>x>>w;
        if(x>0)p+=x*w;
        else q+=(-x)*w;
    }

    if(p==q)puts("0");
    else{
        puts("1");
        if(p<q)printf("1 %lld\n",q-p);
        else printf("-1 %lld\n",p-q);
    }
    return 0;
}

Measured memory_bytes: 1152
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
ll dp[3010][3010] = {};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S,T;
    cin >> S >> T;
    ll ns = S.size(), nt = T.size();
    rep(i,0,ns){
        rep(j,0,nt){
            ll v = max({dp[i+1][j], dp[i][j+1], dp[i][j]});
            if(S[i] == T[j]){
                dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 1);
            }else{
                dp[i+1][j+1] = max(dp[i+1][j+1], v);
            }

        }
    }
    // print(dp[ns][nt]);
    ll cs = ns, ct = nt;
    string ans = "";
    while(cs > 0 && ct > 0){
        if(dp[cs][ct] > dp[cs-1][ct] && dp[cs][ct] > dp[cs][ct-1]){
            ans += S[cs-1];
            cs--; ct--;
        }else{
            if(dp[cs][ct] == dp[cs-1][ct]){
                cs--;
            }else{
                ct--;
            }
        }
    }

    reverse(ans.begin(), ans.end());
    print(ans);
    

}

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

using namespace std;

int fun(int n)
{
    int ans =0 ;
    while(n > 0)
    {
        ans += n%10;
        n = n/10;
    }
    return ans;
}

int main()
{
    int n;
    cin >> n;
    int ans = INT_MAX;
    for(int i=1;i<n;i++)
    {
        ans = min(ans,fun(i) + fun(n - i));
    }
    cout << ans << '\n';
    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;
 
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    int B = sc.nextInt();
    
    int addSum = A+B;
    int subSum = A-B;
    int multiSum = A*B;
    
    int result = addSum;
    if (result < subSum){
    	result = subSum;
    }
    
    if(result < multiSum){
    	result = multiSum;
    }
    
    System.out.println(result);
  }
}

Measured memory_bytes: 21844
while True:
    m,f,r = map(int, input().split())
    if m == -1 and f == -1 and r == -1:
        break
    if m == -1 or f == -1:
        print("F")
        continue
    s = m + f
    if s >= 80:
        print("A")
    elif s >= 65:
        print("B")
    elif s >= 50:
        print("C")
    elif s >=30:
        if r >= 50:
            print("C")
        else:
            print("D")
    else:
        print("F")

Measured memory_bytes: 7660
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
using namespace std;
typedef long long ll;

int main(){

    int n; cin >> n;

    int a[4];
    for(int i=0; i<4; i++){
        a[3-i] = n % 10;
        n /= 10;
    }

    // for(int i=0; i<4; i++){
    //     cout << a[i];
    // }

    int ans;
    for(int bit = 0; bit < 8; bit++){
        ans = a[0];
        for(int i=0; i<3; i++){
            if((bit>>i) & 1){
                ans += a[i+1];
            }
            else{
                ans -= a[i+1];
            }
        }
        if(ans == 7){
            cout << a[0];
            for(int i=0; i<3; i++){
                if((bit>>i) & 1){
                   cout << "+" << a[i+1];
                }
                else{
                    cout << "-" << a[i+1];
                }
            }
            cout << "=7";
            break;
        }
    }

    return 0;
}

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


void solve(long long N, long long M, std::vector<long long> H, std::vector<long long> A, std::vector<long long> B){
    long long R[N+1] = {};
    for (int i = 0; i < M; i++)
    {
        R[A[i]] = max(H[B[i]-1], R[A[i]]);
        R[B[i]] = max(H[A[i]-1], R[B[i]]);
    }
    int ans = 0;
    for (int i = 0; i < N; i++)
    {
        ans += H[i] > R[i+1];
    }
    cout << ans << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> H(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&H[i]);
    }
    std::vector<long long> A(M);
    std::vector<long long> B(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&A[i]);
        scanf("%lld",&B[i]);
    }
    solve(N, M, std::move(H), std::move(A), std::move(B));
    return 0;
}


Measured memory_bytes: 6388
#include <iostream>
#include<string>
#include<vector>
#include<math.h>

using namespace std;
struct pre_ { pre_() { cin.tie(nullptr); ios::sync_with_stdio(false); /*cout << fixed << setprecision(12);*/ } } pre__;


bool IsPrime(int num)
{
	if (num < 2) return false;
	else if (num == 2) return true;
	else if (num % 2 == 0) return false;
	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2)
	{
		if (num % i == 0)
		{
			return false;
		}
	}
	return true;
}

int main() {
	vector<bool> flag(100000 + 10, false);
	vector<int> like2017(100000 + 10, 0);
	vector<int> S(100000 + 10, 0);
	int Q, r, l;

	for (int i = 1; i <= 100000; i++) flag[i] = IsPrime(i);

	for (int i = 1; i <= 100000; i++) {
		if (flag[i] && flag[(i + 1) / 2]) like2017[i] = 1;
	}

	for (int i = 1; i <= 100000; i++) {
		S[i] = like2017[i] + S[i - 1];
	}

		cin >> Q;
	for (int i = 0; i < Q; i++) {
		cin >> l >> r;
		
		int ans = S[r] - S[l-1];

		cout << ans << "\n";
	}


	return 0;
}

Measured memory_bytes: 1408
n = input()
# a,b = map(int,input().split())
# A = list(map(int,input().split()))
# An = [list(map(int,input().split())) for i in range(n)]
if n[2] == n[3] and n[4] == n[5]:
    print('Yes')
else:
    print('No')

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

signed main(){
	int n;cin>>n;
	vector<int>a(n);
	for(auto&& w:a)cin>>w;
	int sm=0;
	for(int i=0;i<n;i++){
		int x=a[i];
		int y=a[x-1];
		if(i==y-1)sm++;
	}
	cout<<sm/2<<endl;
}

Measured memory_bytes: 1024
#include <stdio.h>

int main(){
  int a,b,c,x;
  scanf("%d\n%d\n%d\n%d",&a,&b,&c,&x,stdin);

  //500a+100b+50c == x                                                                  
  int count = 0;
  for(int i = 0; i <= a; ++i){
    int	sum = 500 * i;
    if(sum > x)	break;
    for(int j = 0; j <= b; ++j){
      sum = 500 * i + 100 * j;
      if(sum > x) break;
      for(int k = 0; k <= c; ++k){
        sum = 500 * i + 100 * j + 50 * k;
        if(sum == x) count++;
        if(sum > x) break;
      }
    }
  }
  printf("%d",count);
  return 0;
}

Measured memory_bytes: 128
iN,iK = [int(_) for _ in input().split()]
aX = [int(_) for _ in input().split()]
ans = 10**9
iDiff = iN-iK
for i in range(iDiff + 1):
    iL = aX[i]
    iR = aX[iN-iDiff+i-1]
    ans = min(ans ,abs(iL) + abs(iL-iR),abs(iR) + abs(iR-iL))
print(ans)


Measured memory_bytes: 14228
//  cout << char(i+48) << endl;//なぜかaは47と得る時がある。+48で出力もaにできる。
//  cout << char(97) << endl;//アスキーコードでaを出力
// sort(b.begin(), b.end());//bという配列を小さい方からソート
// reverse(b.begin(), b.end());//bという配列をリターン
// 10桁でdouble出力するパターン１ 
// printf("%.10f\n", ma);
// 10桁でdouble出力するパターン２
//cout << fixed << setprecision(10);
//  cout << ma << endl;

/*01 02 03 12 13 23　と６回見ていくパターン 
for(int i=0;i<n-1;i++){
  for(int j=i+1;j<n;j++){
    }
  }
*///vector<vector<int>> data(3, vector<int>(4));//int型の2次元配列(3×4要素の)の宣言
//int64_t a;
//10のi乗pow(10, i);
/*string s; stringでの文字列を数字型に変える方法
  cin >> s;
  rep(i,s.size()-2) {
  int a= (s.at(i)-'0')*100 + (s.at(i+1)-'0')*10+ s.at(i+2) -'0';
    */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  int a,b,c,x,y;
  cin >> a >> b >> c >> x >> y;
  int A,B,C;
  A = a * x + b * y;
  if(x>y) B = c*y*2+a*(x-y);
  else    B = c*x*2+b*(y-x);
  
  if(x>y) C = c*x*2;
  else    C = c*y*2;
  int ma = A;
  ma = min(ma,A);
  ma = min(ma,B);
  ma = min(ma,C);
  cout << ma << endl;
//  cout << A << B << C << endl;
}

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

using namespace std;
#define int long long
 
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int k, n;
  cin >> k >> n;
  vector<int> a(n);
  for(int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  int ans = k;
  for(int i = 1; i < n; i++) {
    ans = min(ans, k - (a[i]-a[i-1]));
  }
  ans = min(ans, a[n-1] - a[0]);
  cout<<ans<<'\n';
  return 0;
}
  


Measured memory_bytes: 4776
import bisect
import sys
#input = sys.stdin.readline
A, B, Q = list(map(int, input().split()))
s = [int(input()) for i in range(A)]
t = [int(input()) for i in range(B)]
s.append(-100000000000);s.append(100000000000)
t.append(-100000000000);t.append(100000000000)
s = sorted(s, reverse=False)
t = sorted(t, reverse=False)

for i in range(Q):
    xi = int(input())
    sr = s[bisect.bisect_left(s, xi)]
    sl = s[bisect.bisect_left(s, xi) - 1]
    tr = t[bisect.bisect_left(t, xi)]
    tl = t[bisect.bisect_left(t, xi) - 1]

    ans = min(max(sr, tr) - xi, xi - min(sl, tl),
              tr - sl + min(tr - xi, xi - sl), sr - tl + min(sr - xi, xi - tl))
    print(ans)

Measured memory_bytes: 12152
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String [] s = str.split(" ");
		ArrayList<String> a = new ArrayList<>();
		for(String ss:s) {
			ss = ss.replace(",", "");
			ss = ss.replace(".", "");
			if(3<=ss.length() && ss.length()<=6) {
				a.add(ss);
			}
		}
		System.out.println(String.join(" ", a));
		
	}
}



Measured memory_bytes: 25964
// https://atcoder.jp/contests/abc120/tasks/abc120_b

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

// int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
    int a, b, k, cnt = 0;
    vector<int> v;
    cin >> a >> b >> k;

    for (int i = 1; i <= min(a, b); i++) {
        if (a % i == 0 && b % i == 0) {
            cnt++;
            v.push_back(i);
            // cout << i << endl;
        }
    }
    cout << v[cnt - k] << endl;

    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll s,i=0;
    cin>>s;
    vector<ll> v;
    v.pb(s);
    while(1){
        if(v[i]%2==0){
            v.pb(v[i]/2);
        }
        else{
            v.pb(v[i]*3+1);
        }
        i++;
        for(int j=0;j<i;j++){
            if(v[j]==v[i]){
                cout << i+1<<endl;
                return 0;
            }
        }
    }
    return 0;
}


Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <climits>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <complex>

using namespace std;
using pii = pair<int, int>;
using ll = long long;

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

	int n, m;
	cin >> n >> m;
	vector<pii> v(m);
	for (int i = 0; i < m; i++) cin >> v[i].first >> v[i].second;
	sort(v.begin(), v.end(), [](const pii& p1, const pii& p2)->bool {
		return p1.second - p1.first > p2.second - p2.first;
		});
	set<pii> s;
	for (pii& p : v) {
		auto now = s.insert(p).first;
		while (now != s.begin()) {
			auto prv = now;
			prv--;
			if (prv->second >= now->first) {
				pii tmp = { now->first,min(prv->second,now->second) };
				s.erase(prv);
				s.erase(now);
				now = s.insert(tmp).first;
			}
			else break;
		}
		while (true) {
			auto nxt = now;
			nxt++;
			if (nxt == s.end()) break;
			if (now->second >= nxt->first) {
				pii tmp = { nxt->first,min(now->second,nxt->second) };
				s.erase(now);
				s.erase(nxt);
				now = s.insert(tmp).first;
			}
			else break;
		}
	}
	if (s.size() == 1) cout << s.begin()->second - s.begin()->first + 1;
	else cout << 0;

	return 0;
}


Measured memory_bytes: 5760
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N; cin >> N;
  vector<ll> A(N);
  for(int i = 0; i < N; i++) cin >> A[i];
  vector<ll> sums(N);
  sums[N - 1] = 0;
  for(int i = N - 2; i >= 0; i--) {
    sums[i] = (A[i + 1] + sums[i + 1]) % 1000000007;
  }
  //for(int i = 0; i < N; i++) cout << sums[i] << endl;
  ll cnt = 0;
  for(int i = 0; i < N; i++) {
    cnt += (sums[i] * A[i]) % 1000000007;
  }
  cnt %= 1000000007;
  cout << cnt << endl;
}

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

constexpr int inf = 0x3f3f3f3f;
constexpr ll linf = 0x3f3f3f3f3f3f3f3fLL;

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

    int n; cin >> n;
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        if (i % 3 == 0) continue;
        if (i % 5 == 0) continue;
        ans += i;
    }
    cout << ans << endl;
    return 0;
}


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

#define pb         push_back

typedef long long ll;
const ll INF = 1000000000000000000ll;
const ll MOD = 1000000007ll;
const double EPS = 1e-8;

typedef struct _edge{int to, cost;}edge;
typedef pair<int, int> P;

const int MAX_N = 100001;
vector<edge> G[MAX_N];
int n;
ll d[MAX_N];

void dijkstra(int s){
	priority_queue<P, vector<P>, greater<P> > que;
	fill(d, d+n, INF);
	d[s] = 0;
	que.push(P(0, s));

	while(!que.empty()){
		P p = que.top(); que.pop();
		int v = p.second;
		if(d[v] < p.first) continue;
		for(int i=0; i < G[v].size(); i++){
			edge e = G[v][i];
			if(d[e.to] > d[v] + e.cost){
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

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

	for(int i=0; i<n-1; i++){
		int a, b, c;
		cin >> a >> b >> c;

		a--;
		b--;
		edge e1 = {b, c};
		G[a].pb(e1);
		edge e2 = {a, c};
		G[b].pb(e2);
	}

	int q, k;
	cin >> q >> k;
	k--;

	dijkstra(k);


	for(int i=0; i<q; i++){
		int x, y;
		cin >> x >> y;
		x--;
		y--;

		cout << d[x] + d[y] << endl;
	}


	
	return 0;
}


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

int n,k,ans=1e9+7;
int a[100005];

bool cmp(int a,int b)
{
	return a<b;
}

signed main()
{
	cin>>n>>k;
	for (int i=1;i<=n;i++)  cin>>a[i];
	
	sort(a+1,a+n+1,cmp);
	
	for (int i=1;i<=n-k+1;i++)  ans=min(ans,a[i+k-1]-a[i]);
	cout<<ans<<endl;
	
	return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include <array>
#include <set>
#include <cmath>
#include <string>
#include <queue>
#include <list>
#include <stack>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cassert>
#include <iomanip> // std::std::setprecision(X)
#include <limits>  //std::numeric_limits<int>::max(), min()

typedef long long ll;


int main (void) {

  ll N, K; std::cin >> N >> K;

  std::vector<ll> L(K);
  for(int i=0; i<K; i++) {
    if ( i != 0 ) L[i] = (N+i)/K;
    else L[i] = N/K;
  }

  // a ==   0, b ==   0, c ==   0 (mod K) or
  // a == K/2, b == K/2, c == K/2 (mod K) 

  ll ans = 0;
  if ( K % 2 == 0 ) {
    ans += (L[K/2]*L[K/2]*L[K/2]);
  }
  ans += (L[0]*L[0]*L[0]);
  std::cout << ans << std::endl;
  return 0;
}


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

// dp[i][500yen][num_1000yen] := remain ([i] is deleted)
int dp[128][512];
int n;
int P[128];

void init() {
  for(int j = 0; j < 128; ++j) {
    for(int k = 0; k < 512; ++k) {
      dp[j][k] = -1;
    }
  }
}

int main() {
  for(;;) {
    init();
    scanf("%d", &n);
    if( n == 0 ) break;
    for(int i = 0; i < n; ++i) {
      scanf("%d", &P[i]);
    }
    dp[0][0] = 0;
    for(int i = 0; i < n; ++i) {
      for(int j = i; j >= 0; --j) {
        for(int k = 512 - 1; k >= 0; --k) {
          if( dp[j][k] == -1 ) continue;
          int n1kyen = (P[i] + 999) / 1000;
          int rem = dp[j][k] + n1kyen * 1000 - P[i];
          int next_num500yen = j + (rem >= 500 ? 1 : 0);
          int next_num1kyen  = k + n1kyen;
          int next_rem       = rem >= 500 ? rem - 500 : rem;
          int& next = dp[next_num500yen][next_num1kyen];
          next = std::max(next, next_rem);
        }
      }
    }    
    int res = (1 << 28);
    int max = -1;
    for(int j = n; j >= 0; --j) {
      for(int k = 0; k < 512; ++k) {
        if( dp[j][k] == -1 ) continue;
        int cost = k * 1000 - 500 * j - dp[j][k];
        res = std::min(res, cost);
      }
      if( res != (1 << 28) ) {
        max = j;
        break;
      }
    }
    printf("%d %d\n", max, res);
  }

  return 0;
}

Measured memory_bytes: 3432
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
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; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;
        
    }else{
        return a.second < b.second;
    }
}


bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};


int main() {
    ll N;
    string S;
    cin >>S;
    N=S.size();
    ll now=N;
    while(1){
        if(now==0){
            cout<<"YES"<<endl;
            return 0;
        }
        if(now>=7){
            if(S.substr(now-7,7)=="dreamer"){
                now-=7;
                continue;
            }
        }

        if(now>=6){
            if(S.substr(now-6,6)=="eraser"){
                now-=6;
                continue;
            }
        }
        if(now>=5){
            if(S.substr(now-5,5)=="dream"){
                now-=5;
                continue;
            }
        }
        if(now>=5){
            if(S.substr(now-5,5)=="erase"){
                now-=5;
                continue;
            }
        }
        cout<<"NO"<<endl;
        return 0;

    }
    
 


    return 0;
    

}

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

int main() {
  int N,K;
  cin >> N >>K;
  int ans =0;

  for (int i = 1; i <= N; i++) {
    int s;
    cin >> s;
    if(s <= K-s){
      ans +=  s;
  }else{
      ans += K-s;
  }
    
 }
 cout << ans*2 << endl;
}

Measured memory_bytes: 384
#template
def inputlist(): return [int(k) for k in input().split()]
#template
N,K = inputlist()
mod = 10**9+7
def pow_k(a,n,mod):
    if n == 0:
        return 1
    if n % 2 ==0:
        return pow_k(a*a % mod,n//2,mod)
    else:
        return a * pow_k(a,n-1,mod) % mod
def modinv(a, mod):
    return pow(a, mod-2, mod)
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res
for i in range(1,K+1):
    if i <= N-K+1:
        ans = combination(N-K+1,i,mod) * combination(K-1,i-1,mod)
        ans %= mod
        print(ans)
        continue
    print(0)

Measured memory_bytes: 43740
//
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
using namespace std;
typedef pair<int,int> P;
const int M=5000+10,inf=0x3f3f3f3f;
int dp[M],n,m;
P a[M];
int main()
{
    memset(dp,inf,sizeof(dp));dp[0]=0;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int tmp=0,cost,b,z,x;
        scanf("%d%d",&cost,&b);
        while(b--)
            scanf("%d",&x),tmp|=(1<<(x-1));
        a[i]=P(tmp,cost);
    }
    for(int i=0;i<m;i++)
        for(int j=((1<<n)-1);j>=0;j--)
            dp[a[i].fi|j]=min(dp[a[i].fi|j],dp[j]+a[i].se);
    if(dp[((1<<n)-1)]!=inf) printf("%d\n",dp[((1<<n)-1)]);
    else printf("-1\n");
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	for(int n;cin>>n,n;){
		vector<pii> res(n);
		rep(i,n){
			int id; cin>>id;
			int t=0;
			rep(j,4){
				int m,s; cin>>m>>s;
				t+=m*60+s;
			}
			res[i]=mp(t,id);
		}
		sort(allof(res));
		cout<<res[0].second<<endl;
		cout<<res[1].second<<endl;
		cout<<res[res.size()-2].second<<endl;
	}
	
	return 0;
}

Measured memory_bytes: 944