#include <bits/stdc++.h>
using namespace std;
const int maxn=50;
void getnext(char str[]) {
int l= strlen(str), i,j,k, temp;
k=l-2;
while(k-1>=0&&str[k]>str[k+1])k--;
i=k+1;
while(i<1&& str[i]> str[k])i++;
temp = str[k] ;
str[k]= str[i-1];
str[i-1] =temp;
for(i= l-1; i>k; i--)
for(j = k+1; j<i; j++)
if(str[j]>str[j+1]) {
temp = str[i];
str[i]= str[j+1];
str[j + 1] = temp;
}
return;
}
int main() {
char a[maxn];
int n;
cin>>a>>n;
while(n>0) {
getnext(a);
n --;
}
cout << a << endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int w[35000],d[35000],dp[35000];
int main()
{
int n,m;
scanf("%d%d",&n, &m);
for(int i=1; i<=n; ++i)
scanf("%d%d" ,&w[i],&d[i]);
for(int i=1; i<=n; ++i)
{
for(int j=m; j>=w[i]; --j)
dp[j]=max(dp[j],dp[j-w[i]]+d[i]);
}
printf("%d\n" ,dp[m]);
return 0;
}
#include <iostream>
using namespace std;
const int NUM= 5;
int r(int n) {
int i;
if(n<=NUM) return n;
for(int i= 1; i<=NUM; ++i)
if(r(n-i)<0) return i;
return -1;
}
int main() {
int n;
cin>>n;
cout<<r(n)<< endl;
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
const int SIZE= 100;
const int INFINITY = 10000;
const bool LEFT= true;
const bool RIGHT = false;
const bool LEFT_TO_RIGHT= true;
const bool RIGHT_TO_LEFT= false;
int n,hour[SIZE];
bool pos[SIZE];
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int go(bool stage)
{
int i,j, num, tmp,ans;
if(stage == RIGHT_TO_LEFT)
{
num = 0;
ans = 0;
for(i= 1; i<=n; ++i)
if(pos[i]== RIGHT)
{
num++;
if(hour[i]>ans)
ans=hour[i];
}
if(___(1)___)
return ans;
ans = INFINITY;
for(i= 1; i<=n-1; ++i)
if(pos[i] == RIGHT)
for(j=i+1; j<=n; ++j)
if(pos[j] == RIGHT)
{
pos[i]= LEFT;
pos[j]= LEFT;
tmp=max(hour[i],hour[j])+___(2)___;
if(tmp < ans)
ans = tmp;
pos[i] = RIGHT;
pos[j] = RIGHT;
}
return ans;
}
if(stage == LEFT_TO_ RIGHT)
{
ans = INFINITY;
for(i=1; i<=n; ++i)
if(___(3)___)
{
pos[i] = RIGHT;
tmp=___(4)___;
if(tmp < ans)
ans = tmp;
___(5)___;
}
return ans;
}
return 0;
}
int main()
{
int i;
cin>> n;
for(i=1; i<=n; ++i)
{
cin>> hour[i];
pos[i] = RIGHT;
}
cout<< go(RIGHT_TO_LEFT)<<endl;
return 0;
}
#include <cstdio>
#include <cstring>
int n, m, k, ans;
int hash[5][5];
void work(int x, int y, int tot) {
int i, j;
if(tot==k) {
ans++;
return;
}
do {
while (hash[x][y]) {
y++;
if(y==m) {
x++;
y=___(1)___;
}
if(x == n)
return;
}
for(i=x=x-1; i<=x+1; i++)
if(i>=&&i<n)
for(j=y-1; j<=y+1; j++)
if(j>=0 && j<m)
___(2)___;
___(3)___;
for(i=x-1; i<=x+1; i++)
if(i>=0 && i< n)
for(j=y-1; j<=y+1; j++)
if(j>0 &&j<m)
hash[i][i]--;
___(4)___;
if(y== m) {
x++;
y=0;
}
if(x==n)
return;
} while(1);
}
int main() {
scanf("%d%d%d", &n, &m, &k);
ans = 0;
memset(hash, 0, sizeof(hash));
___(5)___;
printf("%d\n", ans);
return 0;
}