﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Crazygir1's Sky</title><link>http://www.cppblog.com/crazygir1/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 19:08:43 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 19:08:43 GMT</pubDate><ttl>60</ttl><item><title>server</title><link>http://www.cppblog.com/crazygir1/archive/2008/07/19/56613.html</link><dc:creator>crazygir1</dc:creator><author>crazygir1</author><pubDate>Sat, 19 Jul 2008 09:36:00 GMT</pubDate><guid>http://www.cppblog.com/crazygir1/archive/2008/07/19/56613.html</guid><wfw:comment>http://www.cppblog.com/crazygir1/comments/56613.html</wfw:comment><comments>http://www.cppblog.com/crazygir1/archive/2008/07/19/56613.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/crazygir1/comments/commentRss/56613.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/crazygir1/services/trackbacks/56613.html</trackback:ping><description><![CDATA[ #include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define LL long long

const int maxn = 1000100;

int n;
LL cost[maxn],f[maxn];
int q[maxn];
LL catchup[maxn];
int qh,qt;

LL GetF(int a,int b) {	
	return f[b]+(LL)(a-b)*(LL)(a-b-1)/2+cost[a];
}	

int FindCatchup(int a,int b) {	// can be implemented as an expression for easier situations
	int l=b+1,r=n,m;
	int ret=n+1;
	while (l<=r) {
		m=(l+r)/2;
		if (GetF(m,b)<=GetF(m,a))	{
			ret=m;
			r=m-1; }
		else l=m+1;
	}
	return ret;
}

int main() {
	int i,j;
	LL k;
	freopen("server.in","r",stdin);
	freopen("server.out","w",stdout);
	cin>>n;
	for (int i=1;i<=n;i++)
		cin>>cost[i];
	qh=0;qt=0;
	q[qh]=0;
	f[0]=0;
	for (int i=1;i<=n;i++) {
		while (qh<qt&&catchup[qh+1]<=i) qh++;
		f[i]=GetF(i,q[qh]);//f[q[qh]]+(LL)(i-q[qh])*(LL)(i-q[qh]-1)/2+cost[i];
		while (qh<=qt) {
			k=FindCatchup(q[qt],i);
			if (qh<qt&&k<=catchup[qt])
				qt--;
			else {
				catchup[++qt]=k;q[qt]=i;break;
			}
		}
	}
	cout<<f[n]<<endl;
	return 0;
}<img src ="http://www.cppblog.com/crazygir1/aggbug/56613.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/crazygir1/" target="_blank">crazygir1</a> 2008-07-19 17:36 <a href="http://www.cppblog.com/crazygir1/archive/2008/07/19/56613.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>