# bzoj4241: 历史研究（回滚莫队）

//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
R int res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R ll x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=1e5+5;
int n,m;ll ans[N],res;
int bl[N],cnt[N],b[N],a[N],tot,S;
struct node{
int l,r,id;
node(){}
node(R int l,R int r,R int id):l(l),r(r),id(id){}
inline bool operator <(const node &b)const{return bl[l]==bl[b.l]?r<b.r:bl[l]<bl[b.l];}
}q[N];
ll solve(int l,int r){
static int cnt[N];ll res=0;
fp(i,l,r)cnt[a[i]]=0;
fp(i,l,r)++cnt[a[i]],cmax(res,1ll*cnt[a[i]]*b[a[i]]);
return res;
}
int calc(int i,int id){
int qr=min(n,id*S),r=qr;res=0;
memset(cnt,0,sizeof(cnt));
for(;bl[q[i].l]==id;++i){
if(bl[q[i].l]==bl[q[i].r]){ans[q[i].id]=solve(q[i].l,q[i].r);continue;}
ll ret=res;
ans[q[i].id]=res;
fp(k,q[i].l,qr)--cnt[a[k]];
res=ret;
}return i;
}
int main(){
//	freopen("testdata.in","r",stdin);