SHARE
TWEET

Untitled

a guest Feb 17th, 2018 242 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int MAX_N = 1e6+10;
  5. const int mod = 1e9+7;
  6.  
  7. ll a[MAX_N],n,fact[MAX_N],rfact[MAX_N];
  8. ll num(int t){
  9.     return fact[n-t-1]*rfact[n-t+1]%mod;
  10. }
  11. main(){
  12.     scanf("%lld", &n);
  13.     for(ll i=1;i<=n;i++)
  14.         scanf("%lld", &a[i]);
  15.     fact[0]=rfact[n+1]=1;
  16.     for(int i=1;i<=n+1;i++)
  17.         fact[i]=(fact[i-1]*i)%mod;
  18.     for(int i=n;i>=1;i--)
  19.         rfact[i]=(rfact[i+1]*i)%mod;
  20.     sort(a+1,a+n+1);
  21.     ll cnt=0,curr=0,ans=0;
  22.     for(ll i=1;i<=n&&a[i]!=a[n];i++){
  23.         if(a[i]==a[i-1])
  24.             cnt++;
  25.         else
  26.             curr+=cnt,cnt=1;
  27.         ans=(ans+num(curr)*a[i]%mod)%mod;
  28.     }
  29.     printf("%d",ans);
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top