跳到正文

个人记录

1

Nauts_mgy约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年11月10日
收录
2026年11月10日
更新
2026年11月21日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5 * 1e5 + 5;
int n;
int c[N];ll a[N];
ll maxn,cmax; int maxc;
ll dq,ans;
ll col[N];

int main(){
	scanf("%d",&n);
	for(int i = 1; i <= n; i++){
		scanf("%d%lld",&c[i],&a[i]);
	}
	maxn = 0; cmax = 0; maxc = 0;
	memset(col, -0x3f, sizeof(col));
	ans = 0;
	for(int i = 1; i <= n; i++){
		int cc = c[i];
		ll val = a[i];
		ll now;
		if(cc != maxc){
			now = maxn + val;
		}else{
			now = cmax + val;
		}
		if(now > col[cc]){
			col[cc] = now;
			if(col[cc] > maxn){
				cmax = maxn;
				maxn = col[cc];
				maxc = cc;
			}else if(col[cc] > cmax && cc != maxc){
				cmax = col[cc];
			}
		}
		ans = max(ans, col[cc]);
	}
	printf("%lld\n", max(ans, 0ll));
	return 0;
}
原文