跳到正文

个人记录

1

whr_1230约 2 分钟
原文

这是 2025年10月29日 存档的第 4 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年10月22日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 2 分钟

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1
C++
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+10;
int n;
int a[maxn],col[maxn],ans;
int get_res()
{
	int pre1=0,pre2=0,sum=0;
	for(int i=1;i<=n;i++)
	{
		if(col[i]==1)
		{
			if(a[i]==a[pre1])sum+=a[pre1];
			pre1=i;
		}
		else 
		{
			if(a[i]==a[pre2])sum+=a[pre2];
			pre2=i;
		}
	}
	return sum;
}
void dfs(int x)
{
	if(x>n)
	{
		ans=max(ans,get_res());
		return;
	}
	col[x]=1;
	dfs(x+1);
	col[x]=2;
	dfs(x+1);
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	ans=0;	
	dfs(1);
	cout<<ans<<"\n";
}
int main()
{
//	freopen("T3.in","r",stdin);
//	freopen("T3.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int _;
	cin>>_;
	while(_--)
	{
		solve();
	}
	return 0;
}
/*
3 
3 
1 2 1 
4 
1 2 3 4 
8 
3 5 2 5 1 2 1 4

*/