Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define cnl cout << nl;
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ull unsigned ll
- #define RV return void
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define fixed(n) fixed << setprecision(n)
- #define cin(v) for(auto&x:v) cin >> x;
- #define cout(v) for(auto&x:v) cout << x << " ";
- void files(){
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- int n , ans , cnt=0;;
- vector < int > v ;
- vector < vector < int > > dp;
- int rec(int mask , int prev){
- if(!mask) {
- return v[prev] ;
- }
- int &ret = dp[mask][prev];
- if(~ret)
- return ret ;
- ret =0;
- for(int i = 0 ; i < n ; i++){
- if((mask >> i) & 1 ){
- if(prev == n + 1)
- ret = max(ret , v[i] + rec( mask^(1<<i) , i ));
- else
- ret = max(ret , abs(v[prev]-v[i])+ rec( mask^(1<<i) , i ));
- }
- }
- return ret ;
- }
- void build(int mask , int prev){
- if(!mask) {
- return ;
- }
- int ret = dp[mask][prev] , ret2=0;
- for(int i = 0 ; i < n ; i++){
- if((mask >> i) & 1 ){
- if(prev == n+1)
- ret2 = v[i] ;
- else
- ret2= abs(v[prev]-v[i]);
- ret2 += rec( mask^(1<<i) , i );
- if(ret2 == ret) cnt++ , build( mask^(1<<i) , i);
- }
- }
- return ;
- }
- void solve(){
- while(cin >> n and n){
- v.assign(n,0);
- for(int i =0 ; i < n and cin >> v[i] ; i++);
- // 2 * n + difference between any adj ( + start + end)
- dp.assign(1 << n , vector < int > (n+2 , -1));
- ans = rec( (1 << n) - 1 , n+1 ) + 2*n;
- cnt = 0;
- build((1 << n) - 1 , n+1);
- cout << ans << " " << cnt << nl;
- // 20 8
- // 24 2
- }
- }
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- // files();
- int testCase=1;
- // cin >> testCase ;
- for(int i=1 ; i <= testCase ; i++){
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement