#include<bits/stdc++.h>
#define N (1005)
#define ll long long
#define debug(x) cerr<<#x<<" : "<<x<<endl
#define CM cerr<<(&MOP2-&MOP1)/1024.0/1024.0<<endl
using namespace std;
template <class T> inline T Min(T x, T y) {
	return x<y?x:y;
}
template <class T> inline T Max(T x, T y) {
	return x>y?x:y;
}
bool MOP1;
int n,A[N][N],p[N];
int read() {
	int v=0;
	char c;
	for(c=getchar(); c<48; c=getchar());
	for(; c>47; c=getchar())v=(v<<3)+(v<<1)+(c^48);
	return v;
}
int cnt=0;
int tot=0;
bool mark[N];
bool flag;
bool MOP2;
int q[2][N];
bool cur;
int main() {
	n=read();
	for(int i=1; i<=n; i++) {
		p[i]=1;
		for(int j=1; j<n; j++)A[i][j]=read();
	}
	cur=1;
	for(int i=1;i<=n;i++)q[cur][i]=A[i][1];
	q[cur][0]=n;
	while(tot<n*(n-1)/2) {
		flag=0;
		cur^=1;
		q[cur][0]=0;
		cnt++;
		memset(mark,0,sizeof mark);
		for(register int i=1; i<=q[cur^1][0]; i++) {
			int now=q[cur^1][i];
			int to=A[now][p[now]];
			if(p[now]==n||mark[now]||mark[to])continue;
			if(A[to][p[to]]==now){
				mark[now]=1,mark[to]=1,p[now]++,p[to]++,tot++,flag=1,q[cur][++q[cur][0]]=now,q[cur][++q[cur][0]]=to;
			}
		}
		if(!flag) {
			puts("-1");
			return 0;
		}
	}
	printf("%d\n",cnt);
	return 0;
}

Score : 500 points

Problem Statement

N players will participate in a tennis tournament. We will call them Player 1, Player 2, \ldots, Player N.

The tournament is round-robin format, and there will be N(N-1)/2 matches in total.
Is it possible to schedule these matches so that all of the following conditions are satisfied? If the answer is yes, also find the minimum number of days required.

Each player plays at most one matches in a day.

Each player i (1 \leq i \leq N) plays one match against Player A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} in this order.

Constraints

3 \leq N \leq 1000

1 \leq A_{i, j} \leq N

A_{i, j} \neq i

A_{i, 1}, A_{i, 2}, \ldots, A_{i, N-1} are all different.

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} \ldots A_{1, N-1}
A_{2, 1} A_{2, 2} \ldots A_{2, N-1}
:
A_{N, 1} A_{N, 2} \ldots A_{N, N-1}

Output

If it is possible to schedule all the matches so that all of the conditions are satisfied, print the minimum number of days required; if it is impossible, print -1.

Sample Input 1

3
2 3
1 3
1 2

Sample Output 1

3

All the conditions can be satisfied if the matches are scheduled for three days as follows:

Day 1: Player 1 vs Player 2

Day 2: Player 1 vs Player 3

Day 3: Player 2 vs Player 3

This is the minimum number of days required.

Sample Input 2

4
2 3 4
1 3 4
4 1 2
3 1 2

Sample Output 2

4

All the conditions can be satisfied if the matches are scheduled for four days as follows:

Day 1: Player 1 vs Player 2, Player 3 vs Player 4

Day 2: Player 1 vs Player 3

Day 3: Player 1 vs Player 4, Player 2 vs Player 3

Day 4: Player 2 vs Player 4

This is the minimum number of days required.

Sample Input 3

3
2 3
3 1
1 2

Sample Output 3

-1

Any scheduling of the matches violates some condition.


#include <bits/stdc++.h>

#define long long long int

using namespace std;

// @author: pashka

int main() {
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<pair<double, pair<double, double>>> a(2 * n);
    for (int i = 0; i < n; i++) {
        cin >> a[i].second.first >> a[i].second.second;
        a[i + n].second.first = -a[i].second.first;
        a[i + n].second.second = -a[i].second.second;
    }
    for (int i = 0; i < 2 * n; i++) {
        a[i].first = atan2(a[i].second.second, a[i].second.first);
    }

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

    double y0 = 0;
    double x0 = 0;
    for (int i = 0; i < 2 * n; i++) {
        double x = a[i].second.first;
        double y = a[i].second.second;
        if (y > 0 || (y == 0 && x < 0)) {
            x0 += x;
            y0 += y;
        }
    }

    double res = 0;

    for (int i = 0; i < 2 * n; i++) {
        double x = a[i].second.first;
        double y = a[i].second.second;
        x0 += x;
        y0 += y;
        res = max(res, hypot(x0, y0));
    }
    cout << setprecision(20) << res;

    return 0;
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score: 600 points

Problem Statement

E869120 is initially standing at the origin (0, 0) in a two-dimensional plane.

He has N engines, which can be used as follows:

When E869120 uses the i-th engine, his X- and Y-coordinate change by x_i and y_i, respectively. In other words, if E869120 uses the i-th engine from coordinates (X, Y), he will move to the coordinates (X + x_i, Y + y_i).

E869120 can use these engines in any order, but each engine can be used at most once. He may also choose not to use some of the engines.

He wants to go as far as possible from the origin.
Let (X, Y) be his final coordinates. Find the maximum possible value of \sqrt{X^2 + Y^2}, the distance from the origin.

Constraints

1 \leq N \leq 100

-1 \ 000 \ 000 \leq x_i \leq 1 \ 000 \ 000

-1 \ 000 \ 000 \leq y_i \leq 1 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
: :
x_N y_N

Output

Print the maximum possible final distance from the origin, as a real value.
Your output is considered correct when the relative or absolute error from the true answer is at most 10^{-10}.

Sample Input 1

3
0 10
5 -5
-5 -5

Sample Output 1

10.000000000000000000000000000000000000000000000000

The final distance from the origin can be 10 if we use the engines in one of the following three ways:

Use Engine 1 to move to (0, 10).

Use Engine 2 to move to (5, -5), and then use Engine 3 to move to (0, -10).

Use Engine 3 to move to (-5, -5), and then use Engine 2 to move to (0, -10).

The distance cannot be greater than 10, so the maximum possible distance is 10.

Sample Input 2

5
1 1
1 0
0 1
-1 0
0 -1

Sample Output 2

2.828427124746190097603377448419396157139343750753

The maximum possible final distance is 2 \sqrt{2} = 2.82842....
One of the ways to achieve it is:

Use Engine 1 to move to (1, 1), and then use Engine 2 to move to (2, 1), and finally use Engine 3 to move to (2, 2).

Sample Input 3

5
1 1
2 2
3 3
4 4
5 5

Sample Output 3

21.213203435596425732025330863145471178545078130654

If we use all the engines in the order 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5, we will end up at (15, 15), with the distance 15 \sqrt{2} = 21.2132... from the origin.

Sample Input 4

3
0 0
0 1
1 0

Sample Output 4

1.414213562373095048801688724209698078569671875376

There can be useless engines with (x_i, y_i) = (0, 0).

Sample Input 5

1
90447 91000

Sample Output 5

128303.000000000000000000000000000000000000000000000000

Note that there can be only one engine.

Sample Input 6

2
96000 -72000
-72000 54000

Sample Output 6

120000.000000000000000000000000000000000000000000000000

There can be only two engines, too.

Sample Input 7

10
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20

Sample Output 7

148.660687473185055226120082139313966514489855137208

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int N;
long long v[8][2];
int st[8];

double res;

void CheckSol()
{
    long long x = 0, y = 0;

    for(int i = 0; i <= 8; i++)
        if(st[i] == 1)
            x += v[i][0], y += v[i][1];

    res = max(res, sqrt(1LL * x * x + y * y));
}

void bk(int p)
{
    if(p == 8)
        {
            CheckSol();
            return ;
        }

    for(int i = 0; i <= 1; i++)
    {
        st[p] = i;
        bk(p + 1);
    }
}

int main()
{
    cin >> N;

    for(int i = 1; i <= N; i++)
    {
        long long x, y; cin >> x >> y;
        int index;

        if(x > 0 && y > 0) index = 0;
        else if(x > 0 && y < 0) index = 1;
        else if(x < 0 && y > 0) index = 2;
        else if(x < 0 && y < 0) index = 3;
        else if(x == 0 && y > 0) index = 4;
        else if(x == 0 && y < 0) index = 5;
        else if(x > 0 && y == 0) index = 6;
        else if(x < 0 && y == 0) index = 7;

        v[index][0] += x;
        v[index][1] += y;
    }

    bk(0);

    cout << fixed << setprecision(45) << res << '\n';

    return 0;
}


// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

// ModInt begin

using ll = long long;
template<ll mod>
struct ModInt {
    ll v;
    ll mod_pow(ll x, ll n) const {
        return (!n) ? 1 : (mod_pow((x*x)%mod,n/2) * ((n&1)?x:1)) % mod;
    }
    ModInt(ll a = 0) : v((a %= mod) < 0 ? a + mod : a) {}
    ModInt operator+ ( const ModInt& b ) const {
        return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v));
    }
    ModInt operator- () const {
        return ModInt(-v);
    }
    ModInt operator- ( const ModInt& b ) const {
        return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v));
    }
    ModInt operator* ( const ModInt& b ) const {return (v * b.v) % mod;}
    ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, mod-2)) % mod;}

    bool operator== ( const ModInt &b ) const {return v == b.v;}
    bool operator!= ( const ModInt &b ) const {return !(*this == b); }
    ModInt& operator+= ( const ModInt &b ) {
        v += b.v;
        if(v >= mod) v -= mod;
        return *this;
    }
    ModInt& operator-= ( const ModInt &b ) {
        v -= b.v;
        if(v < 0) v += mod;
        return *this;
    }
    ModInt& operator*= ( const ModInt &b ) {
        (v *= b.v) %= mod;
        return *this;
    }
    ModInt& operator/= ( const ModInt &b ) {
        (v *= mod_pow(b.v, mod-2)) %= mod;
        return *this;
    }
    ModInt pow(ll x) { return ModInt(mod_pow(v, x)); }
    // operator int() const { return int(v); }
    // operator long long int() const { return v; }
};

template<ll mod>
ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;}
template<ll mod>
istream& operator>> (istream& in, ModInt<mod>& a) {
    in >> a.v;
    return in;
}

// ModInt end
using mint = ModInt<1000000007>;

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

    mint ans(0);
    for(int i=0; i<N; i++) {
        for(int j=i+1; j<N; j++) {
            if(A[i] == A[j]) continue;
            if(A[i] < A[j]) ans += mint(K) * mint(K-1) / mint(2);
            if(A[i] > A[j]) ans += mint(K) * mint(K+1) / mint(2);
        }
    }
    cout << ans << endl;
    return 0;
}

Score : 300 points

Problem Statement

We have a sequence of N integers A~=~A_0,~A_1,~...,~A_{N - 1}.

Let B be a sequence of K \times N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.

Find the inversion number of B, modulo 10^9 + 7.

Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0 \leq i < j \leq K \times N - 1) such that B_i > B_j.

Constraints

All values in input are integers.

1 \leq N \leq 2000

1 \leq K \leq 10^9

1 \leq A_i \leq 2000

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 ... A_{N - 1}

Output

Print the inversion number of B, modulo 10^9 + 7.

Sample Input 1

2 2
2 1

Sample Output 1

3

In this case, B~=~2,~1,~2,~1. We have:

B_0 > B_1

B_0 > B_3

B_2 > B_3

Thus, the inversion number of B is 3.

Sample Input 2

3 5
1 1 1

Sample Output 2

0

A may contain multiple occurrences of the same number.

Sample Input 3

10 998244353
10 9 8 7 5 6 3 4 2 1

Sample Output 3

185297239

Be sure to print the output modulo 10^9 + 7.


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll> P;

int main(void){
    ll n;
    string s;
    cin>>n;
    cin>>s;
    ll ans = 1;
    ll num = 0;
    for(ll i=0;i<2*n;i++){
        if((2*n-1-i)%2==1){
            if(s[i]=='B')num++;
            else{
                ans = ans*num;
                ans = ans%N;
                num--;
            }
        }
        else{
            if(s[i]=='W')num++;
            else{
                ans = ans*num;
                ans = ans%N;
                num--;
            }
        }
    }
    for(ll i=1;i<=n;i++){
        ans = ans*i;
        ans = ans%N;
    }
    cout<<ans<<endl;
    return 0;
}

Score : 500 points

Problem Statement

There are 2N squares arranged from left to right. You are given a string of length 2N representing the color of each of the squares.

The color of the i-th square from the left is black if the i-th character of S is B, and white if that character is W.

You will perform the following operation exactly N times: choose two distinct squares, then invert the colors of these squares and the squares between them. Here, to invert the color of a square is to make it white if it is black, and vice versa.

Throughout this process, you cannot choose the same square twice or more. That is, each square has to be chosen exactly once.

Find the number of ways to make all the squares white at the end of the process, modulo 10^9+7.

Two ways to make the squares white are considered different if and only if there exists i (1 \leq i \leq N) such that the pair of the squares chosen in the i-th operation is different.

Constraints

1 \leq N \leq 10^5

|S| = 2N

Each character of S is B or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of ways to make all the squares white at the end of the process, modulo 10^9+7. If there are no such ways, print 0.

Sample Input 1

2
BWWB

Sample Output 1

4

There are four ways to make all the squares white, as follows:

Choose Squares 1, 3 in the first operation, and choose Squares 2, 4 in the second operation.

Choose Squares 2, 4 in the first operation, and choose Squares 1, 3 in the second operation.

Choose Squares 1, 4 in the first operation, and choose Squares 2, 3 in the second operation.

Choose Squares 2, 3 in the first operation, and choose Squares 1, 4 in the second operation.

Sample Input 2

4
BWBBWWWB

Sample Output 2

288

Sample Input 3

5
WWWWWWWWWW

Sample Output 3

0


#include <iostream>
using namespace std;
typedef long long ll;
ll mod=1e9+7;

int main(){
  ll n;
  string s;
  cin >> n >> s;
  int check=0;
  int x[2*n];
  for(int i=0;i<2*n;i++){
    if(s[i]=='B'&&check%2==0||s[i]=='W'&&check%2==1){
      check++; x[i]=0;
    }
    else{
      check--; x[i]=1;
    }
    cerr << check << endl;
  }
  if(check){
    cout << 0 << endl;
    return 0;
  }
  ll ans=1;
  for(ll i=1;i<=n;i++){
    ans*=i; ans%=mod;
  }
  ll cnt=0;
  for(ll i=2*n-1;i>=0;i--){
    if(x[i]) cnt++;
    else{
      ans*=cnt; ans%=mod; cnt--;
    }
  }
  cout << ans << endl;
}


#include<bits/stdc++.h>
#define pb push_back
#define pair pair<int,int>
#define ll long long
#define f first
#define se second
using namespace std;
double pi=acos(-1.0);
int main(){
 int n;
  scanf("%d",&n);
  double ans=0;
  double x;
  while(n--)
  {
    scanf("%lf",&x);
    ans+=(1.0/x);
  }
  ans=(1.0/ans);
  printf("%.5lf",ans);
}

Score : 200 points

Problem Statement

Given is a sequence of N integers A_1, \ldots, A_N.

Find the (multiplicative) inverse of the sum of the inverses of these numbers, \frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}.

Constraints

1 \leq N \leq 100

1 \leq A_i \leq 1000

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print a decimal number (or an integer) representing the value of \frac{1}{\frac{1}{A_1} + \ldots + \frac{1}{A_N}}.

Your output will be judged correct when its absolute or relative error from the judge's output is at most 10^{-5}.

Sample Input 1

2
10 30

Sample Output 1

7.5

\frac{1}{\frac{1}{10} + \frac{1}{30}} = \frac{1}{\frac{4}{30}} = \frac{30}{4} = 7.5.

Printing 7.50001, 7.49999, and so on will also be accepted.

Sample Input 2

3
200 200 200

Sample Output 2

66.66666666666667

\frac{1}{\frac{1}{200} + \frac{1}{200} + \frac{1}{200}} = \frac{1}{\frac{3}{200}} = \frac{200}{3} = 66.6666....

Printing 6.66666e+1 and so on will also be accepted.

Sample Input 3

1
1000

Sample Output 3

1000

\frac{1}{\frac{1}{1000}} = 1000.

Printing +1000.0 and so on will also be accepted.


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define flush fflush(stdout)
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define reset(x,v) memset(x,v,sizeof(x))
#define sz(x) (int)(x.size())
#define ff first
#define ss second
#define endl "\n"
#define int long long int
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0)return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p;}
const ll MOD = 1000000007;
const ll mod=1000000006;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
/*
 shuffle(x,x+n,rng) // shuffling elements of x in random order
 */

int32_t main()
{
    IOS;
    int n;
    cin>>n;
    int a[n];long double ans,sum=0.0;
    for(int i=0;i<n;i++)
    { cin>>a[i];
        sum=sum+(long double)1/a[i];
    }
    ans=1.0/sum;
    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> l_l;
typedef pair<int , int> i_i;
typedef pair<string , int> s_i;
typedef vector<ll> vel;
typedef vector<int> vei;
typedef vector<char> vec;
typedef vector<bool> veb;
typedef vector<string> ves;
typedef vector<vector<ll>> ve_vel;
typedef vector<vector<int>> ve_vei;
typedef vector<vector<char>> ve_vec;
typedef vector<vector<bool>> ve_veb;
typedef vector<vector<string>> ve_ves;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<(int)(n);i++)
#define rep2(i,n) for(int i=2;i<(int)(n);i++)
#define repk(i,k,n) for(int i=k;i<(int)(n);i++)
#define fs first
#define sc second
#define pub push_back
#define puf push_front
#define pob pop_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define maxel(a) *max_element(all(a))
#define minel(a) *min_element(all(a))
#define acc accumulate
#define EPS (1e-7)
//#define INF (1e9)
#define PI (acos(-1))
#define mod (1000000007)
typedef long long int64;
const int64 INF = 1LL << 58;
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 (a>b) { a=b; return 1; } return 0; }
template<typename T> void remove(T & vector, unsigned int index){vector.erase(vector.begin() + index);}

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

	int N; cin >> N;
	vector<ld> A(N); rep(i,N) cin >> A[i];

	ld bunbo = 0;
	rep(i,N) bunbo += 1.0 / A[i];
	ld ans = 1.0 / bunbo;
	cout << fixed << setprecision(10) << ans << endl;
	return 0;

}


#include <iostream>
#include <string>
#include <vector>
#include <map>

using namespace std;

string tre;

bool moze(int n, string k){
    int j = 0;
   for (int i=0; i < n && j < k.length(); i++)
       if (k[j] == tre[i])
         j++;

   return (j==k.length());
}

int main() {
  string t;
  cin >> tre >> t;
  map <char, bool> ima;
  for (int i = 0; i < tre.length(); i++)
   ima[tre[i]] = true;
  for (int i = 0; i < t.length(); i++)
    if (!ima[t[i]]){
      cout << "-1";
      return 0;
    }
  int l = 1, r = 10000000;
  while (tre.size() < 10000000) tre += tre;
  while (l < r){
    int mid = (l+r)/2;
    if (!moze(mid,t)) l = mid + 1;
    else r = mid;
  }
  cout << l;
}

Score : 500 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.

Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

Notes

A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

Sample Input 1

contest
son

Sample Output 1

10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.

On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.


#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.st << ' ' << p.nd << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

const int N = 101;
int n, m, a[N][N];

struct BipartiteMatcher {
	vector<vector<int>> G;
	vector<int> L, R, Viz;

	BipartiteMatcher(int n, int m) {
		G.resize(n);
		L.resize(n, -1);
		R.resize(m, -1);
		Viz.resize(n);
	}

	void AddEdge(int a, int b) {
		G[a].push_back(b);
	}

	bool Match(int node) {
		if(Viz[node])
			return false;
		Viz[node] = true;

		for(auto vec : G[node]) {
			if(R[vec] == -1 || Match(R[vec])) {
				L[node] = vec;
				R[vec] = node;
				return true;
			}
		}

		return false;
	}
	void Solve() {
		bool ok = true;
		while(ok) {
			ok = false;
			fill(Viz.begin(), Viz.end(), 0);
			for(int i = 0; i < L.size(); ++i)
				if(L[i] == -1)
					ok |= Match(i);
		}
	}
};

int main() {
	ios_base::sync_with_stdio(false);

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

	for(int j = 0; j < m; j++) {
		BipartiteMatcher bm(n, n);
		dbg(j);
		for(int i = 0; i < n; i++)
			for(int k = j; k < m; k++)
				bm.AddEdge(i, a[i][k] / m);
		// dbg_ok;
		bm.Solve();
		// dbg_ok;
		for(int i = 0; i < n; i++) {
			int sch = bm.L[i];
			dbg(i, sch);
			for(int k = j; k < m; k++)
				if(a[i][k] / m == sch) {
					swap(a[i][k], a[i][j]);
					break;
				}
		}
	}

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			cout << a[i][j] << " \n"[j == m - 1];

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++) {
			swap(a[i][j], a[a[i][j] / m][j]);
		}

	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			cout << a[i][j]  << " \n"[j == m - 1];
}

Score : 1100 points

Problem Statement

We have a grid with N rows and M columns of squares.
Each integer from 1 to NM is written in this grid once.
The number written in the square at the i-th row from the top and the j-th column from the left is A_{ij}.

You need to rearrange these numbers as follows:

First, for each of the N rows, rearrange the numbers written in it as you like.

Second, for each of the M columns, rearrange the numbers written in it as you like.

Finally, for each of the N rows, rearrange the numbers written in it as you like.

After rearranging the numbers, you want the number written in the square at the i-th row from the top and the j-th column from the left to be M\times (i-1)+j.
Construct one such way to rearrange the numbers. The constraints guarantee that it is always possible to achieve the objective.

Constraints

1 \leq N,M \leq 100

1 \leq A_{ij} \leq NM

A_{ij} are distinct.

Input

Input is given from Standard Input in the following format:

N M
A_{11} A_{12} ... A_{1M}
:
A_{N1} A_{N2} ... A_{NM}

Output

Print one way to rearrange the numbers in the following format:

B_{11} B_{12} ... B_{1M}
:
B_{N1} B_{N2} ... B_{NM}
C_{11} C_{12} ... C_{1M}
:
C_{N1} C_{N2} ... C_{NM}

Here B_{ij} is the number written in the square at the i-th row from the top and the j-th column from the left after Step 1, and C_{ij} is the number written in that square after Step 2.

Sample Input 1

3 2
2 6
4 3
1 5

Sample Output 1

2 6
4 3
5 1
2 1
4 3
5 6

Sample Input 2

3 4
1 4 7 10
2 5 8 11
3 6 9 12

Sample Output 2

1 4 7 10
5 8 11 2
9 12 3 6
1 4 3 2
5 8 7 6
9 12 11 10


#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define R register

int N, K;
string s, rev, tmp, str;

int main() {
	cin >> N >> K >> s;
	rev = s, reverse(rev.begin(), rev.end());
	tmp = s + rev, reverse(tmp.begin(), tmp.end());
//cout << tmp << endl;
	str = tmp.substr(0, N);
	for(R int i = 1; i < N; ++i) {
		rev = tmp.substr(i, N);
		if(rev < str) str = rev;
	}
	reverse(str.begin(), str.end());
//cout << str << endl;
	for(R int i = 1, j; i < K; ++i) {
		for(j = N - 2; ~j; --j) if(str[j] != str[N - 1]) break;
		str = str + str.substr(j + 1, N);
//cout << str << endl;
		j = str.length();
		str = str.substr(j - N, N);
//cout << str << endl;
	}
	reverse(str.begin(), str.end());
 	cout << str;
 	return 0;
}

Score : 1300 points

Problem Statement

Takahashi has a string S of length N consisting of lowercase English letters.
On this string, he will perform the following operation K times:

Let T be the string obtained by reversing S, and U be the string obtained by concatenating S and T in this order.

Let S' be some contiguous substring of U with length N, and replace S with S'.

Among the strings that can be the string S after the K operations, find the lexicographically smallest possible one.

Constraints

1 \leq N \leq 5000

1 \leq K \leq 10^9

|S|=N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the lexicographically smallest possible string that can be the string S after the K operations.

Sample Input 1

5 1
bacba

Sample Output 1

aabca

When S=bacba, T=abcab, U=bacbaabcab, and the optimal choice of S' is S'=aabca.

Sample Input 2

10 2
bbaabbbaab

Sample Output 2

aaaabbaabb


//
// Created by 伊東麻沙美 on 2019/10/22.
//

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

int main(){

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

    int num1,num2,num3,max;
    num1 = a+b;
    num2 = a-b;
    num3 = a*b;

    max = num1;
    if(max<num2){
        max = num2;
    }
    if(max<num3){
        max = num3;
    }

    cout<<max;
    return 0;
}

Score : 100 points

Problem Statement

We have two integers: A and B.

Print the largest number among A + B, A - B, and A \times B.

Constraints

All values in input are integers.

-100 \leq A,\ B \leq 100

Input

Input is given from Standard Input in the following format:

A B

Output

Print the largest number among A + B, A - B, and A \times B.

Sample Input 1

-13 3

Sample Output 1

-10

The largest number among A + B = -10, A - B = -16, and A \times B = -39 is -10.

Sample Input 2

1 -33

Sample Output 2

34

The largest number among A + B = -32, A - B = 34, and A \times B = -33 is 34.

Sample Input 3

13 3

Sample Output 3

39

The largest number among A + B = 16, A - B = 10, and A \times B = 39 is 39.


#include <bits/stdc++.h>
using namespace std;

int main() {
    int k,x;cin>>k>>x;
    int tmp;

    for(int i=0;i<2*(k-1);++i){
    tmp=x-k+1+i;
    cout<<tmp<<" ";
    }
    tmp=x+k-1;
    cout<<tmp<<endl;
	return 0;
}

Score : 200 points

Problem Statement

There are 2000001 stones placed on a number line. The coordinates of these stones are -1000000, -999999, -999998, \ldots, 999999, 1000000.

Among them, some K consecutive stones are painted black, and the others are painted white.

Additionally, we know that the stone at coordinate X is painted black.

Print all coordinates that potentially contain a stone painted black, in ascending order.

Constraints

1 \leq K \leq 100

0 \leq X \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K X

Output

Print all coordinates that potentially contain a stone painted black, in ascending order, with spaces in between.

Sample Input 1

3 7

Sample Output 1

5 6 7 8 9

We know that there are three stones painted black, and the stone at coordinate 7 is painted black. There are three possible cases:

The three stones painted black are placed at coordinates 5, 6, and 7.

The three stones painted black are placed at coordinates 6, 7, and 8.

The three stones painted black are placed at coordinates 7, 8, and 9.

Thus, five coordinates potentially contain a stone painted black: 5, 6, 7, 8, and 9.

Sample Input 2

4 0

Sample Output 2

-3 -2 -1 0 1 2 3

Negative coordinates can also contain a stone painted black.

Sample Input 3

1 100

Sample Output 3

100


#include <bits/stdc++.h>
using namespace std;

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

  vector<vector<char>>x(n,vector<char>(10));
  for(int i=0; i<n; i++){
   for(int j=0; j<10; j++){
     cin >> x[i][j];
   }
    sort(x.at(i).begin(),x.at(i).end());
  }

  int ans=0;

  for(int i=0; i<n-1; i++){
   for(int j=i+1; j<n; j++){
     int count=0;
     for(int k=0; k<10; k++){
       if(x[i][k]==x[j][k]) count++;
     }
     if(count==10) ans++;
   }
  }

  cout << ans << endl;

}

Score : 300 points

Problem Statement

We will call a string obtained by arranging the characters contained in a string a in some order, an anagram of a.

For example, greenbin is an anagram of beginner. As seen here, when the same character occurs multiple times, that character must be used that number of times.

Given are N strings s_1, s_2, \ldots, s_N. Each of these strings has a length of 10 and consists of lowercase English characters. Additionally, all of these strings are distinct. Find the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Constraints

2 \leq N \leq 10^5

s_i is a string of length 10.

Each character in s_i is a lowercase English letter.

s_1, s_2, \ldots, s_N are all distinct.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the number of pairs of integers i, j (1 \leq i < j \leq N) such that s_i is an anagram of s_j.

Sample Input 1

3
acornistnt
peanutbomb
constraint

Sample Output 1

1

s_1 = acornistnt is an anagram of s_3 = constraint. There are no other pairs i, j such that s_i is an anagram of s_j, so the answer is 1.

Sample Input 2

2
oneplustwo
ninemodsix

Sample Output 2

0

If there is no pair i, j such that s_i is an anagram of s_j, print 0.

Sample Input 3

5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

Sample Output 3

4

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int N; cin >> N;
	string s[N];
	int cc[N][26];
	for(int i=0; i<N; i++) {
		cin >> s[i];
		for (char c : s[i]) {
			int num = c - 'a';
			cc[i][num]++;
		}
	}

	long long int ans = 0;
	for (int i=0; i<N; i++) {
		for (int j=i; j<N; j++) {
		bool isSame = true;
			for (int k=0; k<26; k++) {
				if (cc[i][k] != cc[j][k]) {
					isSame = false;
					break;
				}
			}
			if (isSame) ans++;
		}
	}

	cout << ans;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define Q int t; scanf("%d", &t); for(int q=1; q<=t; q++)
typedef long long int lli;
typedef pair<lli, lli> pi;

int main()
{
    int n, m;
    cin >> n >> m;
    priority_queue<lli>pq;
    vector<pi> v;
    for(int i=0; i<n; i++){
        lli a, b;
        cin >> a >> b;
        v.push_back({a, b});
    }
    sort(v.begin(), v.end());
    int j=0;
    lli ans = 0;
    for(int i=1; i<=m; i++){
        while(j<n && v[j].first <= i){
            pq.push(v[j].second);
            j++;
        }
        if(!pq.empty()){
            ans += pq.top();
            pq.pop();
        }
    }

    cout << ans << "\n";

    return 0;
}

Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

5 3
1 2
1 3
1 4
2 1
2 3

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0


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

signed main(){
	int N,M; cin>>N>>M;
	vector<pair<int,int>> AB(N);
	for(int i=0;i<N;i++){
		cin>>AB[i].first>>AB[i].second;
	}
	sort(AB.begin(),AB.end());
	int p=0,ans=0;
	priority_queue<pair<int,int>> que;
	for(int i=1;i<=M;i++){
		while(AB[p].first<=i and p<N){
			pair<int,int> tmp=make_pair(AB[p].second,AB[p].first);
			que.push(tmp);
			p++;
		}
		if(not que.empty()){
			pair<int,int> tmp=que.top();
			que.pop();
			ans+=tmp.first;
		}
	}
	cout<<ans<<endl;
	return 0;

}


#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
	int n, m;
	cin >> n >> m;
	vector<pair<int, int> > ab;
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		ab.push_back(make_pair(a, b));
	}
	sort(ab.begin(), ab.end());
	long long int ans = 0;
	int cnt = 0;
	priority_queue<int> qu;
	for (int i = 0; i <= m; i++) {
		if (!qu.empty()) {
			ans += qu.top();
			qu.pop();
		}
		while (cnt < n && ab[cnt].first <= i + 1) {
			qu.push(ab[cnt].second);
			cnt++;
		}
	}
	cout << ans << endl;
}


#include<iostream>
#include<algorithm>
#include<queue>
using lint=int64_t;
using namespace std;

bool cmp(pair<lint,lint> l,pair<lint,lint> r)
{
	if(l.first!=r.first)
		return l.first>r.first;
	return l.second>r.second;
}

int main()
{
	lint N,M;
	pair<lint,lint> p[100010]{};

	cin >> N >> M;
	for(lint i=0;i<N;i++)
	{
		cin >> p[i].first;
		cin >> p[i].second;
	}

	sort(p,p+N);

	priority_queue<lint> que;
	lint index=0;
	lint ans=0;
	for(lint i=1;i<=M;i++)
	{
		while(index<N && p[index].first<=i)
		{
			que.push(p[index].second);
			index++;
		}

		if(!que.empty())
		{
			ans+=que.top();
			que.pop();
		}
	}

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


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
#define rep(i, n) for(ll i = 0;i < n;i++)
#define all(i) i.begin(), i.end()
template<class T, class U> bool cmax(T& a, U b) { if (a<b) {a = b; return true;} else return false; }
template<class T, class U> bool cmin(T& a, U b) { if (a>b) {a = b; return true;} else return false; }

int n, m;

bool comp(const pii &l, const pii &r){
  return l.second > r.second;
}

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

  cin >> n >> m;
  vector<pair<ll, ll>> a(n);
  rep(i, n) {
    cin >> a[i].first >> a[i].second;
  }

  sort(all(a), comp);

//  cerr << endl;
//  rep(i, n) {
//    cerr << a[i].first << " " << a[i].second << endl;
//  }
//  cerr << endl;

  multimap<int, ll> d;
  int i = 0;
  int j = 0;
  ll ans = 0;
  while(j < n && i <= m) {
    //cerr << i << " " << j << endl;
    if (m >= i+a[j].first) {
      ans += a[j].second;
      d.insert({m-a[i].first, i});
      i++;
      j++;
    } else {
      auto ite = d.lower_bound(i);
      if (ite != d.end() && (*ite).second <= m-a[j].first) {
        auto tmp = *ite;
        d.erase(ite);
        d.insert({tmp.first, i});
        ans += a[j].second;
        i++;
        j++;
      } else {
        j++;
      }
    }
  }

  cout << ans << endl;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>
#include<complex>

using namespace std;

#define ll long long
#define ld long double
#define EPS 1e-9
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef pair<pii,int> ppi;
typedef pair<int,pii> pip;
typedef vector<pii> vp;
typedef vector<vi> vvi;

int gcd(int a, int b){if(b==0) return a;return gcd(b,a%b);}
int lcm(int a, int b){return a/gcd(a,b)*b;}

signed main(void) {
    int n,m;
    cin >> n >> m;
    vvi v(m);
    rep(i,n){
        int a,b;
        cin >> a >> b;
        if(m-a >= 0 && m-a < m)v[m-a].push_back(b);
    }
    int ans = 0;
    priority_queue<int> q;
    rep(i,m){
        int now = m-i-1;
        rep(j,v[now].size()){
            //cout << "!" << v[now][j] << endl;
            q.push(v[now][j]);
        }
        if(q.size()){
            ans += q.top();
            q.pop();
        }
    }
    cout << ans << endl;
}


#include <stdio.h>

int main()
{
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    int ans=c-a+b;
    printf("%d",ans);
    return 0;
}

Score : 100 points

Problem Statement

We have two bottles for holding water.

Bottle 1 can hold up to A milliliters of water, and now it contains B milliliters of water.

Bottle 2 contains C milliliters of water.

We will transfer water from Bottle 2 to Bottle 1 as much as possible.

How much amount of water will remain in Bottle 2?

Constraints

All values in input are integers.

1 \leq B \leq A \leq 20

1 \leq C \leq 20

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the integer representing the amount of water, in milliliters, that will remain in Bottle 2.

Sample Input 1

6 4 3

Sample Output 1

1

We will transfer two milliliters of water from Bottle 2 to Bottle 1, and one milliliter of water will remain in Bottle 2.

Sample Input 2

8 3 9

Sample Output 2

4

Sample Input 3

12 3 7

Sample Output 3

0


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll p=1000000007ll;
const int N=200005;
template<class IT>inline void cmin(IT &a,IT b){if(b<a)a=b;}
template<class IT>inline void cmax(IT &a,IT b){if(a<b)a=b;}
int a[N],b[N],n,u,s;
inline void solve(int x){
//	printf("solve %d\n",x);
	int i,t,s2=0;u=0;
	for(i=1;i<=n;++i)s2+=(b[i]=a[i]%x);
	sort(b+1,b+n+1);
	t=n-s2/x;
	for(i=1;i<=t;++i)u+=b[i];
}
int main(){
	int i,t,k,ans=0;
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;++i){scanf("%d",&a[i]);s+=a[i];}
//	printf("sum=%d\n",s);
	t=sqrt(s);
	for(i=1;i<=t;++i)if(!(s%i)){
		solve(  i);if(u<=k)cmax(ans,  i);
		solve(s/i);if(u<=k)cmax(ans,s/i);
	}
	printf("%d",ans);
	return 0;
}

Score : 500 points

Problem Statement

We have a sequence of N integers: A_1, A_2, \cdots, A_N.

You can perform the following operation between 0 and K times (inclusive):

Choose two integers i and j such that i \neq j, each between 1 and N (inclusive). Add 1 to A_i and -1 to A_j, possibly producing a negative element.

Compute the maximum possible positive integer that divides every element of A after the operations. Here a positive integer x divides an integer y if and only if there exists an integer z such that y = xz.

Constraints

2 \leq N \leq 500

1 \leq A_i \leq 10^6

0 \leq K \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \cdots A_{N-1} A_{N}

Output

Print the maximum possible positive integer that divides every element of A after the operations.

Sample Input 1

2 3
8 20

Sample Output 1

7

7 will divide every element of A if, for example, we perform the following operation:

Choose i = 2, j = 1. A becomes (7, 21).

We cannot reach the situation where 8 or greater integer divides every element of A.

Sample Input 2

2 10
3 5

Sample Output 2

8

Consider performing the following five operations:

Choose i = 2, j = 1. A becomes (2, 6).

Choose i = 2, j = 1. A becomes (1, 7).

Choose i = 2, j = 1. A becomes (0, 8).

Choose i = 2, j = 1. A becomes (-1, 9).

Choose i = 1, j = 2. A becomes (0, 8).

Then, 0 = 8 \times 0 and 8 = 8 \times 1, so 8 divides every element of A. We cannot reach the situation where 9 or greater integer divides every element of A.

Sample Input 3

4 5
10 1 2 22

Sample Output 3

7

Sample Input 4

8 7
1 7 5 6 8 2 6 5

Sample Output 4

5


#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i,n) for(long long i = 0; i < (n); ++i)
#define ll long long
#define P pair<ll,ll>
#define all(v) (v).begin(),(v).end()

const ll mod = 1e9+7;
const ll INF = 1e18;
const double pi = acos(-1.0);

int main(void)
{
    ll n, ans=0;
    cin >> n;
    vector<ll> a(n+1), b(n);
    rep(i,n+1) cin >> a[i];
    rep(i,n) cin >> b[i];
    rep(i,n){
        ll l = min(a[i], b[i]);
        ans += l;
        a[i] -= l;
        b[i] -= l;
        ll r = min(a[i+1], b[i]);
        ans += r;
        a[i] -= r;
        b[i] -= r;
    }
    cout << ans << endl;
    return 0;
}

Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    long long  a[n+1];
    for(int i=0;i<n+1;i++)
    cin>>a[i];
    long long  b[n];
    long long monster=0;
    for(int i=0;i<n;i++)
    cin>>b[i];
    for(int i=0;i<n;i++)
    {
        if(a[i]<=b[i])
        {
            monster+=a[i];
            b[i]-=a[i];
            if(a[i+1]<=b[i])
            {
                monster+=a[i+1];
                a[i+1]=0;
            }
            else
            {
                monster+=b[i];
                a[i+1]-=b[i];
            }
        }
        else
        {
            monster+=b[i];

        }

    }

        cout<<monster<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 1e9 + 7;
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
template <class T>
void chmin(T &a, T b) { a = min(a, b); }
template <class T>
void chmax(T &a, T b) { a = max(a, b); }
void ok() { cerr << "ok" << endl; }
using ull = uint64_t;
const int bases[64] = {257, 262, 266, 275, 276, 281, 285, 290, 296, 302, 306,
                       310, 311, 313, 323, 333, 344, 345, 350, 357, 367, 370,
                       373, 402, 423, 425, 431, 440, 442, 443, 454, 457, 458,
                       462, 471, 478, 481, 487, 489, 492, 499, 501, 502, 503,
                       506, 514, 524, 532, 535, 541, 550, 552, 557, 559, 562,
                       563, 567, 570, 571, 580, 592, 597, 604, 612};
const ull rmod = 0x1fffffffffffffff;
const ull base = bases[chrono::duration_cast<chrono::microseconds>(chrono::system_clock::now().time_since_epoch()).count() & 63];
const ull mask30 = (1UL << 30) - 1, mask31 = (1UL << 31) - 1;
struct RollingHash {
    vector<ull> hash, pow;
    inline ull mul(const ull &a, const ull &b) const {
        ull mid = (a & mask31) * (b >> 31) + (a >> 31) * (b & mask31);
        ull res = (a >> 31) * (b >> 31) * 2 + (mid >> 30) + ((mid & mask30) << 31);
        res += (a & mask31) * (b & mask31);
        res = (res >> 61) + (res & rmod);
        if (res >= rmod) res -= rmod;
        return res;
    }
    RollingHash(const string &s) {
        hash.resize(s.length() + 1);
        pow.resize(s.length() + 1);
        pow[0] = 1;
        for (int i = 0; i < s.length(); i++) {
            hash[i + 1] = mul(hash[i], base) + s[i];
            pow[i + 1] = mul(pow[i], base);
            if (hash[i + 1] >= rmod) hash[i + 1] -= rmod;
        }
    }
    ull get(int l, int r) { //[l,r]
        ull res = hash[r + 1] + rmod - mul(hash[l], pow[r + 1 - l]);
        if (res >= rmod) res -= rmod;
        return res;
    }
};
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s, t;
    cin >> s >> t;
    int n = s.length(), m = t.length();
    while (s.length() < n + m) {
        s += s;
    }
    RollingHash hs(s), ht(t);
    vector<int> f(n), vis(n, -1);
    for (int i = 0; i < n; i++) {
        if (hs.get(i, i + m - 1) == ht.get(0, m - 1)) {
            f[i] = 1;
        }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (!f[i] || vis[i] != -1) {
            continue;
        }
        int co = 0, id = i;
        vector<int> v;
        while (1) {
            v.push_back(id);
            co++;
            id += m;
            id %= n;
            if (f[id]) {
                if (id == i) {
                    cout << -1 << endl;
                    return 0;
                }
                if (vis[id] != -1) {
                    co += vis[id];
                    break;
                }
                vis[id] = 0;
            } else {
                break;
            }
        }
        vis[i] = co;
        chmax(ans, co);
    }
    cout << ans << endl;
}

Score : 600 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if the number of non-negative integers i satisfying the following condition is finite, and find the maximum value of such i if the number is finite.

There exists a non-negative integer j such that the concatenation of i copies of t is a substring of the concatenation of j copies of s.

Notes

A string a is a substring of another string b if and only if there exists an integer x (0 \leq x \leq |b| - |a|) such that, for any y (1 \leq y \leq |a|), a_y = b_{x+y} holds.

We assume that the concatenation of zero copies of any string is the empty string. From the definition above, the empty string is a substring of any string. Thus, for any two strings s and t, i = 0 satisfies the condition in the problem statement.

Constraints

1 \leq |s| \leq 5 \times 10^5

1 \leq |t| \leq 5 \times 10^5

s and t consist of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If the number of non-negative integers i satisfying the following condition is finite, print the maximum value of such i; if the number is infinite, print -1.

Sample Input 1

abcabab
ab

Sample Output 1

3

The concatenation of three copies of t, ababab, is a substring of the concatenation of two copies of s, abcabababcabab, so i = 3 satisfies the condition.

On the other hand, the concatenation of four copies of t, abababab, is not a substring of the concatenation of any number of copies of s, so i = 4 does not satisfy the condition.

Similarly, any integer greater than 4 does not satisfy the condition, either. Thus, the number of non-negative integers i satisfying the condition is finite, and the maximum value of such i is 3.

Sample Input 2

aa
aaaaaaa

Sample Output 2

-1

For any non-negative integer i, the concatenation of i copies of t is a substring of the concatenation of 4i copies of s. Thus, there are infinitely many non-negative integers i that satisfy the condition.

Sample Input 3

aba
baaab

Sample Output 3

0

As stated in Notes, i = 0 always satisfies the condition.


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int> > mati;
typedef vector<vector<double> > matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<pll> vpll;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define RFOR(i,x,y) for(ll i=(ll)x; i>=(ll)y; --i)
#define RREP(i,x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back

inline void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void){
  cout << "\n";
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
  cout << first << " ";
  OUT(rest...);
  return;
}

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9)+7);

/*
class KMP{
  private:
    string S;
    string W;
    vector<ll> T;
    vector<bool> flag;

  public:
    void make_table(void){
      T.resize(W.size());
      ll i = 2;
      ll j = 0;
      T[0] = -1;
      T[1] = 0;

      while(i<W.size()){
        if(W[i-1]==W[j]){
          T[i] = j+1;
          i++;
          j++;
        }else if(j>0){
          j = T[j];
        }else{
          T[i] = 0;
          i++;
        }
      }
      return;
    }

    KMP(string s, string w){
      S = s;
      W = w;
      make_table();
      flag.resize(S.size(), false);
      return;
    };

    ~KMP(void){
      return;
    };

    ll local_search(ll start){
      ll m = 0; // the start position in S
      ll i = start; // the start position in W

      while(m+i<S.size()){
        if(W[i] == S[m+i]){
          i++;
          if(i==W.size()) return m;
        }else{
          m+=i-T[i];
          if(i>0) i=T[i];
        }
      }
      return -1;
    };

    void total_search(void){
      ll m = 0; // the start position in S
      ll i = 0; // the start position in W

      while(m+i<S.size()){
        if(W[i] == S[m+i]){
          i++;
          if(i==W.size()){
            flag[m] = true;
            m++;
            i=0;
          }
        }else{
          m+=i-T[i];
          if(i>0) i=T[i];
        }
      }
      return;
    };

    bool start_check(ll start){
      return flag[start];
    };
};
*/

class Z_algorithm{
  public:
    string S;
    vector<ll> A;
    Z_algorithm(string s){
      S = s;
      A.resize(S.size());
      return;
    };

    ~Z_algorithm(void){
      return;
    };

    void calc(void){
      A[0] = S.size();
      ll i=1, j=0;
      while(i<S.size()){
        while(i+j < S.size() && S[j] == S[i+j]) ++j;
        A[i] = j;
        if(j==0){
          ++i;
          continue;
        }
        ll k=1;
        while(i+k<S.size() && k+A[k]<j){
          A[i+k] = A[k];
          ++k;
        }
        i += k;
        j -= k;
      }
      return;
    };

    void calc(string W){
      S = W+S;
      calc();
      return;
    };
};

vll dp;
vll jump;
bool total_flag = true;

ll calc(ll pos, const ll init){
  ll n_pos = jump[pos];
  if(n_pos<0){
    return dp[pos] = 0;
  }
  if(n_pos==init){
    total_flag = false;
    return -2;
  }
  if(dp[pos]>=0) return dp[pos];
  return dp[pos] = calc(n_pos, init) + 1;
}

int main(){
  string s_base, t_base;
  IN(s_base);
  IN(t_base);

  string s = s_base;
  string t = t_base;

  while(s.size()<2*t.size()){
    s += s_base;
  }

  jump.resize(s_base.size());
  REP(i,s_base.size()) jump[i] = -1;

  /*
  REP(s_start, s_base.size()){
    bool flag = true;
    REP(t_diff,t.size()){
      if(s[t_diff+s_start] != t[t_diff]){
        flag = false;
        break;
      }
    }
    if(!flag) continue;

    jump[s_start] = (s_start + t.size())%s_base.size();
  }
  */

  /*
  KMP kmp(s, t);
  kmp.total_search();
  REP(i,s_base.size()){
    if(!kmp.start_check(i)) jump[i] = -1;
    else jump[i] = (i+t.size())%s_base.size();
  }
  */

  Z_algorithm ZA(s);
  ZA.calc(t);
  REP(i,s_base.size()){
    if(ZA.A[t.size()+i] < t.size()) continue;
    jump[i] = (i+t.size())%s_base.size();
  }

  REP(i,s_base.size()){
    //printf("%lld %lld\n", i, jump[i]);
  }

  dp.resize(jump.size());
  REP(i,dp.size()){
    dp[i] = -1;
  }

  REP(i,dp.size()){
    calc(i, i);
    if(!total_flag) break;
  }

  ll ans = 0;
  REP(i,dp.size()){
    ans = max(ans, dp[i]);
    //printf("%lld %lld\n", i, dp[i]);
  }

  if(total_flag){
    printf("%lld\n", ans);
  }else{
    printf("-1\n");
  }

  return 0;
}


#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>;

const ll e9 = 1000000000;

int main() {
    ll s;
    cin >> s;
    ll x1 = 0, y1 = 0;
    ll x2 = e9, y2 = 1;
    ll x3 = (s % e9) + e9;
    ll y3 = s / e9 + 1;
    cout << x1 << ' ' << y1 << ' ';
    cout << x2 << ' ' << y2 << ' ';
    cout << x3 << ' ' << y3;
    return 0;
}

Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


#include<stdio.h>
int main(){
  unsigned long long int s;
  scanf("%ulld",&s);
  if(s%2==0){
    printf("0 0 2 0 0 %ulld",s/2);
  }
  else{
    printf("1 0 0 %ld 2 %ulld",s/2+1,s/2);
  }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll INF = 1e9+7;
int main()
{
    ll n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vector<ll> ar = a;
    reverse(ar.begin(), ar.end());
    vector<set<ll>> pos(2*1e5+10);
    for(int i = 0; i < n; i++)
    {
        pos[ar[i]].insert(i+1);
        if(pos[ar[i]].size() == 1)
        {
            pos[ar[i]].insert(i+1+n);
        }
    }

    vector<ll> start(n+1);
    int p = 1;
    ll v = ar[0];
    ll ans = 0;
    while(true)
    {
        ll next = *(pos[v].lower_bound(p+1));
        if(next == n)
        {
            start[ans] = next;
            break;
        }
        else if(next == 2*n)
        {
            ans++;
            start[ans] = next - n;
            break;
        }
        else if(next > n)
        {
            p = next+1-n;
            ans++;
            start[ans] = p-1;
        }
        else
        {
            p = next+1;
            start[ans] = next;
        }
        v = ar[p-1];
    }
    ans++;
    ll count = k % ans;
    if(count == 0) return 0;
    ll s = start[ans-count];
    stack<ll> sta;
    set<ll> tmp;
    for(int i = n-s; i < n; i++)
    {
        if(tmp.find(a[i]) == tmp.end())
        {
            tmp.insert(a[i]);
            sta.push(a[i]);
        }
        else
        {
          while(true)
            {
                ll cand = sta.top();
                sta.pop();
                tmp.erase(cand);
                if(cand == a[i])
                {
                    break;
                }
            }
        }
    }
    stack<ll> ansq;
    ll size = sta.size();
    for(int i = 0; i < size; i++)
    {
        ll cand = sta.top();
        sta.pop();
        ansq.push(cand);
    }
    ll anss = ansq.size();
    for(int i = 0; i < anss; i++)
    {
        ll cand = ansq.top();
        ansq.pop();
        cout << cand << " ";
    }
}

Score : 700 points

Problem Statement

We have a sequence of N \times K integers: X=(X_0,X_1,\cdots,X_{N \times K-1}).
Its elements are represented by another sequence of N integers: A=(A_0,A_1,\cdots,A_{N-1}). For each pair i, j (0 \leq i \leq K-1,\ 0 \leq j \leq N-1), X_{i \times N + j}=A_j holds.

Snuke has an integer sequence s, which is initially empty.
For each i=0,1,2,\cdots,N \times K-1, in this order, he will perform the following operation:

If s does not contain X_i: add X_i to the end of s.

If s does contain X_i: repeatedly delete the element at the end of s until s no longer contains X_i. Note that, in this case, we do not add X_i to the end of s.

Find the elements of s after Snuke finished the operations.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq K \leq 10^{12}

1 \leq A_i \leq 2 \times 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 \cdots A_{N-1}

Output

Print the elements of s after Snuke finished the operations, in order from beginning to end, with spaces in between.

Sample Input 1

3 2
1 2 3

Sample Output 1

2 3

In this case, X=(1,2,3,1,2,3).
We will perform the operations as follows:

i=0: s does not contain 1, so we add 1 to the end of s, resulting in s=(1).

i=1: s does not contain 2, so we add 2 to the end of s, resulting in s=(1,2).

i=2: s does not contain 3, so we add 3 to the end of s, resulting in s=(1,2,3).

i=3: s does contain 1, so we repeatedly delete the element at the end of s as long as s contains 1, which causes the following changes to s: (1,2,3)→(1,2)→(1)→().

i=4: s does not contain 2, so we add 2 to the end of s, resulting in s=(2).

i=5: s does not contain 3, so we add 3 to the end of s, resulting in s=(2,3).

Sample Input 2

5 10
1 2 3 2 3

Sample Output 2

3

Sample Input 3

6 1000000000000
1 1 2 2 3 3

Sample Output 3

s may be empty in the end.

Sample Input 4

11 97
3 1 4 1 5 9 2 6 5 3 5

Sample Output 4

9 2 6


/*
 * Author       : YangDavid
 * Created Time : 2019年07月22日 星期一 11时13分09秒
 */

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int maxn = 555;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, a[maxn][maxn];
ll dp[maxn][maxn], pre1[maxn][maxn], pre2[maxn][maxn];
void upd(ll &x, ll y) { if(x > y) x = y; }

int main() {
    scanf("%d", &n);
    rep(i, n) rep(j, n) if(i != j) scanf("%d", &a[i][j]);
    rep(i, n) rep(j, n) {
        pre1[i][j] = pre1[i - 1][j] + pre1[i][j - 1] - pre1[i - 1][j - 1] + a[i][j];
        pre2[i][j] = pre2[i - 1][j] + pre2[i][j - 1] - pre2[i - 1][j - 1] + (i<j ? a[i][j] : 0);
    }

    memset(dp, 0x3f, sizeof dp);

    dp[0][0] = 0;
    for(int i = 0; i < n; ++i) {
        for(int last = 0; last <= i; ++last) {
            for(int j = i + 1; j <= n; ++j) {
                upd(dp[j][i], dp[i][last] + pre1[j][last] - pre1[i][last] + pre2[j][j] - pre2[i][j] - pre2[j][i] + pre2[i][i]);
            }
        }
    }
    ll ans = INF;
    for(int last = 0; last < n; ++last)
        upd(ans, dp[n][last]);
    printf("%lld\n", ans);

    return 0;
}

Score : 1200 points

Problem Statement

We have a weighted directed graph with N vertices numbered 0 to N-1.

The graph initially has N-1 edges. The i-th edge (0 \leq i \leq N-2) is directed from Vertex i to Vertex i+1 and has a weight of 0.

Snuke will now add a new edge (i → j) for every pair i, j (0 \leq i,j \leq N-1,\ i \neq j).
The weight of the edge will be -1 if i < j, and 1 otherwise.

Ringo is a boy. A negative cycle (a cycle whose total weight is negative) in a graph makes him sad.
He will delete some of the edges added by Snuke so that the graph will no longer contain a negative cycle.
The cost of deleting the edge (i → j) is A_{i,j}. He cannot delete edges that have been present from the beginning.

Find the minimum total cost required to achieve Ringo's objective.

Constraints

3 \leq N \leq 500

1 \leq A_{i,j} \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_{0,1} A_{0,2} A_{0,3} \cdots A_{0,N-1}
A_{1,0} A_{1,2} A_{1,3} \cdots A_{1,N-1}
A_{2,0} A_{2,1} A_{2,3} \cdots A_{2,N-1}
\vdots
A_{N-1,0} A_{N-1,1} A_{N-1,2} \cdots A_{N-1,N-2}

Output

Print the minimum total cost required to achieve Ringo's objective.

Sample Input 1

3
2 1
1 4
3 3

Sample Output 1

2

If we delete the edge (0 → 1) added by Snuke, the graph will no longer contain a negative cycle.
The cost will be 2 in this case, which is the minimum possible.

Sample Input 2

4
1 1 1
1 1 1
1 1 1
1 1 1

Sample Output 2

2

If we delete the edges (1 → 2) and (3 → 0) added by Snuke, the graph will no longer contain a negative cycle.
The cost will be 1+1=2 in this case, which is the minimum possible.

Sample Input 3

10
190587 2038070 142162180 88207341 215145790 38 2 5 20
32047998 21426 4177178 52 734621629 2596 102224223 5 1864
41 481241221 1518272 51 772 146 8805349 3243297 449
918151 126080576 5186563 46354 6646 491776 5750138 2897 161
3656 7551068 2919714 43035419 495 3408 26 3317 2698
455357 3 12 1857 5459 7870 4123856 2402 258
3 25700 16191 102120 971821039 52375 40449 20548149 16186673
2 16 130300357 18 6574485 29175 179 1693 2681
99 833 131 2 414045824 57357 56 302669472 95
8408 7 1266941 60620177 129747 41382505 38966 187 5151064

Sample Output 3

2280211


#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <stack>
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <cfloat>

#define Rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())

#define eb emplace_back
#define dbg(x) cout<<#x" = "<<((x))<<endl
#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

int main(){
  ll N;

  cin >> N;

#ifdef DEBUG
  cout << "N=" << endl;
#endif

  ll arr[N];
  ll max = 0;
  ll max2 = 0;
  int max_index = 0;
  for(int i = 0; i < N; i++){
    cin >> arr[i];
    if(arr[i] > max){
      max2 = max;
      max = arr[i];
      max_index = i;
    } else if (arr[i] > max2){
      max2 = arr[i];
    }
  }

  for(int i = 0; i < N; i++){
    if(i == max_index) cout << max2 << endl;
    else cout << max << endl;
  }

  return 0;
}

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5


#include <bits/stdc++.h>
using namespace std;

bool chmax(long long &a, long long b){
    if(a <= b){
        a = b;
        return true;
    }else{
        return false;
    }
}

int main(){
    int N;
    cin >> N;
    vector<long long> A(N);
    for(int i = 0; i < N; i++)cin >> A[i];
    reverse(A.begin(), A.end());
    long long now = A[0];
    int cnt = 1;
    for(int i = 1; i < N; i++){
        if(chmax(now, A[i])){
            cnt++;
        }
    }
    cout << cnt << endl;
    return 0;
}

Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.