Untitled

 avatar
user_0483151
plain_text
18 days ago
1.6 kB
3
Indexable
// y.ha
#include <bits/stdc++.h>
#define cook '\n'
#define maxn 15
#define Task "D"
#define bit(x,i) ( ( x >> i ) & 1 )
using namespace std;
int n,a[maxn],T[(1<<15)+1][maxn],cnt[10000][maxn] ;

int Calc(int i,int j) {
    return abs(a[j]-a[i]) - a[i] + a[j] ;
}

void solve() {
    memset(T,0,sizeof(T)) ;
    memset(cnt,0,sizeof(cnt)) ;
    for ( int i = 0; i < n; i++ ) {
        T[(1<<i)][i] = 2*a[i] ;
        cnt[2*a[i]][1] ++ ;
    }
    for ( int mask = 0; mask < ( 1 << n ); mask++ ) {
        for ( int q = 0; q < n; q++ ) {
            if ( bit(mask,q) ) continue ;
            for ( int p = 0; p < n; p++ )
            if ( bit(mask,p) ) {
                int newmask = mask | (1 << q) ;
                int x = T[mask][p]+Calc(p,q) ;
                if ( x >= T[newmask][q] ) {
                    int y = T[mask][p] ;
                    int k = __builtin_popcount(newmask) ;
                    cnt[x][k] += cnt[y][k-1] ;
                    T[newmask][q] = x;
                }
            }
        }
    }
    int ans = 0;
    int fullmask = ( 1 << n ) - 1;
    for ( int i = 0; i < n; i++ ) ans = max(ans,T[fullmask][i]) ;
    cout << ans + 2*n << " " << cnt[ans][n] << cook ;
}

int main()
{
    ios_base::sync_with_stdio(0) ;
    cin.tie(nullptr) ;
    if(fopen(Task".INP","r")) {
        freopen(Task".INP","r",stdin);
        freopen(Task".OUT","w",stdout);
    }
    while ( cin >> n ) {
        if ( !n ) break ;
        for ( int i = 0; i < n; i++ ) cin >> a[i] ;
        solve() ;
    }
    return(0) ;
}
Leave a Comment