// #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using lg = long long; using pii = pair; using pll = pair; #define TEST cerr << "TEST" << endl #define AMARI 998244353 //#define AMARI 1000000007 #define TEMOTO ((sizeof(long double) == 16) ? false : true) #define TIME_LIMIT 1980 * (TEMOTO ? 1 : 1000) #define el '\n' #define El '\n' // 疑似乱数(XorShift) unsigned long xor128(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } #define MULTI_TEST_CASE false void solve(void) { int n; cin >> n; lg ans = 0; vector a(n); for(int i = 0; i < n; i++){ cin >> a[i]; if(i != n -1)ans += a[i]; } ans += a.back() % a[0]; for(int i = 1; i < n - 1; i++){ lg temp = ans; temp -= a[i - 1] % a[i]; temp -= a[i] % a[i + 1]; temp -= a[n - 1] % a[0]; temp += a[i - 1] % a[i + 1]; temp += a[n - 1] % a[i]; temp += a[i] % a[0]; ans = max(ans,temp); } cout << ans << el; return; } void calc(void) { int n = 5; vector a(n); for(int i = 0; i < n; i++){ if(i == 0)a[i] = xor128() % 2 + 1; else{ a[i] = a[i - 1] + xor128() % 10 + 1; } } cout << "a = {"; for(int i = 0; i < n; i++){ if(i)cout << ','; cout << a[i]; } cout << "}\n"; lg ans = 0; vector tempv; int cnt = 0; do{ lg temp = 0; for(int i = 0; i < n - 1; i++){ temp += a[i] % a[i + 1]; } temp += a.back() % a[0]; if(temp > ans){ ans = temp; tempv = a; cnt = 1; } if(temp == ans)cnt++; }while(next_permutation(a.begin(),a.end())); cout << ans << el; cout << cnt << el; for(int i = 0; i < n; i++)cout << tempv[i] << ' '; cout << el; cout << el; return; } int main(void) { cin.tie(nullptr); ios::sync_with_stdio(false); /* for(int i = 0; i < 100; i++){ calc(); } */ int t = 1; if(MULTI_TEST_CASE) cin >> t; while(t--) { solve(); } return 0; }