fork download
  1. /*Coded by::
  2.   **Avinash Tiwary**
  3.   **BE/10298/2015**
  4.   **Production Engineer**
  5.   **Producing <code>**
  6. */
  7. #include<bits/stdc++.h>
  8. #define buf ios_base::sync_with_stdio (0), cin.tie (0)
  9. typedef long long ll;
  10. typedef double dob;
  11. #define M5 100009
  12. #define M6 1000009
  13. #define M 1000000007
  14. #define INF 5e17
  15. using namespace std;
  16. typedef vector<ll> V;
  17. typedef queue<ll > Q;
  18. typedef stack<ll> S;
  19. typedef pair<ll,ll> P;
  20. #define pb push_back
  21. bool desc(ll i, ll j) { return i > j; }
  22. ll power(ll x,ll n)
  23. { if(n==0||x==0) return 1;
  24. else if(n%2 == 0) return power((x*x)%M,n/2);
  25. else return (x*power((x*x)%M,(n-1)/2))%M;
  26. }
  27. ll fac[M6],ar[M6];
  28. bool prime01[M6],mark[10000];
  29. void fact(){
  30. fac[0]=1;
  31. for(ll i=1;i<M6;i++) fac[i]=(i*fac[i-1]);
  32. }
  33. void sieve(ll m){
  34. memset(prime01,1,sizeof(prime01));
  35. prime01[1]=0; prime01[0]=0;
  36. for(ll p=2;p*p<m;p++){
  37. if(prime01[p]){
  38. for(ll i=p*2;i<m;i+=p) prime01[i]=0;
  39. }
  40. }
  41. //for(ll i=2;i<m;i++) if(prime01[i]) prime.pb(i);
  42. }
  43. int main(){
  44. buf;
  45. //sieve(M6);
  46. //fact();
  47. ll i,j,k,n,m,test,ans,a,b,t; string s;
  48. test=1;
  49. while(test--){
  50. cin>>n>>k;
  51. std::vector<P> v;
  52. for(i=1;i<=n;i++){
  53. cin>>ar[i];
  54. v.pb({ar[i],i});
  55. }
  56. sort(v.begin(),v.end());
  57. map<ll,ll> vis,out;
  58. ans=0;
  59. set<ll> ss;
  60. for(i=k+1;i<=k+n;i++) ss.insert(i);
  61. for(i=n-1;i>=0;i--){
  62. j=max(v[i].second,k+1);
  63. auto it=ss.lower_bound(j);
  64. out[v[i].second]=*it;
  65. ss.erase(it);
  66. ans+=(*it-v[i].second)*v[i].first;
  67. }
  68. cout<<ans<<"\n";
  69. for(i=1;i<=n;i++) cout<<out[i]<<" ";
  70. }
  71. return 0;
  72. }
Success #stdin #stdout 0s 31856KB
stdin
5 2
4 2 1 10 2
stdout
20
3 6 7 4 5