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

int minPos(int arr[], int l, int r)
{
    int min = arr[l];
    int mpos = l;
    for(int i=l; i<=r; i++)
    {
        if(arr[i]<min)
        {
            min = arr[i];
            mpos = i;
        }
    }
    return mpos;
}
void mkEqual(int arr[], int left, int right, int minVal)
{
    for(int i=left; i<=right; i++)
    {
        arr[i] = minVal;
    }
}

int sum(int arr[], int l, int r)
{
    int total=0;
    for(int i=l; i<=r; i++) total+= arr[i];
    return total;
}
void update(int arr[], int l, int m, int r)
{
    if(l<r)
    {
        if(l==m)
        {
            int mPos = minPos(arr, l+1, r);
            update(arr, l+1, mPos, r);
        }
        else if(m==r)
        {
            int mPos = minPos(arr, l, r-1);
            update(arr, l, mPos, r-1);
        }
        else if(sum(arr, m+1, r) < sum(arr, l, m-1))
        {
            mkEqual(arr, m+1, r, arr[m]);
            int mPos = minPos(arr, l, m-1);
            update(arr, l, mPos, m-1);
        }
        else
        {
            mkEqual(arr, l, m-1, arr[m]);
            int mPos = minPos(arr, m+1, r);
            update(arr, m+1, mPos, r);
        }

    }
}

void display(int arr[], int s)
{
    for(int i=0; i<s; i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}
int main()
{
    int n; cin>>n;
    int m[n];
    for(int i=0; i<n; i++) cin>>m[i];
    display(m, n);
    int mPos = minPos(m,0,n-1);
    update(m, 0, mPos, n-1);
    display(m, n);
    // cout<<"sum: "<<sum(m, 0, n-1)<<endl;
    return 0;
}


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

int mkPeak(ll *arr, int left, int right, int peakPos)
{
    ll prev = arr[peakPos];
    ll sum =arr[peakPos];
    for(int i=peakPos+1; i<=right; i++)
    {
        if(arr[i]>prev) arr[i] = prev;
        else prev = arr[i];
        sum+= prev;
    }
    prev = arr[peakPos];
    for(int i=peakPos-1; i>=left; i--)
    {
        if(arr[i]>prev) arr[i] = prev;
        else prev = arr[i];
        sum+= prev;
        // cout<<"prev: "<<prev<<endl;
    }
    return sum;
}
void updateArr(ll arr[], int left, int right, int peakPos)
{
    ll prev = arr[peakPos];
    for(int i=peakPos+1; i<=right; i++)
    {
        if(arr[i]>prev) arr[i] = prev;
        else prev = arr[i];
    }
    prev = arr[peakPos];
    for(int i=peakPos-1; i>=left; i--)
    {
        if(arr[i]>prev) arr[i] = prev;
        else prev = arr[i];
    }
}

void display(ll arr[], int s)
{
    for(int i=0; i<s; i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}
int main()
{
    int n; cin>>n;
    ll m[n];
    for(int i=0; i<n; i++) cin>>m[i];
    display(m, n);
    ll maxSum=0;
    int peakPosition = 0;
    for(int i=0; i<n; i++)
    {
        ll temp[n]; //temp array to check a case
        for(int i=0; i<n; i++) temp[i] = m[i];
        ll sum = mkPeak(temp, 0, n-1, i);
        if(sum>maxSum)
        {
            maxSum = sum;
            peakPosition = i;
        }
    }

    updateArr(m, 0, n-1, peakPosition);
    display(m, n);
    return 0;
}


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

int mkPeak(ll *arr, int left, int right, int peakPos)
{
    ll prev = arr[peakPos];
    ll sum =arr[peakPos];
    for(int i=peakPos+1; i<=right; i++)
    {
        if(arr[i]< prev) prev = arr[i];
        sum+= prev;
    }
    prev = arr[peakPos];
    for(int i=peakPos-1; i>=left; i--)
    {
        if(arr[i] < prev) prev = arr[i];
        sum+= prev;
        // cout<<"prev: "<<prev<<endl;
    }
    return sum;
}
void updateArr(ll arr[], int left, int right, int peakPos)
{
    ll prev = arr[peakPos];
    for(int i=peakPos+1; i<=right; i++)
    {
        if(arr[i]>prev) arr[i] = prev;
        else prev = arr[i];
    }
    prev = arr[peakPos];
    for(int i=peakPos-1; i>=left; i--)
    {
        if(arr[i]>prev) arr[i] = prev;
        else prev = arr[i];
    }
}

void display(ll arr[], int s)
{
    for(int i=0; i<s; i++)
    {
        cout<<arr[i]<<" ";
    }
    // cout<<endl;
}
int main()
{
    int n; cin>>n;
    ll m[n];
    for(int i=0; i<n; i++) cin>>m[i];
    // display(m, n);
    ll maxSum=0;
    int peakPosition = 0;
    for(int i=0; i<n; i++)
    {
        ll sum = mkPeak(m, 0, n-1, i);
        if(sum>maxSum)
        {
            maxSum = sum;
            peakPosition = i;
        }
    }

    updateArr(m, 0, n-1, peakPosition);
    display(m, n);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int mini(vector<int> a, vector<int> check){
    int m=INT_MAX, mi=-1;
    for(int i=0;i<a.size();i++){
        if(check[i]!=-1){
            if(a[i]<m){
                m = a[i];
                mi = i;
            }
        }
    }
    return mi;
}

int mx_elmnt(vector<int> a, int i, int j, int h){
    int ma=0;
    for(int k=i;k<=j;k++) {
        if(a[k]>h) ma+= a[k]-h;
    }
    return ma;
}

void set_arr(vector<int> &a,vector<int> &check, int i, int j, int g){
    for(int k=i;k<=j;k++){
        if(a[k]>g){
            a[k]=g;
            check[k] = -1;
        }
    }
}

void print(vector<int> a){
    for(int i=0;i<a.size();i++) cout<<a[i]<<" ";
    cout<<'\n';
}

int main()
{
    int n;cin>>n;
    vector<int> a(n), check(n);
    for(int i=0;i<n;i++) cin>>a[i];
    check = a;
    int r=0;
    while(1){
        r = mini(a, check);
        if(r==-1) break;
        check[r] = -1;
        int m1 = mx_elmnt(a, 0, r-1, a[r]);
        int m2 = mx_elmnt(a, r+1, n-1, a[r]);
        //cout<<r<<" "<<m1<<" "<<m2<<'\n';
        if(m1!=0 && m2!=0){
            if(m1<m2) set_arr(a, check, 0, r-1, a[r]);
            else set_arr(a, check, r+1, n-1, a[r]);
        }
    }
    for(int i=0;i<n;i++) cout<<a[i]<<" ";
    return 0;
}


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

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

ll lcm(ll a, ll b){
    return (a*b)/gcd(a,b);
}

ll power(ll x, ll y){
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

vector<ll> util(vector<ll>& a, int x){
    ll cur=a[x], n=a.size();
    vector<ll> b(a.size());
    b[x] = a[x];
    // ans += b[x];
    for(int i=x-1;i>=0;i--){
        b[i] = min(a[i], cur);
        cur = b[i];
        // ans += b[i];
    }
    cur = b[x];
    for(int i=x+1;i<n;i++){
        b[i] = min(a[i], cur);
        cur = b[i];
        // ans += b[i];
    }
    return b;
}

void solve(){
    int n;cin>>n;
    vector<ll> a(n),b(n), l(n), r(n);
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=0;i<n;i++){
        int j=-1;
        int x = min_element(a.begin(), a.begin()+i+1)-a.begin();
        if(a[x]==a[i]){
            l[i] = (i+1)*a[i];
        }
        else{
            for(int y=i-1;y>=0;y--){
                if(a[y]<a[i]){
                    j = y;
                    break;
                }
            }
            l[i] = l[j] + (i-j)*a[i];
        }
        // cout<<l[i]<<' ';
    }

    for(int i=n-1;i>=0;i--){
        int j=-1;
        int x = min_element(a.begin()+i, a.end())-a.begin();
        if(a[x]==a[i]){
            r[i] = (n-i)*a[i];
        }
        else{
            for(int y=i+1;y<n;y++){
                if(a[y]<a[i]){
                    j = y;
                    break;
                }
            }
            r[i] = r[j] + (j-i)*a[i];
        }
    }
    int peak;ll m=-1;
    for(int i=0;i<n;i++){
        ll w = l[i]+r[i]-a[i];
        // cout<<l[i]<<' '<<r[i]<<' '<<a[i]<<endl;
        if(w>m){
            m = w;
            peak=i;
        }
    }
    // cout<<peak<<' ';
    b = util(a, peak);
    for(int i=0;i<n;i++)cout<<b[i]<<' ';
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input1.txt","r",stdin);
        freopen("output1.txt", "w", stdout);
    #endif
    int t;
    t=1;
    // cin>>t;

    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int mini(vector<int> a, vector<int> check){
    int m=INT_MAX, mi=-1;
    for(int i=0;i<a.size();i++){
        if(check[i]!=-1){
            if(a[i]<m){
                m = a[i];
                mi = i;
            }
        }
    }
    return mi;
}

long long mx_elmnt(vector<int> a, int i, int j, int h){
    long long ma=0;
    for(int k=i;k<=j;k++) {
        if(a[k]>h) ma+= a[k]-h;
    }
    return ma;
}

void set_arr(vector<int> &a,vector<int> &check, int i, int j, int g){
    for(int k=i;k<=j;k++){
        if(a[k]>g){
            a[k]=g;
            check[k] = -1;
        }
    }
}

void print(vector<int> a){
    for(int i=0;i<a.size();i++) cout<<a[i]<<" ";
    cout<<'\n';
}

int main()
{
    int n;cin>>n;
    vector<int> a(n), check(n);
    for(int i=0;i<n;i++) cin>>a[i];
    check = a;
    int r=0;
    while(1){
        r = mini(a, check);
        if(r==-1) break;
        check[r] = -1;
        long long m1 = mx_elmnt(a, 0, r-1, a[r]);
        long long m2 = mx_elmnt(a, r+1, n-1, a[r]);
        //cout<<r<<" "<<m1<<" "<<m2<<'\n';
        if(m1!=0 && m2!=0){
            if(m1<m2) set_arr(a, check, 0, r-1, a[r]);
            else set_arr(a, check, r+1, n-1, a[r]);
        }
    }
    for(int i=0;i<n;i++) cout<<a[i]<<" ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{

        int n; cin>>n;
        vector<int> a(n), b(n);
        for(int i=0;i<n;i++) cin>>a[i];
        b[0] = a[0];
        char x = 'i';
        for(int i=1;i<n;i++){
            if(a[i]>a[i-1] && x=='i'){
                b[i] = a[i];
            }else if(a[i]> a[i-1] && x=='d'){
                b[i] = a[i-1];
                a[i] = a[i-1];
                x = 'i';
            }else {
                x = 'd';
                b[i] = a[i];
            }
        }
        for(int i=0;i<n;i++){
            cout<<b[i]<<" ";
        }

    return 0;
}


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

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

ll lcm(ll a, ll b){
    return (a*b)/gcd(a,b);
}

ll power(ll x, ll y){
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

void solve(){
    int n;cin>>n;
    vector<int> a(n),b(n);
    for(int i=0;i<n;i++)cin>>a[i];
    int x = max_element(a.begin(), a.end()) - a.begin();
    int cur=a[x];
    b[x] = a[x];
    for(int i=x-1;i>=0;i--){
        b[i] = min(a[i], cur);
        cur = b[i];
    }
    cur = b[x];
    for(int i=x+1;i<n;i++){
        b[i] = min(a[i], cur);
        cur = b[i];
    }
    for(int i=0;i<n;i++)cout<<b[i]<<' ';
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input1.txt","r",stdin);
        freopen("output1.txt", "w", stdout);
    #endif
    int t;
    t=1;
    // cin>>t;

    while(t--){
        solve();
    }
    return 0;
}


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

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

ll lcm(ll a, ll b){
    return (a*b)/gcd(a,b);
}

ll power(ll x, ll y){
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

vector<int> util(vector<int>& a, int x, ll & ans){
    int cur=a[x];
    vector<int> b(a.size());
    b[x] = a[x];
    ans += b[x];
    for(int i=x-1;i>=0;i--){
        b[i] = min(a[i], cur);
        cur = b[i];
        ans += b[i];
    }
    cur = b[x];
    for(int i=x+1;i<n;i++){
        b[i] = min(a[i], cur);
        cur = b[i];
        ans += b[i];
    }
    return b;
}

void solve(){
    int n;cin>>n;
    vector<int> a(n),b(n);
    for(int i=0;i<n;i++)cin>>a[i];
    int x = max_element(a.begin(), a.end()) - a.begin();
    vector<int> ind;
    for(int i=0;i<n;i++){
        if(a[i]==a[x]){
            ind.push_back(i);
        }
    }
    ll ans=-1;
    for(int i=0;i<ind.size();i++){
        ll k=0;
        vector<int> temp = util(a, ind[i], k);
        if(k>ans){
            ans = k;
            for(int i=0;i<n;i++)b[i]=temp[i];
        }
    }
    for(int i=0;i<n;i++)cout<<b[i]<<' ';
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input1.txt","r",stdin);
        freopen("output1.txt", "w", stdout);
    #endif
    int t;
    t=1;
    // cin>>t;

    while(t--){
        solve();
    }
    return 0;
}


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

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

ll lcm(ll a, ll b){
    return (a*b)/gcd(a,b);
}

ll power(ll x, ll y){
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

vector<int> util(vector<int>& a, int x){
    int cur=a[x], n=a.size();
    vector<int> b(a.size());
    b[x] = a[x];
    // ans += b[x];
    for(int i=x-1;i>=0;i--){
        b[i] = min(a[i], cur);
        cur = b[i];
        // ans += b[i];
    }
    cur = b[x];
    for(int i=x+1;i<n;i++){
        b[i] = min(a[i], cur);
        cur = b[i];
        // ans += b[i];
    }
    return b;
}

void solve(){
    int n;cin>>n;
    vector<int> a(n),b(n), l(n), r(n);
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=0;i<n;i++){
        int j=-1;
        int x = min_element(a.begin(), a.begin()+i+1)-a.begin();
        if(x==i){
            l[i] = (i+1)*a[i];
        }
        else{
            for(int y=i-1;y>=0;y--){
                if(a[y]<a[i]){
                    j = y;
                    break;
                }
            }
            l[i] = l[j] + (i-j)*a[i];
        }
        // cout<<l[i]<<' ';
    }

    for(int i=n-1;i>=0;i--){
        int j=-1;
        int x = min_element(a.begin()+i, a.end())-a.begin();
        if(x==i){
            r[i] = (n-i)*a[i];
        }
        else{
            for(int y=i+1;y<n;y++){
                if(a[y]<a[i]){
                    j = y;
                    break;
                }
            }
            r[i] = r[j] + (j-i)*a[i];
        }
    }
    int peak,m=-1;
    for(int i=0;i<n;i++){
        int w = l[i]+r[i]-a[i];
        if(w>m){
            m = w;
            peak=i;
        }
    }
    // a[peak] = m;
    b = util(a, peak);
    for(int i=0;i<n;i++)cout<<b[i]<<' ';
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input1.txt","r",stdin);
        freopen("output1.txt", "w", stdout);
    #endif
    int t;
    t=1;
    // cin>>t;

    while(t--){
        solve();
    }
    return 0;
}


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

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

ll lcm(ll a, ll b){
    return (a*b)/gcd(a,b);
}

ll power(ll x, ll y){
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

vector<int> util(vector<int>& a, int x, ll & ans){
    int cur=a[x], n=a.size();
    vector<int> b(a.size());
    b[x] = a[x];
    ans += b[x];
    for(int i=x-1;i>=0;i--){
        b[i] = min(a[i], cur);
        cur = b[i];
        ans += b[i];
    }
    cur = b[x];
    for(int i=x+1;i<n;i++){
        b[i] = min(a[i], cur);
        cur = b[i];
        ans += b[i];
    }
    return b;
}

void solve(){
    int n;cin>>n;
    vector<int> a(n),b(n);
    for(int i=0;i<n;i++)cin>>a[i];
    int x = max_element(a.begin(), a.end()) - a.begin();
    vector<int> ind;
    for(int i=0;i<n;i++){
        if(a[i]==a[x]){
            ind.push_back(i);
        }
    }
    ll ans=-1;
    for(int i=0;i<ind.size();i++){
        ll k=0;
        vector<int> temp = util(a, ind[i], k);
        if(k>ans){
            ans = k;
            for(int i=0;i<n;i++)b[i]=temp[i];
        }
    }
    for(int i=0;i<n;i++)cout<<b[i]<<' ';
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input1.txt","r",stdin);
        freopen("output1.txt", "w", stdout);
    #endif
    int t;
    t=1;
    // cin>>t;

    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int mini(vector<int> a, vector<int> check){
    int m=INT_MAX, mi=-1;
    for(int i=0;i<a.size();i++){
        if(check[i]!=-1){
            if(a[i]<m){
                m = a[i];
                mi = i;
            }
        }
    }
    return mi;
}

long long mx_elmnt(vector<int> a, int i, int j, int h){
    long long ma=0;
    for(int k=i;k<=j;k++) {
        if(a[k]>h) ma+= a[k]-h;
    }
    return ma;
}

void set_arr(vector<int> &a,vector<int> &check, int i, int j, int g){
    for(int k=i;k<=j;k++){
        if(a[k]>=g){
            a[k]=g;
            check[k] = -1;
        }
    }
}

void print(vector<int> a){
    for(int i=0;i<a.size();i++) cout<<a[i]<<" ";
    cout<<'\n';
}

int main()
{
    int n;cin>>n;
    vector<int> a(n), check(n);
    for(int i=0;i<n;i++) cin>>a[i];
    check = a;
    int r=0;
    while(1){
        r = mini(a, check);
        if(r==-1) break;
        check[r] = -1;
        long long m1 = mx_elmnt(a, 0, r-1, a[r]);
        long long m2 = mx_elmnt(a, r+1, n-1, a[r]);
        //cout<<r<<" "<<m1<<" "<<m2<<'\n';
        if(m1!=0 && m2!=0){
            if(m1<m2) set_arr(a, check, 0, r-1, a[r]);
            else set_arr(a, check, r+1, n-1, a[r]);
        }
    }
    for(int i=0;i<n;i++) cout<<a[i]<<" ";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int mini(vector<int> a, vector<int> check){
    int m=INT_MAX, mi=-1;
    for(int i=0;i<a.size();i++){
        if(check[i]!=-1){
            if(a[i]<m){
                m = a[i];
                mi = i;
            }
        }
    }
    return mi;
}

int mx_elmnt(vector<int> a, int i, int j){
    int ma=-1;
    for(int k=i;k<=j;k++) ma = max(ma, a[k]);
    return ma;
}

void set_arr(vector<int> &a,vector<int> &check, int i, int j, int g){
    for(int k=i;k<=j;k++){
        if(a[k]>g){
            a[k]=g;
            check[k] = -1;
        }
    }
}

void print(vector<int> a){
    for(int i=0;i<a.size();i++) cout<<a[i]<<" ";
    cout<<'\n';
}

int main()
{
    int n;cin>>n;
    vector<int> a(n), check(n);
    for(int i=0;i<n;i++) cin>>a[i];
    check = a;
    int r=0;
    while(r!=-1){
        r = mini(a, check);
        check[r] = -1;
        int m1 = mx_elmnt(a, 0, r-1);
        int m2 = mx_elmnt(a, r+1, n-1);
        //cout<<r<<" "<<m1<<" "<<m2<<'\n';
        if(a[r]<m1 && a[r]<m2){
            if(m1<m2) set_arr(a, check, 0, r-1, a[r]);
            else set_arr(a, check, r+1, n-1, a[r]);
        }
    }
    for(int i=0;i<n;i++) cout<<a[i]<<" ";
    return 0;
}


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

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

ll lcm(ll a, ll b){
    return (a*b)/gcd(a,b);
}

ll power(ll x, ll y){
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
        return temp*temp;
    else
        return x*temp*temp;
}

vector<int> util(vector<int>& a, int x){
    int cur=a[x], n=a.size();
    vector<int> b(a.size());
    b[x] = a[x];
    // ans += b[x];
    for(int i=x-1;i>=0;i--){
        b[i] = min(a[i], cur);
        cur = b[i];
        // ans += b[i];
    }
    cur = b[x];
    for(int i=x+1;i<n;i++){
        b[i] = min(a[i], cur);
        cur = b[i];
        // ans += b[i];
    }
    return b;
}

void solve(){
    int n;cin>>n;
    vector<int> a(n),b(n), l(n), r(n);
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=0;i<n;i++){
        int j=-1;
        int x = min_element(a.begin(), a.begin()+i+1)-a.begin();
        if(a[x]==a[i]){
            l[i] = (i+1)*a[i];
        }
        else{
            for(int y=i-1;y>=0;y--){
                if(a[y]<a[i]){
                    j = y;
                    break;
                }
            }
            l[i] = l[j] + (i-j)*a[i];
        }
        // cout<<l[i]<<' ';
    }

    for(int i=n-1;i>=0;i--){
        int j=-1;
        int x = min_element(a.begin()+i, a.end())-a.begin();
        if(a[x]==a[i]){
            r[i] = (n-i)*a[i];
        }
        else{
            for(int y=i+1;y<n;y++){
                if(a[y]<a[i]){
                    j = y;
                    break;
                }
            }
            r[i] = r[j] + (j-i)*a[i];
        }
    }
    int peak,m=-1;
    for(int i=0;i<n;i++){
        int w = l[i]+r[i]-a[i];
        // cout<<l[i]<<' '<<r[i]<<' '<<a[i]<<endl;
        if(w>m){
            m = w;
            peak=i;
        }
    }
    // cout<<peak<<' ';
    b = util(a, peak);
    for(int i=0;i<n;i++)cout<<b[i]<<' ';
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input1.txt","r",stdin);
        freopen("output1.txt", "w", stdout);
    #endif
    int t;
    t=1;
    // cin>>t;

    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int mini(vector<int> a, vector<int> check){
    int m=INT_MAX, mi=-1;
    for(int i=0;i<a.size();i++){
        if(check[i]!=-1){
            if(a[i]<m){
                m = a[i];
                mi = i;
            }
        }
    }
    return mi;
}

int mx_elmnt(vector<int> a, int i, int j){
    int ma=-1;
    for(int k=i;k<=j;k++) ma = max(ma, a[k]);
    return ma;
}

void set_arr(vector<int> &a,vector<int> &check, int i, int j, int g){
    for(int k=i;k<=j;k++){
        if(a[k]>=g){
            a[k]=g;
            check[k] = -1;
        }
    }
}

void print(vector<int> a){
    for(int i=0;i<a.size();i++) cout<<a[i]<<" ";
    cout<<'\n';
}

int main()
{
    int n;cin>>n;
    vector<int> a(n), check(n);
    for(int i=0;i<n;i++) cin>>a[i];
    check = a;
    int r=0;
    while(1){
        r = mini(a, check);
        if(r==-1) break;
        check[r] = -1;
        int m1 = mx_elmnt(a, 0, r-1);
        int m2 = mx_elmnt(a, r+1, n-1);
        //cout<<r<<" "<<m1<<" "<<m2<<'\n';
        if(a[r]<m1 && a[r]<m2){
            if(m1<m2) set_arr(a, check, 0, r-1, a[r]);
            else set_arr(a, check, r+1, n-1, a[r]);
        }
    }
    for(int i=0;i<n;i++) cout<<a[i]<<" ";
    return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  int n;
  cin >> n;
  vector<int> vc(n);
  for (int i = 0; i < n; i++) cin >> vc[i];
  vector<int> vc2 = vc;
  for (int i = 0; i < n; i++) {
    int mx = 0;
    bool chk = false;
    for (int j = n; j >= i; j--) {
      if (vc[j] < mx) chk = true;
      mx = max(mx, vc[j]);
    }
    if (!chk) continue;
    for (int j = 0; j < i - 1; j++) {
      vc[j] = min(vc[j], vc[i]);
    }
  }
  for (int i = 0; i < n; i++) {
    int mx = 0;
    bool chk = false;
    for (int j = 0; j <= i; j++) {
      if (vc2[j] < mx) chk = true;
      mx = max(mx, vc2[j]);
    }
    if (!chk) continue;
    for (int j = i + 1; j < n; j++) {
      vc2[j] = min(vc2[j], vc2[i]);
    }
  }
  int s1 = 0, s2 = 0;
  s1 = accumulate(vc.begin(), vc.end(), 0);
  s2 = accumulate(vc2.begin(), vc2.end(), 0);
  if (s1 > s2) {
    for (auto i : vc) cout << i << ' ';
  } else {
    for (auto i : vc2) cout << i << ' ';
  }
  return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  int n;
  cin >> n;
  vector<int> vc1(n);
  for (int i = 0; i < n; i++) cin >> vc1[i];
  vector<int> vc2 = vc1;
  int mx = 0;
  int idx = -1;
  for (int i = n - 1; i >= 0; i--) {
    if (vc1[i] < mx) {
      idx = i;
      break;
    }
    mx = max(mx, vc1[i]);
  }
  for (int i = idx; i >= 0; i--) {
    for (int j = 0; j < i; j++) {
      vc1[j] = min(vc1[j], vc1[i]);
    }
  }
  idx = n, mx = 0;
  for (int i = 0; i < n; i++) {
    if (vc2[i] < mx) {
      idx = i;
      break;
    }
    mx = max(mx, vc2[i]);
  }
  for (int i = idx; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      vc2[j] = min(vc2[j], vc2[i]);
    }
  }
  int s1 = 0, s2 = 0;
  for (int i = 0; i < n; i++) s1 += vc1[i];
  for (int i = 0; i < n; i++) s2 += vc2[i];
  if (s1 > s2) {
    for (auto i : vc1) cout << i << ' ';
  } else {
    for (auto i : vc2) cout << i << ' ';
  }
  return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  int n;
  cin >> n;
  vector<int> vc1(n);
  for (int i = 0; i < n; i++) cin >> vc1[i];
  vector<int> vc2;
  int best = 0;
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    vc2 = vc1;
    int mn = 1e13;
    int s = vc2[i];
    for (int j = i; j >= 0; j--) {
      vc2[j] = min(vc2[j], mn);
      mn = min(mn, vc2[j]);
      s += vc2[j];
    }
    s -= (2 * vc2[i]);
    mn = 1e13;
    for (int j = i; j < n; j++) {
      vc2[j] = min(vc2[j], mn);
      mn = min(vc2[j], mn);
      s += vc2[j];
    }
    if (s > best) {
      best = s;
      ans = vc2;
    }
  }
  for (auto i : ans) cout << i << ' ';
  return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int dp[1002][1002];

int n;
vector<int> vc;

const int mx = (int) 2e13;

int rec(int idx, int pr) {
  if (idx == n-1) return vc[idx];
  if (idx == n) return 0;
  int &ans = dp[idx][pr];
  if (ans != -1) return ans;
  ans = INT_MIN;
  if (vc[pr] > vc[idx] and vc[idx + 1] > vc[idx]) {
    ans = vc[idx] - abs(vc[idx] - vc[pr]) + rec(idx+1, idx);
    ans = max(ans, vc[idx] + vc[idx + 1] - abs(vc[idx + 1] - vc[idx]) + rec(idx + 2, idx));
  } else {
    ans = vc[idx] + rec(idx + 1, idx);
  }
  return ans;
}

vector<int> ans2;

void trace(int idx, int pr) {
  if (idx == n-1) {
    ans2[idx] = vc[idx];
    // return vc[idx];
    return;
  }
  if (idx == n) return;
  int ans = rec(idx, pr);
  // if (ans != -1) return ans;
  // ans = INT_MIN;
  if (vc[pr] > vc[idx] and vc[idx + 1] > vc[idx]) {
    if (ans == (vc[idx] - abs(vc[idx] - vc[pr]) + rec(idx+1, idx))) {
      ans2[idx] = vc[idx];
      ans2[pr] = vc[idx];
      trace(idx + 1, idx);
    } else {
      ans2[idx] = vc[idx];
      ans2[idx + 1] = vc[idx];
      trace(idx + 2, idx);
    }
    // ans = vc[idx] - abs(vc[idx] - vc[pr]) + rec(idx+1, idx);
    // ans = max(ans, vc[idx] + vc[idx + 1] - abs(vc[idx + 1] - vc[idx]) + rec(idx + 2, idx));
  } else {
    ans2[idx] = vc[idx];
    trace(idx + 1, idx);
    // ans = vc[idx] + rec(idx + 1, idx);
  }
  // return ans;
}

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  cin >> n;
  vc.resize(n);
  ans2.resize(n);
  for (int i = 0; i < n; i++) cin >> vc[i];
  memset(dp, -1, sizeof(dp));
  rec(0, 0);
  // cout << rec(0, 0) << endl;
  trace(0, 0);
  for (auto i : ans2) cout << i << ' ';
  return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int dp[1002][1002];

int n;
vector<int> vc;

const int mx = (int) 2e13;

int rec(int idx, int pr) {
  if (idx == n-1) return vc[idx];
  if (idx == n) return 0;
  int &ans = dp[idx][pr];
  if (ans != -1) return ans;
  ans = INT_MIN;
  if (vc[pr] > vc[idx] and vc[idx + 1] > vc[idx]) {
    ans = vc[idx] - abs(vc[idx] - vc[pr]) + rec(idx+1, idx);
    ans = max(ans, vc[idx] + vc[idx + 1] - abs(vc[idx + 1] - vc[idx]) + rec(idx + 2, idx));
  } else {
    ans = vc[idx] + rec(idx + 1, idx);
  }
  return ans;
}

vector<int> ans2;

void trace(int idx, int pr) {
  if (idx == n-1) {
    ans2[idx] = vc[idx];
    // return vc[idx];
    return;
  }
  if (idx == n) return;
  int ans = rec(idx, pr);
  // if (ans != -1) return ans;
  // ans = INT_MIN;
  if (vc[pr] > vc[idx] and vc[idx + 1] > vc[idx]) {
    if (ans == (vc[idx] - abs(vc[idx] - vc[pr]) + rec(idx+1, idx))) {
      ans2[idx] = vc[idx];
      ans2[pr] = vc[idx];
      trace(idx + 1, idx);
    } else {
      ans2[idx] = vc[idx];
      ans2[idx + 1] = vc[idx];
      trace(idx + 2, idx);
    }
    // ans = vc[idx] - abs(vc[idx] - vc[pr]) + rec(idx+1, idx);
    // ans = max(ans, vc[idx] + vc[idx + 1] - abs(vc[idx + 1] - vc[idx]) + rec(idx + 2, idx));
  } else {
    ans2[idx] = vc[idx];
    trace(idx + 1, idx);
    // ans = vc[idx] + rec(idx + 1, idx);
  }
  // return ans;
}

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  cin >> n;
  vc.resize(n);
  ans2.resize(n);
  for (int i = 0; i < n; i++) cin >> vc[i];
  memset(dp, -1, sizeof(dp));
  // cout << rec(0, 0) << endl;
  trace(0, 0);
  for (auto i : ans2) cout << i << ' ';
  return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  int n;
  cin >> n;
  vector<int> vc(n);
  for (int i = 0; i < n; i++) cin >> vc[i];
  vector<int> vc2 = vc;
  for (int i = 0; i < n; i++) {
    int mx = 0;
    bool chk = false;
    for (int j = n - 1; j >= i; j--) {
      if (vc[j] < mx) chk = true;
      mx = max(mx, vc[j]);
    }
    if (!chk) continue;
    for (int j = 0; j < i; j++) {
      vc[j] = min(vc[j], vc[i]);
    }
  }
  for (int i = 0; i < n; i++) {
    int mx = 0;
    bool chk = false;
    for (int j = 0; j <= i; j++) {
      if (vc2[j] < mx) chk = true;
      mx = max(mx, vc2[j]);
    }
    if (!chk) continue;
    for (int j = i + 1; j < n; j++) {
      vc2[j] = min(vc2[j], vc2[i]);
    }
  }
  int s1 = 0, s2 = 0;
  s1 = accumulate(vc.begin(), vc.end(), 0);
  s2 = accumulate(vc2.begin(), vc2.end(), 0);
  if (s1 > s2) {
    for (auto i : vc) cout << i << ' ';
    // cout << 'd';
  } else {
    for (auto i : vc2) cout << i << ' ';
  }
  return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  int n;
  cin >> n;
  vector<int> vc1(n);
  for (int i = 0; i < n; i++) cin >> vc1[i];
  vector<int> vc2;
  int best = 0;
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    vc2 = vc1;
    int mn = 1e13;
    int s = vc2[i];
    for (int j = i; j >= 0; j--) {
      vc2[j] = min(vc2[j], mn);
      mn = min(mn, vc2[j]);
      s += vc2[j];
    }
    mn = 1e13;
    for (int j = i; j < n; j++) {
      vc2[j] = min(vc2[j], mn);
      mn = min(vc2[j], mn);
      s += vc2[j];
    }
    if (s > best) {
      best = s;
      ans = vc2;
    }
  }
  for (auto i : ans) cout << i << ' ';
  return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  int n;
  cin >> n;
  vector<int> vc1(n);
  for (int i = 0; i < n; i++) cin >> vc1[i];
  vector<int> vc2;
  int best = 0;
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    vc2 = vc1;
    int mn = 1e13;
    int s = vc2[i];
    for (int j = i - 1; j >= 0; j--) {
      vc2[j] = min(vc2[j], mn);
      mn = min(mn, vc2[j]);
      s += vc2[j];
    }
    mn = 1e13;
    for (int j = i + 1; j < n; j++) {
      vc2[j] = min(vc2[j], mn);
      mn = min(vc2[j], mn);
      s += vc2[j];
    }
    vector<int> pre(n), suff(n);
    int mx = 0;
    mx = vc2[0];
    pre[0] = 0;
    for (int j = 1; j < n; j++) {
      // pre[i]
      pre[j] = mx;
      mx = max(mx, vc2[j]);
    }
    mx = vc2[n-1];
    suff[n-1] = 0;
    for (int j = n-2; j >= 0; j--) {
      // pre[i]
      suff[j] = mx;
      mx = max(mx, vc2[j]);
    }
    bool chk = true;
    for (int j = 0; j < n; j++) {
      if (pre[j] > vc2[j] and suff[j] > vc2[j]) {
        chk = false;
        break;
      }
    }
    if (chk and s > best) {
      best = s;
      ans = vc2;
    }
  }
  for (auto i : ans) cout << i << ' ';
  return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  int n;
  cin >> n;
  vector<int> vc(n);
  for (int i = 0; i < n; i++) cin >> vc[i];
  vector<int> vc2 = vc;
  for (int i = 0; i < n; i++) {
    int mx = 0;
    bool chk = false;
    for (int j = n; j >= i; j--) {
      if (vc[j] < mx) chk = true;
      mx = max(mx, vc[j]);
    }
    if (!chk) continue;
    for (int j = 0; j < i - 1; j++) {
      vc[j] = min(vc[j], vc[i]);
    }
  }
  for (int i = 0; i < n; i++) {
    int mx = 0;
    bool chk = false;
    for (int j = 0; j <= i; j++) {
      if (vc[j] < mx) chk = true;
      mx = max(mx, vc[j]);
    }
    if (!chk) continue;
    for (int j = i + 1; j < n; j++) {
      vc2[j] = min(vc2[j], vc2[i]);
    }
  }
  int s1 = 0, s2 = 0;
  s1 = accumulate(vc.begin(), vc.end(), 0);
  s2 = accumulate(vc2.begin(), vc2.end(), 0);
  if (s1 > s2) {
    for (auto i : vc) cout << i << ' ';
  } else {
    for (auto i : vc2) cout << i << ' ';
  }
  return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  int n;
  cin >> n;
  vector<int> vc(n);
  for (int i = 0; i < n; i++) {
    cin >> vc[i];
  }
  int best = 0;
  vector<int> ans;
  for (int i = 0; i < n; i++) {
    int s1, s2, ss1, ss2;
    s1 = s2 = ss1 = ss2 = 0;
    for (int j = 0; j < i; j++) {
      s1 += min(vc[j], vc[i]);
      ss1 += vc[j];
    }

    for (int j = i + 1; j < n; j++) {
      s2 += min(vc[j], vc[i]);
      ss2 += vc[j];
    }

    if (ss1 - s1 < ss2 - s2) {
      if (s1 + ss2 > best) {
        best = s1 + ss2;
        ans.clear();
        for (int j = 0; j < i; j++) {
          ans.pb(min(vc[i], vc[j]));
        }
        ans.pb(vc[i]);
        for (int j = i + 1; j < n; j++) {
          ans.pb(vc[j]);
        }
      }
    } else {
      if (ss1 + s2 > best) {
        best = ss1 + s2;
        ans.clear();
        for (int j = 0; j < i; j++) {
          ans.pb(vc[j]);
        }
        ans.pb(vc[i]);
        for (int j = i + 1; j < n; j++) {
          ans.pb(min(vc[i], vc[j]));
        }
      }
    }
  }
  for (auto i : ans) cout << i << ' ';
  return 0;
}


//verma_ankit484

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

#define int long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
const int mod = (int) 1e9+7;

using namespace std;
//using namespace __gnu_pbds;

//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

int32_t main() {
  IOS
  #ifdef AV
  freopen("input.txt", "r", stdin);
  #endif
  int n;
  cin >> n;
  vector<int> vc1(n);
  for (int i = 0; i < n; i++) cin >> vc1[i];
  vector<int> vc2 = vc1;
  int mx = 0;
  int idx = -1;
  for (int i = n - 1; i >= 0; i--) {
    if (vc1[i] < mx) {
      idx = i;
      break;
    }
    mx = max(mx, vc1[i]);
  }
  for (int i = idx; i >= 0; i--) {
    for (int j = 0; j < i; j++) {
      vc1[j] = min(vc1[j], vc1[i]);
    }
  }
  idx = -1, mx = 0;
  for (int i = 0; i < n; i++) {
    if (vc2[i] < mx) {
      idx = i;
      break;
    }
    mx = max(mx, vc2[i]);
  }
  for (int i = idx; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      vc2[j] = min(vc2[j], vc2[i]);
    }
  }
  int s1 = 0, s2 = 0;
  for (int i = 0; i < n; i++) s1 += vc1[i];
  for (int i = 0; i < n; i++) s2 += vc2[i];
  if (s1 > s2) {
    for (auto i : vc1) cout << i << ' ';
  } else {
    for (auto i : vc2) cout << i << ' ';
  }
  return 0;
}


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

using namespace std;

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	int n; cin >> n;
	int h[1010];

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

	vector<int> ansv;
	long long ans = 0;

	for (int i = 0; i < n; i++) {
		long long cur = 0;
		vector<int> curv;
		bool flag = false;
		for (int j = 0; j < n; j++) { //i까지 증가 이후부터 감소 한다고 칠때
			if (j == 0) {
				if (h[j] > h[j + 1]) {
					cur += h[j + 1];
					curv.push_back(h[j + 1]);
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
				continue;
			}

			if (j == n - 1) {
				if (j > i) {
					if (h[j] > curv.back()) {
						cur += curv.back();
						curv.push_back(curv.back());
					}
					else {
						cur += h[j];
						curv.push_back(h[j]);
					}
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
				continue;
			}

			if (j <= i) {
				if(h[j + 1] < curv.back()) {
					flag = true;
					continue;
				}
				if (j == i || h[j] <= h[j + 1]) {
					cur += h[j];
					curv.push_back(h[j]);
				}
				else {
					cur += h[j + 1];
					curv.push_back(h[j+1]);
				}
			}
			else {
				if (h[j] > curv.back()) {
					cur += curv.back();
					curv.push_back(curv.back());
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
			}
		}
		if (!flag && ans < cur) {
			ans = cur;
			ansv = curv;
		}
	}

	vector<int> curv;
	long long cur = h[0];
	curv.push_back(h[0]);
	for (int i = 1; i < n; i++) { //계속 감소 하는경우
		if (h[i] <= curv.back()) {
			cur += h[i];
			curv.push_back(h[i]);
		}
		else {
			cur += curv.back();
			curv.push_back(curv.back());
		}
	}

	if (ans < cur) {
		ans = cur;
		ansv = curv;
	}

	for (auto it : ansv) {
		cout << it << ' ';
	}

	return 0;
}


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

using namespace std;

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	int n; cin >> n;
	int h[1010];

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

	vector<int> ansv;
	long long ans = 0;

	for (int i = 0; i < n; i++) {
		long long cur = 0;
		vector<int> curv;
		for (int j = 0; j < n; j++) { //i까지 증가 이후부터 감소 한다고 칠때
			if (j == 0) {
				if (h[j] > h[j + 1]) {
					cur += h[j + 1];
					curv.push_back(h[j + 1]);
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
				continue;
			}

			if (j == n - 1) {
				if (j > i) {
					if (h[j] > curv.back()) {
						cur += curv.back();
						curv.push_back(curv.back());
					}
					else {
						cur += h[j];
						curv.push_back(h[j]);
					}
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
				continue;
			}

			if (j <= i) {

				if(h[j + 1] < curv.back()) {
					continue;
				}
				if (j == i || h[j] <= h[j + 1]) {
					cur += h[j];
					curv.push_back(h[j]);
				}
				else {
					cur += h[j + 1];
					curv.push_back(h[j+1]);
				}
			}
			else {
				if (h[j] > curv.back()) {
					cur += curv.back();
					curv.push_back(curv.back());
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
			}
		}
		if (ans < cur) {
			ans = cur;
			ansv = curv;
		}
	}

	vector<int> curv;
	long long cur = h[0];
	curv.push_back(h[0]);
	for (int i = 1; i < n; i++) { //계속 감소 하는경우
		if (h[i] <= curv.back()) {
			cur += h[i];
			curv.push_back(h[i]);
		}
		else {
			cur += curv.back();
			curv.push_back(curv.back());
		}
	}

	if (ans < cur) {
		ans = cur;
		ansv = curv;
	}

	for (auto it : ansv) {
		cout << it << ' ';
	}

	return 0;
}


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

using namespace std;

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	int n; cin >> n;
	int h[1010];

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

	vector<int> ansv;
	long long ans = 0;
	for (int i = 0; i < n; i++) {
		long long cur = 0;
		vector<int> curv;
		for (int j = 0; j < n; j++) { //i까지 증가 이후부터 감소 한다고 칠때
			if (j == 0) {
				if (h[j] > h[j + 1]) {
					cur += h[j + 1];
					curv.push_back(h[j + 1]);
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
				continue;
			}
			if (j <= i) {
				if (j == i || h[j] <= h[j + 1]) {
					cur += h[j];
					curv.push_back(h[j]);
				}
				else {
					cur += h[j + 1];
					curv.push_back(h[j+1]);
				}
			}
			else {
				if (h[j] > h[j - 1]) {
					cur += h[j - 1];
					curv.push_back(h[j-1]);
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
			}
		}

		if (ans < cur) {
			ans = cur;
			ansv = curv;
		}
	}

	for (int i = 0; i < n; i++) {
		vector<int> curv;
		long long cur = 0;
		for (int j = 0; j < n; j++) { //i까지 감소 이후부터 증가 한다고 칠때
			if (j == 0 || j == n-1) {
				cur += h[j];
				curv.push_back(h[j]);
				continue;
			}

			if (j <= i) {
				if (h[j] > h[j - 1]) {
					cur += h[j - 1];
					curv.push_back(h[j-1]);
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
			}
			else {
				if (h[j] <= h[j + 1]) {
					cur += h[j];
					curv.push_back(h[j]);
				}
				else {
					cur += h[j + 1];
					curv.push_back(h[j+1]);
				}
			}
		}
		if (ans < cur) {
			ans = cur;
			ansv = curv;
		}
	}

	for (auto it : ansv) {
		cout << it << ' ';
	}

	return 0;
}


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

using namespace std;

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	int n; cin >> n;
	long long h[1010];

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

	vector<int> ansv;
	long long ans = 0;

	for (int i = 1; i < n; i++) {
		long long cur = 0;
		vector<int> curv;
		bool flag = false;
		for (int j = 0; j < n; j++) { //i까지 증가 이후부터 감소 한다고 칠때
			if (j == 0) {
				if (h[j] > h[j + 1]) {
					cur += h[j + 1];
					curv.push_back(h[j + 1]);
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
			}
			else if (j <= i) {
				if (j == i) {
					cur += h[j];
					if (curv.back() > h[j]) {
						flag = true;
						continue;
					}
					curv.push_back(h[j]);
				}
				else if (h[j] <= h[j + 1]) {
					cur += h[j];
					if (curv.back() > h[j]) {
						flag = true;
						continue;
					}
					curv.push_back(h[j]);
				}
				else {
					cur += h[j + 1];
					if (curv.back() > h[j+1]) {
						flag = true;
						continue;
					}
					curv.push_back(h[j + 1]);
				}
			}
			else {
				if (h[j] > curv.back()) {
					cur += curv.back();
					curv.push_back(curv.back());
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
			}
		}
		if (!flag && ans < cur) {
			ans = cur;
			ansv = curv;
		}
	}

	vector<int> curv;
	long long cur = h[0];
	curv.push_back(h[0]);
	for (int i = 1; i < n; i++) { //계속 감소 하는경우
		if (h[i] <= curv.back()) {
			cur += h[i];
			curv.push_back(h[i]);
		}
		else {
			cur += curv.back();
			curv.push_back(curv.back());
		}
	}

	if (ans < cur) {
		ans = cur;
		ansv = curv;
	}

	for (auto it : ansv) {
		cout << it << ' ';
	}

	return 0;
}


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

using namespace std;

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	int n; cin >> n;
	int h[1010];

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

	vector<int> ansv;
	long long ans = 0;

	for (int i = 0; i < n; i++) {
		long long cur = 0;
		vector<int> curv;
		for (int j = 0; j < n; j++) { //i까지 증가 이후부터 감소 한다고 칠때
			if (j == 0) {
				if (h[j] > h[j + 1]) {
					cur += h[j + 1];
					curv.push_back(h[j + 1]);
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
				continue;
			}

			if (j == n - 1) {
				if (j > i) {
					if (h[j] > curv.back()) {
						cur += curv.back();
						curv.push_back(curv.back());
					}
					else {
						cur += h[j];
						curv.push_back(h[j]);
					}
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
				continue;
			}

			if (j <= i) {
				if (j == i || h[j] <= h[j + 1]) {
					cur += h[j];
					curv.push_back(h[j]);
				}
				else {
					cur += h[j + 1];
					curv.push_back(h[j+1]);
				}
			}
			else {
				if (h[j] > curv.back()) {
					cur += curv.back();
					curv.push_back(curv.back());
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
			}
		}
		if (ans < cur) {
			ans = cur;
			ansv = curv;
		}
	}

	vector<int> curv;
	long long cur = h[0];
	curv.push_back(h[0]);
	for (int i = 1; i < n; i++) { //계속 감소 하는경우
		if (h[i] <= curv.back()) {
			cur += h[i];
			curv.push_back(h[i]);
		}
		else {
			cur += curv.back();
			curv.push_back(curv.back());
		}
	}

	if (ans < cur) {
		ans = cur;
		ansv = curv;
	}

	for (auto it : ansv) {
		cout << it << ' ';
	}

	return 0;
}


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

using namespace std;

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(NULL);

	int n; cin >> n;
	int h[1010];

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

	vector<int> ansv;
	long long ans = 0;

	for (int i = 0; i < n; i++) {
		long long cur = 0;
		vector<int> curv;
		bool flag = false;
		for (int j = 0; j < n; j++) { //i까지 증가 이후부터 감소 한다고 칠때
			if (j == 0) {
				if (h[j] > h[j + 1]) {
					cur += h[j + 1];
					curv.push_back(h[j + 1]);
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
				continue;
			}
			if (j <= i) {
				if (j == n - 1 || j == i) {
					cur += h[j];
					curv.push_back(h[j]);
				}
				else if (h[j + 1] < curv.back() || h[j] < curv.back()) {
					flag = true;
					continue;
				}
				else if (h[j] <= h[j + 1]) {
					cur += h[j];
					curv.push_back(h[j]);
				}
				else {
					cur += h[j + 1];
					curv.push_back(h[j + 1]);
				}
			}
			else {
				if (h[j] > curv.back()) {
					cur += curv.back();
					curv.push_back(curv.back());
				}
				else {
					cur += h[j];
					curv.push_back(h[j]);
				}
			}
		}
		if (!flag && ans < cur) {
			ans = cur;
			ansv = curv;
		}
	}

	vector<int> curv;
	long long cur = h[0];
	curv.push_back(h[0]);
	for (int i = 1; i < n; i++) { //계속 감소 하는경우
		if (h[i] <= curv.back()) {
			cur += h[i];
			curv.push_back(h[i]);
		}
		else {
			cur += curv.back();
			curv.push_back(curv.back());
		}
	}

	if (ans < cur) {
		ans = cur;
		ansv = curv;
	}

	for (auto it : ansv) {
		cout << it << ' ';
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 300010;
struct Node {
    int id;
    int val;
    Node() {
    }
    Node(int id, int val) : id(id), val(val) {
    }
    bool operator < (const Node& cmp) const {
        return val < cmp.val;
    }
};
std::vector<Node> v[2];
struct E {
    void Input() {
        scanf("%d%d", &n, &w);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a[i], &b[i]);
            if(b[i] - a[i] >= a[i]) {
                v[0].push_back(Node(i, a[i]));
                v[0].push_back(Node(i, b[i] - a[i]));
            } else {
                v[1].push_back(Node(i, b[i]));
            }
        }
    }
    int n, w;
    int a[N], b[N];
    long long s1[N], s2[N];
    int preMax[N], sufMin[N];
    int typeOfResult;
    int cntOfFirstSet;
    int cntOfSecondSet;
    long long result;
    int ans[N];
    void Output() {
        printf("%lld\n", result);
        if(result == 2371198969837LL) {
            printf("typeOfResult=%d\n",typeOfResult);
        }
        for(int i = 0; i < cntOfFirstSet; i++) {
            ans[v[1][i].id] = 2;
        }
        for(int i = 0; i < cntOfSecondSet; i++) {
            ans[v[0][i].id]++;
        }
        if(typeOfResult == 1) {
            //
        } else if(typeOfResult == 2) {
            for(int i = 0; i < cntOfFirstSet; i++) {
                if(b[v[1][i].id] - a[v[1][i].id] == preMax[cntOfFirstSet]) {
                    ans[v[1][i].id]--;
                    break;
                }
            }
        } else {
            for(int i = cntOfFirstSet; ; i++) {
                if(sufMin[i + 1] == a[v[1][i].id]) {
                    ans[v[1][i].id]++;
                    break;
                }
            }
        }
        long long tmp = 0;
        for(int i = 0; i < n; i++) {
            printf("%d", ans[i]);
            if(ans[i] == 1)tmp += a[i];
            else if(ans[i] == 2)tmp += b[i];
        }
    }
    void Update(long long cost, int _cntOfFirstSet, int _cntOfSecondSet, int _type) {
        if(result == - 1 || cost < result) {
            result = cost;
            cntOfSecondSet = _cntOfSecondSet;
            cntOfFirstSet = _cntOfFirstSet;
            typeOfResult = _type;
        }
    }
    void Gao() {
        std::sort(v[0].begin(), v[0].end());
        std::sort(v[1].begin(), v[1].end());
        int cnt1 = v[0].size(), cnt2 = v[1].size();
        for(int i = 0; i < cnt1; i++) {
            s1[i + 1] = s1[i] + v[0][i].val;
        }
        for(int i = 0; i < cnt2; i++) {
            s2[i + 1] = s2[i] + v[1][i].val;
            preMax[i + 1] = std::max(preMax[i], b[v[1][i].id] - a[v[1][i].id]);
        }
        for(int i = cnt2; i >= 1; i--) {
            if(i < cnt2)
                sufMin[i] = std::min(sufMin[i + 1], a[v[1][i - 1].id]);
            else
                sufMin[i] = a[v[1][i - 1].id];
        }
        result = -1;
        for(int i = 0; i <= cnt2; i++) {
            if(2 * i <= w && w - 2 * i <= cnt1) {
                Update(s2[i] + s1[w - 2 * i], i, w - 2 * i, 1);
            }
            if(i)
                if(2 * i - 1 <= w && w - (2 * i - 1) <= cnt1) {
                    Update(s2[i] - preMax[i] + s1[w - (2 * i - 1)], i, w - (2 * i - 1), 2);
                }
            if(i < cnt2)
                if(2 * i + 1 <= w && w - (2 * i + 1) <= cnt1) {
                    Update(s2[i] + sufMin[i + 1] + s1[w - (2 * i + 1)], i, w - (2 * i + 1), 3);
                }
        }
    }
}e;
int main() {
    e.Input();
    e.Gao();
    e.Output();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 300010;
struct Node {
    int id;
    int val;
    Node() {
    }
    Node(int id, int val) : id(id), val(val) {
    }
    bool operator < (const Node& cmp) const {
        return val < cmp.val;
    }
};
std::vector<Node> v[2];
struct E {
    void Input() {
        scanf("%d%d", &n, &w);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a[i], &b[i]);
            if(b[i] - a[i] >= a[i]) {
                v[0].push_back(Node(i, a[i]));
                v[0].push_back(Node(i, b[i] - a[i]));
            } else {
                v[1].push_back(Node(i, b[i]));
            }
        }
    }
    int n, w;
    int a[N], b[N];
    long long s1[N], s2[N];
    int preMax[N], sufMin[N];
    int typeOfResult;
    int cntOfFirstSet;
    int cntOfSecondSet;
    long long result;
    int ans[N];
    void Output() {
        printf("%lld\n", result);
        for(int i = 0; i < cntOfFirstSet; i++) {
            ans[v[1][i].id] = 2;
        }
        for(int i = 0; i < cntOfSecondSet; i++) {
            ans[v[0][i].id]++;
        }
        if(typeOfResult == 1) {
            //
        } else if(typeOfResult == 2) {
            for(int i = 0; i < cntOfFirstSet; i++) {
                if(b[v[1][i].id] - a[v[1][i].id] == preMax[cntOfFirstSet]) {
                    ans[v[1][i].id]--;
                    break;
                }
            }
        } else {
            for(int i = cntOfFirstSet; ; i++) {
                if(sufMin[i + 1] == a[v[1][i].id]) {
                    ans[v[1][i].id]++;
                    break;
                }
            }
        }
        long long tmp = 0;
        for(int i = 0; i < n; i++) {
            printf("%d", ans[i]);
            if(ans[i] == 1)tmp += a[i];
            else if(ans[i] == 2)tmp += b[i];
        }
        if(tmp != result) while(1);
    }
    void Update(long long cost, int _cntOfFirstSet, int _cntOfSecondSet, int _type) {
        if(result == - 1 || cost < result) {
            result = cost;
            cntOfSecondSet = _cntOfSecondSet;
            cntOfFirstSet = _cntOfFirstSet;
            typeOfResult = _type;
        }
    }
    void Gao() {
        std::sort(v[0].begin(), v[0].end());
        std::sort(v[1].begin(), v[1].end());
        int cnt1 = v[0].size(), cnt2 = v[1].size();
        for(int i = 0; i < cnt1; i++) {
            s1[i + 1] = s1[i] + v[0][i].val;
        }
        for(int i = 0; i < cnt2; i++) {
            s2[i + 1] = s2[i] + v[1][i].val;
            preMax[i + 1] = std::max(preMax[i], b[v[1][i].id] - a[v[1][i].id]);
        }
        for(int i = cnt2; i >= 1; i--) {
            if(i < cnt2)
                sufMin[i] = std::min(sufMin[i + 1], a[v[1][i - 1].id]);
            else
                sufMin[i] = a[v[1][i - 1].id];
        }
        result = -1;
        for(int i = 0; i <= cnt2; i++) {
            if(2 * i <= w && w - 2 * i <= cnt1) {
                Update(s2[i] + s1[w - 2 * i], i, w - 2 * i, 1);
            }
            if(i)
                if(2 * i - 1 <= w && w - (2 * i - 1) <= cnt1) {
                    Update(s2[i] - preMax[i] + s1[w - (2 * i - 1)], i, w - (2 * i - 1), 2);
                }
            if(i < cnt2)
                if(2 * i + 1 <= w && w - (2 * i + 1) <= cnt1) {
                    Update(s2[i] + sufMin[i + 1] + s1[w - (2 * i + 1)], i, w - (2 * i + 1), 3);
                }
        }
    }
}e;
int main() {
    e.Input();
    e.Gao();
    e.Output();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 300010;
struct Node {
    int id;
    int val;
    Node() {
    }
    Node(int id, int val) : id(id), val(val) {
    }
    bool operator < (const Node& cmp) const {
        return val < cmp.val;
    }
};
std::vector<Node> v[2];
struct E {
    void Input() {
        scanf("%d%d", &n, &w);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a[i], &b[i]);
            if(b[i] - a[i] >= a[i]) {
                v[0].push_back(Node(i, a[i]));
                v[0].push_back(Node(i, b[i] - a[i]));
            } else {
                v[1].push_back(Node(i, b[i]));
            }
        }
    }
    int n, w;
    int a[N], b[N];
    long long s1[N], s2[N];
    int preMax[N], sufMin[N];
    int typeOfResult;
    int cntOfFirstSet;
    int cntOfSecondSet;
    long long result;
    int ans[N];
    void Output() {
        printf("%lld\n", result);
        if(result == 2371198969837LL) {
            printf("typeOfResult=%d\n",typeOfResult);
        }
        for(int i = 0; i < cntOfFirstSet; i++) {
            ans[v[1][i].id] = 2;
        }
        for(int i = 0; i < cntOfSecondSet; i++) {
            ans[v[0][i].id]++;
        }
        if(typeOfResult == 1) {
            //
        } else if(typeOfResult == 2) {
            for(int i = 0; i < cntOfFirstSet; i++) {
                if(b[v[1][i].id] - a[v[1][i].id] == preMax[cntOfFirstSet]) {
                    ans[v[1][i].id]--;
                    break;
                }
            }
        } else {
            for(int i = cntOfFirstSet; ; i++) {
                if(sufMin[i + 1] == a[v[1][i].id]) {
                    ans[v[1][i].id]++;
                    break;
                }
            }
        }
        long long tmp = 0;
        if(result == 2371198969837LL)
        for(int i = 0; i < n; i++) {
            if(ans[i] == 1)tmp += a[i];
            else if(ans[i] == 2)tmp += b[i];
        }
        if(result == 2371198969837LL) {
            printf("tmp=%lld\n",tmp);
        }
    }
    void Update(long long cost, int _cntOfFirstSet, int _cntOfSecondSet, int _type) {
        if(result == - 1 || cost < result) {
            result = cost;
            cntOfFirstSet = _cntOfFirstSet;
            cntOfSecondSet = _cntOfSecondSet;
            typeOfResult = _type;
        }
    }
    void Gao() {
        std::sort(v[0].begin(), v[0].end());
        std::sort(v[1].begin(), v[1].end());
        int cnt1 = v[0].size(), cnt2 = v[1].size();
        for(int i = 0; i < cnt1; i++) {
            s1[i + 1] = s1[i] + v[0][i].val;
        }
        for(int i = 0; i < cnt2; i++) {
            s2[i + 1] = s2[i] + v[1][i].val;
            preMax[i + 1] = std::max(preMax[i], b[v[1][i].id] - a[v[1][i].id]);
        }
        for(int i = cnt2; i >= 1; i--) {
            if(i < cnt2)
                sufMin[i] = std::min(sufMin[i + 1], a[v[1][i - 1].id]);
            else
                sufMin[i] = a[v[1][i - 1].id];
        }
        result = -1;
        for(int i = 0; i <= cnt2; i++) {
            if(2 * i <= w && w - 2 * i <= cnt1) {
                Update(s2[i] + s1[w - 2 * i], i, w - 2 * i, 1);
            }
            if(i)
                if(2 * i - 1 <= w && w - (2 * i - 1) <= cnt1) {
                    Update(s2[i] - preMax[i] + s1[w - (2 * i - 1)], i, w - (2 * i - 1), 2);
                }
            if(i < cnt2)
                if(2 * i + 1 <= w && w - (2 * i + 1) <= cnt1) {
                    Update(s2[i] + sufMin[i + 1] + s1[w - (2 * i + 1)], i, w - (2 * i + 1), 3);
                }
        }
    }
}e;
int main() {
    e.Input();
    e.Gao();
    e.Output();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 300010;
struct Node {
    int id;
    int val;
    Node() {
    }
    Node(int id, int val) : id(id), val(val) {
    }
    bool operator < (const Node& cmp) const {
        return val < cmp.val;
    }
};
std::vector<Node> v[2];
struct E {
    void Input() {
        scanf("%d%d", &n, &w);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a[i], &b[i]);
            if(b[i] - a[i] >= a[i]) {
                v[0].push_back(Node(i, a[i]));
                v[0].push_back(Node(i, b[i] - a[i]));
            } else {
                v[1].push_back(Node(i, b[i]));
            }
        }
    }
    int n, w;
    int a[N], b[N];
    long long s1[N], s2[N];
    int preMax[N], sufMin[N];
    int typeOfResult;
    int cntOfFirstSet;
    int cntOfSecondSet;
    long long result;
    int ans[N];
    void Output() {
        printf("%lld\n%d\n", result, typeOfResult);
        for(int i = 0; i < cntOfFirstSet; i++) {
            ans[v[1][i].id] = 2;
        }
        for(int i = 0; i < cntOfSecondSet; i++) {
            ans[v[0][i].id]++;
        }
        if(typeOfResult == 1) {
            //
        } else if(typeOfResult == 2) {
            for(int i = 0; i < cntOfFirstSet; i++) {
                if(b[v[1][i].id] - a[v[1][i].id] == preMax[cntOfFirstSet]) {
                    ans[v[1][i].id]--;
                    break;
                }
            }
        } else {
            for(int i = cntOfFirstSet; ; i++) {
                if(sufMin[i + 1] == a[v[1][i].id]) {
                    ans[v[1][i].id]++;
                    break;
                }
            }
        }
        long long tmp = 0;
        for(int i = 0; i < n; i++) {
            printf("%d", ans[i]);
            if(ans[i] == 1)tmp += a[i];
            else if(ans[i] == 2)tmp += b[i];
        }
        if(tmp != result) while(1);
    }
    void Update(long long cost, int _cntOfFirstSet, int _cntOfSecondSet, int _type) {
        if(result == - 1 || cost < result) {
            result = cost;
            cntOfSecondSet = _cntOfSecondSet;
            cntOfFirstSet = _cntOfFirstSet;
            typeOfResult = _type;
        }
    }
    void Gao() {
        std::sort(v[0].begin(), v[0].end());
        std::sort(v[1].begin(), v[1].end());
        int cnt1 = v[0].size(), cnt2 = v[1].size();
        for(int i = 0; i < cnt1; i++) {
            s1[i + 1] = s1[i] + v[0][i].val;
        }
        for(int i = 0; i < cnt2; i++) {
            s2[i + 1] = s2[i] + v[1][i].val;
            preMax[i + 1] = std::max(preMax[i], b[v[1][i].id] - a[v[1][i].id]);
        }
        for(int i = cnt2; i >= 1; i--) {
            if(i < cnt2)
                sufMin[i] = std::min(sufMin[i + 1], a[v[1][i - 1].id]);
            else
                sufMin[i] = a[v[1][i - 1].id];
        }
        result = -1;
        for(int i = 0; i <= cnt2; i++) {
            if(2 * i <= w && w - 2 * i <= cnt1) {
                Update(s2[i] + s1[w - 2 * i], i, w - 2 * i, 1);
            }
            if(i)
                if(2 * i - 1 <= w && w - (2 * i - 1) <= cnt1) {
                    Update(s2[i] - preMax[i] + s1[w - (2 * i - 1)], i, w - (2 * i - 1), 2);
                }
            if(i < cnt2)
                if(2 * i + 1 <= w && w - (2 * i + 1) <= cnt1) {
                    Update(s2[i] + sufMin[i + 1] + s1[w - (2 * i + 1)], i, w - (2 * i + 1), 3);
                }
        }
    }
}e;
int main() {
    e.Input();
    e.Gao();
    e.Output();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 300010;
struct Node {
    int id;
    int val;
    Node() {
    }
    Node(int id, int val) : id(id), val(val) {
    }
    bool operator < (const Node& cmp) const {
        return val < cmp.val;
    }
};
std::vector<Node> v[2];
struct E {
    void Input() {
        scanf("%d%d", &n, &w);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a[i], &b[i]);
            if(b[i] - a[i] >= a[i]) {
                v[0].push_back(Node(i, a[i]));
                v[0].push_back(Node(i, b[i] - a[i]));
            } else {
                v[1].push_back(Node(i, b[i]));
            }
        }
    }
    int n, w;
    int a[N], b[N];
    long long s1[N], s2[N];
    int preMax[N], sufMin[N];
    int typeOfResult;
    int cntOfFirstSet;
    int cntOfSecondSet;
    long long result;
    int ans[N];
    void Output() {
        printf("%lld\n", result);
        for(int i = 0; i < cntOfFirstSet; i++) {
            ans[v[1][i].id] = 2;
        }
        for(int i = 0; i < cntOfSecondSet; i++) {
            ans[v[0][i].id]++;
        }
        if(typeOfResult == 1) {
            //
        } else if(typeOfResult == 2) {
            for(int i = 0; i < cntOfFirstSet; i++) {
                if(b[v[1][i].id] - a[v[1][i].id] == preMax[cntOfFirstSet]) {
                    ans[v[1][i].id]--;
                    break;
                }
            }
        } else {
            for(int i = cntOfFirstSet; ; i++) {
                if(sufMin[i + 1] == a[v[1][i].id]) {
                    ans[v[1][i].id]++;
                    break;
                }
            }
        }
        for(int i = 0; i < n; i++) {
            printf("%d", ans[i]);
        }
    }
    void Update(long long cost, int _cntOfFirstSet, int _cntOfSecondSet, int _type) {
        if(result == - 1 || cost < result) {
            result = cost;
            cntOfSecondSet = _cntOfSecondSet;
            cntOfFirstSet = _cntOfFirstSet;
            typeOfResult = _type;
        }
    }
    void Gao() {
        std::sort(v[0].begin(), v[0].end());
        std::sort(v[1].begin(), v[1].end());
        int cnt1 = v[0].size(), cnt2 = v[1].size();
        for(int i = 0; i < cnt1; i++) {
            s1[i + 1] = s1[i] + v[0][i].val;
        }
        for(int i = 0; i < cnt2; i++) {
            s2[i + 1] = s2[i] + v[1][i].val;
            preMax[i + 1] = std::max(preMax[i], b[v[1][i].id] - a[v[1][i].id]);
        }
        for(int i = cnt2; i >= 1; i--) {
            if(i < cnt2)
                sufMin[i] = std::min(sufMin[i + 1], a[v[1][i - 1].id]);
            else
                sufMin[i] = a[v[1][i - 1].id];
        }
        result = -1;
        for(int i = 0; i <= cnt2; i++) {
            if(2 * i <= w && w - 2 * i <= cnt1) {
                Update(s2[i] + s1[w - 2 * i], i, w - 2 * i, 1);
            }
            if(i)
                if(2 * i - 1 <= w && w - (2 * i - 1) <= cnt1) {
                    Update(s2[i] - preMax[i] + s1[w - (2 * i - 1)], i, w - (2 * i - 1), 2);
                }
            if(i < cnt2)
                if(2 * i + 1 <= w && w - (2 * i + 1) <= cnt1) {
                    Update(s2[i] + sufMin[i + 1] + s1[w - (2 * i + 1)], i, w - (2 * i + 1), 3);
                }
        }
    }
}e;
int main() {
    e.Input();
    e.Gao();
    e.Output();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 600010;
struct Node {
    int id;
    int val;
    Node() {
    }
    Node(int id, int val) : id(id), val(val) {
    }
    bool operator < (const Node& cmp) const {
        return val < cmp.val;
    }
};
std::vector<Node> v[2];
struct E {
    void Input() {
        scanf("%d%d", &n, &w);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a[i], &b[i]);
            if(b[i] - a[i] >= a[i]) {
                v[0].push_back(Node(i, a[i]));
                v[0].push_back(Node(i, b[i] - a[i]));
            } else {
                v[1].push_back(Node(i, b[i]));
            }
        }
    }
    int n, w;
    int a[N], b[N];
    long long s1[N], s2[N];
    int preMax[N], sufMin[N];
    int typeOfResult;
    int cntOfFirstSet;
    int cntOfSecondSet;
    long long result;
    int ans[N];
    void Output() {
        printf("%lld\n", result);
        for(int i = 0; i < cntOfFirstSet; i++) {
            ans[v[1][i].id] = 2;
        }
        for(int i = 0; i < cntOfSecondSet; i++) {
            ans[v[0][i].id]++;
        }
        if(typeOfResult == 1) {
            //
        } else if(typeOfResult == 2) {
            for(int i = 0; i < cntOfFirstSet; i++) {
                if(b[v[1][i].id] - a[v[1][i].id] == preMax[cntOfFirstSet]) {
                    ans[v[1][i].id]--;
                    break;
                }
            }
        } else {
            for(int i = cntOfFirstSet; ; i++) {
                if(sufMin[i + 1] == a[v[1][i].id]) {
                    ans[v[1][i].id]++;
                    break;
                }
            }
        }
        long long tmp = 0;
        for(int i = 0; i < n; i++) {
             printf("%d",ans[i]);
            if(ans[i] == 1)tmp += a[i];
            else if(ans[i] == 2)tmp += b[i];
        }
    }
    void Update(long long cost, int _cntOfFirstSet, int _cntOfSecondSet, int _type) {
        if(result == - 1 || cost < result) {
            result = cost;
            cntOfFirstSet = _cntOfFirstSet;
            cntOfSecondSet = _cntOfSecondSet;
            typeOfResult = _type;
        }
    }
    void Gao() {
        std::sort(v[0].begin(), v[0].end());
        std::sort(v[1].begin(), v[1].end());
        int cnt1 = v[0].size(), cnt2 = v[1].size();
        for(int i = 0; i < cnt1; i++) {
            s1[i + 1] = s1[i] + v[0][i].val;
        }
        for(int i = 0; i < cnt2; i++) {
            s2[i + 1] = s2[i] + v[1][i].val;
            preMax[i + 1] = std::max(preMax[i], b[v[1][i].id] - a[v[1][i].id]);
        }
        for(int i = cnt2; i >= 1; i--) {
            if(i < cnt2)
                sufMin[i] = std::min(sufMin[i + 1], a[v[1][i - 1].id]);
            else
                sufMin[i] = a[v[1][i - 1].id];
        }
        result = -1;
        for(int i = 0; i <= cnt2; i++) {
            if(2 * i <= w && w - 2 * i <= cnt1) {
                Update(s2[i] + s1[w - 2 * i], i, w - 2 * i, 1);
            }
            if(i)
                if(2 * i - 1 <= w && w - (2 * i - 1) <= cnt1) {
                    Update(s2[i] - preMax[i] + s1[w - (2 * i - 1)], i, w - (2 * i - 1), 2);
                }
            if(i < cnt2)
                if(2 * i + 1 <= w && w - (2 * i + 1) <= cnt1) {
                    Update(s2[i] + sufMin[i + 1] + s1[w - (2 * i + 1)], i, w - (2 * i + 1), 3);
                }
        }
    }
}e;
int main() {
    e.Input();
    e.Gao();
    e.Output();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 300010;
struct Node {
    int id;
    int val;
    Node() {
    }
    Node(int id, int val) : id(id), val(val) {
    }
    bool operator < (const Node& cmp) const {
        return val < cmp.val;
    }
};
std::vector<Node> v[2];
struct E {
    void Input() {
        scanf("%d%d", &n, &w);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a[i], &b[i]);
            if(b[i] - a[i] >= a[i]) {
                v[0].push_back(Node(i, a[i]));
                v[0].push_back(Node(i, b[i] - a[i]));
            } else {
                v[1].push_back(Node(i, b[i]));
            }
        }
    }
    int n, w;
    int a[N], b[N];
    int s1[N], s2[N];
    int preMax[N], sufMin[N];
    int typeOfResult;
    int cntOfFirstSet;
    int cntOfSecondSet;
    int result;
    int ans[N];
    void Output() {
        printf("%d\n", result);
        for(int i = 0; i < cntOfFirstSet; i++) {
            ans[v[1][i].id] = 2;
        }
        for(int i = 0; i < cntOfSecondSet; i++) {
            ans[v[0][i].id]++;
        }
        if(typeOfResult == 1) {
            //
        } else if(typeOfResult == 2) {
            for(int i = 0; i < cntOfFirstSet; i++) {
                if(b[v[1][i].id] - a[v[1][i].id] == preMax[cntOfFirstSet]) {
                    ans[v[1][i].id]--;
                    break;
                }
            }
        } else {
            for(int i = cntOfFirstSet; ; i++) {
                if(sufMin[i + 1] == a[v[1][i].id]) {
                    ans[v[1][i].id]++;
                    break;
                }
            }
        }
        for(int i = 0; i < n; i++) {
            printf("%d", ans[i]);
        }
    }
    void Update(int cost, int _cntOfFirstSet, int _cntOfSecondSet, int _type) {
        if(result == - 1 || cost < result) {
            result = cost;
            cntOfSecondSet = _cntOfSecondSet;
            cntOfFirstSet = _cntOfFirstSet;
            typeOfResult = _type;
        }
    }
    void Gao() {
        std::sort(v[0].begin(), v[0].end());
        std::sort(v[1].begin(), v[1].end());
        int cnt1 = v[0].size(), cnt2 = v[1].size();
        for(int i = 0; i < cnt1; i++) {
            s1[i + 1] = s1[i] + v[0][i].val;
        }
        for(int i = 0; i < cnt2; i++) {
            s2[i + 1] = s2[i] + v[1][i].val;
            preMax[i + 1] = std::max(preMax[i], b[v[1][i].id] - a[v[1][i].id]);
        }
        for(int i = cnt2; i >= 1; i--) {
            if(i < cnt2)
                sufMin[i] = std::min(sufMin[i + 1], a[v[1][i - 1].id]);
            else
                sufMin[i] = a[v[1][i - 1].id];
        }
        result = -1;
        for(int i = 0; i <= cnt2; i++) {
            if(2 * i <= w && w - 2 * i <= cnt1) {
                Update(s2[i] + s1[w - 2 * i], i, w - 2 * i, 1);
            }
            if(i)
                if(2 * i - 1 <= w && w - (2 * i - 1) <= cnt1) {
                    Update(s2[i] - preMax[i] + s1[w - (2 * i - 1)], i, w - (2 * i - 1), 2);
                }
            if(i < cnt2)
                if(2 * i + 1 <= w && w - (2 * i + 1) <= cnt1) {
                    Update(s2[i] + sufMin[i + 1] + s1[w - (2 * i + 1)], i, w - (2 * i + 1), 3);
                }
        }
    }
}e;
int main() {
    e.Input();
    e.Gao();
    e.Output();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 300010;
struct Node {
    int id;
    int val;
    Node() {
    }
    Node(int id, int val) : id(id), val(val) {
    }
    bool operator < (const Node& cmp) const {
        return val < cmp.val;
    }
};
std::vector<Node> v[2];
struct E {
    void Input() {
        scanf("%d%d", &n, &w);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a[i], &b[i]);
            if(b[i] - a[i] >= a[i]) {
                v[0].push_back(Node(i, a[i]));
                v[0].push_back(Node(i, b[i] - a[i]));
            } else {
                v[1].push_back(Node(i, b[i]));
            }
        }
    }
    int n, w;
    int a[N], b[N];
    int s1[N], s2[N];
    int preMax[N], sufMin[N];
    int typeOfResult;
    int cntOfFirstSet;
    int cntOfSecondSet;
    int result;
    int ans[N];
    void Output() {
        printf("%d\n", result);
        for(int i = 0; i < cntOfFirstSet; i++) {
            ans[v[1][i].id] = 2;
        }
        for(int i = 0; i < cntOfSecondSet; i++) {
          //  printf("id=%d\n", v[0][i].id);
            ans[v[0][i].id]++;
        }
        if(typeOfResult == 1) {
        } else if(typeOfResult == 2) {
            for(int i = 0; i < cntOfFirstSet; i++) {
                if(b[v[1][i].id] - a[v[1][i].id] == preMax[cntOfFirstSet]) {
                    ans[v[1][i].id]--;
                    break;
                }
            }
        } else {
            for(int i = cntOfFirstSet + 1; ; i++) {
                if(sufMin[i] == a[v[1][i - 1].id]) {
                    ans[v[1][i - 1].id]++;
                }
            }
        }
        for(int i = 0; i < n; i++) {
            printf("%d", ans[i]);
        }
    }
    void Update(int cost, int _cntOfFirstSet, int _cntOfSecondSet, int _type) {
        if(result == - 1 || cost < result) {
            result = cost;
            cntOfSecondSet = _cntOfSecondSet;
            cntOfFirstSet = _cntOfFirstSet;
            typeOfResult = _type;
        }
    }
    void Gao() {
        std::sort(v[0].begin(), v[0].end());
        std::sort(v[1].begin(), v[1].end());
        int cnt1 = v[0].size(), cnt2 = v[1].size();
        for(int i = 0; i < cnt1; i++) {
            s1[i + 1] = s1[i] + v[0][i].val;
        }
        for(int i = 0; i < cnt2; i++) {
            s2[i + 1] = s2[i] + v[1][i].val;
            preMax[i + 1] = std::max(preMax[i], b[v[1][i].id] - a[v[1][i].id]);
        }
        for(int i = cnt2; i >= 1; i--) {
            if(i < cnt2)
                sufMin[i] = std::min(sufMin[i + 1], a[v[1][i - 1].id]);
            else
                sufMin[i] = a[v[1][i - 1].id];
        }
        result = -1;
        for(int i = 0; i <= cnt2; i++) {
            if(2 * i <= w && w - 2 * i <= cnt1) {
                Update(s2[i] + s1[w - 2 * i], i, w - 2 * i, 1);
            }
            if(i)
                if(2 * i - 1 <= w && w - (2 * i - 1) <= cnt1) {
                    Update(s2[i] - preMax[i] + s1[w - (2 * i - 1)], i, w - (2 * i - 1), 2);
                }
            if(i < cnt2)
                if(2 * i + 1 <= w && w - (2 * i + 1) <= cnt1) {
                    Update(s2[i] + sufMin[i + 1] + s1[w - (2 * i + 1)], i, w - (2 * i + 1), 3);
                }
        }
    }
}e;
int main() {
    e.Input();
    e.Gao();
    e.Output();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 600010;
struct Node {
    int id;
    int val;
    Node() {
    }
    Node(int id, int val) : id(id), val(val) {
    }
    bool operator < (const Node& cmp) const {
        return val < cmp.val;
    }
};
std::vector<Node> v[2];
struct E {
    void Input() {
        scanf("%d%d", &n, &w);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a[i], &b[i]);
            if(b[i] - a[i] >= a[i]) {
                v[0].push_back(Node(i, a[i]));
                v[0].push_back(Node(i, b[i] - a[i]));
            } else {
                v[1].push_back(Node(i, b[i]));
            }
        }
    }
    int n, w;
    int a[N], b[N];
    long long s1[N], s2[N];
    int preMax[N], sufMin[N];
    int typeOfResult;
    int cntOfFirstSet;
    int cntOfSecondSet;
    long long result;
    int ans[N];
    void Output() {
        printf("%lld\n", result);
        if(result == 2371198969837LL) {
            printf("typeOfResult=%d\n",typeOfResult);
        }
        for(int i = 0; i < cntOfFirstSet; i++) {
            ans[v[1][i].id] = 2;
        }
        for(int i = 0; i < cntOfSecondSet; i++) {
            ans[v[0][i].id]++;
        }
        if(typeOfResult == 1) {
            //
        } else if(typeOfResult == 2) {
            for(int i = 0; i < cntOfFirstSet; i++) {
                if(b[v[1][i].id] - a[v[1][i].id] == preMax[cntOfFirstSet]) {
                    ans[v[1][i].id]--;
                    break;
                }
            }
        } else {
            for(int i = cntOfFirstSet; ; i++) {
                if(sufMin[i + 1] == a[v[1][i].id]) {
                    ans[v[1][i].id]++;
                    break;
                }
            }
        }
        long long tmp = 0;
        for(int i = 0; i < n; i++) {
        if(result != 2371198969837LL)  printf("%d",ans[i]);
            if(ans[i] == 1)tmp += a[i];
            else if(ans[i] == 2)tmp += b[i];
        }
        if(result == 2371198969837LL) {
            printf("tmp=%lld\n",tmp);
        }
    }
    void Update(long long cost, int _cntOfFirstSet, int _cntOfSecondSet, int _type) {
        if(result == - 1 || cost < result) {
            result = cost;
            cntOfFirstSet = _cntOfFirstSet;
            cntOfSecondSet = _cntOfSecondSet;
            typeOfResult = _type;
        }
    }
    void Gao() {
        std::sort(v[0].begin(), v[0].end());
        std::sort(v[1].begin(), v[1].end());
        int cnt1 = v[0].size(), cnt2 = v[1].size();
        for(int i = 0; i < cnt1; i++) {
            s1[i + 1] = s1[i] + v[0][i].val;
        }
        for(int i = 0; i < cnt2; i++) {
            s2[i + 1] = s2[i] + v[1][i].val;
            preMax[i + 1] = std::max(preMax[i], b[v[1][i].id] - a[v[1][i].id]);
        }
        for(int i = cnt2; i >= 1; i--) {
            if(i < cnt2)
                sufMin[i] = std::min(sufMin[i + 1], a[v[1][i - 1].id]);
            else
                sufMin[i] = a[v[1][i - 1].id];
        }
        result = -1;
        for(int i = 0; i <= cnt2; i++) {
            if(2 * i <= w && w - 2 * i <= cnt1) {
                Update(s2[i] + s1[w - 2 * i], i, w - 2 * i, 1);
            }
            if(i)
                if(2 * i - 1 <= w && w - (2 * i - 1) <= cnt1) {
                    Update(s2[i] - preMax[i] + s1[w - (2 * i - 1)], i, w - (2 * i - 1), 2);
                }
            if(i < cnt2)
                if(2 * i + 1 <= w && w - (2 * i + 1) <= cnt1) {
                    Update(s2[i] + sufMin[i + 1] + s1[w - (2 * i + 1)], i, w - (2 * i + 1), 3);
                }
        }
    }
}e;
int main() {
    e.Input();
    e.Gao();
    e.Output();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
const int N = 300010;
struct Node {
    int id;
    int val;
    Node() {
    }
    Node(int id, int val) : id(id), val(val) {
    }
    bool operator < (const Node& cmp) const {
        return val < cmp.val;
    }
};
std::vector<Node> v[2];
struct E {
    void Input() {
        scanf("%d%d", &n, &w);
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &a[i], &b[i]);
            if(b[i] - a[i] >= a[i]) {
                v[0].push_back(Node(i, a[i]));
                v[0].push_back(Node(i, b[i] - a[i]));
            } else {
                v[1].push_back(Node(i, b[i]));
            }
        }
    }
    int n, w;
    int a[N], b[N];
    long long s1[N], s2[N];
    int preMax[N], sufMin[N];
    int typeOfResult;
    int cntOfFirstSet;
    int cntOfSecondSet;
    long long result;
    int ans[N];
    void Output() {
        printf("%lld\n", result);
        if(result == 2371198969837LL) {
            printf("typeOfResult=%d\n",typeOfResult);
        }
        for(int i = 0; i < cntOfFirstSet; i++) {
            ans[v[1][i].id] = 2;
        }
        for(int i = 0; i < cntOfSecondSet; i++) {
            ans[v[0][i].id]++;
        }
        if(typeOfResult == 1) {
            //
        } else if(typeOfResult == 2) {
            for(int i = 0; i < cntOfFirstSet; i++) {
                if(b[v[1][i].id] - a[v[1][i].id] == preMax[cntOfFirstSet]) {
                    ans[v[1][i].id]--;
                    break;
                }
            }
        } else {
            for(int i = cntOfFirstSet; ; i++) {
                if(sufMin[i + 1] == a[v[1][i].id]) {
                    ans[v[1][i].id]++;
                    break;
                }
            }
        }
        long long tmp = 0;
        for(int i = 0; i < n; i++) {
        if(result != 2371198969837LL)  printf("%d ",ans[i]);
            if(ans[i] == 1)tmp += a[i];
            else if(ans[i] == 2)tmp += b[i];
        }
        if(result == 2371198969837LL) {
            printf("tmp=%lld\n",tmp);
        }
    }
    void Update(long long cost, int _cntOfFirstSet, int _cntOfSecondSet, int _type) {
        if(result == - 1 || cost < result) {
            result = cost;
            cntOfFirstSet = _cntOfFirstSet;
            cntOfSecondSet = _cntOfSecondSet;
            typeOfResult = _type;
        }
    }
    void Gao() {
        std::sort(v[0].begin(), v[0].end());
        std::sort(v[1].begin(), v[1].end());
        int cnt1 = v[0].size(), cnt2 = v[1].size();
        for(int i = 0; i < cnt1; i++) {
            s1[i + 1] = s1[i] + v[0][i].val;
        }
        for(int i = 0; i < cnt2; i++) {
            s2[i + 1] = s2[i] + v[1][i].val;
            preMax[i + 1] = std::max(preMax[i], b[v[1][i].id] - a[v[1][i].id]);
        }
        for(int i = cnt2; i >= 1; i--) {
            if(i < cnt2)
                sufMin[i] = std::min(sufMin[i + 1], a[v[1][i - 1].id]);
            else
                sufMin[i] = a[v[1][i - 1].id];
        }
        result = -1;
        for(int i = 0; i <= cnt2; i++) {
            if(2 * i <= w && w - 2 * i <= cnt1) {
                Update(s2[i] + s1[w - 2 * i], i, w - 2 * i, 1);
            }
            if(i)
                if(2 * i - 1 <= w && w - (2 * i - 1) <= cnt1) {
                    Update(s2[i] - preMax[i] + s1[w - (2 * i - 1)], i, w - (2 * i - 1), 2);
                }
            if(i < cnt2)
                if(2 * i + 1 <= w && w - (2 * i + 1) <= cnt1) {
                    Update(s2[i] + sufMin[i + 1] + s1[w - (2 * i + 1)], i, w - (2 * i + 1), 3);
                }
        }
    }
}e;
int main() {
    e.Input();
    e.Gao();
    e.Output();
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int,int> PII;
typedef pair<int,PII> PIII;

#define LL long long
#define ULL unsigned long long
#define m_p make_pair
#define l_b lower_bound
#define p_b push_back
#define w1 first
#define w2 second
#define maxlongint 2147483647
#define biglongint 2139062143

const int maxn=300005;

struct avl
{
    avl *lchild,*rchild,*father;
    int num,depth,size,biao;
    LL sum;
} *root,*null;

int a[maxn],b[maxn],getans[maxn],place[maxn];
LL sum[maxn];
PIII ls[maxn];
int N,W;
LL ans;

void update(avl *p)
{
    if (p==null) return;
    p->depth=max(p->lchild->depth,p->rchild->depth)+1;
    p->sum=p->lchild->sum+p->rchild->sum+p->num;
    p->size=p->lchild->size+p->rchild->size+1;
}

void spinl(avl *a)
{
    avl *b=a->lchild;
    a->lchild=b->rchild;
    b->rchild->father=a;
    b->rchild=a;
    b->father=a->father;
    a->father=b;
    update(a);
    update(b);
    if (b!=null)
    {
        if (b->father->lchild==a) b->father->lchild=b;
                            else  b->father->rchild=b;
    }
}

void spinr(avl *a)
{
    avl *b=a->rchild;
    a->rchild=b->lchild;
    b->lchild->father=a;
    b->lchild=a;
    b->father=a->father;
    a->father=b;
    update(a);
    update(b);
    if (b!=null)
    {
        if (b->father->lchild==a) b->father->lchild=b;
                            else  b->father->rchild=b;
    }
}

void insert(avl *&p,int num,int biao,avl *father)
{
    if (p==null)
    {
        avl *q=new avl;
        q->father=father;
        q->lchild=q->rchild=null;
        q->num=num;
        q->sum=num;
        q->size=1;
        q->biao=biao;
        p=q;
    }
    else
    {
        if ((num>p->num)||((num==p->num)&&(biao>p->biao))) insert(p->rchild,num,biao,p); else insert(p->lchild,num,biao,p);
        update(p);
        if (p->depth>2)
            if (abs(p->lchild->depth-p->rchild->depth)>=2)
            {
                if (p->lchild->depth>p->rchild->depth)
                    spinl(p);
                else
                    spinr(p);
            }
    }
}

void copyit(avl *p,avl *q)
{
    p->num=q->num,p->biao=q->biao,p->sum=q->sum;
}

void erasenode(avl *p)
{
    avl *gx;
    if ((p->lchild==null)&&(p->rchild==null))
    {
        if (p->father->lchild==p) p->father->lchild=null; else p->father->rchild=null;
        gx=p->father;
    }
    else
    {
        if (p->lchild==null)
        {
            if (p->father->lchild==p) p->father->lchild=p->rchild; else p->father->rchild=p->rchild;
            p->rchild->father=p->father;
            gx=p->father;
        }
        else
            if (p->rchild==null)
            {
                if (p->father->lchild==p) p->father->lchild=p->lchild; else p->father->rchild=p->lchild;
                p->lchild->father=p->father;
                gx=p->father;
            }
            else
            {
                avl *q=p->rchild;
                if (q->lchild==null)
                {
                    copyit(p,q);
                    p->rchild=q->rchild;
                    q->rchild->father=p;
                    gx=p;
                }
                else
                {
                    while (q->lchild!=null) q=q->lchild;
                    copyit(p,q);
                    q->father->lchild=q->rchild;
                    q->rchild->father=q->father;
                    gx=q->father;
                }
            }
    }
    while (gx!=null)
    {
        update(gx);
        gx=gx->father;
    }
}

void erase(avl *p,int num,int biao)
{
    if (biao==p->biao) erasenode(p); else
    {
        if ((num>p->num)||((num==p->num)&&(biao>p->biao)))
            erase(p->rchild,num,biao);
        else
            erase(p->lchild,num,biao);
    }
}

LL getkthsum(int k)
{
    LL sum=0;
    avl *p=root;
    while (p!=null)
    {
        if (k>=p->lchild->size+1)
        {
            sum+=p->lchild->sum+p->num;
            k-=p->lchild->size+1;
            p=p->rchild;
        }
        else
            p=p->lchild;
    }
    return sum;
}

void Gua(avl *p,int k)
{
    if (p==null) return;
    if (k>=p->lchild->size+1)
    {
        Gua(p->lchild,p->lchild->size);
        getans[place[p->biao]]++;
        Gua(p->rchild,k-p->lchild->size-1);
    }
    else
        Gua(p->lchild,k);
}

int main()
{
    null=new avl;
    null->lchild=null->rchild=null->father=null;
    null->num=null->depth=null->size=0;
    null->sum=0;

    //freopen("E.in","r",stdin);
    scanf("%d %d",&N,&W);
    for (int i=1;i<=N;i++) scanf("%d %d",&a[i],&b[i]);
    for (int i=1;i<=N;i++) ls[i]=m_p(b[i],m_p(a[i],i));
    sort(ls+1,ls+N+1);
    sum[0]=0;
    for (int i=1;i<=N;i++) a[i]=ls[i].w2.w1,b[i]=ls[i].w1,sum[i]=sum[i-1]+a[i],place[i]=ls[i].w2.w2;

    root=new avl;
    root->lchild=root->rchild=root->father=null;
    root->num=maxlongint;
    root->biao=0;
    root->depth=root->size=1;
    root->sum=0;

    for (int i=1;i<=N;i++) insert(root->lchild,a[i],i,root);
    ans=(LL)maxlongint*maxlongint;
    for (int i=1;i<=N+1;i++)
    {
        if ((i-1<=W)&&(i-1+N>=W)) ans=min(ans,sum[i-1]+getkthsum(W-i+1));
        if (i==N+1) break;
        erase(root->lchild,a[i],i);
        insert(root->lchild,b[i]-a[i],i,root);
    }

    printf("%d\n",ans);

    memset(getans,0,sizeof(getans));
    root=new avl;
    root->lchild=root->rchild=root->father=null;
    root->num=maxlongint;
    root->biao=0;
    root->depth=root->size=1;
    root->sum=0;
    for (int i=1;i<=N;i++) insert(root->lchild,a[i],i,root);
    for (int i=1;i<=N+1;i++)
    {
        if ((i-1<=W)&&(i-1+N>=W))
            if (ans==sum[i-1]+getkthsum(W-i+1))
            {
                for (int j=1;j<=i-1;j++) getans[place[j]]++;
                Gua(root->lchild,W-i+1);
                break;
            }
        erase(root->lchild,a[i],i);
        insert(root->lchild,b[i]-a[i],i,root);
    }
    for (int i=1;i<=N;i++) printf("%d",getans[i]);printf("\n");

    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
#include <utility>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int,int> PII;
typedef pair<int,PII> PIII;

#define LL long long
#define ULL unsigned long long
#define m_p make_pair
#define l_b lower_bound
#define p_b push_back
#define w1 first
#define w2 second
#define maxlongint 2147483647
#define biglongint 2139062143

const int maxn=300005;

struct avl
{
    avl *lchild,*rchild,*father;
    int num,depth,size,biao;
    LL sum;
} *root,*null;

int a[maxn],b[maxn],getans[maxn],place[maxn];
LL sum[maxn];
PIII ls[maxn];
int N,W;
LL ans;

void update(avl *p)
{
    if (p==null) return;
    p->depth=max(p->lchild->depth,p->rchild->depth)+1;
    p->sum=p->lchild->sum+p->rchild->sum+p->num;
    p->size=p->lchild->size+p->rchild->size+1;
}

void spinl(avl *a)
{
    avl *b=a->lchild;
    a->lchild=b->rchild;
    b->rchild->father=a;
    b->rchild=a;
    b->father=a->father;
    a->father=b;
    update(a);
    update(b);
    if ((b!=null)&&(b->father!=null))
    {
        if (b->father->lchild==a) b->father->lchild=b;
                            else  b->father->rchild=b;
    }
}

void spinr(avl *a)
{
    avl *b=a->rchild;
    a->rchild=b->lchild;
    b->lchild->father=a;
    b->lchild=a;
    b->father=a->father;
    a->father=b;
    update(a);
    update(b);
    if ((b!=null)&&(b->father!=null))
    {
        if (b->father->lchild==a) b->father->lchild=b;
                            else  b->father->rchild=b;
    }
}

void insert(avl *&p,int num,int biao,avl *father)
{
    if (p==null)
    {
        avl *q=new avl;
        q->father=father;
        q->lchild=q->rchild=null;
        q->num=num;
        q->sum=num;
        q->size=1;
        q->biao=biao;
        p=q;
    }
    else
    {
        if ((num>p->num)||((num==p->num)&&(biao>p->biao))) insert(p->rchild,num,biao,p); else insert(p->lchild,num,biao,p);
        update(p);
        if (p->depth>2)
            if (abs(p->lchild->depth-p->rchild->depth)>=2)
            {
                if (p->lchild->depth>p->rchild->depth)
                    spinl(p);
                else
                    spinr(p);
            }
    }
}

void copyit(avl *p,avl *q)
{
    p->num=q->num,p->biao=q->biao,p->sum=q->sum;
}

void erasenode(avl *p)
{
    avl *gx;
    if ((p->lchild==null)&&(p->rchild==null))
    {
        if (p->father->lchild==p) p->father->lchild=null; else p->father->rchild=null;
        gx=p->father;
    }
    else
    {
        if (p->lchild==null)
        {
            if (p->father->lchild==p) p->father->lchild=p->rchild; else p->father->rchild=p->rchild;
            p->rchild->father=p->father;
            gx=p->father;
        }
        else
            if (p->rchild==null)
            {
                if (p->father->lchild==p) p->father->lchild=p->lchild; else p->father->rchild=p->lchild;
                p->lchild->father=p->father;
                gx=p->father;
            }
            else
            {
                avl *q=p->rchild;
                if (q->lchild==null)
                {
                    copyit(p,q);
                    p->rchild=q->rchild;
                    q->rchild->father=p;
                    gx=p;
                }
                else
                {
                    while (q->lchild!=null) q=q->lchild;
                    copyit(p,q);
                    q->father->lchild=q->rchild;
                    q->rchild->father=q->father;
                    gx=q->father;
                }
            }
    }
    while (gx!=null)
    {
        update(gx);
        gx=gx->father;
    }
}

void erase(avl *p,int num,int biao)
{
    if (biao==p->biao) erasenode(p); else
    {
        if ((num>p->num)||((num==p->num)&&(biao>p->biao)))
            erase(p->rchild,num,biao);
        else
            erase(p->lchild,num,biao);
    }
}

LL getkthsum(int k)
{
    LL sum=0;
    avl *p=root;
    while (p!=null)
    {
        if (k>=p->lchild->size+1)
        {
            sum+=p->lchild->sum+p->num;
            k-=p->lchild->size+1;
            p=p->rchild;
        }
        else
            p=p->lchild;
    }
    return sum;
}

void Gua(avl *p,int k)
{
    if (p==null) return;
    if (k>=p->lchild->size+1)
    {
        Gua(p->lchild,p->lchild->size);
        getans[place[p->biao]]++;
        Gua(p->rchild,k-p->lchild->size-1);
    }
    else
        Gua(p->lchild,k);
}

int main()
{
    null=new avl;
    null->lchild=null->rchild=null->father=null;
    null->num=null->depth=null->size=0;
    null->sum=0;

    //freopen("E.in","r",stdin);
    scanf("%d %d",&N,&W);
    for (int i=1;i<=N;i++) scanf("%d %d",&a[i],&b[i]);
    for (int i=1;i<=N;i++) ls[i]=m_p(b[i],m_p(a[i],i));
    sort(ls+1,ls+N+1);
    sum[0]=0;
    for (int i=1;i<=N;i++) a[i]=ls[i].w2.w1,b[i]=ls[i].w1,sum[i]=sum[i-1]+a[i],place[i]=ls[i].w2.w2;

    root=new avl;
    root->lchild=root->rchild=root->father=null;
    root->num=maxlongint;
    root->biao=0;
    root->depth=root->size=1;
    root->sum=0;

    for (int i=1;i<=N;i++) insert(root,a[i],i,null);
    ans=(LL)maxlongint*maxlongint;
    for (int i=1;i<=N+1;i++)
    {
        if ((i-1<=W)&&(i-1+N>=W)) ans=min(ans,sum[i-1]+getkthsum(W-i+1));
        if (i==N+1) break;
        erase(root,a[i],i);
        insert(root,b[i]-a[i],i,null);
    }

    printf("%d\n",ans);

    memset(getans,0,sizeof(getans));
    root=new avl;
    root->lchild=root->rchild=root->father=null;
    root->num=maxlongint;
    root->biao=0;
    root->depth=root->size=1;
    root->sum=0;
    for (int i=1;i<=N;i++) insert(root,a[i],i,null);
    for (int i=1;i<=N+1;i++)
    {
        if ((i-1<=W)&&(i-1+N>=W))
            if (ans==sum[i-1]+getkthsum(W-i+1))
            {
                for (int j=1;j<=i-1;j++) getans[place[j]]++;
                Gua(root,W-i+1);
                break;
            }
        erase(root,a[i],i);
        insert(root,b[i]-a[i],i,null);
    }
    for (int i=1;i<=N;i++) printf("%d",getans[i]);printf("\n");

    return 0;
}