#include <bits/stdc++.h>
using namespace std;
#define int long long
#define OYY LLONG_MAX
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mid (start+end)/2
#define lim 65
#define fi first
#define se second
int n;// 1 2 3
int cev=LLONG_MAX;
bool stop=true;
string s;
int dp[(1<<16)][16];
inline int dpp(int sira,int mask,int tut,string st){
if(sira==n){
cev=min(cev,tut);
stop=true;
}
//if(~dp[mask][sira])return dp[mask][sira];
int yum=LLONG_MAX;
for(int i=0;i<n;i++){
if(mask&(1<<i))continue;
if(st.size()>0 && s[i]==st.back())continue;
yum=min(yum,dpp(sira+1,mask|(1<<i),tut+abs(sira-i),st+s[i]));
}
return dp[mask][sira]=yum;
}
int32_t main(){
faster
memset(dp,-1,sizeof(dp));
cin>>n>>s;
string st="";
dpp(0,0,0,st);
if(stop==false)cout<<-1<<'\n';
else cout<<cev/2<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
29 ms |
8540 KB |
Output is correct |
5 |
Execution timed out |
1057 ms |
8540 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
29 ms |
8540 KB |
Output is correct |
5 |
Execution timed out |
1057 ms |
8540 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Runtime error |
9 ms |
17172 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
29 ms |
8540 KB |
Output is correct |
5 |
Execution timed out |
1057 ms |
8540 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |