結果

問題 No.1525 Meximum Sum
ユーザー vjudge1vjudge1
提出日時 2024-10-20 21:50:10
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 518 bytes
コンパイル時間 1,930 ms
コンパイル使用メモリ 200,268 KB
実行使用メモリ 10,572 KB
最終ジャッジ日時 2024-10-20 21:50:15
合計ジャッジ時間 3,530 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
long long n,a[500500],at[500500];
long long ans,tot,l,r;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n; 
    for(int i=1;i<=n;i++){
        cin>>a[i];
        at[a[i]]=i;
    }
    l=r=at[0];
    tot=1;
    for(int i=1;i<=n-1;i++){
    	if(l<at[i]&&at[i]<r){
    		tot++;
    		continue;
		}
		else if(l>at[i]){
			ans+=(n-r+1)*(l-at[i])*tot;
			tot++;
			l=at[i];
		}
		else{
			ans+=(at[i]-r)*l*tot;
			tot++;
			r=at[i];
		}
	}
	cout<<ans+tot;
}
0