// Written By NewbieChd
#include <cstdio>
#include <cctype>
#include <vector>
#include <algorithm>
using namespace std;

const int BUF = 1000000;
char buf[BUF], *p1, *p2;
inline char getChar() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, BUF, stdin), p1 == p2) ? EOF : *p1++; }
inline int read() {
    register int f = 0;
    char c;
    while (!isdigit(c = getChar())) {}
    do
        f = f * 10 + (c ^ 48);
    while (isdigit(c = getChar()));
    return f;
}

const int maxN = 300007;
int p[maxN];
long long dp[maxN];
bool flag[maxN];
vector <int> ans;
inline void judge(int i) {
    int x = min(p[i], p[i + 1]);
    if (x)
        ans.push_back(i), p[i] -= x, p[i + 1] -= x;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("a.in", "r", stdin);
    // freopen("a.out", "w", stdout);
#endif
    int n = read(), i;
    dp[0] = 0, dp[1] = p[1], dp[2] = min(p[1], p[2]);
    for (i = 1; i <= n; ++i)
        p[i] = read(), dp[i] = min(dp[i - 2] + p[i], dp[i - 3] + max(p[i - 1], p[i])),
        flag[i] = dp[i] == dp[i - 3] + max(p[i - 1], p[i]);
    for (i = n - (dp[n - 1] <= dp[n]); i > 0; i = i - 2 - flag[i]) {
        judge(i - 1);
        if (flag[i])
            judge(i - 2);
        judge(i);
    }
    printf("%d\n", (int)ans.size());
    for (auto x : ans)
        printf("%d\n", x);
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define N 300005
using namespace std;
int a[N],p[N],out[N];
ll f[N];
bool v[N];
int n,m,cnt;
void work(int p){
int t=min(a[p],a[p+1]);
if(t)out[++m]=p;
a[p]-=t;a[p+1]-=t;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
ll O=f[max(i-2,0)]+a[i];
ll E=f[max(i-3,0)]+max(a[i],a[i-1]);
f[i]=min(O,E);v[i]=(f[i]!=O);
}
ll ans=min(f[n-1],f[n]);
for(int i=n-(ans==f[n-1]);i>0;i-=2+v[i])
p[++cnt]=i;
reverse(p+1,p+cnt+1);
for(int i=1;i<=cnt;i++){
int pre=p[i-1],now=p[i];
if(v)work(now-1);
work(pre+1);
work(now);
}
printf("%d\n",m);
for(int i=1;i<=m;i++)
printf("%d\n",out[i]);
}

#include<bits/stdc++.h>
//this code is written by Itst
using namespace std;

#define int long long
const int _ = 3e5 + 7;
int dp[_] , pre[_] , N , val[_];
vector < int > stp;

void move(int x){
	if(x == N) return;
	if(val[x] == 0 || val[x + 1] == 0) return;
	stp.push_back(x);
	int t = min(val[x] , val[x + 1]);
	val[x] -= t; val[x + 1] -= t;
}

signed main(){
	ios::sync_with_stdio(0); cin >> N;
	for(int i = 1 ; i <= N ; ++i) cin >> val[i];
	dp[1] = 0; dp[2] = val[1];
	for(int i = 3 ; i <= N + 1 ; ++i)
		if(dp[i - 3] + max(val[i - 1] , val[i - 2]) < dp[i - 2] + val[i - 1])
			dp[i] = dp[pre[i] = i - 3] + max(val[i - 1] , val[i - 2]);
		else dp[i] = dp[pre[i] = i - 2] + val[i - 1];
	for(int i = dp[N] < dp[N + 1] ? N : N + 1 ; i > 1 ; i = pre[i])
		if(pre[i] == i - 3){move(i - 2); move(i - 1); if(i > 3) move(i - 3);}
		else{move(i - 1); if(i > 2) move(i - 2);}
	cout << stp.size() << endl;
	for(int i = 0 ; i < stp.size() ; ++i) cout << stp[i] << endl;
	return 0;
}

#include<bits/stdc++.h>

using namespace std;

using INT = long long;

const int NN =  303030;
int p[NN];
INT dp[NN], ans[NN];

int CalcAns(int n), CalcDp(int n);

vector<int> buf;

int CalcAns(int n) {
	if(n <= 1) return 0;
	if(ans[n] == dp[n]) return CalcDp(n);
	return CalcDp(n-1);
}

int n;

int CalcDp(int n) {
	if(n == 1) {
		buf.push_back(1);
		return 0;
	}
	if(dp[n] == ans[n-1] + p[n]) {
		if(n < ::n) buf.push_back(n);
		buf.push_back(n-1);
		return CalcAns(n-1);
	}
	if(dp[n] == ans[n-2] + max(p[n], p[n-1])) {
		buf.push_back(n-1);
		if(n < ::n) buf.push_back(n);
		if(n > 2) buf.push_back(n-2);
		return CalcAns(n-2);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	cin >> n;
	for(int i=1; i<=n; i++) {
		scanf("%d", p+i);
	}
	dp[1] = p[1];
	for(int i=2; i<=n; i++) {
		dp[i] = min(ans[i-1] + p[i], ans[i-2] + max(p[i], p[i-1]));
		ans[i] = min(dp[i-1], dp[i]);
	}
	CalcAns(n);
	vector<int> tmp;
	for(auto u : buf) {
		if(u <= 0 || u >= n) continue;
		int x = min(p[u], p[u+1]);
		if(x == 0) continue;
		p[u] -= x; p[u+1] -= x;
		tmp.push_back(u);
	}
	cout << tmp.size() << endl;
	for(auto u : tmp) printf("%d ", u);
}

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x;i <= y; ++ i)
#define repd(i,x,y) for(register int i = x;i >= y; -- i)
using namespace std;
typedef long long ll;
template<typename T>inline void read(T&x)
{
	char c;int sign = 1;x = 0;
	do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
	do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
	x *= sign;
}

const int N = 3e5 + 50;

int a[N],g[N],n;
int res[N],tot;ll f[N];

void work(int i)
{
	int x = min(a[i],a[i + 1]); if(!x) return;
	res[++ tot] = i; a[i] -= x; a[i + 1] -= x;
}

int main()
{
	read(n);
	rep(i,1,n) read(a[i]);
	rep(i,1,n)
	{
		f[i] = min(f[max(i-2,0)] + a[i],f[max(i-3,0)] + max(a[i],a[i-1]));
		if(f[i] == f[max(i-3,0)] + max(a[i],a[i-1])) g[i] = 1;
	}
	ll ans = min(f[n],f[n-1]);
	for(int i = n - (ans == f[n-1]);i > 0;i = i - 2 - g[i])
	{
		work(i - 1);
		if(g[i]) work(i - 2);
		work(i);
	}
	printf("%d\n",tot);
	rep(i,1,tot) printf("%d\n",res[i]);

	return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>

#define pb push_back
#define mp make_pair

using namespace std;

template<typename T>inline void upmin(T &x,T y) { y<x?x=y:0; }
template<typename T>inline void upmax(T &x,T y) { x<y?x=y:0; }

typedef unsigned int u32;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod pi=acos(-1);
const int oo=1<<30;
const LL OO=1e18;

const int N=3e5+100;

int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}

LL f[N];bool fr[N];
int p[N];
//Ϊ��������γ��Ȳ�����2
VI ans;
LL res;
inline void work(int k) {
	if (p[k]&&p[k+1]) {
		int t=min(p[k],p[k+1]);
		res+=t;
		ans.pb(k);
		p[k]-=t;
		p[k+1]-=t;
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	int n=gi()+1,i;LL a,b;
	for (i=1;i<=n;i++) {
		p[i]=i==n?0:gi();
		a=f[max(i-2,0)]+p[i];
		b=f[max(i-3,0)]+max(p[i-1],p[i]);
		f[i]=min(a,b);
		fr[i]=f[i]==b;
	}
	for (i=n;i>0;i-=2+fr[i]) {
		work(i-1);
		if (fr[i]) work(i-2);
		work(i);
	}
	if (res!=f[n])
		return 0;
	cout<<ans.size()<<endl;
	for (int t:ans)
		printf("%d\n",t);
	return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>

#define pb push_back
#define mp make_pair

using namespace std;

template<typename T>inline void upmin(T &x,T y) { y<x?x=y:0; }
template<typename T>inline void upmax(T &x,T y) { x<y?x=y:0; }

typedef unsigned int u32;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod pi=acos(-1);
const int oo=1<<30;
const LL OO=1e18;

const int N=3e5+100;

int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}

LL f[N];bool fr[N];
int p[N];
//Ϊ��������γ��Ȳ�����2
VI ans;
inline void work(int k) {
	if (p[k]&&p[k+1]) {
		int t=min(p[k],p[k+1]);
		ans.pb(k);
		p[k]-=t;
		p[k+1]-=t;
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	int n=gi()+1,i;LL a,b;
	for (i=1;i<=n;i++) {
		p[i]=i==n?0:gi();
		a=f[max(i-2,0)]+p[i];
		b=f[max(i-3,0)]+max(p[i-1],p[i]);
		f[i]=min(a,b);
		fr[i]=f[i]==b;
	}
	for (i=n;i>0;i-=2+fr[i]) {
		work(i-1);
		if (fr[i]) work(i-2);
		work(i);
	}
	cout<<ans.size()<<endl;
	for (int t:ans)
		printf("%d\n",t);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using INT=long long;
const int NN=372727;
int a[NN],val[NN],b[NN];
INT dp[NN][2];
vector <int> ANS;
void sol(int k){
	int mn=min(a[k],a[k+1]);
	if(mn==0) return;
	ANS.push_back(k);
	a[k]-=mn;a[k+1]-=mn;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i),b[i]=a[i];
	dp[0][0]=dp[0][1]=0;
	for(int i=1;i<=n;i++){
		dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i];
		if(i>1) dp[i][0]=min(dp[i][0],min(dp[i-2][1],dp[i-2][1])+max(a[i],a[i-1]));

		dp[i][1]=dp[i-1][0];
	}
	int st=n,fl=0;
	if(dp[n][1]<=dp[n][0]) fl=1;
	while(st){
		if(fl){
			st--;
			fl=0;
		}
		else {
			if(dp[st][0]==dp[st-1][0]+b[st]) sol(st-1),sol(st),st--;
			else if(dp[st][0]==dp[st-1][1]+b[st]) sol(st),sol(st-1),st--,fl=1;
			else if(dp[st][0]==dp[st-2][0]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=0;
			else if(dp[st][0]==dp[st-2][1]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=1;
		}
	}
	cout<<ANS.size()<<endl;
	for(int v:ANS) printf("%d\n",v);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back
const int maxN=303000;

int n;
int A[maxN],fr[maxN];
ll F[maxN];
vector<int> O;

void Decr(int x);
int main(){
    scanf("%d",&n);
    for (int i=1;i<=n;i++){
	scanf("%d",&A[i]);
	F[i]=min(F[max(i-2,0)]+A[i],F[max(i-3,0)]+max(A[i],A[i-1]));
	if (F[i]==F[max(i-3,0)]+max(A[i],A[i-1])) fr[i]=1;
    }
    ll Ans=min(F[n],F[n-1]);
    for (int i=Ans==F[n]?n:n-1;i>=1;i=i-2-fr[i]){
	Decr(i-1);if (fr[i]) Decr(i-2);
	Decr(i);
    }
    printf("%d\n",(int)O.size());
    for (int i=0;i<O.size();i++) printf("%d\n",O[i]);return 0;
}
void Decr(int x){
    if (A[x]&&A[x+1]){
	O.pb(x);int mn=min(A[x],A[x+1]);
	A[x]-=mn;A[x+1]-=mn;
    }
    return;
}

#include<bits/stdc++.h>
using namespace std;
#define Set(a,b) memset(a,b,sizeof(a))
template<class T>inline void init(T&x){
	x=0;char ch=getchar();bool t=0;
	for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') t=1;
	for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+(ch-48);
	if(t) x=-x;return;
}typedef long long ll;
const int N=3e5+10;
const ll INF=1e18;
int P[N];
ll f[N];int fr[N];
int n;
vector<int> opt;
inline void Deal(int i){int x=min(P[i],P[i+1]);if(!x) return;opt.emplace_back(i);P[i]-=x,P[i+1]-=x;}
int main()
{
	init(n);for(int i=1;i<=n;++i) init(P[i]);
	for(int i=1;i<=n;++i) {
		ll b;
		f[i]=min(f[max(i-2,0)]+P[i],b=(f[max(i-3,0)]+max(P[i],P[i-1])));
		if(f[i]==b) fr[i]=max(i-3,0);else fr[i]=max(i-2,0);
	}
	ll ans=min(f[n-1],f[n]);
	for(int i=(n-(ans==f[n-1]));i;i=fr[i]) {Deal(i-1);if(fr[i]==i-3) Deal(i-2);Deal(i);}
	printf("%d\n",(int)opt.size());
	for(int v:opt) printf("%d\n",v);
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
	#define eprintf(...) 42
#endif

typedef long long ll;
typedef pair<int, int> pii;
#define mp make_pair

const ll INF = (ll)1e16;
const int N = 900300;
const int K = 40;
int n;
ll a[N];
ll sum[N];
ll dp[N][2];
int par[N][2][3];
int intPoint[N];
int m;
vector<int> ans;

ll getCost(int L, int R, int f1, int f2) {
	int len = R - L;
	if (len == 1) {
		if (a[L] == 0) return 0;
		if (f1 == 0 || f2 == 0) return -1;
		return a[L];
	}
	if (len == 2) {
		if (a[L] == a[L + 1]) return 0;
		if (a[L] < a[L + 1]) {
			if (f2 == 0) return -1;
			return abs(a[L + 1] - a[L]);
		} else {
			if (f1 == 0) return -1;
			return abs(a[L + 1] - a[L]);
		}
	}
	if (f1 == 0 && a[L] > a[L + 1]) return -1;
	if (f2 == 0 && a[R - 1] > a[R - 2]) return -1;
	ll cost = abs(sum[R] - sum[L]);
	if (len == 3) {
		if (a[L] + a[L + 2] <= a[L + 1]) return cost;
		if (f1 == 1 && f2 == 1) return cost;
		if (f1 == 0 && f2 == 0) return -1;
		return cost;
	}
	return cost;
}

void restoreAns(int L, int R, int f1, int f2) {
	//eprintf("restore %d %d %d %d\n", L, R, f1, f2);
	int len = R - L;
	if (len == 1) {
		return;
	}
	if (len == 2) {
		if (a[L] == 0 || a[L + 1] == 0) return;
		ans.push_back(L);
		return;
	}
	if (f1 == 0 && a[L] != 0 && a[L + 1] != 0) {
		ll x = min(a[L], a[L + 1]);
		ans.push_back(L);
		a[L] -= x;
		a[L + 1] -= x;
	}
	if (f2 == 0 && a[R - 1] != 0 && a[R - 2] != 0) {
		ll x = min(a[R - 1], a[R - 2]);
		ans.push_back(R - 2);
		a[R - 2] -= x;
		a[R - 1] -= x;
	}
	for (int i = L; i < R - 1; i++) {
		if (a[i] != 0 && a[i + 1] != 0) {
			ll x = min(a[i], a[i + 1]);
			ans.push_back(i);
			a[i] -= x;
			a[i + 1] -= x;
		}
	}
}

int main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
		sum[i + 1] = sum[i];
		if (i % 2 == 0)
			sum[i + 1] += a[i];
		else
			sum[i + 1] -= a[i];
	}
	for (int i = 0; i <= n; i++) {
		if (i < 5 || i > n - 5) {
			intPoint[m++] = i;
			continue;
		}
		if (a[i - 1] <= a[i - 2] || a[i] <= a[i + 1])
			intPoint[m++] = i;
	}
	for (int i = 0; i <= m; i++)
		dp[i][0] = dp[i][1] = -INF;
	dp[0][0] = 0;
	dp[0][1] = 0;
	for (int i = 0; i < m; i++)
		for (int f1 = 0; f1 < 2; f1++) {
			if (dp[i][f1] < 0) continue;
			for (int j = i + 1; j < min(m, i + K); j++) {
				for (int f2 = 0; f2 < 2 - f1; f2++)
					for (int f3 = 0; f3 < 2; f3++) {
						ll cost = getCost(intPoint[i], intPoint[j], f2, f3);
						if (cost == -1) continue;
						if (dp[i][f1] + cost > dp[j][f3]) {
							dp[j][f3] = dp[i][f1] + cost;
							par[j][f3][0] = i;
							par[j][f3][1] = f1;
							par[j][f3][2] = f2;
						}
					}
			}
		}
	int x = m - 1;
	int f = 0;
	if (dp[x][1] > dp[x][0])
		f = 1;
	while(x > 0) {
		restoreAns(intPoint[par[x][f][0]], intPoint[x], par[x][f][2], f);
		int nx = par[x][f][0];
		f = par[x][f][1];
		x = nx;
	}
	printf("%d\n", (int)ans.size());
	for (int y : ans)
		printf("%d\n", 1 + y);

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const int nax=300*1007;

int n;
ll tab[nax];
ll dp[nax];

vector <int> wyn;

int main()
{
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
		scanf("%lld", &tab[i]);
	for (int i=n-1; i; i--)
		dp[i]=min(dp[i+2]+tab[i+1], dp[i+3]+max(tab[i+1], tab[i+2]));
	int czy=1;
	if (dp[2]+tab[1]<dp[1])
		czy++;
	if (czy==2)
		wyn.push_back(1);
	for (int i=czy; i<n;)
	{
		if (dp[i+2]+tab[i+1]<dp[i+3]+max(tab[i+1], tab[i+2]))
		{
		    wyn.push_back(i+1);
		    wyn.push_back(i);
			i+=2;
		}
		else
		{
		    wyn.push_back(i+1);
		    wyn.push_back(i+2);
		    wyn.push_back(i);
			i+=3;
		}
	}
	auto pom=wyn;
	wyn.clear();
	for (int i : pom)
	{
		if (i<n && tab[i] && tab[i+1])
		{
			ll x=min(tab[i], tab[i+1]);
			tab[i]-=x;
			tab[i+1]-=x;
			wyn.push_back(i);
		}
	}
	printf("%d\n", (int)wyn.size());
	for (int i : wyn)
		printf("%d\n", i);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=300005;
int n,a[maxn],cnt,p[maxn],ans[maxn];
LL f[maxn];
bool v[maxn];
int Get(int pos){
  int t=min(a[pos],a[pos+1]);
  if(t) ans[++ans[0]]=pos;
  a[pos]-=t; a[pos+1]-=t; return t;
}
int main() {
  scanf("%d",&n);
  for(int i=1;i<=n;i++){
    scanf("%d",&a[i]);
    LL t1=f[max(i-2,0)]+a[i], t2=f[max(i-3,0)]+max(a[i-1],a[i]);
    f[i]=min(t1,t2); v[i]=(f[i]!=t1);
  }
  LL res=min(f[n-1],f[n]);
  for(int i=n-(res==f[n-1]);i>0;i-=2+v[i]) p[++cnt]=i;
  reverse(p+1,p+cnt+1);
  for(int i=1;i<=cnt;i++){
    int pre=p[i-1],cur=p[i],ctr=0;
    if(v[cur]) ctr+=Get(cur-1);
    ctr+=Get(pre+1); ctr += Get(cur);
  }
  printf("%d\n",ans[0]);
  for(int i=1;i<=ans[0];i++) printf("%d\n",ans[i]);
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=301000;
map<int,ll> dp[N];
map<int,PII> pre[N],way[N];
int n,a[N];
PII cur;
VI ret,ret2;
void upd(int p,int v,ll cost,PII ww) {
	if (!dp[p].count(v)||cost<dp[p][v]) {
		dp[p][v]=cost;
		way[p][v]=ww;
		pre[p][v]=cur;
	}
}
void dfs(int n,int v) {
	if (n==1) return;
	if (way[n][v].fi!=-1) ret.pb(way[n][v].fi);
	if (way[n][v].se!=-1) ret.pb(way[n][v].se);
	dfs(pre[n][v].fi,pre[n][v].se);
}
int main() {
	scanf("%d",&n);
	rep(i,1,n+1) scanf("%d",a+i);
	rep(i,1,n+1) dp[i].clear(),pre[i].clear(),way[i].clear();
	dp[1][a[1]]=0;
	rep(i,1,n) {
		ll mv=1ll<<60;
		for (auto p:dp[i]) {
			int v=p.fi; ll w=p.se;
			if (w>=mv) continue;
			mv=w;
			cur=mp(i,v);
			if (v==0) upd(i+1,a[i+1],w,mp(-1,-1));
			upd(i+1,max(a[i+1]-v,0),w+min(v,a[i+1]),mp(i,-1));
			if (i+2<=n&&a[i+1]<=a[i+2]) upd(i+2,a[i+2]-a[i+1],w+a[i+1],mp(i+1,-1));
			if (i+2<=n&&a[i+1]>=a[i+2]) upd(i+2,0,w+a[i+2]+min(v,(a[i+1]-a[i+2])),mp(i,i+1));
		}
	}
	int mv=-1; ll w=1ll<<60;
	for (auto p:dp[n]) if (p.se<w) w=p.se,mv=p.fi;
	dfs(n,mv);
	reverse(all(ret));
	for (auto p:ret) {
		if (a[p]>0&&a[p+1]>0) {
			int z=min(a[p],a[p+1]);
			a[p]-=z; a[p+1]-=z;
			ret2.pb(p);
		}
	}
	printf("%d\n",SZ(ret2));
	for (auto p:ret2) printf("%d ",p);
	puts("");
}

#include<bits/stdc++.h>
#define ll long long
#define N 300005
using namespace std;
int a[N],p[N],out[N],n,m,cnt;
ll f[N];
bool v[N];
inline void work(int p){
	int t=min(a[p],a[p+1]);
	if (t) out[++m]=p;
	a[p]-=t; a[p+1]-=t;
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		ll O=f[max(i-2,0)]+a[i];
		ll E=f[max(i-3,0)]+max(a[i],a[i-1]);
		f[i]=min(O,E); v[i]=(f[i]!=O);
	}
	ll ans=min(f[n-1],f[n]);
	for (int i=n-(ans==f[n-1]);i>0;i-=2+v[i])
		p[++cnt]=i;
	reverse(p+1,p+cnt+1);
	for (int i=1;i<=cnt;i++){
		int pre=p[i-1],now=p[i];
		if (v) work(now-1);
		work(pre+1);
		work(now);
	}
	printf("%d\n",m);
	for (int i=1;i<=m;i++)	printf("%d\n",out[i]);
}

#include <bits/stdc++.h>
using namespace std;

const int DIM = 300005;

int arr[DIM]; long long dp[DIM];
pair<int, int> seg[DIM]; vector<int> ans;

void solve(int p) {
    int val = min(arr[p], arr[p + 1]);
    if (val)
        ans.push_back(p);
    arr[p] -= val; arr[p + 1] -= val;
}

int main(void)
{
    int n;
    cin >> n;

    for (int i = 1; i <= n; ++i) {
        cin >> arr[i];

        dp[i] = min(dp[max(i - 2, 0)] + arr[i],
                    dp[max(i - 3, 0)] + max(arr[i - 1], arr[i]));

        seg[i] = dp[i] == dp[max(i - 3, 0)] + max(arr[i - 1], arr[i]) ?
                 make_pair(i - 2, i) : make_pair(i - 1, i);
    }

  //  cout << min(dp[n - 1], dp[n]) << "\n\n";

    for (int i = n - (dp[n - 1] <= dp[n]); i >= 1; i = seg[i].first - 1) {
        if (seg[i].first + 2 == seg[i].second)
            solve(i - 1);
        solve(seg[i].first); solve(i);
    }

    cout << ans.size() << "\n";
    for (int x : ans) cout << x << "\n";

    return 0;
}

#include<bits/stdc++.h>
#define LL long long
#define N 300005
using namespace std;
int a[N],p[N],out[N];
LL f[N];
bool v[N];
int n,m,cnt;
void work(int p){
	int t=min(a[p],a[p+1]);
	if (t) out[++m]=p;
	a[p]-=t; a[p+1]-=t;
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		LL O=f[max(i-2,0)]+a[i];
		LL E=f[max(i-3,0)]+max(a[i],a[i-1]);
		f[i]=min(O,E); v[i]=(f[i]!=O);
	}
	LL ans=min(f[n-1],f[n]);
	for (int i=n-(ans==f[n-1]);i>0;i-=2+v[i])
		p[++cnt]=i;
	reverse(p+1,p+cnt+1);
	for (int i=1;i<=cnt;i++){
		int pre=p[i-1],now=p[i];
		if (v) work(now-1);
		work(pre+1);
		work(now);
	}
	printf("%d\n",m);
	for (int i=1;i<=m;i++)
		printf("%d\n",out[i]);
}

#include <bits/stdc++.h>
using namespace std;
const int N = 5000005;
using ll = long long;
ll f[N],pre[N];int n,a[N];vector<int> ans;
void move(int x)
{
	if (x == n || a[x] == 0 || a[x + 1] == 0) return;
	ans.push_back(x);int t = min(a[x],a[x + 1]);a[x] -= t;a[x + 1] -= t;
}
int main ()
{
	ios::sync_with_stdio(false);
	cin >> n;for (int i = 1;i <= n;i++) cin >> a[i];
	f[2] = a[1];
	for (int i = 3;i <= n + 1;i++)
		if (f[i - 3] + max(a[i - 1],a[i - 2]) < f[i - 2] + a[i - 1])
			f[i] = f[pre[i] = i - 3] + max(a[i - 1],a[i - 2]);
		else f[i] = f[pre[i] = i - 2] + a[i - 1];
	for (int i = f[n] < f[n + 1] ? n : n + 1;i > 1;i = pre[i])
		if (pre[i] == i - 3) { move(i - 2),move(i - 1); if (i > 3) move(i - 3); }
		else {move(i - 1);if (i > 2) move(i - 2);}
	cout << ans.size() << endl;for (auto &i : ans) cout << i << endl;
	return 0;
}

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int gi(){
	int x=0,w=1;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')w=0,ch=getchar();
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return w?x:-x;
}
#define ll long long
const int N=3e5+5;
int n,a[N],mrk[N],ans[N],m;ll dp[N];
void work(int x){
	if(x>0&&a[x]&&a[x+1]){
		int d=min(a[x],a[x+1]);ans[++m]=x;
		a[x]-=d;a[x+1]-=d;
	}
}
int main(){
	n=gi()+1;
	for(int i=1;i<=n;++i){
		if(i<n)a[i]=gi();
		ll x=dp[max(i-3,0)]+max(a[i],a[i-1]);
		ll y=dp[max(i-2,0)]+a[i];
		dp[i]=min(x,y);if(dp[i]==x)mrk[i]=1;
	}
	for(int i=n;i>0;i-=2+mrk[i]){
		work(i-1);
		if(mrk[i])work(i-2);
		work(i);
	}
	printf("%d\n",m);
	for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define re register
#define dmax(a,b) ((a)>(b)?(a):(b))
#define dmin(a,b) ((a)<(b)?(a):(b))
long long f[300100];
int p[300100],v[300100],ans[300100],m,q[300100];
void dd(re int a)
{
	re int x=dmin(p[a],p[a+1]);
	p[a]-=x;p[a+1]-=x;
	if(x)ans[++m]=a;
}
int main()
{
	re int n,cnt=0;
	scanf("%d",&n);
	for(re int i=1;i<=n;i++)
	{
		scanf("%d",&p[i]);
		re long long x=f[dmax(i-3,0)]+dmax(p[i],p[i-1]),y=f[dmax(i-2,0)]+p[i];
		if(x>y)f[i]=y,v[i]=1;
		else f[i]=x;
	}
	re int n1;
	if(f[n-1]<=f[n])n1=n-1;else n1=n;
	for(;n1>0;n1-=3-v[n1])q[++cnt]=n1;
	for(re int i=cnt;i;i--)
	{
		if(!v[q[i]])dd(q[i]-1);
		dd(q[i]);
		dd(q[i+1]+1);
	}
	printf("%d\n",m);
	for(re int i=1;i<=m;i++)printf("%d\n",ans[i]);
}

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);}
#define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
#define SZ 666666
int n,p[SZ];
vector<int> v;
void go(int x)
{
	int dt=min(p[x],p[x+1]);
	if(!dt) return;
	v.pb(x); p[x]-=dt; p[x+1]-=dt;
}
ll f[SZ]; int g[SZ];
int ss[SZ],sn=0;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%d",p+i);
	for(int i=1;i<=n;++i)
	{
		ll one=f[max(i-2,0)]+p[i],
		two=f[max(i-3,0)]+max(p[i-1],p[i]);
		if(one<two) f[i]=one,g[i]=2;
		else f[i]=two,g[i]=3;
	}
	ll ans=min(f[n-1],f[n]);
	for(int i=n-(ans==f[n-1]);i>0;i-=g[i]) ss[++sn]=i;
	reverse(ss+1,ss+1+sn);
	for(int i=1;i<=sn;++i)
	{
		int x=ss[i];
		if(g[x]==3) go(x-1);
		go(ss[i-1]+1); go(x);
	}
	printf("%d\n",int(v.size()));
	for(auto x:v) printf("%d\n",x);
}

#include<bits/stdc++.h>

using namespace std;

#define NN 300010

using INT = long long;
using pii = pair<int, int>;

int a[NN], d[NN], h[NN];
INT dp[NN];

vector<int> ans;

inline int go(int u) {
	int mn = min(a[u], a[u+1]);
	if(!mn) return 0;
	ans.push_back(u);
	a[u] -= mn; a[u+1] -= mn;
	return 0;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base::sync_with_stdio(0); cout.tie(0);

	int n;
	cin >> n;
	for(int i=1; i<=n; i++) cin >> a[i];

	for(int i=1; i<=2; i++) dp[i] = a[i], d[i] = 1;

	for(int i=3; i<=n; i++) {
		if(dp[i-2] + a[i] < dp[i-3] + max(a[i], a[i-1]))
			dp[i] = dp[i-2] + a[i], d[i] = 1;
		else dp[i] = dp[i-3] + max(a[i], a[i-1]), d[i] = 2;
	}

	for(int i = n - (dp[n] >= dp[n-1]); i>0; i-=d[i]+1) h[i] = h[i-d[i]+1] = 1;
	for(int i=1; i<=n; i++) if(h[i] and h[i+1]) go(i);
	for(int i=1; i<=n; i++) if(h[i]) go(i-1), go(i);

	cout << ans.size() << endl;
	for(int x : ans) cout << x << endl;
}

#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef long long INT;
typedef pair <int, int> pii;
typedef vector <int> VI;

#define NN 300300
int p[NN];
INT dp[NN];
int d[NN];
int h[NN];
VI ans;

void f(int i){
	int mn=min(p[i], p[i+1]);
	if(mn){
		ans.pb(i);
		p[i]-=mn;
		p[i+1]-=mn;
	}
}

int main (){
#ifndef ONLINE_JUDGE
	freopen ("in.in", "r", stdin);
	freopen ("out.out", "w", stdout);
#endif

	int n;
	cin >> n;
	for(int i=1; i<=n; i++) scanf("%d", p+i);

	dp[1]=p[1]; d[1]=1;
	dp[2]=p[2]; d[2]=1;

	for(int i=3; i<=n; i++){
		if(dp[i-2]+p[i]<dp[i-3]+max(p[i], p[i-1])){
			dp[i]=dp[i-2]+p[i];
			d[i]=1;
		}
		else {
			dp[i]=dp[i-3]+max(p[i], p[i-1]);
			d[i]=2;
		}
	}

	for(int i=dp[n]<dp[n-1] ? n : n-1; i>0; i-=d[i]+1) h[i]=h[i-d[i]+1]=1;
	for(int i=1; i<=n; i++) if(h[i] && h[i+1]) f(i);
	for(int i=1; i<=n; i++) if(h[i]) f(i-1), f(i);

	cout << ans.size() << endl;
	for(int x : ans) printf("%d\n", x);

	return 0;
}

#include"bits/stdc++.h"
#define NN 300300

using namespace std;
typedef long long INT;

INT dp[NN];
int a[NN], h[NN], d[NN];
vector<int> ans;

inline void calc(int i) {
	int mn=min(a[i], a[i+1]);
	if(!mn) return ;
	ans.push_back(i);
	a[i]-=mn, a[i+1]-=mn;
}

main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n; scanf("%d", &n);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	for(int i=1; i<=2; i++) dp[i]=a[i], d[i]=1;

	for(int i=3; i<=n; i++) {
		if(dp[i-2]+a[i]<dp[i-3]+max(a[i], a[i-1])) dp[i]=dp[i-2]+a[i], d[i]=1;
		else dp[i]=dp[i-3]+max(a[i], a[i-1]), d[i]=2;
	}

	for(int i=n-(dp[n]>=dp[n-1]); i>0; i-=d[i]+1) {
		h[i]=h[i-d[i]+1]=1;
	}
	for(int i=1; i<=n; i++) if(h[i]) {
		if(h[i+1]) calc(i);
		calc(i-1), calc(i);
	}

	printf("%d\n", ans.size());
	for(int x: ans) printf("%d\n", x);
}

#include <bits/stdc++.h>
using namespace std;

#define N 303030

int n, p[N];
long long dp[N];

bool bad[N];

vector <int> v;

long long ans;

void doing(int k) {
	int val = min(p[k], p[k+1]);
	if (val) v.push_back(k);
	p[k] -= val, p[k+1] -= val;
	ans += val;
}

void oper(int k, int tag) {
	if (tag == 1) doing(k), doing(k - 1);
	else doing(k), doing(k - 1), doing(k + 1);
}

int tar;

int main() {
	//freopen("r.in", "r", stdin);
	tar = (int)clock();
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) scanf("%d", &p[i]);
	dp[1] = p[1];
	dp[2] = p[2];
	for (int i = 3; i <= n + 1; i ++) {
		dp[i] = min(dp[i-2] + p[i], dp[i-3] + max(p[i], p[i-1]));
	}
	int k;
	if (dp[n] < dp[n+1]) k = n;
	else k = n + 1;
	bad[k+1] = 1;
	while (k > 2) {
		if (dp[k] == dp[k-2] + p[k]) bad[k-1] = 1, k = k - 2;
		else bad[k-2] = 1, k = k - 3;
	}
	for (int i = 1; i <= n; i ++) {
		if (!bad[i]) {
			int j = i;
			while (!bad[j]) j ++;
			oper(i, j - i);
			i = j;
		}
	}
	while (clock() - tar <= 1900);
	printf("%d\n", (int)v.size());
	for (int i = 0; i < v.size(); i ++) printf("%d\n", v[i]);

	return 0;
}

#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = (l), i##end = (r);i <= i##end;++i)
using std::cin; using std::cout;
const int maxn = 300200;
typedef long long ll;
const int mod = 998244353;
inline ll pow(ll a,int b,int ans = 1) {
	for(;b;b >>= 1,a = a * a % mod) if(b & 1)
		ans = ans * a % mod;
	return ans;
}
inline ll inverse(int x){ return pow(x, mod - 2); }
int n;
int a[maxn], go[maxn];
ll dp[maxn];
std::vector<int> v;
inline void clear(int x) {
	if(0 < x && x < n && a[x] && a[x + 1]) {
		v.push_back(x);
		int min = std::min(a[x], a[x + 1]);
		a[x] -= min, a[x + 1] -= min;
	}
}
int main() {
	std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;++i) {
		cin >> a[i];
	}
	dp[2] = a[1];
	for(int i = 3;i <= n + 1;++i) {
		ll v0 = dp[i - 3] + std::max(a[i - 1], a[i - 2]), v1 = dp[i - 2] + a[i - 1];
		dp[i] = std::min(v0, v1);
		go[i] = v0 < v1 ? i - 3 : i - 2;
	}
	for(int i = dp[n] < dp[n + 1] ? n : n + 1;i;i = go[i]) {
		if(go[i] == i - 2) {
			clear(i - 1);
			clear(i - 2);
		} else {
			clear(i - 2);
			clear(i - 1);
			clear(i - 3);
		}
	}
	cout << v.size() << '\n';
	for(int i : v) {
		cout << i << '\n';
	}
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef unsigned int uint;
#define MAX 300105
bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"YES" : "NO");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    assert(y>=0);
    x+=y; if (x>=mod) x-=mod;
    assert(x>=0 && x<mod);
}
void et() {
    puts("-1"); exit(0);
}
ll fastPow(ll x, ll y, int mod=MOD) {
    ll ans = 1;
    while(y>0) {
        if(y&1) ans = (x * ans)%mod;
        x = x*x%mod;
        y>>=1;
    }
    return ans;
}
ll gcd1(ll x, ll y) {
    ll z = y;
    while(x%y!=0) {
        z = x%y; x = y; y = z;
    }
    return z;
}

int p[MAX];
ll dp[MAX];
int pre[MAX];
vector<int> ans;
void wk(int i) {
    if (i<=0) return;
    if (p[i]==0||p[i+1]==0) return;
    ans.push_back(i);
    int t=min(p[i],p[i+1]);
    p[i]-=t;p[i+1]-=t;
}
void fmain(int ID) {
    scanf("%d", &n);
    REQ(i,n) scanf("%d", p+i);
    REQ(i,n) {
        if (dp[max(i-2, 0)]+p[i] < dp[max(i-3, 0)]+max(p[i],p[i-1])) {
            dp[i] = dp[max(i-2, 0)]+p[i];
            pre[i]=max(i-2, 0);
        } else {
            dp[i]=dp[max(i-3, 0)]+max(p[i],p[i-1]);
            pre[i]=max(i-3, 0);
        }
    }
    ll A=min(dp[n-1], dp[n]);
    for(int i=dp[n-1]<dp[n] ? n-1 : n; i; i=pre[i]) {
        if (pre[i] == max(i-3,0)) {
            wk(i-1);
        }
        wk(pre[i]+1);
        wk(i);
    }
    printf("%d\n", (int)ans.size());
    for(int c:ans) printf("%d\n", c);
}

int main() {
    int t=1;
//    scanf("%d", &t);
    REQ(i,t) {
        fmain(i);
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

int n,a[300005];
int best[300005][8][2][2];
ll dp[300005][2];
int cut[300005][8][2][2];
P1 prv[300005][2];
int main(){
	scanf("%d",&n);
	repn(i,n){
		scanf("%d",&a[i]);
	}
	rep(i,300005)rep(j,8)rep(x,2)rep(y,2){
		best[i][j][x][y] = -INF;
		dp[i][x] = -INF;
	}
	repn(i,n){
		for(int j=1;j<=6&&i+j-1<=n;j++){
			ll sum[2]={};
			for(int x=i;x<=i+j-1;x++){
				sum[x%2]+=a[x];
			}
			for(int x=i;x<=i+j-1;x++){
				if(sum[x%2]<sum[1-x%2]) continue;
				ll w=a[x]-sum[x%2]+sum[1-x%2];
				ll v=0;
				for(int y=i;y<=i+j-1;y++){
					v*=-1;
					if(y!=x) v+=a[y];
					else v+=w;
					if(v<0) goto plz;
				}
				if(v!=0) goto plz;
				if(best[i][j][(x==i)][(x==i+j-1)]<sum[x%2]-sum[1-x%2]){
					best[i][j][(x==i)][(x==i+j-1)]=sum[x%2]-sum[1-x%2];
					cut[i][j][(x==i)][(x==i+j-1)]=x;
				}
				plz:;;
			}
		}
	}
	dp[0][0] = 0;
	rep(i,n)rep(j,2){
		if(dp[i][j]<0) continue;
		for(int k=1;i+k<=n&&k<=6;k++){
			rep(b,2)rep(c,2){
				if(best[i+1][k][b][c]<0) continue;
				if(j&&b) continue;
				if(dp[i+k][c]<dp[i][j]+best[i+1][k][b][c]){
					dp[i+k][c]=dp[i][j]+best[i+1][k][b][c];
					prv[i+k][c]=mp(b,mp(i,j));
				}
			}
		}
	}
	pair<ll,pair<int,int> >ans;
	rep(j,2) ans=max(ans,make_pair(dp[n][j],make_pair(n,j)));
	int X=ans.sc.fi,Y=ans.sc.sc;
	vector<int>use;
	while(make_pair(X,Y)!=make_pair(0,0)){
		int b=prv[X][Y].fi, i=prv[X][Y].sc.fi, j=prv[X][Y].sc.sc;
		int k=X-i, c=Y;
		for(int q=i+1;q<cut[i+1][k][b][c];q++)
		{
			if(a[q]==0||a[q+1]==0)continue;
			use.pb(q);
			int V=min(a[q],a[q+1]);
			a[q]-=V;
			a[q+1]-=V;
		}
		for(int q=i+k-1;q>=cut[i+1][k][b][c];q--)
		{
			if(a[q]==0||a[q+1]==0)continue;
			use.pb(q);
			int V=min(a[q],a[q+1]);
			a[q]-=V;
			a[q+1]-=V;
		}
		X=i;Y=j;
	}
	printf("%d\n",use.size());
	rep(i,use.size()){
		printf("%d\n",use[i]);
	}
}

/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
template<class T, class... U> void DEBUG(const T &head, const U &... tail) {
#ifdef HOME
    _W('#'); _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...);
#endif
}
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int n;
int p[SIZE];
LL dp[SIZE];
VI ans;
void move(int x){
    if(x<1||x>=n)return;
    int mi=min(p[x],p[x+1]);
    if(!mi)return;
    ans.PB(x);
    p[x]-=mi;
    p[x+1]-=mi;
}
void go(int x){
    move(x-1);
    move(x);
}
void go(int x,int y){
    move(x);
    move(x-1);
    move(y);
}
int main(){
    R(n);
    if(n==1){
        W(0);
        return 0;
    }
    FOR(i,1,n){
        R(p[i]);
    }
    dp[2]=p[1];
    FOR(i,3,n+2){
        dp[i]=dp[i-2]+p[i-1];
        dp[i]=min(dp[i],dp[i-3]+max(p[i-1],p[i-2]));
    }
    int now=n+2;
    while(now>1){
        if(dp[now]==dp[now-2]+p[now-1]){
            go(now-1);
            now-=2;
        }
        else{
            go(now-2,now-1);
            now-=3;
        }
    }
    W(SZ(ans));
    W(ans);
    return 0;
}

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize

const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 300005;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

namespace input {
    template<class T> void re(complex<T>& x);
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);

    template<class T> void re(T& x) { cin >> x; }
    void re(double& x) { string t; re(t); x = stod(t); }
    void re(ld& x) { string t; re(t); x = stold(t); }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
        re(first); re(rest...);
    }

    template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}

using namespace input;

namespace output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);

    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
        pr(first); pr(rest...);
    }

    template<class T1, class T2> void pr(const pair<T1,T2>& x) {
        pr("{",x.f,", ",x.s,"}");
    }
    template<class T> void prContain(const T& x) {
        pr("{");
        bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
        pr("}");
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }

    void ps() { pr("\n"); }
    template<class Arg> void ps(const Arg& first) {
        pr(first); ps(); // no space at end of line
    }
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
        pr(first," "); ps(rest...); // print w/ spaces
    }
}

using namespace output;

namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
    }
}

using namespace io;

template<class T> T invGeneral(T a, T b) {
    a %= b; if (a == 0) return b == 1 ? 0 : -1;
    T x = invGeneral(b,a);
    return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
}

template<class T> struct modular {
    T val;
    explicit operator T() const { return val; }
    modular() { val = 0; }
    modular(const ll& v) {
        val = (-MOD <= v && v <= MOD) ? v : v % MOD;
        if (val < 0) val += MOD;
    }

    friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
    friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
    friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }
    friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }

    modular operator-() const { return modular(-val); }
    modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
    modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
    modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
    friend modular pow(modular a, ll p) {
        modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans;
    }
    friend modular inv(const modular& a) {
        auto i = invGeneral(a.val,MOD); assert(i != -1);
        return i;
    } // equivalent to return exp(b,MOD-2) if MOD is prime
    modular& operator/=(const modular& m) { return (*this) *= inv(m); }

    friend modular operator+(modular a, const modular& b) { return a += b; }
    friend modular operator-(modular a, const modular& b) { return a -= b; }
    friend modular operator*(modular a, const modular& b) { return a *= b; }

    friend modular operator/(modular a, const modular& b) { return a /= b; }
};

typedef modular<int> mi;
typedef pair<mi,mi> pmi;
typedef vector<mi> vmi;
typedef vector<pmi> vpmi;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll dp[MX];
int n;
vi p, P, ret;

void mov(int x) {
	if (x >= n-1 || x < 0) return;
	int mn = min(P[x],P[x+1]); if (!mn) return;
	ret.pb(x+1); P[x] -= mn, P[x+1] -= mn;
}

int main() {
    setIO(); re(n); p.rsz(n); re(p); P = p;
    FOR(i,1,n+1) {
		dp[i] = p[i-1]+dp[max(0,i-2)];
		if (i > 1) ckmin(dp[i],max(p[i-1],p[i-2])+dp[max(0,i-3)]);
    }
    int i = (dp[n] < dp[n-1] ? n : n-1);
    // ps(dp[cur]);
    // ps("HUH",i); exit(0);
    while (i > 0) {
    	if (dp[i] == p[i-1]+dp[max(0,i-2)]) {
    		mov(i-1);
    		mov(i-2);
    		// ps("HUH",i,P[i-1]); exit(0);
    		assert(P[i-1] == 0);
    		i -= 2;
    	} else {
    		mov(i-2);
    		mov(i-1);
    		mov(i-3);
    		assert(P[i-1]+P[i-2] == 0);
    		i -= 3;
    	}
    }
    ps(sz(ret));
    trav(t,ret) ps(t);
}

/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do smth instead of nothing and stay organized
*/

#include<bits/stdc++.h>
using namespace std;
#define N 300010
#define int long long
int n,f[N],p[N],v[N];
vector<int>r;
void m(int i){
	if(i<0)return;
	int x=min(p[i],p[i+1]);
	if(!x)return;
	r.push_back(i);
	p[i]-=x;p[i+1]-=x;
}
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>p[i];
	memset(f,63,sizeof(f));
	f[0]=0;
	for(int i=1;i<=n;i++){
		f[i]=min(f[max(i-2,0ll)]+p[i],f[max(i-3,0ll)]+max(p[i],p[i-1]));
		if(f[i]==f[max(i-3,0ll)]+max(p[i],p[i-1]))v[i]=1;
	}
	if(f[n]>f[n-1])n--;
	for(int i=n;i>0;i=i-2-v[i]){
		m(i-1);
		if(v[i])m(i-2);
		m(i);
	}
	cout<<r.size()<<'\n';
	for(auto x:r)cout<<x<<'\n';
}

#include <bits/stdc++.h>
using namespace std;

inline int gi()
{
	char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	int sum = 0;
	while('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
	return sum;
}

typedef long long ll;
const int maxn = 300005;

int n, m, cnt, a[maxn], p[maxn], out[maxn];
ll f[maxn];
bool v[maxn];

int descension(int pos)
{
	int dt = min(a[pos], a[pos + 1]);
	if(dt)
		out[++m] = pos;
	a[pos] -= dt;
	a[pos + 1] -= dt;
	return dt;
}

int main()
{
	n = gi();

	for(int i = 1; i <= n; ++i) {
		a[i] = gi();
		ll odd = f[max(i - 2, 0)] + a[i], even = f[max(i - 3, 0)] + max(a[i - 1], a[i]);
		f[i] = min(odd, even);
		v[i] = f[i] != odd;
	}

	ll ans = min(f[n - 1], f[n]);
	for(int i = n - (ans == f[n - 1]); i > 0; i -= 2 + v[i]) p[++cnt] = i;
	reverse(p + 1, p + cnt + 1);
	for(int i = 1; i <= cnt; ++i) {
		int pre = p[i - 1], cur = p[i], ctr = 0;
		if(v[cur])
			ctr += descension(cur - 1);
		ctr += descension(pre + 1);
		ctr += descension(cur);
		assert(ctr == f[cur] - f[pre]);
	}
	printf("%d\n", m);
	for(int i = 1; i <= m; ++i) printf("%d\n", out[i]);

	return 0;
}

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <queue>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <memory.h>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef pair<int, int> PII;
const double pi = 3.1415926535897932384626433832795;
const double eps = 1e-12;
const int INF = 1<<30;
const LL LINF = 1LL<<62;

#define pb push_back
#define mp make_pair
#define ALL(a) (a).begin(),(a).end()
#define CLR(a, v) memset((a),(v),sizeof(a))
#define sqr(a) ((a)*(a))
const int BUFFER_SZ = 1 << 20;
const int MOD = 1000*1000*1000+7;

char BUFFER[BUFFER_SZ];
int gi() { int x; scanf("%d", &x); return x; }
LL gll() { LL x; scanf("%lld", &x); return x; }
VI gvi(int n) { VI a; while(n-->0) a.pb(gi()); return a;}
string gs() { scanf("%s", BUFFER); return string(BUFFER); }
LL randl() { return abs(1ll*rand()*rand()*rand()*rand()*rand()*rand()); }
template <class T> void print(vector<T> &x, string format) {
  format += "%c";
  for (int i = 0; i < x.size(); ++i) printf(format.c_str(), x[i], i+1==x.size() ? '\n' : ' ');
}
// ---------------------------------------------------------- //

map<LL,pair<LL,LL>> ne,nene; // first: score; second.first: cur ; second.second prev

void Add(map<LL,pair<LL,LL>>& m, LL score, LL cur, LL prev) {

  //cout << "Update: " << score << " " << cur << " " << prev << endl;
  if (m.empty()) {
    m[score] = make_pair(cur,prev);
    return;
  }

  auto it = m.lower_bound(score);
  // check element with lower score. if its cur is smaller as well return
  if (it!=m.begin()) {
    it--;
    //cout << "lb--: " << it->first << endl;
    if (it->second.first <= cur) {
      //cout << "ignoring" << endl;
      return;
    }
    it++;
  }
  // if nothign has greater score then add
  if (it==m.end()) {
    m[score] = make_pair(cur,prev);
    return;
  }
  // remove all scores that are worse
  vector<LL> toremove;
  while(it!=m.end() && it->second.second >= cur) {
    toremove.push_back(it->first);
    it++;
  }

  for (auto t : toremove) {
    //cout << "removing: " << t << " " << m[t].second << endl;
    m.erase(t);
  }

  if (m.find(score)==m.end())
    m[score] = make_pair(cur,prev);
}

LL count(vector<int> gold) {
  ne.clear();
  nene.clear();
  gold.insert(gold.begin(),0);
  gold.push_back(0);
  gold.push_back(0);
  int n = gold.size();
  if (n<5)
    return 0;

  Add(ne,0,gold[1],gold[0]);

  for (int i=1;i<n;i++) {
    if (i+1==n)
      return ne.begin()->first;
    auto cur = ne;
    ne = nene;
    nene.clear();

    for (auto el : cur) {
      //cout << i << ": score=" << el.first << " cur=" << el.second.first << " prev=" << el.second.second << endl;
      LL x = el.second.second;
      LL y = el.second.first;
      LL z = gold[i+1];
      // case 1: make this 0
      if (y<=x+z && i+1 <n) {
        Add(ne,el.first+y,max(z-y,0ll),0); // score, cur, prev
      }
      // case 2: make next 0
      x = el.second.first;
      y = gold[i+1];
      z = gold[i+2];
      if (y<=x+z && i+2 < n) {
        Add(nene,el.first+y,max(z-y,0ll),0);
      }
    }
  }
  return -1;
}

LL dp[333333];
int go[333333];
int A[333333];
int solve(VI a) {
  int n = a.size();
  for (int i = 1; i <= n; ++i) A[i] = a[i-1];
  A[++n]=0;
  dp[0]=0;
  dp[1]=0;
  go[1]=-1;
  for (int i = 2; i <= n; ++i) {
    LL r1 = (i-2 < 0 ? 0 : dp[i-2]) + A[i-1];
    LL r2 = (i-3 < 0 ? 0 : dp[i-3]) + max(A[i-1],A[i-2]);

    dp[i] = min(r1,r2);
    go[i] = r1<r2 ? -2 : -3;
  }
  //for (int i = 0; i <= n; ++i) printf("%d ", dp[i]); printf("\n");
  LL res = min(dp[n],dp[n-1]);
  LL res2 = count(a);
  if (res!=res2) {
    int ttt = 0;
    cout << 7/ttt;
  }
  //printf("%d\n", res);
  VI R;
  int x = dp[n]==res ? n : n-1;

  while(x>1) {
    int y = x+go[x];
    if (y+1==x) break;
    if (y+2==x) {
      int d = min(A[x-1],A[x]);
      A[x-1] -=d;
      A[x]-=d;
      if (d) R.pb(x-1);
      d = min(A[x-1],A[x-2]);
      A[x-1]-=d;
      A[x-2]-=d;
      if (d) R.pb(x-2);
    } else if (y+3==x) {
      int d = min(A[y+1], A[y+2]);
      A[y+1]-=d;
      A[y+2]-=d;
      if (d) R.pb(y+1);
      d = min(A[y+2],A[y+3]);
      A[y+2]-=d;
      A[y+3]-=d;
      if (d) R.pb(y+2);
      d = min(A[y],A[y+1]);
      if (d) R.pb(y);
    }
    x=y;
  }
  printf("%d\n", R.size());
  for (int x : R) printf("%d\n", x);
}

int main() {
#ifdef DEAD_RABBIT
  freopen("in.txt","r",stdin);
  clock_t start = clock();
#endif
  int n = gi();
  VI a = gvi(n);
  solve(a);
#ifdef DEAD_RABBIT
  fprintf(stdout, "Time: %.3lf\n", 1.0*(clock()-start)/CLOCKS_PER_SEC);
#endif
  return 0;
}

#include <bits/stdc++.h>

#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif

#ifdef __WIN32
#define LLFORMAT "I64"
#define Rand() ((rand() << 15) + rand())
#else
#define LLFORMAT "ll"
#define Rand() (rand())
#endif

using namespace std;

const int maxn = 3e5 + 10;

const long long oo = 1ll << 62;

int n, g[maxn][2], a[maxn];
long long f[maxn][2];
bool del[maxn];
vector<int> ans;

inline bool upd(long long &x, long long y) { return x < y ? 0 : (x = y, 1); }

int main() {
	scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", a + i), f[i][0] = f[i][1] = oo;
	f[0][0] = 0; f[0][1] = oo;
	for (int i = 1; i <= n; ++i) {
		if(upd(f[i][0], a[i] + min(f[i - 1][0], f[i - 1][1]))) g[i][0] = 1;
		if(i > 1 && upd(f[i][0], max(a[i], a[i - 1]) + min(f[i - 2][0], f[i - 2][1]))) g[i][0] = 2;
		if(upd(f[i][1], f[i - 1][0])) g[i][1] = 0;
	}
	for (int i = n, j = f[n][1] < f[n][0]; i; ) {
		if(!g[i][j]) --i, j = 0;
		else if(g[i][j] == 1) del[i] = 1, --i, j = f[i][1] <= f[i][0];
		else del[i] = del[i - 1] = 1, i -= 2, j = f[i][1] <= f[i][0];
	}
	for (int i = 1; i <= n; ++i) {
		if(del[i]) {
			int j = i;
			while(j + 1 <= n && del[j + 1]) ++j;
			while(i <= j) {
				if(!a[i]) ++i;
				else {
					if(!a[i + 1]) { if(a[i - 1]) ans.push_back(i - 1); ++i; }
					else { int x = min(a[i], a[i + 1]); ans.push_back(i); a[i] -= x; a[i + 1] -= x; }
				}
			}
			--i;
		}
	}
	printf("%d\n", ans.size()); for (int i: ans) printf("%d\n", i);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define db long double

int n;
int a[300007];
ll dp[300007];
int to[300007];

vector<int> ans;

void calc(int l, int r){
	int now = a[l + 1];
	for (int i = l + 1; i <= r - 1; i++){
		int nxt = a[i + 1];
		if (now > 0 && nxt > 0){
			ans.pb(i);
			int mi = min(now, nxt);
			now -= mi;
			nxt -= mi;
		}
		if (now > 0) ans.pb(i - 1);
		now = nxt;
	}
}

int main(){
   //	freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];

    for (int i = n - 1; i >= 0; i--){
    	ll val = 0;
    	dp[i] = (ll)1e18 + 7;
    	int now = a[i + 1];
    	for (int j = i + 2; j <= n + 1; j++){
    		if (j - i > 3) break;
    		if (dp[j] + val + now < dp[i]){
    			dp[i] = dp[j] + val + now;
    			to[i] = j;
    		}
    		val += now;
    		now = a[j] - min(a[j], now);
    	}
    }

    int pos = 0;
    if (dp[1] < dp[0]) pos = 1;

    while(1){
    	if (pos >= n) break;
    	calc(pos, to[pos]);
    	pos = to[pos];
    }

    cout << ans.size() << "\n";
    for (int x : ans) cout << x << ' ';
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define db long double

int n;
int a[300007];
ll dp[300007];
int to[300007];

vector<int> ans;

void calc(int l, int r){
	int now = a[l + 1];
	for (int i = l + 1; i <= r - 1; i++){
		int nxt = a[i + 1];
		if (now > 0 && nxt > 0){
			ans.pb(i);
			int mi = min(now, nxt);
			now -= mi;
			nxt -= mi;
		}
		if (now > 0) ans.pb(i - 1);
		now = nxt;
	}
}

int main(){
   //	freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];

    for (int i = n - 1; i >= 0; i--){
    	ll val = 0;
    	dp[i] = (ll)1e18 + 7;
    	int now = a[i + 1];
    	for (int j = i + 2; j <= n + 1; j++){
    		if (j - i > 4) break;
    		if (dp[j] + val + now < dp[i]){
    			dp[i] = dp[j] + val + now;
    			to[i] = j;
    		}
    		val += now;
    		now = a[j] - min(a[j], now);
    	}
    }

    int pos = 0;
    if (dp[1] < dp[0]) pos = 1;

    while(1){
    	if (pos >= n) break;
    	calc(pos, to[pos]);
    	pos = to[pos];
    }

    cout << ans.size() << "\n";
    for (int x : ans) cout << x << ' ';
}

#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int MAXN = 300005;

template <typename T> inline void read(T &AKNOI) {
	T x = 0, flag = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') flag = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	AKNOI = flag * x;
}

int n, a[MAXN], ds[MAXN];
LL dp[MAXN];
vector<int> ans;

void wow(int i) {
	int d = min(a[i], a[i + 1]);
	if (d) ans.push_back(i);
	a[i] -= d;
	a[i + 1] -= d;
}

void solve() {
	read(n);
	for (int i = 1; i <= n; ++i) {
		read(a[i]);
	}
	for (int i = 1; i <= n; ++i) {
		LL tmp = dp[max(i - 3, 0)] + max(a[i], a[i - 1]);
		dp[i] = min(dp[max(i - 2, 0)] + a[i], tmp);
		ds[i] = (dp[i] == tmp);
	}
	int ed = n - (min(dp[n], dp[n - 1]) == dp[n - 1]);
	for (int i = ed; i > 0; i -= (2 + ds[i])) {
		wow(i - 1);
		if (ds[i]) wow(i - 2);
		wow(i);
	}
	printf("%d\n", ans.size());
	for (auto i : ans) {
		printf("%d\n", i);
	}
}

int main() {
	solve();
	return 0;
}

# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) ((x)&1)
#define sqr(x) ((x)*(x))
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,a,b) for (int i=(a); i<=(b); ++i)
#define per(i,a,b) for (int i=(a); i>=(b); --i)
#define rep_it(it,x) for (__typeof((x).begin()) it=(x).begin(); it!=(x).end(); it++)
#define ____ puts("\n_______________\n\n")
#define debug(x) ____; cout<< #x << " => " << (x) << endl
#define debug_pair(x) cout<<"\n{ "<<(x).fir<<" , "<<(x).sec<<" }\n"
#define debug_arr(x,n) ____; cout<<#x<<":\n"; rep(i,0,n) cout<<#x<<"["<<(i)<<"] => "<<x[i]<<endl
#define debug_arr2(x,n,m) ____; cout<<#x<<":\n"; rep(i,0,n) rep(j,0,m) cout<<#x<<"["<<(i)<<"]["<<(j)<<"]= "<<x[i][j]<<((j==m)?"\n\n":"    ")
#define debug_set(x) ____; cout<<#x<<": \n"; rep_it(it,x) cout<<(*it)<<" "; cout<<endl
#define debug_map(x) ____; cout<<#x<<": \n"; rep_it(it,x) debug_pair(*it)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const ll ooo=9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N=301005;
const double PI=acos(-1.0);
const double eps=1e-9;
int o(int x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

int a[N],op[N],tmp[N],ans[N],n; ll dp[N];

void U(int k,int x,ll t=0) { if (dp[x]+t<dp[k]) dp[k]=dp[x]+t,op[k]=x; }

int main() {
    //file_put();

    scanf("%d",&n);
	rep(i,1,n) scanf("%d",&a[i]);
	rep(i,0,n) dp[i]=ooo;
	if (n==1) return 0*printf("%d\n",0); else U(n,n+2,a[n]);
	per(i,n-1,0) {
		if (a[i]==0) { U(i,i+1),U(i,i+2); continue; }
		U(i,i+2,a[i]);
		if (a[i]>=a[i+1]) { U(i,i+3,a[i]); continue; }
		int tt=a[i+1]-a[i];
		if (i+2<=n) {
			U(i,i+3,a[i+1]);
			if (tt>=a[i+2]) U(i,i+4,a[i+1]);
		}
	}
	for (int t=op[0]; t; t=op[t]) ans[++ans[0]]=t;
	per(i,ans[0],1) {
		if (a[ans[i]]==0) continue;
		if (i==ans[0] || ans[i+1]-ans[i]<=2 || a[ans[i]]>=a[ans[i]+1])
			if (a[ans[i]]<=a[ans[i]+1]) {
				a[ans[i]+1]-=a[ans[i]];
				a[ans[i]]=0;
				tmp[++tmp[0]]=ans[i];
			} else {
				if (a[ans[i]+1]) tmp[++tmp[0]]=ans[i];
				tmp[++tmp[0]]=ans[i]-1;
				a[ans[i]]-=a[ans[i]+1];
				a[ans[i]-1]-=a[ans[i]];
				a[ans[i]+1]=a[ans[i]]=0;
			}
		else {
			tmp[++tmp[0]]=ans[i];
			tmp[++tmp[0]]=ans[i]+1;
			a[ans[i]+1]-=a[ans[i]];
			a[ans[i]]=0;
			a[ans[i]+2]-=a[ans[i]+1];
			a[ans[i]+1]=0;
		}
	}
	/*debug_arr(op,n+1);
	debug_arr(dp,n+1);
	debug(dp[0]);
	printf("%d\n",ans[0]);
	rep(i,1,ans[0]) printf("%d ",ans[i]);
	printf("\n");*/
	printf("%d\n",tmp[0]);
	rep(i,1,tmp[0]) printf("%d\n",tmp[i]);

    return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define N 300005
using namespace std;
int a[N],p[N],out[N];
ll f[N];
bool v[N];
int n,m,cnt;
void work(int p){
	int t=min(a[p],a[p+1]);
	if (t) out[++m]=p;
	a[p]-=t; a[p+1]-=t;
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		ll O=f[max(i-2,0)]+a[i];
		ll E=f[max(i-3,0)]+max(a[i],a[i-1]);
		f[i]=min(O,E); v[i]=(f[i]!=O);
	}
	ll ans=min(f[n-1],f[n]);
	for (int i=n-(ans==f[n-1]);i>0;i-=2+v[i])
		p[++cnt]=i;
	reverse(p+1,p+cnt+1);
	for (int i=1;i<=cnt;i++){
		int pre=p[i-1],now=p[i];
		if (v) work(now-1);
		work(pre+1);
		work(now);
	}
	printf("%d\n",m);
	for (int i=1;i<=m;i++)
		printf("%d\n",out[i]);
}

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const long long infll = (long long) 1.01e18;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

#ifdef DEBUG
mt19937 mrand(300);
#else
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
#endif

int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int maxn = (int) 3e5 + 5;
int n;
int a[maxn];

bool read() {
  if (scanf("%d", &n) < 1) {
    return false;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  return true;
}

const int L = 10;
long long s[maxn], ss[maxn];
long long dp[maxn][2];
pair<int, int> p[maxn][2];

long long getCost(int l, int r, int tl, int tr) {
  assert(l < r);
  if (r - l == 1) {
    if (!tl || !tr) {
      if (a[l]) {
        return infll;
      }
    }
    return 0;
  }
  if (!tl && a[l] > a[l + 1]) {
    return infll;
  }
  if (!tr && a[r - 1] > a[r - 2]) {
    return infll;
  }
  if (r - l == 2) {
    return min(a[l], a[r - 1]);
  }
  long long res = 0;
  long long lx = 0, rx = 0;
  if (!tl) {
    res += a[l];
    lx = a[l];
    l++;
  }
  if (!tr) {
    res += a[r - 1];
    rx = a[r - 1];
    r--;
  }
  if (r - l == 1) {
    if (lx + rx > a[l]) {
      return infll;
    }
    return res;
  }
  long long sum = s[r - 1] - s[l + 1];
  sum += (a[l] - lx) * ((l & 1) ? -1 : 1);
  sum += (a[r - 1] - rx) * (((r - 1) & 1) ? -1 : 1);
  long long ssum = ss[r - 1] - ss[l + 1];
  ssum += a[l] - lx;
  ssum += a[r - 1] - rx;
  res += (ssum - abs(sum)) / 2;
  return res;
}

vector<int> ans;

void doOp(int i) {
  int x = min(a[i], a[i + 1]);
  if (!x) {
    return;
  }
  a[i] -= x;
  a[i + 1] -= x;
  ans.push_back(i);
}

void getAns(int l, int r, int tl, int tr) {
  assert(l < r);
  if (r - l == 1) {
    if (!tl || !tr) {
      assert(!a[l]);
    }
    return;
  }
  assert(!(!tl && a[l] > a[l + 1]));
  assert(!(!tr && a[r - 1] > a[r - 2]));
  if (r - l == 2) {
    doOp(l);
    return;
  }
  if (!tl) {
    doOp(l);
    l++;
  }
  if (!tr) {
    doOp(r - 2);
    r--;
  }
  for (int i = l; i < r - 1; i++) {
    doOp(i);
  }
}

void solve() {
  s[0] = 0;
  for (int i = 0; i < n; i++) {
    s[i + 1] = s[i] + a[i] * ((i & 1) ? -1 : 1);
  }
  ss[0] = 0;
  for (int i = 0; i < n; i++) {
    ss[i + 1] = ss[i] + a[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < 2; j++) {
      dp[i][j] = infll;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int t = 0; t < 2; t++) {
      auto cur = dp[i][t];
      if (cur >= infll) {
        continue;
      }
      for (int j = i + 1; j <= n && j - i <= L; j++) {
        int l = (t ^ 1);
        for (int r = 0; r < 2; r++) {
          auto &nxt = dp[j][r];
          auto val = getCost(i, j, l, r);
          if (cur + val < nxt) {
            nxt = cur + val;
            p[j][r] = make_pair(i, t);
          }
        }
      }
    }
  }
  ans.clear();
  for (int i = n, t = 1; i > 0;) {
    int pi = p[i][t].first, pt = p[i][t].second;
    getAns(pi, i, (pt ^ 1), t);
    i = pi;
    t = pt;
  }
  printf("%d\n", sz(ans));
  for (int i = 0; i < sz(ans); i++) {
    printf("%d\n", ans[i] + 1);
  }
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (read()) {
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
template <typename T> void read(T &x)
{
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x)
{
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x)
{
	write(x);
	puts("");
}
long long a[MAXN], f[MAXN];
int n, m, p, ans[MAXN];
bool odd[MAXN];
void operate(int pos)
{
	if (pos >= n || pos <= 0) return;
	int tmp = min(a[pos], a[pos + 1]);
	if (tmp == 0) return;
	ans[++m] = pos;
	a[pos] -= tmp; a[pos + 1] -= tmp;
}
int main()
{
	read(n);
	for (int i = 1; i <= n; i++)read(a[i]);
	for (int i = 1; i <= n; i++)
	{
		long long fo = f[max(i - 2, 0)] + a[i];
		long long fe = f[max(i - 3, 0)] + max(a[i], a[i - 1]);
		f[i] = min(fo, fe);
		odd[i] = f[i] == fo;
	}
	long long vans = min(f[n - 1], f[n]);
	if (vans == f[n - 1]) p = n - 1;
	else p = n;
	while (p != 0)
	{
		if (odd[p])
		{
			operate(p);
			operate(p - 1);
			p -= 2;
		}
		else
		{
			operate(p - 1);
			operate(p - 2);
			operate(p);
			p -= 3;
		}
		p = max(p, 0);
	}
	writeln(m);
	for (int i = 1; i <= m; i++)writeln(ans[i]);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,a) for(int i=start[(a)];i;i=e[i].next)
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
inline int read()
{
    int sum=0,p=1;char ch=getchar();
    while(!(('0'<=ch && ch<='9') || ch=='-'))ch=getchar();
    if(ch=='-')p=-1,ch=getchar();
    while('0'<=ch && ch<='9')sum=sum*10+ch-48,ch=getchar();
    return sum*p;
}

const int maxn=3e5+20;

int n,a[maxn];

inline void init()
{
    n=read();REP(i,1,n)a[i]=read();
}

ll dp[maxn];int pre[maxn];
int val[maxn],na[maxn];
vector<int>ans,nans;

inline void work(int x){ int t=min(na[x],na[x+1]);if(t)nans.push_back(x),na[x]-=t,na[x+1]-=t;}

inline void doing()
{
    dp[0]=0;dp[1]=a[1];pre[1]=1; dp[2]=a[2]; pre[2]=1;
    REP(i,3,n)
    {
	ll A=dp[i-3]+max(a[i],a[i-1]),B=dp[i-2]+a[i];
	if(A<B)dp[i]=A,pre[i]=2;
	else dp[i]=B,pre[i]=1;
    }
    int now=dp[n-1]<dp[n]?n-1:n;

    REP(i,1,n)na[i]=a[i];
    while(now>0)
    {
	int t=pre[now];
	if(t==2)
	{
	    if(na[now-1]>na[now])work(now-1),work(now-2);
	    else work(now-1),work(now);
	}else
	{
	    work(now); work(now-1);
	}
	now-=t+1;
    }
    printf("%d\n",SZ(nans));
    for(int x:nans)printf("%d\n",x);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("E.in","r",stdin);
    freopen("E.out","w",stdout);
#endif
    init();
    doing();
    return 0;
}

#include<bits/stdc++.h>

#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)

using namespace std;

typedef long long LL;
typedef double db;

int get(){
	char ch;
	while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-');
	if (ch=='-'){
		int s=0;
		while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0';
		return -s;
	}
	int s=ch-'0';
	while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0';
	return s;
}

const int N = 3e5+5;

int n;
int a[N];
LL f[N];
int lst[N];

int ans[N*5],m;
int all;

void op(int x){
	if (x<1||x>=n)return;
	if (!a[x]||!a[x+1])return;
	int v=min(a[x],a[x+1]);
	a[x]-=v,a[x+1]-=v;
	ans[++m]=x;
	all+=v;
}

int main(){
	n=get();
	fo(i,1,n)a[i]=get();
	fo(i,1,n)f[i]=1e18;
	f[1]=a[1];lst[1]=1;
	f[2]=a[1]+max(a[2]-a[1],0);lst[2]=2;
	fo(i,2,n){
		if (i>1&&f[i]>f[i-2]+a[i]){
			f[i]=f[i-2]+a[i];
			lst[i]=1;
		}
		if (i>2&&f[i]>f[i-3]+a[i-1]+max(a[i]-a[i-1],0)){
			f[i]=f[i-3]+a[i-1]+max(a[i]-a[i-1],0);
			lst[i]=2;
		}
	}
	int x=f[n]<f[n-1]?n:n-1;
	for(;x>0;x-=lst[x]+1){
		if (lst[x]==1){
			op(x);
			op(x-1);
		}
		else{
			op(x-1);
			op(x);
			op(x-2);
		}
	}
	printf("%d\n",m);
	fo(i,1,m)printf("%d\n",ans[i]);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define N 500007
#define ll long long
ll a[N],f[N];
int pre[N];
vector<int> ans;
void reunion(int x)
{
    int v=min(a[x],a[x+1]);
    if(v)
    {
        ans.push_back(x);
        a[x]-=v,a[x+1]-=v;
    }
}
int main()
{
    int n,i;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%I64d",&a[i]);
    n++;
    a[n]=0;
    f[1]=0,f[2]=a[1];pre[2]=0;
    pre[1]=-1,pre[0]=-1;
    for(i=3;i<=n;i++)
    {
        ll f1=f[i-2]+a[i-1];
        ll f2=f[i-3]+a[i-2]+a[i-1]-min(a[i-2],a[i-1]);
        if(f1<f2)f[i]=f1,pre[i]=i-2;
        else f[i]=f2,pre[i]=i-3;
    }
    int cur;
    if(f[n-1]<f[n])cur=n-1;
    else cur=n;
    while(pre[cur]!=-1)
    {
        int p=pre[cur];
        if(p==cur-2)
        {
            reunion(cur-1);
            reunion(p);
        }
        else
        {
            reunion(p+1);
            reunion(cur-1);
            reunion(p);
        }
        cur=p;
    }
    int s=ans.size();
    printf("%d\n",s);
    for(i=0;i<s;i++)
        printf("%d\n",ans[i]);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
long long a[MAXN], f[MAXN];
int n, m, p, ans[MAXN];
bool odd[MAXN];
void operate(int pos) {
	if (pos >= n || pos <= 0) return;
	int tmp = min(a[pos], a[pos + 1]);
	if (tmp == 0) return;
	ans[++m] = pos;
	a[pos] -= tmp; a[pos + 1] -= tmp;
}
int main() {
	read(n);
	for (int i = 1; i <= n; i++)
		read(a[i]);
	for (int i = 1; i <= n; i++) {
		long long fo = f[max(i - 2, 0)] + a[i];
		long long fe = f[max(i - 3, 0)] + max(a[i], a[i - 1]);
		f[i] = min(fo, fe);
		odd[i] = f[i] == fo;
	}
	long long vans = min(f[n - 1], f[n]);
	if (vans == f[n - 1]) p = n - 1;
	else p = n;
	while (p != 0) {
		if (odd[p]) {
			operate(p);
			operate(p - 1);
			p -= 2;
		} else {
			operate(p - 1);
			operate(p - 2);
			operate(p);
			p -= 3;
		}
		p = max(p, 0);
	}
	writeln(m);
	for (int i = 1; i <= m; i++)
		writeln(ans[i]);
	return 0;
}

//In the Name of God
//Ya Ali

#include<bits/stdc++.h>

#define int long long

#define err(A) cout << #A << " = " << (A) << endl

using namespace std;

const int inf = 1e17+30;
const int maxn = 300300;

int n;

int a[maxn];

int dp[maxn];
int p[maxn];

bool mark[maxn];

vector<int> ans;

void chk(int i, int x, int pr)
{
  if (x < dp[i])
    dp[i] = x, p[i] = pr;
}

void follow(int i)
{
  if (i < 1)
    return;
  mark[i] = true;
  for (int j = p[i] + 2; j < i; j ++)
    mark[j] = true;
  follow(p[i]);
}

void add(int i)
{
  int c = min(a[i], a[i + 1]);
  if (c)
    {
      a[i] -= c;
      a[i + 1] -= c;
      ans.push_back(i);
    }
}

int32_t main()
{
  ios::sync_with_stdio(0); cin.tie(0);

  cin >> n;

  for (int i = 1; i <= n; i ++)
    cin >> a[i];

  for (int i = 1; i <= n; i ++)
    {
      dp[i] = inf;
      chk(i, dp[i - 1] + a[i], i - 1);
      chk(i, (i > 1 ? dp[i - 2] : 0) + a[i], i - 2);
      chk(i, (i > 2 ? dp[i - 3] : 0) + max(a[i], a[i - 1]), i - 3);
    }

  if (dp[n] < dp[n - 1])
    follow(n);
  else
    follow(n - 1);

  for (int i = 1; i <= n; i ++)
    if(mark[i] and a[i])
      add(i);

  for (int i = 1; i <= n; i ++)
    if(a[i] and a[i + 1])
      add(i);

  cout << ans.size() << "\n";
  for(int i:ans)
    cout << i << "\n";

  return 0;
}

#include <cstdio>
#include <vector>
#include <cassert>
#include <cstring>
#include <algorithm>
#define meow(args...) fprintf(stderr, args)
const int N=3e5+5;
int n, p[N], cho[N];
long long f[N];
std::vector<int> ans;
template<class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {return b<a?(a=b, true):false;}
template<class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {return a<b?(a=b, true):false;}
template<class Type> Type read() {
	Type a;
	bool b;
	unsigned char c;
	while(c=getchar(), c-'0'>9&c!='-');
	for(a=(b=c=='-')?0:c-'0'; (c=getchar()-'0')<=9; a=a*10+c);
	return b?-a:a;
}
void dec(int x) {
	int d=std::min(p[x], p[x+1]);
	if(d==0) return;
	ans.push_back(x);
	p[x]-=d;
	p[x+1]-=d;
}
int main() {
	int (*rd)()=read<int>;
	n=rd();
	for(int i=1; i<=n; ++i) {
		p[i]=rd();
		long long f2=f[std::max(i-2, 0)]+p[i], f3=f[std::max(i-3, 0)]+std::max(p[i-1], p[i]);
		f[i]=std::min(f2, f3);
		cho[i]=f2<f3?2:3;
	}
	std::vector<int> pos;
	for(int i=n-(f[n]>f[n-1]); i>0; i-=cho[i]) pos.push_back(i);
	std::reverse(pos.begin(), pos.end());
	int last=0;
	for(int cur: pos) {
		if(cho[cur]) dec(cur-1);
		dec(last+1);
		dec(cur);
		last=cur;
	}
	printf("%d\n", (int)ans.size());
	for(int e: ans) printf("%d\n", e);
	return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 300030
#define ll long long
using namespace std;
int n,a[maxn],ans[maxn],m;
ll dp[maxn];bool d[maxn];
void init(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i)scanf("%d",&a[i]);
}
void opt(int pos){
	if(pos<=0||pos==n)return;
	int tmp=min(a[pos],a[pos+1]);
	if(!tmp)return;
	ans[++m]=pos;
	a[pos]-=tmp,a[pos+1]-=tmp;
}
void solve(){
	for(int i=1;i<=n;++i){
		ll one=dp[max(i-2,0)]+a[i],two=dp[max(i-3,0)]+max(a[i-1],a[i]);
		dp[i]=min(one,two);
		d[i]=dp[i]==one;
	}
	for(int i=(dp[n]<dp[n-1])?n:n-1;i>0;){
		if(d[i]){
			opt(i);
			opt(i-1);
			i-=2;
		}
		else{
			opt(i-1);
			opt(i);
			opt(i-2);
			i-=3;
		}
	}
	printf("%d\n",m);
	for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
}
int main(){
	init();
	solve();
	return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
const int N=3e5+6;
int f[N],n,a[N],g[N];
vector<int>vec;
void d(int x){int p=min(a[x],a[x+1]);if(p)a[x]-=p,a[x+1]-=p,vec.push_back(x);}
main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>a[i];
	f[1]=a[1],f[2]=a[2],g[1]=g[2]=2;
	for(int i=3;i<=n;++i){
		f[i]=f[i-2]+a[i];
		if(f[i]>f[i-3]+max(a[i],a[i-1]))
		f[i]=f[i-3]+max(a[i],a[i-1]),g[i]=3;else g[i]=2;
	}
	for(int i=f[n]<f[n-1]?n:n-1;i>0;i-=g[i]){
		d(i-1);if(g[i]==3)d(i-2);d(i);
	}
	cout<<vec.size()<<'\n';
	for(int i:vec)cout<<i<<'\n';
	return 0;
}

#include <bits/stdc++.h>

typedef long long int int64;
int64 dp[300009];int prev[300009];
int64 a[300009];
int f[300009];int fn;
static const int64 INF = 1000000000000000000LL;
void desc(int i) {
	int64 val = std::min(a[i],a[i+1]);
	if(val == 0) return;
	a[i] -= val,a[i+1] -= val;
	f[fn++] = i;
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i = 0;i < n;++i) {
		scanf("%I64d",&a[i]);
	}
	dp[0] = a[0],prev[0] = -1;
	dp[1] = a[1],prev[1] = -1;
	for(int i = 2;i < n;++i) {
		int64 ans = INF;int pre = -2;
		if(dp[i-2] + a[i] < ans) {
			ans = dp[i-2] + a[i];
			pre = i-2;
		}
		if((i == 2 ? 0 : dp[i-3]) + std::max(a[i-1],a[i]) < ans) {
			ans = (i == 2 ? 0 : dp[i-3]) + std::max(a[i-1],a[i]);
			pre = i-3;
		}
		dp[i] = ans,prev[i] = pre;
	}
	int cur;
	if(dp[n-1] < dp[n-2])
		cur = n-1;
	else
		cur = n-2;
	while(cur != -1) {
		desc(cur-1),desc(cur);
		if(prev[cur] == cur-3) desc(cur-2);
		cur = prev[cur];
	}
	printf("%d\n",fn);
	for(int i = 0;i < fn;++i) printf("%d\n",f[i]+1);
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 3e5 + 5;
const LL INF = 1e18;
LL dp[N][2]; tuple<int, int, int> his[N][2];
vector<int> ans;
LL work(LL &a, LL &b, bool &gg) {
	LL c = min(a, b);
	a -= c;
	b -= c;
	if (c == 0) gg = 1;
	return c;
}
LL work(vector<int> a, int off) {
	int n = (int) a.size();
	for (int i = 1; i <= n; ++ i) {
		dp[i][0] = dp[i][1] = INF;
		for (int l = 0; l <= 2; ++ l) {
			for (int r = 0; r <= 2; ++ r) {
				if (l + r + 1 <= i) {
					int j = i - (l + r + 1);
					static LL tmp[5];
					for (int k = 0; k < (l + r + 1); ++ k) tmp[k] = a[j + k];
					LL base = 0;
					bool gg = 0;
					if (l >= 1) base += work(tmp[0], tmp[1], gg);
					if (l >= 2) base += work(tmp[1], tmp[2], gg);
					if (r >= 1) base += work(tmp[l + r - 1], tmp[l + r], gg);
					if (r >= 2) base += work(tmp[l + r - 2], tmp[l + r - 1], gg);
					for (int k = 0; k < (l + r + 1); ++ k) if (k != l) gg |= tmp[k];
					if (gg) continue;
					LL remain = tmp[l];
					int sb = l ? 1 : !remain;
					base += dp[j][sb];
					int tb = r ? 0 : !!remain;
					//printf("%d %d upd %d %d, %lld  sb=%d\n", l, r, i, tb, base, sb);
					if (dp[i][tb] > base) {
						dp[i][tb] = base;
						his[i][tb] = make_tuple(l, r, sb);
					}
				}
			}
		}
		if (dp[i][0] < dp[i][1]) {
			dp[i][1] = dp[i][0];
			his[i][1] = his[i][0];
		}
	}
	for (int i = n, j, k = 1; i; i = j) {
		int l, r;
		tie(l, r, k) = his[i][k];
		j = i - (l + r + 1);
		if (l >= 1) ans.push_back(j + off);
		if (l >= 2) ans.push_back(j + off + 1);
		if (r >= 1) ans.push_back(i + off - 2);
		if (r >= 2) ans.push_back(i + off - 3);
	}
	//printf("dp=%lld\n", dp[n][1]);
	return dp[n][1];
}
int main() {
	int n, las = 1;
	scanf("%d", &n);
	vector<int> a;
	for (int i = 1; i <= n; ++ i) {
		int x;
		scanf("%d", &x);
		if (!x) {
			if (!a.empty()) work(a, las);
			a.clear();
			las = i + 1;
		}
		else a.push_back(x);
	}
	if (!a.empty()) work(a, las);
	printf("%d\n", (int) ans.size());
	for (int x : ans) printf("%d\n", x);
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
inline int read(){
	int x=0,f=1;char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
	return x*f;
}
const int N=300010;
int a[N],p[N],t[N],lt,flag[N],ans[N];
ll f[N];
inline void calc(int x){
	int d=min(a[x],a[x+1]);
	if (d){
		ans[++ans[0]]=x;
		a[x]-=d,a[x+1]-=d;
	}
}
int main(){
	int n=read();
	for (int i=1;i<=n;++i){
		a[i]=read();
		ll p1=f[max(i-2,0)]+a[i],p2=f[max(i-3,0)]+max(a[i-1],a[i]);
		f[i]=min(p1,p2);
		if (f[i]==p1) p[i]=max(i-2,0);
		else p[i]=max(i-3,0),flag[i]=1;
	}
	for (int i=f[n-1]<=f[n]?n-1:n;i;i=p[i]) t[++lt]=i;
	for (int i=lt;i;--i){
		int now=t[i],lst=t[i+1];
		if (flag[now]) calc(now-1);
		calc(lst+1),calc(now);
	}
	printf("%d\n",ans[0]);
	for (int i=1;i<=ans[0];++i) printf("%d\n",ans[i]);
	return 0;
}

#include <bits/stdc++.h>

const int MAXN = 300010;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
int A[MAXN], n;
LL dp[MAXN]; int pre[MAXN];
void gmin(int at, int v, int fr) {
	fr = std::max(fr, 0);
	if (dp[fr] + v < dp[at]) {
		dp[at] = dp[fr] + v;
		pre[at] = fr;
	}
}
int li[MAXN], bak;
void add(int at) {
	if (at < 0 || at == n || A[at] == 0 || A[at + 1] == 0) return ;
	li[++bak] = at; int v = std::min(A[at], A[at + 1]);
	A[at] -= v, A[at + 1] -= v;
}
int main() {
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	std::cin >> n;
	for (int i = 1; i <= n; ++i) std::cin >> A[i];
	for (int i = 1; i <= n + 1; ++i) {
		dp[i] = INF;
		gmin(i, A[i - 1], i - 2);
		if (i > 1) gmin(i, std::max(A[i - 1], A[i - 2]), i - 3);
	}
	int now = std::min_element(dp + n, dp + n + 2) - dp;
	while (now) {
		if (pre[now] == now - 3)
			add(now - 2), add(now - 1), add(now - 3);
		else
			add(now - 1), add(now - 2);
		now = pre[now];
	}
	std::cout << bak << '\n';
	for (int i = 1; i <= bak; ++i)
		std::cout << li[i] << '\n';
	return 0;
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
inline int read(){
	int x=0,f=1;char ch;
	do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
	do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
	return x*f;
}
const int N=300010;
int a[N],p[N],t[N],lt,flag[N],ans[N];
ll f[N];
inline void calc(int x){
	int d=min(a[x],a[x+1]);
	if (d){
		ans[++ans[0]]=x;
		a[x]-=d,a[x+1]-=d;
	}
}
int main(){
	int n=read();
	for (int i=1;i<=n;++i){
		a[i]=read();
		ll p1=f[max(i-2,0)]+a[i],p2=f[max(i-3,0)]+max(a[i-1],a[i]);
		f[i]=min(p1,p2);
		if (f[i]==p2) p[i]=max(i-3,0),flag[i]=1;
		else p[i]=max(i-2,0);
	}
	for (int i=f[n-1]<=f[n]?n-1:n;i;i=p[i]) t[++lt]=i;
	for (int i=lt;i;--i){
		int now=t[i],lst=t[i+1];
		if (flag[now]) calc(now-1);
		calc(lst+1),calc(now);
	}
	printf("%d\n",ans[0]);
	for (int i=1;i<=ans[0];++i) printf("%d\n",ans[i]);
	return 0;
}

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <list>
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXN=300000;

int n;
int a[MAXN];
ll dp[MAXN]; // dp[i] is mincost to have no two consecutive positive integers in [0..i] in which the last element is allowed be positive
int act[MAXN];

bool canpos[MAXN];
int b[MAXN];
int ret[MAXN-1],nret;

void solve() {
	nret=0;
	if(n==1) return;
	REP(i,n) dp[i]=LLONG_MAX,act[i]=-1; dp[0]=0,act[0]=0; dp[1]=a[0],act[1]=1;
	REP(i,n) {
		//printf("%d: %lld\n",i,dp[i]);
		if(i+2<n) { ll val=dp[i]+a[i+1]; if(val<dp[i+2]) dp[i+2]=val,act[i+2]=1; }
		if(i+3<n) { ll val=dp[i]+max(a[i+1],a[i+2]); if(val<dp[i+3]) dp[i+3]=val,act[i+3]=2; }
	}
	int lst=n-1; ll cost=dp[n-1]; if(dp[n-2]+a[n-1]<cost) lst=n-2,cost=dp[n-2]+a[n-1];
	//printf("cost=%lld\n",cost);
	REP(i,n) canpos[i]=false; for(int i=lst;i>=0;i-=act[i]+1) canpos[i]=true;
	//printf("canpos:"); REP(i,n) printf("%c",canpos[i]?'*':'0'); puts("");
	REP(i,n) b[i]=a[i];
	REP(i,n) if(!canpos[i]&&b[i]>0) {
		if(i+1<n&&!canpos[i+1]&&b[i+1]>0) {
			ret[nret++]=i;
			int cur=min(b[i],b[i+1]); b[i]-=cur; b[i+1]-=cur;
			if(b[i]>0&&i-1>=0&&b[i-1]>0) { ret[nret++]=i-1; int oth=min(b[i-1],b[i]); b[i-1]-=oth; b[i]-=oth; }
			if(b[i+1]>0&&i+2<n&&b[i+2]>0) { ret[nret++]=i+1; int oth=min(b[i+1],b[i+2]); b[i+1]-=oth; b[i+2]-=oth; }
		} else {
			if(b[i]>0&&i-1>=0&&b[i-1]>0) { ret[nret++]=i-1; int oth=min(b[i-1],b[i]); b[i-1]-=oth; b[i]-=oth; }
			if(b[i]>0&&i+1<n&&b[i+1]>0) { ret[nret++]=i; int oth=min(b[i],b[i+1]); b[i]-=oth; b[i+1]-=oth; }
		}
	}
}

void run() {
	scanf("%d",&n); REP(i,n) scanf("%d",&a[i]);
	solve();
	printf("%d\n",nret); REP(i,nret) printf("%d\n",ret[i]+1);
}

int main() {
	run();
	return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
const int N=3e5+6;
int f[N],n,a[N],g[N];
vector<int>vec;
void d(int x){int p=min(a[x],a[x+1]);if(p)a[x]-=p,a[x+1]-=p,vec.push_back(x);}
main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>a[i];
	f[1]=a[1],f[2]=a[2],g[1]=g[2]=2;
	for(int i=3;i<=n;++i){
		f[i]=f[i-2]+a[i];
		if(f[i]>f[i-3]+max(a[i],a[i-1]))
		f[i]=f[i-3]+max(a[i],a[i-1]),g[i]=3;else g[i]=2;
	}
	for(int i=f[n]<f[n-1]?n:n-1;i>0;i-=g[i]){
		d(i-1);if(g[i]==3)d(i-2);d(i);
	}
	cout<<vec.size()<<'\n';
	for(int i:vec)cout<<i<<'\n';
	return 0;
}//hello

#include <bits/stdc++.h>
using namespace std;

const int DIM = 300005;

int arr[DIM]; long long dp[DIM];
pair<int, int> seg[DIM]; vector<int> ans;

void solve(int p) {
    int val = min(arr[p], arr[p + 1]);
    if (val)
        ans.push_back(p);
    arr[p] -= val; arr[p + 1] -= val;
}

int main(void)
{
    int n;
    cin >> n;

    for (int i = 1; i <= n; ++i) {
        cin >> arr[i];

        dp[i] = min(dp[max(i - 2, 0)] + arr[i],
                    dp[max(i - 3, 0)] + max(arr[i - 1], arr[i]));

        seg[i] = dp[i] == dp[max(i - 3, 0)] + max(arr[i - 1], arr[i]) ?
                 make_pair(i - 2, i) : make_pair(i - 1, i);
    }

  //  cout << min(dp[n - 1], dp[n]) << "\n\n";

    for (int i = n - (dp[n - 1] <= dp[n]); i >= 1; i = seg[i].first - 1) {
        if (seg[i].first + 2 == seg[i].second)
            solve(i - 1);
        solve(seg[i].first); solve(i);
    }

    cout << ans.size() << "\n";
    for (int x : ans) cout << x << "\n";

    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define MAX 300300
inline int read()
{
	int x=0;bool t=false;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')t=true,ch=getchar();
	while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
	return t?-x:x;
}
int n,p[MAX],g[MAX];ll f[MAX];
vector<int> Ans;
void Work(int i){int x=min(p[i],p[i+1]);if(!x)return;Ans.push_back(i);p[i]-=x;p[i+1]-=x;}
int main()
{
	n=read();
	for(int i=1;i<=n;++i)p[i]=read();
	memset(f,63,sizeof(f));f[0]=0;
	for(int i=1;i<=n;++i)
	{
		f[i]=min(f[max(i-2,0)]+p[i],f[max(i-3,0)]+max(p[i],p[i-1]));
		if(f[i]==f[max(i-3,0)]+max(p[i],p[i-1]))g[i]=1;
	}
	ll ans=min(f[n],f[n-1]);
	for(int i=n-(ans==f[n-1]);i>0;i=i-2-g[i])
	{
		Work(i-1);if(g[i])Work(i-2);Work(i);
	}
	printf("%d\n",(int)Ans.size());
	for(int v:Ans)printf("%d\n",v);
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long

#define maxN 300011

int n, i, pos;
ll p[maxN], s[maxN];

pair<ll, ll> dp[maxN];
pair<ll, ll> from_here[maxN], until_here[maxN];
vector<int> moves;

void upd(pair<ll, ll>& a, pair<ll, ll> b) {
    a = min(a, b);
}

ll execute(ll& a, ll& b) {
    ll aux = min(a, b);
    a -= aux;
    b -= aux;
    return 2LL * aux;
}

ll magic3(ll a, ll b, ll c) {
    ll ans = 0;
    ans += execute(b, c);
    ans += 2 * b + c;
    return ans;
}

pair<ll, ll> get_from_here(int pos) {
    return mp(dp[pos].first + magic3(p[pos], p[pos + 1], p[pos + 2]) - s[pos + 2], pos);
}

ll case_one(ll a, ll b, ll c) {
    ll ans = 0;
    ans += 2LL * b;
    //ans += execute(a, b);
    //ans += execute(b, c);
    return ans;
}

ll case_two(ll a, ll b, ll c, ll d) {
    ll ans = 0;
    ans += execute(b, c);
    //ans += execute(a, b);
    //ans += execute(c, d);
    ans += 2LL * (b + c);
    return ans;
}

ll case_three_one(ll a, ll b, ll c, ll d, ll e) {
    ll ans = 0;
    ans += execute(b, c);
    ans += execute(c, d);
    //ans += execute(a, b);
    //ans += execute(d, e);
    ans += 2LL * (b + d);
    return ans;
}

ll case_three_two(ll a, ll b, ll c, ll d, ll e) {
    ll ans = 0;
    ans += execute(c, d);
    ans += execute(b, c);
    //ans += execute(a, b);
    //ans += execute(d, e);
    ans += 2LL * (b + d);
    return ans;
}

ll case_three(ll a, ll b, ll c, ll d, ll e) {
    return min(case_three_one(a, b, c, d, e),
               case_three_two(a, b, c, d, e));
}

void get_moves(int l, int r) {
    int i, j;

    if (l + 1 >= r) return;

    if (r - l - 1 == 3) {
        if (case_three_two(p[l], p[l + 1], p[l + 2], p[l + 3], p[l + 4]) <
            case_three_one(p[l], p[l + 1], p[l + 2], p[l + 3], p[l + 4]) ) {

            if (p[l + 2] > 0 && p[l + 3] > 0) {
                execute(p[l + 2], p[l + 3]);
                moves.pb(l + 2);
            }

        }
    }

    i = l + 1; j = r - 1;
    while (i < j) {
        if (p[i] > 0 && p[i + 1] > 0) {
            execute(p[i], p[i + 1]);
            moves.pb(i);
        }

        if (p[j] > 0 && p[j - 1] > 0) {
            execute(p[j], p[j - 1]);
            moves.pb(j - 1);
        }

        i++; j--;
    }

    if (p[l] > 0 && p[l + 1] > 0) {
        execute(p[l], p[l + 1]);
        moves.pb(l);
    }

    if (p[r] > 0 && p[r - 1] > 0) {
        execute(p[r - 1], p[r]);
        moves.pb(r - 1);
    }
}

int main()
{
   // freopen("test.in","r",stdin);

    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        scanf("%lld", &p[i]);
        s[i] = s[i - 1] + p[i];
    }

    //! add position 0
    dp[0] = mp(0, 0);
    from_here[0] = until_here[0] = get_from_here(0);

    //! add position 1
    dp[1] = mp(0, 0);
    from_here[1] = get_from_here(1);
    until_here[1] = min(until_here[0], from_here[1]);

    for (i = 2; i <= n + 1; i++) {
        //! size 1
        dp[i] = mp(dp[i - 2].first + case_one(p[i - 2], p[i - 1], p[i]), i - 2);

        //! size 2
        if (i - 3 >= 0) {
            upd(dp[i], mp(dp[i - 3].first + case_two(p[i - 3], p[i - 2], p[i - 1], p[i]), i - 3));
        }

        //! size 3
        if (i - 4 >= 0) {
            upd(dp[i], mp(dp[i - 4].first + case_three(p[i - 4], p[i - 3], p[i - 2], p[i - 1], p[i]), i - 4));
        }

        //! size >= 4
        if (i - 5 >= 0) {
            upd(dp[i], mp( until_here[i - 5].first + s[i - 3] + magic3(p[i], p[i - 1], p[i - 2]), until_here[i - 5].second));
        }

        from_here[i] = get_from_here(i);
        until_here[i] = min(until_here[i - 1], from_here[i]);
    }

    pair<ll, ll> best;
    best = min(mp(dp[n].first, n), mp(dp[n + 1].first, n + 1));
    pos = best.second;

    vector<int> sol = {};
    while (pos != 0) {
        sol.pb(pos);
        pos = dp[pos].second;
    }
    sol.pb(0);

    reverse(sol.begin(), sol.end());
    for (i = 0; i + 1 < sol.size(); i++)
        get_moves(sol[i], sol[i + 1]);

    printf("%d\n", moves.size());
    for (auto e : moves) printf("%d\n", e);

    cerr << "\n\n Total cost " << best.first << ' ' << best.first / 2 << '\n';

    return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)

inline int read()
{
	int data = 0, w = 1; char ch = getchar();
	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if (ch == '-') w = -1, ch = getchar();
	while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
	return data * w;
}

const int maxn(3e5 + 10);
int n, p[maxn], t[maxn]; long long f[maxn];
std::vector<int> ans;

void Doit(int x)
{
	int c = std::min(p[x], p[x + 1]); if (!c) return;
	ans.push_back(x), p[x] -= c, p[x + 1] -= c;
}

int main()
{
	n = read();
	for (int i = 1; i <= n; i++) p[i] = read();
	memset(f, 0x3f, sizeof f), f[0] = 0;
	for (int i = 1; i <= n; i++)
	{
		auto _1 = f[std::max(i - 2, 0)] + p[i];
		auto _2 = f[std::max(i - 3, 0)] + std::max(p[i], p[i - 1]);
		if (_2 <= _1) f[i] = _2, t[i] = 1; else f[i] = _1;
	}
	for (int i = n - (f[n - 1] <= f[n]); i > 0; i -= 2 + t[i])
		{ Doit(i - 1); if (t[i]) Doit(i - 2); Doit(i); }
	printf("%d\n", (int) ans.size());
	for (int i : ans) printf("%d\n", i);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

inline int gi()
{
	char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	int sum = 0;
	while('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
	return sum;
}

typedef long long ll;
const int maxn = 300005;

int n, m, cnt, a[maxn], p[maxn], out[maxn];
ll f[maxn];
bool v[maxn];

int descension(int pos)
{
	int dt = min(a[pos], a[pos + 1]);
	if(dt)
		out[++m] = pos;
	a[pos] -= dt;
	a[pos + 1] -= dt;
	return dt;
}

int main()
{
	n = gi();

	for(int i = 1; i <= n; ++i) {
		a[i] = gi();
		ll odd = f[max(i - 2, 0)] + a[i], even = f[max(i - 3, 0)] + max(a[i - 1], a[i]);
		f[i] = min(odd, even);
		v[i] = f[i] != odd;
	}

	ll ans = min(f[n - 1], f[n]);
	for(int i = n - (ans == f[n - 1]); i > 0; i -= 2 + v[i]) p[++cnt] = i;
	reverse(p + 1, p + cnt + 1);
	for(int i = 1; i <= cnt; ++i) {
		int pre = p[i - 1], cur = p[i], ctr = 0;
		if(v[cur])
			ctr += descension(cur - 1);
		ctr += descension(pre + 1);
		ctr += descension(cur);
		assert(ctr == f[cur] - f[pre]);
	}
	printf("%d\n", m);
	for(int i = 1; i <= m; ++i) printf("%d\n", out[i]);

	return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
const int N=1000005;
int n,a[N],ans[N];
int fl[N],q[N];
ll f[N];
void work(int x){
	int v=min(a[x],a[x+1]);
	if (!v) return;
	a[x]-=v; a[x+1]-=v; ans[++*ans]=x;
}
int main(){
	scanf("%d",&n);
	For(i,1,n) scanf("%d",&a[i]);
	For(i,1,n){
		ll v1=f[max(i-2,0)]+a[i];
		ll v2=f[max(i-3,0)]+max(a[i],a[i-1]);
		f[i]=min(v1,v2); fl[i]=(f[i]!=v1);
	}
	int v=n-(f[n-1]<f[n]),cnt=0;
	for (;v;v-=2+fl[v]) q[++cnt]=v;
	reverse(q+1,q+cnt+1);
	For(i,1,cnt){
		int x=q[i-1],y=q[i];
		work(y-1); work(x+1); work(y);
	}
	For(i,0,*ans)
		printf("%d\n",ans[i]);
}

#include<bits/stdc++.h>
using namespace std;

const int N=300100;
int a[N],b[N],n,cnt,la[N];
long long f[N];
vector<int>ans;
void ins(int x){
	int tmp=min(a[x],a[x+1]);
	if(tmp)ans.push_back(x);
	a[x]-=tmp;a[x+1]-=tmp;
}

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];
		f[i]=min(f[max(i-2,0)]+a[i],f[max(i-3,0)]+max(a[i],a[i-1]));
		la[i]=i-(f[i]==f[max(i-2,0)]+a[i]?2:3);
	}
	for(int i=n-(f[n-1]<=f[n]);i>=1;i=la[i])b[++cnt]=i;
	sort(b+1,b+cnt+1);
	for(int i=1;i<=cnt;++i){ins(b[i]-1);ins(b[i-1]+1);ins(b[i]);}
	cout<<ans.size()<<'\n';
	for(int i:ans)cout<<i<<'\n';
	return 0;
}

#include<bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il ll gi(){
	ll x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))f^=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}
int p[300010],g[300010],ans[300010],ANS;ll f[300010];
il vd fuck(int x){
	int k=std::min(p[x],p[x+1]);if(!k)return;
	p[x]-=k,p[x+1]-=k;ans[++ANS]=x;
}
int main(){
#ifdef XZZSB
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n=gi();
	for(int i=1;i<=n;++i)p[i]=gi();
	memset(f,63,sizeof f);
	f[1]=0;g[1]=0;
	f[2]=p[1];g[2]=1;
	f[3]=p[2];g[3]=1;
	for(int i=1;i<=n;++i){
		if(f[i+3]>f[i]+std::max(p[i+1],p[i+2]))f[i+3]=f[i]+std::max(p[i+1],p[i+2]),g[i+3]=2;
		if(f[i+2]>f[i]+p[i+1])f[i+2]=f[i]+p[i+1],g[i+2]=1;
	}
	int x=f[n]<f[n+1]?n:n+1;
	while(x>1){
		if(g[x]==1)fuck(x-2),fuck(x-1);
		else fuck(x-2),fuck(x-1),fuck(x-3);
		x-=g[x]+1;
	}
	printf("%d\n",ANS);
	for(int i=1;i<=ANS;++i)printf("%d\n",ans[i]);
	return 0;
}

#include<bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il ll gi(){
	ll x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))f^=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}
int p[300010];
ll f[300010],sum;int g[300010];
int ans[300010],ANS;
il vd fuck(int x){
	int k=std::min(p[x],p[x+1]);if(!k)return;
	p[x]-=k,p[x+1]-=k;ans[++ANS]=x;sum+=k;
}
int main(){
#ifdef XZZSB
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n=gi();
	for(int i=1;i<=n;++i)p[i]=gi();
	memset(f,63,sizeof f);
	f[1]=0;g[1]=0;
	f[2]=p[1];g[2]=1;
	f[3]=p[2];g[3]=1;
	for(int i=1;i<=n;++i){
		if(f[i+3]>f[i]+std::max(p[i+1],p[i+2]))f[i+3]=f[i]+std::max(p[i+1],p[i+2]),g[i+3]=2;
		if(f[i+2]>f[i]+p[i+1])f[i+2]=f[i]+p[i+1],g[i+2]=1;
	}
	int x=f[n]<f[n+1]?n:n+1;
	while(x>1){
		if(g[x]==1)fuck(x-2),fuck(x-1);
		else fuck(x-2),fuck(x-1),fuck(x-3);
		x-=g[x]+1;
	}
	assert(std::min(f[n],f[n+1])==sum);
	printf("%d\n",ANS);
	for(int i=1;i<=ANS;++i)printf("%d\n",ans[i]);
	/*
	ANS=gi();
	for(int i=1;i<=ANS;++i)
		ans[i]=gi(),fuck(ans[i]);
	for(int i=1;i<=n;++i)printf("%d ",p[i]);
	*/
	return 0;
}

#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

typedef long long ll;

const ll INF = 1e18L + 5;
const int nax = 3e5 + 5;

struct S {
	ll score;
	pair<int,int> last_state;
	vector<int> moves;
};

bool done[nax][2];
S dp[nax][2];
int in[nax], memo[nax];

ll rec(int n, int anything) {
	if(n == 0) return 0;
	assert(n >= 1);
	if(done[n][anything]) return dp[n][anything].score;
	done[n][anything] = true;
	dp[n][anything].score = INF;
	if(anything) {
		rec(n, 0);
		dp[n][anything] = dp[n][0];
	}
	auto consider = [&] (vector<int> moves) {
		int first = n;
		for(int x : moves) first = min(first, x);
		if(first < 1) return;
		for(int i = first; i <= n; ++i) in[i] = memo[i];
		ll cost = 0;
		for(int x : moves) {
			int here = min(in[x], in[x+1]);
			if(here == 0) return;
			cost += here;
			in[x] -= here;
			in[x+1] -= here;
		}
		if(!anything && in[n]) return;
		for(int i = first; i < n; ++i)
			if(in[i] && in[i+1]) return;
		bool ple = in[first] == 0;
		cost += rec(first - 1, ple);
		if(cost < dp[n][anything].score) {
			dp[n][anything].score = cost;
			dp[n][anything].last_state = {first - 1, ple};
			dp[n][anything].moves = moves;
		}
	};
	//~ consider({});
	//~ consider({n-1});
	//~ consider({n-1,n-2});
	//~ consider({n-2,n-1});
	//~ consider({n-3,n-2,n-1});
	//~ consider({n-2,n-3,n-1});
	//~ consider({n-1,n-2,n-3});
	//~ consider({n-1,n-3,n-2});
	//~ consider({n-2,n-1,n-3});
	//~ consider({n-3,n-1,n-2});

	//~ consider({n-3,n-1,n-2});
	//~ consider({n-1,n-3,n-2});

	consider({});
	consider({n-1});
	consider({n-2, n-1});
	consider({n-1, n-2});
	consider({n-1, n-2, n-3});
	consider({n-3, n-2, n-1});
	consider({n-1, n-3, n-2});

	consider({n-4,n-3,n-2,n-1});
	consider({n-1,n-2,n-3,n-4});
	consider({n-1,n-2,n-4,n-3});
	consider({n-1,n-4,n-3,n-2});

	if(0)for(int len = 0; len <= 3; ++len) {
		vector<int> moves(len);
		for(int i = 1; i <= len; ++i)
			moves[i-1] = n - i;
		sort(moves.begin(), moves.end());
		do {
			consider(moves);
		} while(next_permutation(moves.begin(), moves.end()));
	}
	debug() << imie(n) imie(anything) imie(dp[n][anything].score);
	if(anything) assert(dp[n][anything].score < INF);
	return dp[n][anything].score;
}

int main() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &in[i]);
		memo[i] = in[i];
	}
	vector<int> moves;
	rec(n, 1);
	pair<int,int> p{n, 1};
	while(p.first) {
		for(int x : dp[p.first][p.second].moves) moves.push_back(x);
		p = dp[p.first][p.second].last_state;
	}
	printf("%d\n", (int) moves.size());
	for(int x : moves) printf("%d\n", x);
}

#include <bits/stdc++.h>

#define fo(i,a,b) for (int i=(a); i<(b); i++)
#define sz(v) int(v.size())

#define eb emplace_back
#define pb push_back

using namespace std;

typedef pair<int,int> pii;
typedef long long ll;

ll a[300005];
int n;
ll c[300005][6][2];
ll rm[300005][6][2];

void clc(int i, int j) {
    fo(t,0,2) {
        vector<ll> v(a+i, a+j);
        if (t) reverse(v.begin(), v.end());
        c[i][j-i][t] = 0;
        fo(k,0,sz(v)-1) {
            ll r = min(v[k], v[k+1]);
            c[i][j-i][t] += r;
            v[k] -= r;
            v[k+1] -= r;
        }
        c[i][j-i][t] += v.back();
        rm[i][j-i][t] = v.back();
        fo(k,0,sz(v)-1) {
            if (v[k] != 0) {
                c[i][j-i][t] = 1e12;
            }
        }
        //printf("c[%d][%d][%d]=%lld\n", i, j, t, c[i][j-i][t]);
    }
}

ll cdp[300005][2];
int coptl[300005][2];
int coptl2[300005][2];
int coptb[300005][2];

ll dp(int i, bool p0) {
    if (i == n) return 0;
    if (i == n-1 && p0) return 0;
    if (cdp[i][p0] != -1) return cdp[i][p0];

    ll &res = cdp[i][p0] = 1e18;
    fo(l,1,6) {
        if (i + l < n) {
            ll cc = c[i][l][0];
            ll rr = rm[i][l][0];
            if (rr <= a[i+l]) {
                ll nres = cc + dp(i + l + 1, a[i+l]-rr == 0);
                if (nres < res) {
                    res = nres;
                    coptb[i][p0] = 1;
                    coptl[i][p0] = l;
                }
            }
            fo(l2,1,6) {
                if (i + l + 1 + l2 <= n) {
                    ll cc2 = c[i+l+1][l2][1];
                    ll rr2 = rm[i+l+1][l2][1];
                    if (rr + rr2 <= a[i+l]) {
                        ll nres = cc + cc2 + dp(i + l + l2 + 1, true);
                        if (nres < res) {
                            res = nres;
                            coptb[i][p0] = 2;
                            coptl[i][p0] = l;
                            coptl2[i][p0] = l2;
                        }
                    }
                }
            }
        }
    }
    if (p0) {
        ll nres = dp(i+1, a[i] == 0);
        if (nres < res) {
            res = nres;
            coptb[i][p0] = 3;
        }
        fo(l,1,6) {
            if (i + 1 + l <= n) {
                ll cc = c[i+1][l][1];
                ll rr = rm[i+1][l][1];
                if (rr <= a[i]) {
                    ll nres = cc + dp(i + l + 1, true);
                    if (nres < res) {
                        res = nres;
                        coptb[i][p0] = 4;
                        coptl[i][p0] = l;
                    }
                }
            }
        }
    }
    return res;
}

vector<int> ans;

void bt(int i, int p0) {
    if (i == n) return;
    if (i == n-1 && p0) return;

    if (coptb[i][p0] == 1) {
        int l = coptl[i][p0];
        fo(j,i,i+l) {
            if (a[j] > 0 && a[j+1] > 0) {
                int r = min(a[j], a[j+1]);
                a[j] -= r;
                a[j+1] -= r;
                ans.eb(j+1);
            }
        }
        bt(i+l+1,a[i+l] == 0);
    }
    if (coptb[i][p0] == 2) {
        int l = coptl[i][p0];
        fo(j,i,i+l) {
            if (a[j] > 0 && a[j+1] > 0) {
                int r = min(a[j], a[j+1]);
                a[j] -= r;
                a[j+1] -= r;
                ans.eb(j+1);
            }
        }
        int l2 = coptl2[i][p0];
        for (int j = i+l+l2; j > i+l; j--) {
            if (a[j-1] > 0 && a[j] > 0) {
                int r = min(a[j-1], a[j]);
                a[j-1] -= r;
                a[j] -= r;
                ans.eb(j);
            }
        }
        bt(i+l+1+l2, true);
    }
    if (coptb[i][p0] == 3) {
        bt(i+1, a[i] == 0);
    }
    if (coptb[i][p0] == 4) {
        int l = coptl[i][p0];
        for (int j = i+l; j > i; j--) {
            if (a[j-1] > 0 && a[j] > 0) {
                int r = min(a[j-1], a[j]);
                a[j-1] -= r;
                a[j] -= r;
                ans.eb(j);
            }
        }
        bt(i+l+1, true);
    }
}

int main() {
    memset(cdp, -1, sizeof cdp);
    scanf("%d", &n);
    fo(i,0,n) scanf("%lld", a+i);
    fo(i,0,n) {
        fo(l,1,6) {
            if (i + l <= n) {
                clc(i, i+l);
            }
        }
    }
    ll rdp = dp(0, true);
    //printf("%lld\n", rdp);
    bt(0, true);

    printf("%d\n", sz(ans));
    for (int x : ans) printf("%d\n", x);
}

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<int(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef double ld;

template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

const int N=300005;
int p[N],g[N],n,res;
ll f[N];
vi ans;

void doit(int k){
	int x=min(p[k],p[k+1]);
	if(!x) return;
	ans.pb(k),p[k]-=x,p[k+1]-=x;
}

int main(){
	read(n);
	rep(i,1,n) read(p[i]);
	memset(f,0x3f,sizeof f);
	f[0]=0;
	rep(i,1,n){
		int A=f[max(i-2,0)]+p[i];
		int B=f[max(i-3,0)]+max(p[i],p[i-1]);
		if(A<B) f[i]=A;
		else f[i]=B,g[i]=1;
	}
	res=min(f[n],f[n-1]);
	for(int i=n-(res==f[n-1]);i>0;i=i-2-g[i]){
		doit(i-1);
		if(g[i]) doit(i-2);
		doit(i);
	}
	cout<<ans.size()<<endl;
	for(auto x:ans) printf("%d\n",x);
	return 0;
}

#pragma comment(linker,"/stack:200000000")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
using INT=long long;
const int NN=372727;
int a[NN],val[NN];
INT ans;
vector <int> ANS;
void sol(int k){
	int mn=min(a[k],a[k+1]);
	if(mn==0) return;
	ans+=mn;ANS.push_back(k);
	a[k]-=mn;a[k+1]-=mn;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	int fl=0;
	for(int i=1;i<=n;i++){
		if(a[i]==0) continue;
		if(i==n) sol(n-1);
		if(a[i]>=a[i+1]){
			int j=i;
			int N=1;
			while(a[i]>=a[i+1] and i<n and a[i+1]) N++,i++;
			if(!(N&1)) i--;
			sol(i-1);sol(i-2);
			for(int k=j;k<j-2;k+=2) sol(k);
			sol(j-1);
		}
		else {
			int j=i;
			int N=1;
			while(a[i]<=a[i+1] and i<n) N++,i++;
			for(int k=i-1;k>=j;k-=2) sol(k);
		}
	}
	cout<<ans<<endl;
	for(int v:ANS) printf("%d\n",v);
	return 0;
}

#include <algorithm>
#include <iostream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

int __gcd(int x, int y) { if(!y) return x; return __gcd(y, x % y); }

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define sag (k + k + 1)
#define sol (k + k)

#define type(x) __typeof(x.begin())

#define orta ((bas + son) >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
int mod = 1e9 + 7;
const int logN = 18;
const int inf = 1e9 + 9;

const int N = 3e5 + 5;

int go[N], n, m, x, y, z, a[N];
ll dp[N];
vector< int > ans;

void print(int x, int y) {
	int cur = a[x + 1];
	FOR(i, x + 1, y - 1) {
		int nc = a[i + 1];
		if(cur && a[i + 1]) {
			ans.pb(i);
			int tt = min(cur, a[i + 1]);
			cur -= tt;
			nc -= tt;
		}
		if(cur) {
			ans.pb(i - 1);
		}
		cur = nc;
	}
}

void print(int x) {
	if(x >= n) return ;
	int cur = x, go = :: go[x];
	print(cur, go);
	print(go);
}

int main() {

	scanf("%d", &n);

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
	}

	ROF(i, n - 1, 0) {
		ll cost = 0;
		dp[i] = linf;
		int cur = 0;
		FOR(j, i + 2, n + 1) {
			if(j - i > 1000) break;
			cur = a[j - 1] - min(cur, a[j - 1]);
			cost += cur;
			if(dp[j] + cost < dp[i]) {
				go[i] = j;
				dp[i] = dp[j] + cost + cur;
			}
		}
	}

	if(dp[0] < dp[1])
		print(0);
	else
		print(1);

	cout << ans.size() << endl;

	foreach(it, ans)
		printf("%d ", *it);

	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef  long long ll;
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define inf9 (1000000000)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
#define readmp(n) for(int i=0,u,v;i<n;i++) {scanf("%d%d",&u,&v); mp[u].push_back(v); mp[v].push_back(u);}
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef unsigned int uint;
#define MAX 300105
bool debug = 0;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
string direc="RDLU";
ll ln, lk, lm;
void etp(bool f = 0) {
    puts(f ?"YES" : "NO");
    exit(0);
}
void addmod(int &x, int y, int mod = MOD) {
    assert(y>=0);
    x+=y; if (x>=mod) x-=mod;
    assert(x>=0 && x<mod);
}
void et() {
    puts("-1"); exit(0);
}
ll fastPow(ll x, ll y, int mod=MOD) {
    ll ans = 1;
    while(y>0) {
        if(y&1) ans = (x * ans)%mod;
        x = x*x%mod;
        y>>=1;
    }
    return ans;
}
ll gcd1(ll x, ll y) {
    ll z = y;
    while(x%y!=0) {
        z = x%y; x = y; y = z;
    }
    return z;
}

int p[MAX];
int dp[MAX], pre[MAX];
vector<int> ans;
void wk(int i) {
    if (i<=0) return;
    if (p[i]==0||p[i+1]==0) return;
    ans.push_back(i);
    int t=min(p[i],p[i+1]);
    p[i]-=t;p[i+1]-=t;
}
void fmain(int ID) {
    scanf("%d", &n);
    REQ(i,n) scanf("%d", p+i);
    REQ(i,n) {
        if (dp[max(i-2, 0)]+p[i] < dp[max(i-3, 0)]+max(p[i],p[i-1])) {
            dp[i] = dp[max(i-2, 0)]+p[i];
            pre[i]=max(i-2, 0);
        } else {
            dp[i]=dp[max(i-3, 0)]+max(p[i],p[i-1]);
            pre[i]=max(i-3, 0);
        }
    }
    int A=min(dp[n-1], dp[n]);
    for(int i=dp[n-1]<dp[n] ? n-1 : n; i; i=pre[i]) {
        if (pre[i] == max(i-3,0)) {
            wk(i-1);
        }
        wk(pre[i]+1);
        wk(i);
    }
    printf("%d\n", (int)ans.size());
    for(int c:ans) printf("%d\n", c);
}

int main() {
    int t=1;
//    scanf("%d", &t);
    REQ(i,t) {
        fmain(i);
    }
    return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
const int N=3e5+6;
int f[N],n,a[N],g[N];
vector<int>vec;
void d(int x){int p=min(a[x],a[x+1]);if(p)a[x]-=p,a[x+1]-=p,vec.push_back(x);}
main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>a[i];
	f[1]=a[1],f[2]=a[2],g[1]=g[2]=2;
	for(int i=3;i<=n;++i){
		f[i]=f[i-2]+a[i];
		if(i>3&&f[i]>f[i-3]+max(a[i],a[i-1]))
		f[i]=f[i-3]+max(a[i],a[i-1]),g[i]=3;else g[i]=2;
	}
	for(int i=f[n]>f[n-1]?n:n-1;i>0;i-=g[i]){
		d(i-1);if(g[i]==3)d(i-2);d(i);
	}
	cout<<vec.size()<<'\n';
	for(int i:vec)cout<<i<<'\n';
	return 0;
}

#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 300005;

vector <int> E;
int n, v[N], len[N];
long long dp[N];
bool in[N];

void Mod (int u) {
	int w = min (v[u], v[u + 1]);
	if (w > 0) v[u] -= w, v[u + 1] -= w, E.push_back (u);
}

int main () {
	scanf ("%d", &n);
	for (int i = 1; i <= n; i ++) scanf ("%d", v + i);
	dp[1] = v[1]; len[1] = 1;
	dp[2] = v[2]; len[2] = 1;
	for (int i = 3; i <= n; i ++) {
		int w1 = dp[i - 2] + v[i];
		int w2 = dp[i - 3] + max (v[i - 1], v[i]);
		if (w1 < w2) dp[i] = w1, len[i] = 1; else dp[i] = w2, len[i] = 2;
	}
	for (int u = n; u > 0; u -= len[u] + 1) for (int v = 0; v < len[u]; v ++) in[u - v] = true;
	for (int u = 1; u < n; u ++) if (in[u] && in[u + 1]) Mod (u);
	for (int u = 1; u < n; u ++) Mod (u);
	printf ("%u\n", E.size());
	for (int u : E) printf ("%d\n", u);
	return 0;
}

#include <limits>
#include <algorithm>
#include <iostream>
#include <vector>

enum class BackwardOption {ONE_SEQUENCE, TWO_SEQUENCE};

struct State {
    int min_steps = std::numeric_limits<int>::max();
    BackwardOption backward_option;

    bool operator<(const State& other) const {
        return min_steps < other.min_steps;
    }
};

void PerformStep(std::vector<int>& steps, std::vector<int>& p, const int index) {
    if (p[index] > 0 && p[index + 1] > 0) {
        steps.push_back(index);
        int min_number = std::min(p[index], p[index + 1]);
        p[index] -= min_number;
        p[index + 1] -= min_number;
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    int n;
    std::cin >> n;
    std::vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> p[i];
    }
    std::vector<State> min_steps(n + 1);
    min_steps[0].min_steps = 0;
    min_steps[1].min_steps = 0;
    for (int i = 0; i < n; ++i) {
        if (i >= 1 && i - 1 != 1) {
            min_steps[i + 1] = std::min(min_steps[i + 1], {min_steps[i - 1].min_steps + p[i], BackwardOption::ONE_SEQUENCE});
        }
        if (i >= 2 && i - 2 != 1) {
            min_steps[i + 1] = std::min(min_steps[i + 1], {min_steps[i - 2].min_steps + std::max(p[i], p[i - 1]),  BackwardOption::TWO_SEQUENCE});
        }
    }
    int last_length;
    if (min_steps[n - 1] < min_steps[n]) {
        last_length = n - 1;
    } else {
        last_length = n;
    }
    std::vector<int> steps;
    while (last_length > 1) {
        if (min_steps[last_length].backward_option == BackwardOption::ONE_SEQUENCE) {
            PerformStep(steps, p, last_length - 2);
            last_length -= 2;
        } else {
            PerformStep(steps, p, last_length - 2);
            PerformStep(steps, p, last_length - 3);
            last_length -= 3;
        }
    }
    std::cout << steps.size() << std::endl;
    for (auto index: steps) {
        std::cout << index + 1 << std::endl;
    }
    return 0;
}

#include<bits/stdc++.h>
#define NN 300100
#define inf 1000000000000000000

using namespace std;
using INT=long long;
using pii=pair<int, int>;

int a[NN], pre[NN][2], arr[NN];
INT dp[NN][2];
vector<int>vec;

void solve(int nn) {
	dp[1][1] = 1;
	dp[1][0] = inf;
	for(int i=2; i<=nn; i++) {
		dp[i][1] = dp[i-1][0];
		dp[i][0] = inf;
		if(a[i]<=a[i-1]) {
			dp[i][0] = min(dp[i][0], a[i] + dp[i-2][0]);
			dp[i][0] = min(dp[i][0], max(a[i], a[i-1]) + dp[i-2][1]);
		}
	}
	INT ans = min(dp[nn][0], dp[nn][1]);
	int op=0;
	if(ans == dp[nn][1]) op=1;
	int pre=nn;
	while(pre>1) {
		if(op==1) {
			pre--;
			op^=1;
			continue;
		}
		if(dp[pre][op] == a[pre] + dp[pre-2][0]) {
			vec.push_back(pre-1);
			pre-=2;
			op = 0;
			continue;
		}
		vec.push_back(pre-1);
		if(pre>2) vec.push_back(pre-2);
		pre -= 2;
		op = 1;
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	cin>>n;
	for(int i=1; i<=n; i++) scanf("%d", &a[i]);
	if(n==1) {
		puts("0");
		return 0;
	}
	int st=0;
	for(int i=1; i<=n; i++) {
		if(a[i]) {
			st=i;
			break;
		}
	}
	if(st==0) {
		puts("0");
		return 0;
	}
	while(st <= n) {
		int nn=0;
		for(int i=st; i<=n; i++) {
			if(a[i]==0) break;
			arr[++nn]=a[i];
		}
		if(nn>1)solve(nn);
		int id=0;
		for(int i=st+nn; i<=n; i++) {
			if(a[i]) {
				id=i;
				break;
			}
		}
		if(id==0) break;
		st=id;
	}
	cout<<vec.size()<<endl;
	for(int v : vec) printf("%d\n", v);

	return 0;
}

/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
template<class T, class... U> void DEBUG(const T &head, const U &... tail) {
#ifdef HOME
    _W('#'); _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...);
#endif
}
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int n;
int p[SIZE];
LL dp[SIZE];
VI ans;
void go(int x){
    if(!p[x])return;
    if(p[x]<=p[x-1]){
        p[x-1]-=p[x];
        p[x]=0;
        ans.PB(x-1);
    }
    else{
        p[x+1]-=p[x];
        p[x]=0;
        ans.PB(x);
    }
}
void go(int x,int y){
    ans.PB(x);
    int mi=min(p[x],p[y]);
    p[x]-=mi; p[y]-=mi;
    if(p[x]){
        ans.PB(x-1);
        p[x-1]-=p[x];
        p[x]=0;
    }
    if(p[y]){
        ans.PB(y);
        p[y+1]-=p[y];
        p[y]=0;
    }
}
int main(){
    R(n);
    if(n==1){
        W(0);
        return 0;
    }
    else if(n==2){
        W(1);
        W(1);
        return 0;
    }
    FOR(i,1,n){
        R(p[i]);
    }
    dp[2]=p[1];
    FOR(i,3,n){
        dp[i]=dp[i-2]+p[i-1];
        dp[i]=min(dp[i],dp[i-3]+max(p[i-1],p[i-2]));
    }
    int now;
    if(dp[n]>dp[n-1]){
        go(n);
        now=n-1;
    }
    else now=n;
    while(now>1){
        if(dp[now]==dp[now-2]+p[now-1]){
            go(now-1);
            now-=2;
        }
        else{
            go(now-2,now-1);
            now-=3;
        }
    }
    W(SZ(ans));
    W(ans);
    return 0;
}

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<queue>
#include<map>
#include<set>
#define LL long long
#define db double

using namespace std;
const int N=3e5+10;
int rd()
{
    int x=0,w=1;char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*w;
}
int n,a[N],pr[N],an[N],ta;
LL f[N];
int wk(int x)
{
	int dt=min(a[x],a[x+1]);
	a[x]-=dt,a[x+1]-=dt;
	return dt;
}

int main()
{
	n=rd();
	for(int i=1;i<=n;++i) a[i]=rd();
	if(n==1){puts("0");return 0;}
	if(n==2){puts("1");puts("1");return 0;}
	memset(f,0x3f3f3f,sizeof(f));
	f[1]=0,f[2]=a[1];
	for(int i=3;i<=n;++i)
	{
		if(f[i-2]+a[i-1]<f[i-3]+max(a[i-1],a[i-2]))
			f[i]=f[i-2]+a[i-1],pr[i]=i-2;
		else
			f[i]=f[i-3]+max(a[i-1],a[i-2]),pr[i]=i-3;
	}
	int x=n;
	while(x)
	{
		if(x-pr[x]<=2)
		{
			if(wk(x-2)) an[++ta]=x-2;
			if(wk(x-1)) an[++ta]=x-1;
		}
		else
		{
			if(wk(x-3)) an[++ta]=x-3;
			if(wk(x-2)) an[++ta]=x-2;
			if(wk(x-1)) an[++ta]=x-1;
		}
		x=pr[x];
	}
	printf("%d\n",ta);
	for(int i=1;i<=ta;++i) printf("%d\n",an[i]);
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 300030
#define ll long long
using namespace std;
int n,a[maxn],ans[maxn],m;
ll dp[maxn];bool d[maxn];
void init(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i)scanf("%d",&a[i]);
}
void opt(int pos){
	if(pos<=0||pos==n)return;
	int tmp=min(a[pos],a[pos+1]);
	if(!tmp)return;
	ans[++m]=pos;
	a[pos]-=tmp,a[pos+1]-=tmp;
}
void solve(){
	for(int i=1;i<=n;++i){
		ll one=dp[max(i-2,0)]+a[i],two=dp[max(i-3,0)]+max(a[i-1],a[i]);
		dp[i]=min(one,two);
		d[i]=dp[i]==one;
	}
	for(int i=(dp[n]<dp[n-1])?n:n-1;i>0;){
		if(d[i]){
			opt(i);
			opt(i-1);
			i-=2;
		}
		else{
			opt(i);
			opt(i-1);
			opt(i-2);
			i-=3;
		}
	}
	printf("%d\n",m);
	for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
}
int main(){
	init();
	solve();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using INT=long long;
const int NN=372727;
int a[NN],val[NN],b[NN];
INT dp[NN][2];
vector <int> ANS;
void sol(int k){
	int mn=min(a[k],a[k+1]);
	if(mn==0) return;
	ANS.push_back(k);
	a[k]-=mn;a[k+1]-=mn;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i),b[i]=a[i];
	dp[0][0]=dp[0][1]=0;
	for(int i=1;i<=n;i++){
		dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i];
//		if(i>1) dp[i][0]=min(dp[i][0],min(dp[i-2][1],dp[i-2][1])+max(a[i],a[i-1]));

		dp[i][1]=dp[i-1][0];
	}
	int st=n,fl=0;
	if(dp[n][1]<=dp[n][0]) fl=1;
	while(st){
		if(fl){
			st--;
			fl=0;
		}
		else {
			if(dp[st][0]==dp[st-1][0]+b[st]) sol(st-1),sol(st),st--;
			else if(dp[st][0]==dp[st-1][1]+b[st]) sol(st),sol(st-1),st--,fl=1;
//			else if(dp[st][0]==dp[st-2][0]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=0;
//			else if(dp[st][0]==dp[st-2][1]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=1;
		}
	}
	cout<<ANS.size()<<endl;
	for(int v:ANS) printf("%d\n",v);
	return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define int long long
const int N=3e5+6;
int f[N],n,a[N],g[N];
vector<int>vec;
void d(int x){int p=min(a[x],a[x+1]);if(p)a[x]-=p,a[x+1]-=p,vec.push_back(x);}
main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>a[i];
	f[1]=a[1],f[2]=a[2],g[1]=g[2]=2;
	for(int i=3;i<=n;++i){
		f[i]=f[i-2]+a[i];
		if(i>3&&f[i]>f[i-3]+max(a[i],a[i-1]))
		f[i]=f[i-3]+max(a[i],a[i-1]),g[i]=3;else g[i]=2;
	}
	for(int i=f[n]<f[n-1]?n:n-1;i>0;i-=g[i]){
		d(i-1);if(g[i]==3)d(i-2);d(i);
	}
	cout<<vec.size()<<'\n';
	for(int i:vec)cout<<i<<'\n';
	return 0;
}

#include<bits/stdc++.h>
#define il inline
#define vd void
typedef long long ll;
il ll gi(){
	ll x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))f^=ch=='-',ch=getchar();
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f?x:-x;
}
int p[300010];
ll f[300010];int g[300010];
int ans[300010],ANS;
il vd fuck(int x){
	int k=std::min(p[x],p[x+1]);if(!k)return;
	p[x]-=k,p[x+1]-=k,ans[++ANS]=x;
}
int main(){
#ifdef XZZSB
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n=gi();
	for(int i=1;i<=n;++i)p[i]=gi();
	memset(f,63,sizeof f);f[1]=0;
	for(int i=1;i<=n;++i){
		f[i+3]=f[i]+std::max(p[i+1],p[i+2]),g[i+3]=2;
		if(f[i+2]>f[i]+p[i+1])f[i+2]=f[i]+p[i+2],g[i+2]=1;
	}
	int x=f[n]<f[n+1]?n:n+1;
	while(x>1){
		if(g[x]==1)fuck(x-2),fuck(x-1);
		else fuck(x-2),fuck(x-1),fuck(x-3);
		x-=g[x]+1;
	}
	printf("%d\n",ANS);
	for(int i=1;i<=ANS;++i)printf("%d\n",ans[i]);
	return 0;
}

#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

typedef long long ll;

const ll INF = 1e18L + 5;
const int nax = 3e5 + 5;

struct S {
	ll score;
	pair<int,int> last_state;
	vector<int> moves;
};

bool done[nax][2];
S dp[nax][2];
int in[nax], memo[nax];

ll rec(int n, int anything) {
	if(n == 0) return 0;
	assert(n >= 1);
	if(done[n][anything]) return dp[n][anything].score;
	done[n][anything] = true;
	dp[n][anything].score = INF;
	if(anything) {
		rec(n, 0);
		dp[n][anything] = dp[n][0];
	}
	auto consider = [&] (vector<int> moves) {
		int first = n;
		for(int x : moves) first = min(first, x);
		if(first < 1) return;
		for(int i = first; i <= n; ++i) in[i] = memo[i];
		ll cost = 0;
		for(int x : moves) {
			int here = min(in[x], in[x+1]);
			if(here == 0) return;
			cost += here;
			in[x] -= here;
			in[x+1] -= here;
		}
		if(!anything && in[n]) return;
		for(int i = first; i < n; ++i)
			if(in[i] && in[i+1]) return;
		bool ple = in[first] == 0;
		cost += rec(first - 1, ple);
		if(cost < dp[n][anything].score) {
			dp[n][anything].score = cost;
			dp[n][anything].last_state = {first - 1, ple};
			dp[n][anything].moves = moves;
		}
	};
	consider({});
	consider({n-1});
	consider({n-3,n-1,n-2});
	consider({n-1,n-3,n-2});

	for(int len = 2; len <= 5; ++len) {
		vector<int> moves(len);
		for(int i = 1; i <= len; ++i)
			moves[i-1] = n - i;
		consider(moves);
		reverse(moves.begin(), moves.end());
		consider(moves);
	}
	debug() << imie(n) imie(anything) imie(dp[n][anything].score);
	if(anything) assert(dp[n][anything].score < INF);
	return dp[n][anything].score;
}

int main() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &in[i]);
		memo[i] = in[i];
	}
	vector<int> moves;
	rec(n, 1);
	pair<int,int> p{n, 1};
	while(p.first) {
		for(int x : dp[p.first][p.second].moves) moves.push_back(x);
		p = dp[p.first][p.second].last_state;
	}
	printf("%d\n", (int) moves.size());
	for(int x : moves) printf("%d\n", x);
}

#include<bits/stdc++.h>
using namespace std;
vector<int> ans;
int n,f[300010],p[300010],g[300010];
void work(int x)
{
    int vl=min(p[x],p[x+1]);
    if(vl)
    {
        ans.push_back(x);
        p[x]-=vl;p[x+1]-=vl;
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&p[i]);
    f[0]=0;
    for(int i=1;i<=n;i++)
    {
        f[i]=min(f[max(i-2,0)]+p[i],f[max(i-3,0)]+max(p[i-1],p[i]));
        if(f[i]==f[max(i-3,0)]+max(p[i],p[i-1]))g[i]=1;
    }
    for(int i=n-(f[n-1]<=f[n]);i>0;i=i-2-g[i])
    {
        work(i-1);
        if(g[i])work(i-2);
        work(i);
    }
    printf("%d\n",ans.size());
    for(int &x:ans)printf("%d\n",x);
    return 0;
}

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<int(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef double ld;

template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

const int N=300005;
int p[N],f[N],g[N],n,res;
vi ans;

void doit(int k){
	int x=min(p[k],p[k+1]);
	if(!x) return;
	ans.pb(k),p[k]-=x,p[k+1]-=x;
}

int main(){
	read(n);
	rep(i,1,n) read(p[i]);
	memset(f,0x3f,sizeof f);
	f[0]=0;
	rep(i,1,n){
		int A=f[max(i-2,0)]+p[i];
		int B=f[max(i-3,0)]+max(p[i],p[i-1]);
		if(A<B) f[i]=A;
		else f[i]=B,g[i]=1;
	}
	res=min(f[n],f[n-1]);
	for(int i=n-(res==f[n-1]);i>0;i=i-2-g[i]){
		doit(i-1);
		if(g[i]) doit(i-2);
		doit(i);
	}
	cout<<ans.size()<<endl;
	for(auto x:ans) printf("%d\n",x);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 3e5+5;
int n, a[N];
LL d[N];
bool odd[N];
vector<int> op;
void operate(int x) {
    if (x <= 0 || x > n) return;
    int mi = min(a[x], a[x+1]);
    if (!mi) return;
    op.push_back(x);
    a[x] -= mi, a[x+1] -= mi;
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
        int fo = d[max(i-2, 0)] + a[i];
        int fe = d[max(i-3, 0)] + max(a[i], a[i-1]);
        d[i] = min(fo, fe);
        if (d[i] == fo) odd[i] = 1;
    }
    LL ans = min(d[n], d[n-1]);
    for (int i = n-(ans==d[n-1]); i >= 1;) {
        if (odd[i]) {
            operate(i);
            operate(i-1);
            i -= 2;
        } else {
            operate(i-1);
            operate(i);
            operate(i-2);
            i -= 3;
        }
    }
    printf("%d\n", ans);
    for (auto x: op) printf("%d\n", x);
}

#include <algorithm>
#include <iostream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

int __gcd(int x, int y) { if(!y) return x; return __gcd(y, x % y); }

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define sag (k + k + 1)
#define sol (k + k)

#define type(x) __typeof(x.begin())

#define orta ((bas + son) >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
int mod = 1e9 + 7;
const int logN = 18;
const int inf = 1e9 + 9;

const int N = 3e5 + 5;

int go[N], n, m, x, y, z, a[N];
ll dp[N];
vector< int > ans;

void print(int x, int y) {
	int cur = a[x + 1];
	FOR(i, x + 1, y - 1) {
		int nc = a[i + 1];
		if(cur && a[i + 1]) {
			ans.pb(i);
			int tt = min(cur, a[i + 1]);
			cur -= tt;
			nc -= tt;
		}
		if(cur) {
			ans.pb(i - 1);
		}
		cur = nc;
	}
}

void print(int x) {
	if(x >= n) return ;
	int cur = x, go = :: go[x];
	print(cur, go);
	print(go);
}

int main() {

	scanf("%d", &n);

	FOR(i, 1, n) {
		scanf("%d", &a[i]);
	}

	ROF(i, n - 1, 0) {
		ll cost = 0;
		dp[i] = linf;
		int cur = a[i + 1];
		FOR(j, i + 2, n + 1) {
			if(dp[j] + cost + cur < dp[i]) {
				go[i] = j;
				dp[i] = dp[j] + cost + cur;
			}
			cost += cur;
			cur = a[j] - min(cur, a[j]);
		}
	}

	if(dp[0] < dp[1])
		print(0);
	else
		print(1);

	cout << min(dp[0], dp[1]) << endl;

	cout << ans.size() << endl;

	foreach(it, ans)
		printf("%d ", *it);

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=300005;
int n,a[N],f[N],pre[N],q[N],top;
void down(int i){
	int t=min(a[i],a[i+1]);
	if(!t)return;
	q[++top]=i;
	a[i+1]-=t;a[i]-=t;
}
int main(){
	//freopen("aa.in","r",stdin);
	int i,j;
	scanf("%d",&n);
	for(i=1;i<=n;++i){
		scanf("%d",&a[i]);
	}
	for(i=1;i<=n;++i){
		int x=f[max(i-2,0)]+a[i];
		int y=f[max(i-3,0)]+max(a[i-1],a[i]);
		if(x<y){
			f[i]=x;
			pre[i]=2;
		}else{
			f[i]=y;
			pre[i]=3;
		}
	}
	int ans=min(f[n-1],f[n]);
	for(i=n-(ans==f[n-1]);i;i=max(0,i-pre[i])){
		if(pre[i]==3){
			down(i-2);
			down(i-1);
			down(i);
		}else{
			down(i-1);
			down(i);
		}
	}
	printf("%d\n",top);
	for(i=1;i<=top;++i)printf("%d\n",q[i]);
	return 0;
}

#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

typedef long long ll;

const ll INF = 1e18L + 5;
const int nax = 3e5 + 5;

struct S {
	ll score;
	pair<int,int> last_state;
	vector<int> moves;
};

bool done[nax][2];
S dp[nax][2];
int in[nax], memo[nax];

ll rec(int n, int anything) {
	if(n == 0) return 0;
	assert(n >= 1);
	if(done[n][anything]) return dp[n][anything].score;
	done[n][anything] = true;
	dp[n][anything].score = INF;
	if(anything) {
		rec(n, 0);
		dp[n][anything] = dp[n][0];
	}
	auto consider = [&] (vector<int> moves) {
		int first = n;
		for(int x : moves) first = min(first, x);
		if(first < 1) return;
		for(int i = first; i <= n; ++i) in[i] = memo[i];
		ll cost = 0;
		for(int x : moves) {
			int here = min(in[x], in[x+1]);
			if(here == 0) return;
			cost += here;
			in[x] -= here;
			in[x+1] -= here;
		}
		if(!anything && in[n]) return;
		for(int i = first; i < n; ++i)
			if(in[i] && in[i+1]) return;
		bool ple = in[first] == 0;
		cost += rec(first - 1, ple);
		if(cost < dp[n][anything].score) {
			dp[n][anything].score = cost;
			dp[n][anything].last_state = {first - 1, ple};
			dp[n][anything].moves = moves;
		}
	};
	consider({});
	//~ consider({n-1});
	//~ consider({n-3,n-1,n-2});
	//~ consider({n-1,n-3,n-2});

	for(int len = 1; len <= 3; ++len) {
		vector<int> moves(len);
		for(int i = 1; i <= len; ++i)
			moves[i-1] = n - i;
		sort(moves.begin(), moves.end());
		do {
			consider(moves);
		} while(next_permutation(moves.begin(), moves.end()));
	}
	debug() << imie(n) imie(anything) imie(dp[n][anything].score);
	if(anything) assert(dp[n][anything].score < INF);
	return dp[n][anything].score;
}

int main() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &in[i]);
		memo[i] = in[i];
	}
	vector<int> moves;
	rec(n, 1);
	pair<int,int> p{n, 1};
	while(p.first) {
		for(int x : dp[p.first][p.second].moves) moves.push_back(x);
		p = dp[p.first][p.second].last_state;
	}
	printf("%d\n", (int) moves.size());
	for(int x : moves) printf("%d\n", x);
}

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<int(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef double ld;

template<class T> inline void read(T &x){
	int f=0;x=0;char ch=getchar();
	for(;!isdigit(ch);ch=getchar())f|=(ch=='-');
	for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(f)x=-x;
}

const int N=300005;
int p[N],f[N],g[N],n,res;
vi ans;

void doit(int k){
	int x=min(p[k],p[k+1]);
	if(!x) return;
	ans.pb(k),p[k]-=x,p[k+1]-=x;
}

int main(){
	read(n);
	rep(i,1,n) read(p[i]);
	memset(f,0x3f,sizeof f);
	f[0]=0;
	rep(i,1,n){
		int A=f[max(i-2,0)]+p[i];
		int B=f[max(i-3,0)]+max(p[i],p[i-1]);
		if(A<B) f[i]=A;
		else f[i]=B,g[i]=1;
	}
	res=min(f[n],f[n-1]);
	for(int i=n-(res==f[n-1]);i>0;i=i-2-g[i]){
		doit(i-1);
		if(g[i]) doit(i-2);
		doit(i);
	}
	cout<<res<<endl;
	for(auto x:ans) printf("%d\n",x);
	return 0;
}

# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) ((x)&1)
#define sqr(x) ((x)*(x))
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,a,b) for (int i=(a); i<=(b); ++i)
#define per(i,a,b) for (int i=(a); i>=(b); --i)
#define rep_it(it,x) for (__typeof((x).begin()) it=(x).begin(); it!=(x).end(); it++)
#define ____ puts("\n_______________\n\n")
#define debug(x) ____; cout<< #x << " => " << (x) << endl
#define debug_pair(x) cout<<"\n{ "<<(x).fir<<" , "<<(x).sec<<" }\n"
#define debug_arr(x,n) ____; cout<<#x<<":\n"; rep(i,0,n) cout<<#x<<"["<<(i)<<"] => "<<x[i]<<endl
#define debug_arr2(x,n,m) ____; cout<<#x<<":\n"; rep(i,0,n) rep(j,0,m) cout<<#x<<"["<<(i)<<"]["<<(j)<<"]= "<<x[i][j]<<((j==m)?"\n\n":"    ")
#define debug_set(x) ____; cout<<#x<<": \n"; rep_it(it,x) cout<<(*it)<<" "; cout<<endl
#define debug_map(x) ____; cout<<#x<<": \n"; rep_it(it,x) debug_pair(*it)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const ll ooo=9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N=301005;
const double PI=acos(-1.0);
const double eps=1e-9;
int o(int x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

int a[N],op[N],tmp[N],ans[N],n; ll dp[N];

void U(int k,int x,ll t=0) { if (dp[x]+t<dp[k]) dp[k]=dp[x]+t,op[k]=x; }

int main() {
    //file_put();

    scanf("%d",&n);
	rep(i,1,n) scanf("%d",&a[i]);
	rep(i,0,n) dp[i]=oo;
	if (n==1) return 0*printf("%d\n",0); else dp[n]=a[n];
	per(i,n-1,0) {
		if (a[i]==0) { U(i,i+1),U(i,i+2); continue; }
		U(i,i+2,a[i]);
		if (a[i]>=a[i+1]) { U(i,i+3,a[i]); continue; }
		int tt=a[i+1]-a[i];
		if (i+2<=n) {
			if (i+3<=n) U(i,i+3,a[i+1]);
			if (i+4<=n) U(i,i+4,a[i+1]);
		}
	}
	for (int t=op[0]; t; t=op[t]) ans[++ans[0]]=t;
	per(i,ans[0],1) {
		if (a[ans[i]]==0) continue;
		if (i==ans[0] || ans[i+1]-ans[i]<=2 || a[ans[i]]>=a[ans[i]+1])
			if (a[ans[i]]<=a[ans[i]+1]) {
				a[ans[i]+1]-=a[ans[i]];
				a[ans[i]]=0;
				tmp[++tmp[0]]=ans[i];
			} else {
				if (a[ans[i]+1]) tmp[++tmp[0]]=ans[i];
				tmp[++tmp[0]]=ans[i]-1;
				a[ans[i]]-=a[ans[i]+1];
				a[ans[i]-1]-=a[ans[i]];
				a[ans[i]+1]=a[ans[i]]=0;
			}
		else {
			tmp[++tmp[0]]=ans[i];
			tmp[++tmp[0]]=ans[i]+1;
			a[ans[i]+1]-=a[ans[i]];
			a[ans[i]]=0;
			a[ans[i]+2]-=a[ans[i]+1];
			a[ans[i]+1]=0;
		}
	}
	//debug(dp[0]);
	printf("%d\n",tmp[0]);
	rep(i,1,tmp[0]) printf("%d\n",tmp[i]);

    return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 3e5+5;
int  n;
vector<int>ANS;
int a[maxn];
void WK(int x) {
	int o = min(a[x],a[x-1]);
	if(!o) return;
	ANS.push_back(x-1);
	a[x] -= o; a[x-1] -= o;
}
int chos[maxn];
int f[maxn];
main() {
	scanf("%lld",&n);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++) {
		f[i] = min( f[max(i-2,0ll)] + a[i], f[max(i-3,0ll)] + max(a[i],a[i-1]) );
		if(f[i]==f[max(i-3,0ll)]+max(a[i],a[i-1])) chos[i] = 1;
	}
	int ans = min(f[n],f[n-1]);
	int FR = n - (ans==f[n-1]);
	for(int i=FR;i>=1;) {
		WK(i);
		if(chos[i]==1) {
			WK(i-1);
		}
		WK(i+1);
		if(chos[i]) i-=3;
		else i-=2;
	}
	printf("%lld\n",ANS.size());
	for(auto p:ANS) {
		printf("%d\n",p);
	}
}

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<vector>
using namespace std;
const long long inf=1e18;
int a[330000],n,ans[330000],cnt;
long long dp[330000];
int main()
{
	scanf("%d",&n);
	int i,j,s,p,q;
	for(i=0;i<n;i++)
	   scanf("%d",&a[i]);
    dp[n-1]=0;
    dp[n]=0;
	for(i=n-2;i>=0;i--)
	{
		dp[i]=min(dp[i+2]+a[i+1],dp[i+3]+a[i+2]+min(a[i],max(0,a[i+1]-a[i+2])));
	}
	i=0;
	cnt=0;
//	printf("dp[0]=%lld\n",dp[0]);
	while(i<n)
	{
		if(dp[i]!=dp[i+3]+a[i+2]+min(a[i],max(0,a[i+1]-a[i+2])))
		{
			if(min(a[i],a[i+1])!=0)
			     ans[cnt++]=i;
            if(a[i+1]>a[i])
                 ans[cnt++]=i+1;
			i=i+2;
		}
		else
		{
			if(min(a[i+1],a[i+2])!=0)
			    ans[cnt++]=i+1;
            if(min(a[i],max(0,a[i+1]-a[i+2]))!=0)
                ans[cnt++]=i;
             if(a[i+1]<a[i+2])
                ans[cnt++]=i+2;
             i=i+3;
		}
	}
	printf("%d\n",cnt);
	for(i=0;i<cnt;i++)
		printf("%d\n",ans[i]+1);
	return 0;
}//

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<vector>
using namespace std;
const long long inf=1e18;
int a[330000],n,ans[330000],cnt;
long long dp[330000];
int main()
{
	scanf("%d",&n);
	int i,j,s,p,q;
	for(i=0;i<n;i++)
	   scanf("%d",&a[i]);
    dp[n-1]=0;
    dp[n]=0;
	for(i=n-2;i>=0;i--)
    	dp[i]=min(dp[i+2]+a[i+1],dp[i+1]+min(a[i],max(0,a[i+1]-a[i+2])));
	i=0;
	cnt=0;
	while(i<n)
	{
		if(dp[i]!=dp[i+1]+min(a[i],max(0,a[i+1]-a[i+2])))
		{
			if(min(a[i],a[i+1])!=0)
			     ans[cnt++]=i;
            i=i+2;
		}
		else
		{
			if(min(a[i+1],a[i+2])!=0)
			    ans[cnt++]=i+1;
            if(min(a[i],max(0,a[i+1]-a[i+2]))!=0)
                ans[cnt++]=i;
            if(max(0,a[i+1]-a[i+2])-a[i]>0)
            	i++;
            else if(a[i+1]<a[i+2])
                i+=2;
            else
                i=i+3;
		}
	}
	printf("%d\n",cnt);
	for(i=0;i<cnt;i++)
		printf("%d\n",ans[i]+1);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define N 303030

int n, p[N];
long long dp[N];

bool bad[N];

vector <int> v;

long long ans;

void doing(int k) {
	int val = min(p[k], p[k+1]);
	if (val) v.push_back(k);
	p[k] -= val, p[k+1] -= val;
	ans += val;
}

void oper(int k, int tag) {
	if (tag == 1) doing(k), doing(k - 1);
	else doing(k), doing(k - 1), doing(k + 1);
}

int tar;

int main() {
	//freopen("r.in", "r", stdin);
	tar = (int)clock();
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) scanf("%d", &p[i]);
	dp[1] = p[1];
	dp[2] = max(p[1], p[2]);
	for (int i = 3; i <= n + 1; i ++) {
		dp[i] = min(dp[i-2] + p[i], dp[i-3] + max(p[i], p[i-1]));
	}
	int k;
	if (dp[n] < dp[n+1]) k = n;
	else k = n + 1;
	bad[k+1] = 1;
	while (k > 2) {
		if (dp[k] == dp[k-2] + p[k]) bad[k-1] = 1, k = k - 2;
		else bad[k-2] = 1, k = k - 3;
	}
	for (int i = 1; i <= n; i ++) {
		if (!bad[i]) {
			int j = i;
			while (!bad[j]) j ++;
			oper(i, j - i);
			i = j;
		}
	}
	//while (clock() - tar <= 1900);
	printf("%d\n", (int)v.size());
	for (int i = 0; i < v.size(); i ++) printf("%d\n", v[i]);

	return 0;
}

//In the Name of God
//Ya Ali

#include<bits/stdc++.h>

#define err(A) cout << #A << " = " << (A) << endl

using namespace std;

const int maxn = 300300;

int n;

int a[maxn];

int dp[maxn];
int p[maxn];

bool mark[maxn];

vector<int> ans;

void chk(int i, int x, int pr)
{
  if (x < dp[i])
    dp[i] = x, p[i] = pr;
}

void follow(int i)
{
  if (i < 1)
    return;
  mark[i] = true;
  for (int j = p[i] + 2; j < i; j ++)
    mark[j] = true;
  follow(p[i]);
}

void add(int i)
{
  int c = min(a[i], a[i + 1]);
  if (c)
    {
      a[i] -= c;
      a[i + 1] -= c;
      ans.push_back(i);
    }
}

int main()
{
  ios::sync_with_stdio(0); cin.tie(0);

  cin >> n;

  for (int i = 1; i <= n; i ++)
    cin >> a[i];

  for (int i = 1; i <= n; i ++)
    {
      chk(i, dp[i - 1] + a[i], i - 1);
      chk(i, (i > 1 ? dp[i - 2] : 0) + a[i], i - 2);
      chk(i, (i > 2 ? dp[i - 3] : 0) + max(a[i], a[i - 1]), i - 3);
    }

  if (dp[n] < dp[n - 1])
    follow(n);
  else
    follow(n - 1);

  for (int i = 1; i <= n; i ++)
    if(mark[i] and a[i])
      add(i);

  for (int i = 1; i <= n; i ++)
    if(a[i] and a[i + 1])
      add(i);

  cout << ans.size() << "\n";
  for(int i:ans)
    cout << i << "\n";

  return 0;
}

#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int MAX = 3e5 + 100;
int n,t[MAX];
int dp[MAX][2];
vector<int> res;
bool ruszone[MAX];
int RES = 0;
void rusz(int i){
  if(i < 0 || i >= n-1 || ruszone[i])return;
  ruszone[i] = 1;
  int pom = min(t[i],t[i+1]);
  RES += pom;
  if(pom){
    res.PB(i+1);
    t[i] -= pom;
    t[i+1] -= pom;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> n;
  R(i,n)cin >> t[i];
  R(i,n)dp[i+1][0] = dp[i+1][1] = -1e18;
  R(i,n){
    maxi(dp[i+1][0], max(dp[i][0], dp[i][1]) - t[i]);
    maxi(dp[i+1][1], dp[i][0] + t[i]);
    if(i > 1){
      if(t[i-2] >= t[i-1]){
        maxi(dp[i+1][1], dp[i-2][1] - t[i-2] + t[i] + t[i-1]);
      }
      if(t[i] >= t[i-1]){
        maxi(dp[i+1][0], dp[i-1][1] + t[i-1] - t[i]);
      }
    }
  }
  int ak = 0;
  if(dp[n][1] > dp[n][0]){
    ak = 1;
  }
  debug(dp[n][ak]);
  vector<int> xx;
  for(int i = n - 1; i>=0; i--){
    if(ak == 0 && dp[i+1][0] == dp[i][0] - t[i]){
      xx.PB(i - 2);
      xx.PB(i);
    }else{
      if(i > 1){
        if(t[i-2] >= t[i-1] && dp[i+1][1] == dp[i-2][1] - t[i-2] + t[i] + t[i-1]){
          xx.PB(i-2);
          i-=2;
          ak = !ak;
        }else if(t[i] >= t[i-1] && dp[i+1][0] == dp[i-1][1] + t[i-1] - t[i]){
          xx.PB(i-1);
          i--;
        }
      }
      ak = !ak;
    }
  }
  for(int el:xx){
    rusz(el);
  }
  R(i,n-1){
    rusz(i);
  }
  cout << SZ(res) << "\n";
  for(int el:res){
    cout << el << "\n";
  }
  debug(RES);
}

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x;i <= y; ++ i)
#define repd(i,x,y) for(register int i = x;i >= y; -- i)
using namespace std;
typedef long long ll;
template<typename T>inline void read(T&x)
{
	char c;int sign = 1;x = 0;
	do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
	do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
	x *= sign;
}

const int N = 3e5 + 50;

int f[N],a[N],g[N],n;
int res[N],tot;

void work(int i)
{
	int x = min(a[i],a[i + 1]); if(!x) return;
	res[++ tot] = i; a[i] -= x; a[i + 1] -= x;
}

int main()
{
	read(n);
	rep(i,1,n) read(a[i]);
	rep(i,1,n)
	{
		f[i] = min(f[max(i-2,0)] + a[i],f[max(i-3,0)] + max(a[i],a[i-1]));
		if(f[i] == f[max(i-3,0)] + max(a[i],a[i-1])) g[i] = 1;
	}
	ll ans = min(f[n],f[n-1]);
	for(int i = n - (ans == f[n-1]);i > 0;i = i - 2 - g[i])
	{
		work(i - 1);
		if(g[i]) work(i - 2);
		work(i);
	}
	printf("%d\n",tot);
	rep(i,1,tot) printf("%d\n",res[i]);

	return 0;
}

// luogu-judger-enable-o2
#include<bits/stdc++.h>
//this code is written by Itst
using namespace std;

#define int long long
const int _ = 3e5 + 7;
int dp[_] , pre[_] , N , val[_];
vector < int > stp;

void move(int x){
	if(x == N) return;
	stp.push_back(x);
	int t = min(val[x] , val[x + 1]);
	val[x] -= t; val[x + 1] -= t;
}

signed main(){
	ios::sync_with_stdio(0); cin >> N;
	for(int i = 1 ; i <= N ; ++i) cin >> val[i];
	dp[1] = 0; dp[2] = val[2];
	for(int i = 3 ; i <= N + 1 ; ++i)
		if(dp[i - 3] + max(val[i - 1] , val[i - 2]) < dp[i - 2] + val[i - 1])
			dp[i] = dp[pre[i] = i - 3] + max(val[i - 1] , val[i - 2]);
		else dp[i] = dp[pre[i] = i - 2] + val[i - 1];
	for(int i = dp[N] < dp[N + 1] ? N : N + 1 ; i > 1 ; i = pre[i])
		if(pre[i] == i - 3){move(i - 2); move(i - 1); if(i > 3) move(i - 3);}
		else {move(i - 1); if(i > 2) move(i - 2);}
	cout << stp.size() << endl;
	for(int i = 0 ; i < stp.size() ; ++i) cout << stp[i] << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define N 300005

int a[N], dp[N], len[N], n;
int ord[N], m;

void oper(int k) {
    int val = min(a[k], a[k+1]);
    if (val) {
        ord[m++] = k;
        a[k] -= val, a[k+1] -= val;
    }
    return;
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i ++) {
        int f = (i > 1 ? dp[i-2] : 0) + a[i];
        int s = (i > 2 ? dp[i-3] : 0) + max(a[i], a[i-1]);
        dp[i] = min(f, s);
        len[i] = 2 - (dp[i] == f);
    }
    int ans = min(dp[n], dp[n-1]);
    vector <int> vec;
    for (int pt = n - (dp[n-1] == ans); pt > 0; pt -= len[pt] + 1) vec.push_back(pt);
    for (int i = 0; i < vec.size(); i ++) {
        int k = vec[i];
        if (len[k] == 1) {
			oper(k);
			if (a[k] > 0) oper(k-1);
        }
        else {
            oper(k-1);
            if (a[k]>0) oper(k);
            if (a[k-1]>0) oper(k-2);
        }
    }
    printf("%d\n", m);
    for (int i = 0; i < m; i ++) printf("%d\n", ord[i]);
    return 0;
}

# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) ((x)&1)
#define sqr(x) ((x)*(x))
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,a,b) for (int i=(a); i<=(b); ++i)
#define per(i,a,b) for (int i=(a); i>=(b); --i)
#define rep_it(it,x) for (__typeof((x).begin()) it=(x).begin(); it!=(x).end(); it++)
#define ____ puts("\n_______________\n\n")
#define debug(x) ____; cout<< #x << " => " << (x) << endl
#define debug_pair(x) cout<<"\n{ "<<(x).fir<<" , "<<(x).sec<<" }\n"
#define debug_arr(x,n) ____; cout<<#x<<":\n"; rep(i,0,n) cout<<#x<<"["<<(i)<<"] => "<<x[i]<<endl
#define debug_arr2(x,n,m) ____; cout<<#x<<":\n"; rep(i,0,n) rep(j,0,m) cout<<#x<<"["<<(i)<<"]["<<(j)<<"]= "<<x[i][j]<<((j==m)?"\n\n":"    ")
#define debug_set(x) ____; cout<<#x<<": \n"; rep_it(it,x) cout<<(*it)<<" "; cout<<endl
#define debug_map(x) ____; cout<<#x<<": \n"; rep_it(it,x) debug_pair(*it)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const ll ooo=9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N=301005;
const double PI=acos(-1.0);
const double eps=1e-9;
int o(int x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

int a[N],op[N],tmp[N],ans[N],n; ll dp[N];

void U(int k,int x,ll t=0) { if (dp[x]+t<dp[k]) dp[k]=dp[x]+t,op[k]=x; }

int main() {
    //file_put();

    scanf("%d",&n);
	rep(i,1,n) scanf("%d",&a[i]);
	rep(i,0,n) dp[i]=ooo;
	if (n==1) return 0*printf("%d\n",0); else dp[n]=a[n];
	per(i,n-1,0) {
		if (a[i]==0) { U(i,i+1),U(i,i+2); continue; }
		U(i,i+2,a[i]);
		if (a[i]>=a[i+1]) { U(i,i+3,a[i]); continue; }
		int tt=a[i+1]-a[i];
		if (i+2<=n) {
			if (i+3<=n) U(i,i+3,a[i+1]);
			if (i+4<=n && tt>=a[i+2]) U(i,i+4,a[i+1]);
		}
	}
	for (int t=op[0]; t && t<=n; t=op[t]) ans[++ans[0]]=t;
	per(i,ans[0],1) {
		if (a[ans[i]]==0) continue;
		if (i==ans[0] || ans[i+1]-ans[i]<=2 || a[ans[i]]>=a[ans[i]+1])
			if (a[ans[i]]<=a[ans[i]+1]) {
				a[ans[i]+1]-=a[ans[i]];
				a[ans[i]]=0;
				tmp[++tmp[0]]=ans[i];
			} else {
				if (a[ans[i]+1]) tmp[++tmp[0]]=ans[i];
				tmp[++tmp[0]]=ans[i]-1;
				a[ans[i]]-=a[ans[i]+1];
				a[ans[i]-1]-=a[ans[i]];
				a[ans[i]+1]=a[ans[i]]=0;
			}
		else {
			tmp[++tmp[0]]=ans[i];
			tmp[++tmp[0]]=ans[i]+1;
			a[ans[i]+1]-=a[ans[i]];
			a[ans[i]]=0;
			a[ans[i]+2]-=a[ans[i]+1];
			a[ans[i]+1]=0;
		}
	}
	//debug_arr(op,n+1);
	//debug_arr(dp,n+1);
	//debug(dp[0]);
	/*printf("%d\n",ans[0]);
	rep(i,1,ans[0]) printf("%d ",ans[i]);
	printf("\n");*/
	printf("%d\n",tmp[0]);
	rep(i,1,tmp[0]) printf("%d\n",tmp[i]);

    return 0;
}

#include <bits/stdc++.h>
#define NN 300100
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using INT = long long;
using pii = pair<int, int>;

int p[NN];
INT dp[NN][2];
int vst[NN];
vector<int> ans;
int N;
int val, flag = 1;
void calc(int n){
	if(dp[n][0] == val){
		if(n >= 2 && min(dp[n - 2][0], dp[n - 2][1]) == val - max(p[n], p[n - 1])){
			if(!vst[n - 1]) {
				int V = min(p[n - 1], p[n]);
				if(V) ans.push_back(n - 1);
				vst[n - 1] = 1;
				val -= V;
				if(!val) return ;
				p[n - 1] -= V; p[n] -= V;
			}
			if(n != N) {
				if(!vst[n]) {
					int V = min(p[n + 1], p[n]);
					if(V) ans.push_back(n);
					vst[n] = 1;
					val -= V;
					if(!val) return ;
					p[n + 1] -= V; p[n] -= V;
				}
			}
			if(!vst[n - 2]){
				int V = min(p[n - 2], p[n - 1]);
				if(V) ans.push_back(n - 2);
				vst[n - 2] = 1;
				val -= V;
				p[n - 2] -= V; p[n - 1] -= V;
			}
			flag = 2;
			return ;
		}
		if(n > 1) {
			if(!vst[n - 1]) {
				int V = min(p[n - 1], p[n]);
				if(V) ans.push_back(n - 1);
				vst[n - 1] = 1;
				val -= V;
				if(!val) return ;
				p[n - 1] -= V; p[n] -= V;
			}
		}
		if(n != N) {
			if(!vst[n]){
				int V = min(p[n + 1], p[n]);
				if(V)ans.push_back(n);
				vst[n] = 1;
				val -= V;
				p[n + 1] -= V; p[n] -= V;
			}
		}
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	ios_base :: sync_with_stdio(0);cin.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> p[i];
	for(int i = 1; i <= n; i++) for(int j = 0; j < 2; j++) dp[i][j] = inf;
	dp[1][0] = p[1], dp[1][1] = 0;
	for(int i = 2; i <= n; i++){
		dp[i][0] = min(dp[i - 1][1], dp[i - 1][0]) + p[i];
		if(i >= 2) dp[i][0] = min(dp[i][0], min(dp[i - 2][0], dp[i - 2][1]) + max(p[i], p[i - 1]));
		dp[i][1] = dp[i - 1][0];
	}
	val = min(dp[n][1], dp[n][0]);
//	for(int i = 1; i <= n; i++) cout << dp[i][0]<<" "<<dp[i][1]<<endl;
	N = n;
	while(val > 0) {
//		cout << val << " "<< n <<endl;
		calc(n);
		n -= flag;
		flag = 1;
	}
	cout << ans.size() <<endl;
	for(int i = 0; i < ans.size(); i++) cout << ans[i] << endl;
	cout << endl;
	return 0;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>

#define pb push_back
#define mp make_pair

using namespace std;

template<typename T>inline void upmin(T &x,T y) { y<x?x=y:0; }
template<typename T>inline void upmax(T &x,T y) { x<y?x=y:0; }

typedef unsigned int u32;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod pi=acos(-1);
const int oo=1<<30;
const LL OO=1e18;

const int N=3e5+100;

int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}

LL f[N];bool fr[N];
int p[N];
//Ϊ��������γ��Ȳ�����2
VI ans;
inline void work(int k) {
	if (p[k]&&p[k+1]) {
		int t=min(p[k],p[k+1]);
		ans.pb(k);
		p[k]-=t;
		p[k+1]-=t;
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	int n=gi(),i;LL a,b;
	for (i=1;i<=n;i++) {
		p[i]=gi();
		a=f[max(i-2,0)]+p[i];
		b=f[max(i-3,0)]+max(p[i-1],p[i]);
		f[i]=min(a,b);
		fr[i]=f[i]==b;
	}
	for (i=n;i>0;i-=2+fr[i]) {
		work(i-1);
		work(i);
		if (fr[i]) work(i-2);
	}
	cout<<ans.size()<<endl;
	for (int t:ans)
		printf("%d\n",t);
	return 0;
}

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <cstring>
#include <string>
#include <queue>
#define fi first
#define se second
#define pb push_back

using namespace std;

typedef long long ll;

typedef pair<int,int> PII;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}

inline void read(int &x){
  char c=nc(); int f=1; x=0;
  for(;c>'9'||c<'0';c=nc())f=(c=='-'?-1:1);for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc()); x*=f;
}

inline int read(char *x,char L='a',char R='z'){
  int len=0; char c=nc();
  for(;c>R||c<L;c=nc());for(;c>=L&&c<=R;x[++len]=c,c=nc()); x[len+1]=0; return len;
}

const int N=300010;
const int P=1;

int n,a[N];
ll f[N],frm[N];

vector<int> ans;

int p[N];

inline void push(int x){
  if(min(a[x],a[x+1])==0) return ;
  ans.pb(x);
  a[x]-=min(a[x],a[x+1]);
  a[x+1]-=min(a[x],a[x+1]);
}

int main(){
  read(n);
  for(int i=1;i<=n;i++) read(a[i]);
  f[1]=a[1];
  f[2]=min(a[2],max(a[1],a[2]));
  for(int i=3;i<=n;i++){
    if(f[i-2]+a[i]<f[i-3]+max(a[i-1],a[i]))
      f[i]=f[i-2]+a[i],frm[i]=i-2;
    else
      f[i]=f[i-3]+max(a[i],a[i-1]),frm[i]=i-3;
  }
  for(int i=f[n]>f[n-1]?n-1:n;i;i=frm[i]){
    p[++*p]=i;
  }
  for(int i=*p;i;i--){
    int cur=p[i],lst=p[i+1];
    if(lst+2!=cur) push(cur-1);
    push(lst+1); push(cur);
  }
  printf("%d\n",(int)ans.size());
  for(int i=0;i<ans.size();i++) printf("%d\n",ans[i]);
  return 0;
}

#include <limits>
#include <algorithm>
#include <iostream>
#include <vector>

enum class BackwardOption {ONE_SEQUENCE, TWO_SEQUENCE};

struct State {
    int min_steps = std::numeric_limits<int>::max();
    BackwardOption backward_option;

    bool operator<(const State& other) const {
        return min_steps < other.min_steps;
    }
};

void PerformStep(std::vector<int>& steps, std::vector<int>& p, const int index) {
    if (p[index] > 0 && p[index + 1] > 0) {
        steps.push_back(index);
        int min_number = std::min(p[index], p[index + 1]);
        p[index] -= min_number;
        p[index + 1] -= min_number;
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    int n;
    std::cin >> n;
    std::vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> p[i];
    }
    std::vector<State> min_steps(n + 1);
    min_steps[0].min_steps = 0;
    min_steps[1].min_steps = 0;
    for (int i = 0; i < n; ++i) {
        if (i >= 1 && i - 1 != 1) {
            min_steps[i + 1] = std::min(min_steps[i + 1], {min_steps[i - 1].min_steps + p[i], BackwardOption::ONE_SEQUENCE});
        }
        if (i >= 2 && i - 2 != 1) {
            min_steps[i + 1] = std::min(min_steps[i + 1], {min_steps[i - 2].min_steps + std::max(p[i], p[i - 1]),  BackwardOption::TWO_SEQUENCE});
        }
    }
    int last_length;
    if (min_steps[n - 1] < min_steps[n]) {
        last_length = n - 1;
    } else {
        last_length = n;
    }
    std::vector<int> steps;
    while (last_length > 1) {
        if (min_steps[last_length].backward_option == BackwardOption::ONE_SEQUENCE) {
            PerformStep(steps, p, last_length - 2);
            PerformStep(steps, p, last_length - 1);
            last_length -= 2;
        } else {
            PerformStep(steps, p, last_length - 2);
            PerformStep(steps, p, last_length - 3);
            PerformStep(steps, p, last_length - 1);
            last_length -= 3;
        }
    }
    std::cout << steps.size() << std::endl;
    for (auto index: steps) {
        std::cout << index + 1 << std::endl;
    }
    return 0;
}

/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
template<class T, class... U> void DEBUG(const T &head, const U &... tail) {
#ifdef HOME
    _W('#'); _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...);
#endif
}
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int n;
int p[SIZE];
LL dp[SIZE];
VI ans;
void go(int x){
    if(!p[x])return;
    if(p[x]<=p[x-1]){
        p[x-1]-=p[x];
        p[x]=0;
        ans.PB(x-1);
    }
    else{
        p[x+1]-=p[x];
        p[x]=0;
        ans.PB(x);
    }
}
void go(int x,int y){
    int mi=min(p[x],p[y]);
    if(mi){
        ans.PB(x);
        p[x]-=mi; p[y]-=mi;
    }
    if(p[x]&&p[x-1]){
        ans.PB(x-1);
        p[x-1]-=p[x];
        p[x]=0;
    }
    if(p[y]&&p[y+1]){
        ans.PB(y-1);
        p[y+1]-=p[y];
        p[y]=0;
    }
}
int main(){
    R(n);
    if(n==1){
        W(0);
        return 0;
    }
    FOR(i,1,n){
        R(p[i]);
    }
    dp[2]=p[1];
    FOR(i,3,n+1){
        dp[i]=dp[i-2]+p[i-1];
        dp[i]=min(dp[i],dp[i-3]+max(p[i-1],p[i-2]));
    }
    int now=n+1;
    while(now>1){
        if(dp[now]==dp[now-2]+p[now-1]){
            go(now-1);
            now-=2;
        }
        else{
            go(now-2,now-1);
            now-=3;
        }
    }
    W(SZ(ans));
    W(ans);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 3e5+5;
int n, a[N];
LL d[N];
bool odd[N];
vector<int> op;
void operate(int x) {
    if (x <= 0 || x > n) return;
    int mi = min(a[x], a[x+1]);
    if (!mi) return;
    op.push_back(x);
    a[x] -= mi, a[x+1] -= mi;
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
        LL fo = d[max(i-2, 0)] + a[i];
        LL fe = d[max(i-3, 0)] + max(a[i], a[i-1]);
        d[i] = min(fo, fe);
        if (d[i] == fo) odd[i] = 1;
    }
    LL ans = min(d[n], d[n-1]);
    for (int i = n-(ans==d[n-1]); i >= 1;) {
        if (odd[i]) {
            operate(i);
            operate(i-1);
            i -= 2;
        } else {
            operate(i-1);
            operate(i);
            operate(i-2);
            i -= 3;
        }
    }
    printf("%lld\n", ans);
    for (auto x: op) printf("%d\n", x);
}

#include<bits/stdc++.h>
#define NN 300100
#define inf 100000000000000000

using namespace std;
using INT=long long;
using pii=pair<int, int>;

int a[NN], pre[NN][2], arr[NN];
INT dp[NN][2];
vector<int>vec;

void solve(int nn) {
	dp[1][1] = 0;
	dp[1][0] = inf;
	for(int i=2; i<=nn; i++) {
		dp[i][1] = dp[i-1][0];
		dp[i][0] = inf;
		if(arr[i]<=arr[i-1]){
			dp[i][0] = min(dp[i][0], arr[i] + dp[i-2][0]);
			dp[i][0] = min(dp[i][0], max(arr[i], arr[i-1]) + min(dp[i-2][0], dp[i-2][1]));
		}else dp[i][1] = min(dp[i][1], arr[i-1] + min(dp[i-2][0], dp[i-2][1]));
	}
	INT ans = min(dp[nn][0], dp[nn][1]);
	int op=0;
	if(ans == dp[nn][1]) op=1;
	int pre=nn;
	while(pre>1) {
		if(op==1) {
			if(arr[pre]>arr[pre-1] && dp[pre][op] == arr[pre-1] + min(dp[pre-2][0], dp[pre-2][1])) {
				vec.push_back(pre-1);
				pre -= 2;
				if(dp[pre][0] < dp[pre][1]) op = 0;
				continue;
			}
			pre--;
			op^=1;
			continue;
		}
		if(arr[pre]<=arr[pre-1] && dp[pre][op] == arr[pre] + dp[pre-2][0]) {
			vec.push_back(pre-1);
			pre-=2;
			op = 0;
			continue;
		}
		vec.push_back(pre - 1);
		if(pre > 2) vec.push_back(pre - 2);
		pre -= 2;
		if(dp[pre][1]<dp[pre][0]) op = 1;
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	cin>>n;
	for(int i=1; i<=n; i++) scanf("%d", &a[i]);
	if(n==1) {
		puts("0");
		return 0;
	}
	int st=0;
	for(int i=1; i<=n; i++) {
		if(a[i]) {
			st=i;
			break;
		}
	}
	if(st==0) {
		puts("0");
		return 0;
	}
	while(st <= n) {
		int nn=0;
		for(int i=st; i<=n; i++) {
			if(a[i]==0) break;
			arr[++nn]=a[i];
		}
		if(nn>1)solve(nn);
		int id=0;
		for(int i=st+nn; i<=n; i++) {
			if(a[i]) {
				id=i;
				break;
			}
		}
		if(id==0) break;
		st=id;
	}
	cout<<vec.size()<<endl;
	for(int v : vec) printf("%d\n", v);

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,a) for(int i=start[(a)];i;i=e[i].next)
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
inline int read()
{
    int sum=0,p=1;char ch=getchar();
    while(!(('0'<=ch && ch<='9') || ch=='-'))ch=getchar();
    if(ch=='-')p=-1,ch=getchar();
    while('0'<=ch && ch<='9')sum=sum*10+ch-48,ch=getchar();
    return sum*p;
}

const int maxn=3e5+20;

int n,a[maxn];

inline void init()
{
    n=read();REP(i,1,n)a[i]=read();
}

ll dp[maxn];int pre[maxn];
int val[maxn],na[maxn];
vector<int>ans,nans;

inline void work(int x){ int t=min(na[x],na[x+1]);if(t)nans.push_back(x),na[x]-=t,na[x+1]-=t;}

inline void doing()
{
    dp[0]=0;dp[1]=a[1];pre[1]=1; dp[2]=a[2]; pre[2]=1;
    REP(i,3,n)
    {
	int A=dp[i-3]+max(a[i],a[i-1]),B=dp[i-2]+a[i];
	if(A<B)dp[i]=A,pre[i]=2;
	else dp[i]=B,pre[i]=1;
    }
    int now=dp[n-1]<dp[n]?n-1:n;

    REP(i,1,n)na[i]=a[i];
    while(now>0)
    {
	int t=pre[now];
	if(t==2)
	{
	    if(na[now-1]>na[now])work(now-1),work(now-2);
	    else work(now-1),work(now);
	}else
	{
	    work(now); work(now-1);
	}
	now-=t+1;
    }
    printf("%d\n",SZ(nans));
    for(int x:nans)printf("%d\n",x);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("E.in","r",stdin);
    freopen("E.out","w",stdout);
#endif
    init();
    doing();
    return 0;
}

#include <limits>
#include <algorithm>
#include <iostream>
#include <vector>

enum class BackwardOption {ONE_SEQUENCE, TWO_SEQUENCE};

struct State {
    int min_steps = std::numeric_limits<int>::max();
    BackwardOption backward_option;

    bool operator<(const State& other) const {
        return min_steps < other.min_steps;
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    int n;
    std::cin >> n;
    std::vector<int> p(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> p[i];
    }
    std::vector<State> min_steps(n + 1);
    min_steps[0].min_steps = 0;
    min_steps[1].min_steps = 3 * n;
    for (int i = 0; i < n; ++i) {
        if (i >= 1) {
            min_steps[i + 1] = std::min(min_steps[i + 1], {min_steps[i - 1].min_steps + p[i], BackwardOption::ONE_SEQUENCE});
        }
        if (i >= 2) {
            min_steps[i + 1] = std::min(min_steps[i + 1], {min_steps[i - 2].min_steps + std::max(p[i], p[i - 1]),  BackwardOption::TWO_SEQUENCE});
        }
    }
    int last_length;
    if (min_steps[n - 1] < min_steps[n]) {
        last_length = n - 1;
    } else {
        last_length = n;
    }
    std::vector<int> steps;
    while (last_length > 1) {
        if (min_steps[last_length].backward_option == BackwardOption::ONE_SEQUENCE) {
            steps.push_back(last_length - 2);
            last_length -= 2;
        } else {
            steps.push_back(last_length - 2);
            steps.push_back(last_length - 3);
            last_length -= 3;
        }
    }
    std::cout << steps.size() << std::endl;
    for (auto index: steps) {
        std::cout << index + 1 << std::endl;
    }
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long

#define maxN 300011

int n, i, pos;
ll p[maxN], s[maxN];

pair<ll, ll> dp[maxN];
pair<ll, ll> from_here[maxN], until_here[maxN];
vector<int> moves;

void upd(pair<ll, ll>& a, pair<ll, ll> b) {
    a = min(a, b);
}

ll execute(ll& a, ll& b) {
    ll aux = min(a, b);
    a -= aux;
    b -= aux;
    return 2LL * aux;
}

ll magic3(ll a, ll b, ll c) {
    ll ans = 0;
    ans += execute(b, c);
    ans += 2 * b + c;
    return ans;
}

pair<ll, ll> get_from_here(int pos) {
    return mp(dp[pos].first + magic3(p[pos], p[pos + 1], p[pos + 2]) - s[pos + 2], pos);
}

ll case_one(ll a, ll b, ll c) {
    ll ans = 0;
    ans += 2LL * b;
    //ans += execute(a, b);
    //ans += execute(b, c);
    return ans;
}

ll case_two(ll a, ll b, ll c, ll d) {
    ll ans = 0;
    ans += execute(b, c);
    //ans += execute(a, b);
    //ans += execute(c, d);
    ans += 2LL * (b + c);
    return ans;
}

ll case_three_one(ll a, ll b, ll c, ll d, ll e) {
    ll ans = 0;
    ans += execute(b, c);
    ans += execute(c, d);
    //ans += execute(a, b);
    //ans += execute(d, e);
    ans += 2LL * (b + d);
    return ans;
}

ll case_three_two(ll a, ll b, ll c, ll d, ll e) {
    ll ans = 0;
    ans += execute(c, d);
    ans += execute(b, c);
    //ans += execute(a, b);
    //ans += execute(d, e);
    ans += 2LL * (b + d);
    return ans;
}

ll case_three(ll a, ll b, ll c, ll d, ll e) {
    return min(case_three_one(a, b, c, d, e),
               case_three_two(a, b, c, d, e));
}

void get_moves(int l, int r) {
    int i, j;

    if (l + 1 >= r) return;

    if (r - l - 1 == 3) {
        if (case_three_two(p[l], p[l + 1], p[l + 2], p[l + 3], p[l + 4]) <
            case_three_one(p[l], p[l + 1], p[l + 2], p[l + 3], p[l + 4]) ) {

            if (p[l + 2] > 0 && p[l + 3] > 0) {
                execute(p[l + 2], p[l + 3]);
                moves.pb(l + 2);
            }

        }
    }

    i = l + 1; j = r - 1;
    while (i < j) {
        if (p[i] > 0 && p[i + 1] > 0) {
            execute(p[i], p[i + 1]);
            moves.pb(i);
        }

        if (p[j] > 0 && p[j - 1] > 0) {
            execute(p[j], p[j - 1]);
            moves.pb(j - 1);
        }

        i++; j--;
    }

    if (p[l] > 0 && p[l + 1] > 0) {
        execute(p[l], p[l + 1]);
        moves.pb(l);
    }

    if (p[r] > 0 && p[r - 1] > 0) {
        execute(p[r - 1], p[r]);
        moves.pb(r - 1);
    }
}

int main()
{
    //freopen("test.in","r",stdin);

    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        scanf("%lld", &p[i]);
        s[i] = s[i - 1] + p[i];
    }

    //! add position 0
    dp[0] = mp(0, 0);
    from_here[0] = until_here[0] = get_from_here(0);

    //! add position 1
    dp[0] = mp(0, 0);
    from_here[0] = get_from_here(1);
    until_here[1] = min(until_here[0], from_here[1]);

    for (i = 2; i <= n + 1; i++) {
        //! size 1
        dp[i] = mp(dp[i - 2].first + case_one(p[i - 2], p[i - 1], p[i]), i - 2);

        //! size 2
        if (i - 3 >= 0) {
            upd(dp[i], mp(dp[i - 3].first + case_two(p[i - 3], p[i - 2], p[i - 1], p[i]), i - 3));
        }

        //! size 3
        if (i - 4 >= 0) {
            upd(dp[i], mp(dp[i - 4].first + case_three(p[i - 4], p[i - 3], p[i - 2], p[i - 1], p[i]), i - 4));
        }

        //! size >= 4
        if (i - 5 >= 0) {
            upd(dp[i], mp( until_here[i - 4].first + s[i - 3] + magic3(p[i - 2], p[i - 1], p[i]), until_here[i - 4].second));
        }

        from_here[i] = get_from_here(i);
        until_here[i] = max(until_here[i - 1], from_here[i]);
    }

    pair<ll, ll> best;
    best = min(mp(dp[n].first, n), mp(dp[n + 1].first, n + 1));
    pos = best.second;

    vector<int> sol = {};
    while (pos != 0) {
        sol.pb(pos);
        pos = dp[pos].second;
    }
    sol.pb(0);

    reverse(sol.begin(), sol.end());
    for (i = 0; i + 1 < sol.size(); i++)
        get_moves(sol[i], sol[i + 1]);

    printf("%d\n", moves.size());
    for (auto e : moves) printf("%d\n", e);

    cerr << "\n\n Total cost " << best.first << ' ' << best.first / 2 << '\n';

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define N 303030

int n, p[N];
long long dp[N];

bool bad[N];

vector <int> v;

long long ans;

void doing(int k) {
	int val = min(p[k], p[k+1]);
	if (val) v.push_back(k);
	p[k] -= val, p[k+1] -= val;
	ans += val;
}

void oper(int k, int tag) {
	if (tag == 1) doing(k), doing(k - 1);
	else doing(k), doing(k - 1), doing(k + 1);
}

int tar;

int main() {
	//freopen("r.in", "r", stdin);
	tar = (int)clock();
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) scanf("%d", &p[i]);
	dp[1] = p[1];
	dp[2] = max(p[1], p[2]);
	for (int i = 3; i <= n + 1; i ++) {
		dp[i] = min(dp[i-2] + p[i], dp[i-3] + max(p[i], p[i-1]));
	}
	int k;
	if (dp[n] < dp[n+1]) k = n;
	else k = n + 1;
	bad[k+1] = 1;
	while (k > 0) {
		if (k > 2 && dp[k] == dp[k-2] + p[k]) bad[k-1] = 1, k = k - 2;
		else bad[k-2] = 1, k = k - 3;
	}
	for (int i = 1; i <= n; i ++) {
		if (!bad[i]) {
			int j = i;
			while (!bad[j]) j ++;
			oper(i, j - i);
			i = j;
		}
	}
	while (clock() - tar <= 1900);
	printf("%d\n", (int)v.size());
	for (int i = 0; i < v.size(); i ++) printf("%d\n", v[i]);

	return 0;
}

#pragma comment(linker,"/stack:200000000")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
using INT=long long;
using pii=pair<INT,int>;
const int NN=372727;
pii dp[NN][2];
int a[NN],val[NN];
INT ans;
vector <int> ANS;
void sol(int k){
	int mn=min(a[k],a[k+1]);
	ans+=mn;ANS.push_back(k);
	a[k]-=mn;a[k+1]-=mn;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	int fl=0;
	for(int i=1;i<=n;i++){
		if(a[i]==0) continue;
		if(a[i]>=a[i+1]){
			int j=i;
			int N=1;
			while(a[i]>=a[i+1] and i<n and a[i+1]) N++,i++;
			if(N&1){
				for(int k=i-1;k>=j;k--) sol(k);
				fl=0;
			}
			else {
				if(N==2){
					if(i==n){
						sol(n-1);
						break;
					}
					else i--;
				}
				else {
					i--;
					for(int k=i-1;k>=j;k--) sol(k);
				}
			}
		}
		else {
			int j=i;
			int N=1;
			while(a[i]<=a[i+1] and i<n) N++,i++;
			for(int k=j;k<i;k++) sol(k);
			i--;
			fl=0;
		}
	}
	cout<<ANS.size()<<endl;
	for(int v:ANS) printf("%d\n",v);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using INT=long long;
const int NN=372727;
int a[NN],val[NN],b[NN];
INT dp[NN][2];
vector <int> ANS;
void sol(int k){
	int mn=min(a[k],a[k+1]);
	if(mn==0) return;
	ANS.push_back(k);
	a[k]-=mn;a[k+1]-=mn;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i),b[i]=a[i];
	dp[0][0]=dp[0][1]=0;
	for(int i=1;i<=n;i++){
		dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i];
		if(i>1) dp[i][0]=min(dp[i][0],min(dp[i-2][1],dp[i-2][1])+max(a[i],a[i-1]));

		dp[i][1]=dp[i-1][0];
	}
	cout<<min(dp[n][0],dp[n][1])<<endl;
	int st=n,fl=0;
	if(dp[n][1]<=dp[n][0]) fl=1;
	while(st){
		if(fl){
			st--;
			fl=0;
		}
		else {
			if(dp[st][0]==dp[st-1][0]+b[st]) sol(st-1),sol(st),st--;
			else if(dp[st][0]==dp[st-1][1]+b[st]) sol(st-1),sol(st),st--,fl=1;
			else if(dp[st][0]==dp[st-2][0]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=0;
			else if(dp[st][0]==dp[st-2][1]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=1;
		}
	}
	for(int v:ANS) printf("%d\n",v);
	return 0;
}

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int gi(){
	int x=0,w=1;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')w=0,ch=getchar();
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return w?x:-x;
}
#define ll long long
const int N=3e5+5;
int n,a[N],mrk[N],ans[N],m;ll dp[N];
void work(int x){
	if(a[x]&&a[x+1]){
		int d=min(a[x],a[x+1]);ans[++m]=x;
		a[x]-=d;a[x+1]-=d;
	}
}
int main(){
	n=gi()+1;
	for(int i=1;i<=n;++i){
		if(i<n)a[i]=gi();
		ll x=dp[max(i-3,0)]+max(a[i],a[i-1]);
		ll y=dp[max(i-2,0)]+a[i];
		dp[i]=min(x,y);if(dp[i]==x)mrk[i]=1;
	}
	for(int i=n;i;--i){
		work(i-1);
		if(mrk[i])work(i-2);
	}
	printf("%d\n",m);
	for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
	return 0;
}

#pragma comment(linker,"/stack:200000000")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
using INT=long long;
using pii=pair<INT,int>;
const int NN=372727;
pii dp[NN][2];
int a[NN],val[NN];
INT ans;
vector <int> ANS;
void sol(int k){
	int mn=min(a[k],a[k+1]);
	ans+=mn;ANS.push_back(k);
	a[k]-=mn;a[k+1]-=mn;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	int fl=0;
	for(int i=1;i<=n;i++){
		if(a[i]==0) continue;
		if(a[i]>=a[i+1]){
			int j=i;
			int N=1;
			while(a[i]>=a[i+1] and i<n and a[i+1]) N++,i++;
			if(N&1){
				for(int k=i-1;k>=j;k--) sol(k);
			}
			else {
				if(N==2){
					if(i==n){
						sol(n-1);
						break;
					}
					else i--;
				}
				else {
					i--;
					for(int k=i-1;k>=j;k--) sol(k);
				}
			}
		}
		else {
			int j=i;
			int N=1;
			while(a[i]<=a[i+1] and i<n) N++,i++;
			for(int k=i-1;k>=j;k-=2) sol(k);
			i--;
		}
	}
	cout<<ans<<endl;
	for(int v:ANS) printf("%d\n",v);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define N 303030

int n, p[N];
long long dp[N];

bool bad[N];

vector <int> v;

long long ans;

void doing(int k) {
	int val = min(p[k], p[k+1]);
	if (val) v.push_back(k);
	p[k] -= val, p[k+1] -= val;
	ans += val;
}

void oper(int k, int tag) {
	if (tag == 1) doing(k), doing(k + 1);
	else doing(k), doing(k + 1), doing(k - 1);
}

int tar;

int main() {
	//freopen("r.in", "r", stdin);
	tar = (int)clock();
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) scanf("%d", &p[i]);
	dp[1] = p[1];
	dp[2] = max(p[1], p[2]);
	for (int i = 3; i <= n + 1; i ++) {
		dp[i] = min(dp[i-2] + p[i], dp[i-3] + max(p[i], p[i-1]));
	}
	int k;
	if (dp[n] < dp[n+1]) k = n;
	else k = n + 1;
	bad[k+1] = 1;
	while (k > 0) {
		if (dp[k] == dp[k-2] + p[k]) bad[k-1] = 1, k = k - 2;
		else bad[k-2] = 1, k = k - 3;
	}
	for (int i = 1; i <= n; i ++) {
		if (!bad[i]) {
			int j = i;
			while (!bad[j]) j ++;
			oper(i, j - i);
			i = j;
		}
	}
	//while (clock() - tar <= 1800);
	printf("%d\n", (int)v.size());
	for (int i = 0; i < v.size(); i ++) printf("%d\n", v[i]);

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using INT=long long;
const int NN=372727;
int a[NN],val[NN],b[NN];
INT dp[NN][2];
vector <int> ANS;
void sol(int k){
	int mn=min(a[k],a[k+1]);
	if(mn==0) return;
	ANS.push_back(k);
	a[k]-=mn;a[k+1]-=mn;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i),b[i]=a[i];
	dp[0][0]=dp[0][1]=0;
	for(int i=1;i<=n;i++){
		dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i];
//		if(i>1) dp[i][0]=min(dp[i][0],min(dp[i-2][1],dp[i-2][1])+max(a[i],a[i-1]));

		dp[i][1]=dp[i-1][0];
	}
	int st=n,fl=0;
	if(dp[n][1]<=dp[n][0]) fl=1;
	while(st){
		if(fl){
			st--;
			fl=0;
		}
		else {
			if(dp[st][0]==dp[st-1][0]+b[st]) sol(st-1),sol(st),st--;
			else if(dp[st][0]==dp[st-1][1]+b[st]) sol(st),sol(st-1),st--,fl=1;
//			else if(dp[st][0]==dp[st-2][0]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=0;
//			else if(dp[st][0]==dp[st-2][1]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=1;
		}
	}
	cout<<ANS.size()<<endl;
	for(int v:ANS) printf("%d\n",v);
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long

#define maxN 300011

int n, i, pos;
ll p[maxN], s[maxN];

pair<ll, ll> dp[maxN];
pair<ll, ll> from_here[maxN], until_here[maxN];
vector<int> moves;

void upd(pair<ll, ll>& a, pair<ll, ll> b) {
    a = min(a, b);
}

ll execute(ll& a, ll& b) {
    ll aux = min(a, b);
    a -= aux;
    b -= aux;
    return 2LL * aux;
}

ll magic3(ll a, ll b, ll c) {
    ll ans = 0;
    ans += execute(b, c);
    ans += 2 * b + c;
    return ans;
}

pair<ll, ll> get_from_here(int pos) {
    return mp(dp[pos].first + magic3(p[pos], p[pos + 1], p[pos + 2]) - s[pos + 2], pos);
}

ll case_one(ll a, ll b, ll c) {
    ll ans = 0;
    ans += 2LL * b;
    //ans += execute(a, b);
    //ans += execute(b, c);
    return ans;
}

ll case_two(ll a, ll b, ll c, ll d) {
    ll ans = 0;
    ans += execute(b, c);
    //ans += execute(a, b);
    //ans += execute(c, d);
    ans += 2LL * (b + c);
    return ans;
}

ll case_three_one(ll a, ll b, ll c, ll d, ll e) {
    ll ans = 0;
    ans += execute(b, c);
    ans += execute(c, d);
    //ans += execute(a, b);
    //ans += execute(d, e);
    ans += 2LL * (b + d);
    return ans;
}

ll case_three_two(ll a, ll b, ll c, ll d, ll e) {
    ll ans = 0;
    ans += execute(c, d);
    ans += execute(b, c);
    //ans += execute(a, b);
    //ans += execute(d, e);
    ans += 2LL * (b + d);
    return ans;
}

ll case_three(ll a, ll b, ll c, ll d, ll e) {
    return min(case_three_one(a, b, c, d, e),
               case_three_two(a, b, c, d, e));
}

void get_moves(int l, int r) {
    int i, j;

    if (l + 1 >= r) return;

    if (r - l - 1 == 3) {
        if (case_three_two(p[l], p[l + 1], p[l + 2], p[l + 3], p[l + 4]) <
            case_three_one(p[l], p[l + 1], p[l + 2], p[l + 3], p[l + 4]) ) {

            if (p[l + 2] > 0 && p[l + 3] > 0) {
                execute(p[l + 2], p[l + 3]);
                moves.pb(l + 2);
            }

        }
    }

    i = l + 1; j = r - 1;
    while (i < j) {
        if (p[i] > 0 && p[i + 1] > 0) {
            execute(p[i], p[i + 1]);
            moves.pb(i);
        }

        if (p[j] > 0 && p[j - 1] > 0) {
            execute(p[j], p[j - 1]);
            moves.pb(j - 1);
        }

        i++; j--;
    }

    if (p[l] > 0 && p[l + 1] > 0) {
        execute(p[l], p[l + 1]);
        moves.pb(l);
    }

    if (p[r] > 0 && p[r - 1] > 0) {
        execute(p[r - 1], p[r]);
        moves.pb(r - 1);
    }
}

int main()
{
   // freopen("test.in","r",stdin);

    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        scanf("%lld", &p[i]);
        s[i] = s[i - 1] + p[i];
    }

    //! add position 0
    dp[0] = mp(0, 0);
    from_here[0] = until_here[0] = get_from_here(0);

    //! add position 1
    dp[0] = mp(0, 0);
    from_here[0] = get_from_here(1);
    until_here[1] = min(until_here[0], from_here[1]);

    for (i = 2; i <= n + 1; i++) {
        //! size 1
        dp[i] = mp(dp[i - 2].first + case_one(p[i - 2], p[i - 1], p[i]), i - 2);

        //! size 2
        if (i - 3 >= 0) {
            upd(dp[i], mp(dp[i - 3].first + case_two(p[i - 3], p[i - 2], p[i - 1], p[i]), i - 3));
        }

        //! size 3
        if (i - 4 >= 0) {
            upd(dp[i], mp(dp[i - 4].first + case_three(p[i - 4], p[i - 3], p[i - 2], p[i - 1], p[i]), i - 4));
        }

        //! size >= 4
        if (i - 5 >= 0) {
            upd(dp[i], mp( until_here[i - 4].first + s[i - 3] + magic3(p[i - 2], p[i - 1], p[i]), until_here[i - 4].second));
        }

        from_here[i] = get_from_here(i);
        until_here[i] = min(until_here[i - 1], from_here[i]);
    }

    pair<ll, ll> best;
    best = min(mp(dp[n].first, n), mp(dp[n + 1].first, n + 1));
    pos = best.second;

    vector<int> sol = {};
    while (pos != 0) {
        sol.pb(pos);
        pos = dp[pos].second;
    }
    sol.pb(0);

    reverse(sol.begin(), sol.end());
    for (i = 0; i + 1 < sol.size(); i++)
        get_moves(sol[i], sol[i + 1]);

    printf("%d\n", moves.size());
    for (auto e : moves) printf("%d\n", e);

    cerr << "\n\n Total cost " << best.first << ' ' << best.first / 2 << '\n';

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define N 303030

int n, p[N];
long long dp[N];

bool bad[N];

vector <int> v;

long long ans;

void doing(int k) {
	int val = min(p[k], p[k+1]);
	if (val) v.push_back(k);
	p[k] -= val, p[k+1] -= val;
	ans += val;
}

void oper(int k, int tag) {
	if (tag == 1) doing(k), doing(k - 1);
	else doing(k), doing(k - 1), doing(k + 1);
}

int tar;

int main() {
	//freopen("r.in", "r", stdin);
	tar = (int)clock();
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) scanf("%d", &p[i]);
	dp[1] = p[1];
	dp[2] = max(p[1], p[2]);
	for (int i = 3; i <= n + 1; i ++) {
		dp[i] = min(dp[i-2] + p[i], dp[i-3] + max(p[i], p[i-1]));
	}
	int k;
	if (dp[n] < dp[n+1]) k = n;
	else k = n + 1;
	bad[k+1] = 1;
	while (k > 0) {
		if (k == 1 || dp[k] == dp[k-2] + p[k]) bad[k-1] = 1, k = k - 2;
		else bad[k-2] = 1, k = k - 3;
	}
	for (int i = 1; i <= n; i ++) {
		if (!bad[i]) {
			int j = i;
			while (!bad[j]) j ++;
			oper(i, j - i);
			i = j;
		}
	}
//	while (clock() - tar <= 1900);
	printf("%d\n", (int)v.size());
	for (int i = 0; i < v.size(); i ++) printf("%d\n", v[i]);

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define N 303030

int n, p[N];
long long dp[N];

bool bad[N];

vector <int> v;

long long ans;

void doing(int k) {
	int val = min(p[k], p[k+1]);
	if (val) v.push_back(k);
	p[k] -= val, p[k+1] -= val;
	ans += val;
}

void oper(int k, int tag) {
	if (tag == 1) doing(k), doing(k - 1);
	else doing(k), doing(k - 1), doing(k + 1);
}

int tar;

int main() {
	//freopen("r.in", "r", stdin);
	tar = (int)clock();
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) scanf("%d", &p[i]);
	dp[1] = p[1];
	dp[2] = max(p[1], p[2]);
	for (int i = 3; i <= n + 1; i ++) {
		dp[i] = min(dp[i-2] + p[i], dp[i-3] + max(p[i], p[i-1]));
	}
	int k;
	if (dp[n] < dp[n+1]) k = n;
	else k = n + 1;
	bad[k+1] = 1;
	while (k > 0) {
		if (k > 2 && dp[k] == dp[k-2] + p[k]) bad[k-1] = 1, k = k - 2;
		else bad[k-2] = 1, k = k - 3;
	}
	for (int i = 1; i <= n; i ++) {
		if (!bad[i]) {
			int j = i;
			while (!bad[j]) j ++;
			oper(i, j - i);
			i = j;
		}
	}
	//while (clock() - tar <= 1900);
	printf("%d\n", (int)v.size());
	for (int i = 0; i < v.size(); i ++) printf("%d\n", v[i]);

	return 0;
}

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const int nax=300*1007;

int n;
ll tab[nax];
ll dp[nax];

vector <int> wyn;

int main()
{
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
		scanf("%lld", &tab[i]);
	for (int i=n-1; i; i--)
		dp[i]=min(dp[i+2]+tab[i+1], dp[i+3]+max(tab[i+1], tab[i+2]));
	for (int i=1; i<n;)
	{
		if (dp[i+2]+tab[i+1]<dp[i+3]+max(tab[i+1], tab[i+2]))
		{
		    wyn.push_back(i+1);
		    wyn.push_back(i);
			i+=2;
		}
		else
		{
		    wyn.push_back(i+1);
		    wyn.push_back(i+2);
		    wyn.push_back(i);
			i+=3;
		}
	}
	auto pom=wyn;
	wyn.clear();
	for (int i : pom)
	{
		if (i<n && tab[i] && tab[i+1])
		{
			ll x=min(tab[i], tab[i+1]);
			tab[i]-=x;
			tab[i+1]-=x;
			wyn.push_back(i);
		}
	}
	printf("%d\n", (int)wyn.size());
	for (int i : wyn)
		printf("%d\n", i);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=300005;
int n,a[N],f[N],pre[N],q[N],top;
void down(int i){
	int t=min(a[i],a[i+1]);
	if(!t)return;
	q[++top]=i;
	a[i+1]-=t;a[i]-=t;
}
int main(){
	//freopen("aa.in","r",stdin);
	int i,j;
	scanf("%d",&n);
	for(i=1;i<=n;++i){
		scanf("%d",&a[i]);
	}
	for(i=1;i<=n;++i){
		int x=f[max(i-2,0)]+a[i];
		int y=f[max(i-3,0)]+max(a[i-1],a[i]);
		if(x<y){
			f[i]=x;
			pre[i]=2;
		}else{
			f[i]=y;
			pre[i]=3;
		}
	}
	int ans=min(f[n-1],f[n]);
	for(i=n-(ans==f[n-1]);i;i=max(0,i-pre[i])){
		if(pre[i]==3){
			down(i-1);
			down(i);
			down(i-2);
		}else{
			down(i);
			down(i-1);
		}
	}
	printf("%d\n",top);
	for(i=1;i<=top;++i)printf("%d\n",q[i]);
	return 0;
}

# include <iostream>
# include <sstream>
# include <cstdio>
# include <cstdlib>
# include <algorithm>
# include <string>
# include <cstring>
# include <cmath>
# include <stack>
# include <queue>
# include <vector>
# include <list>
# include <map>
# include <set>
# include <deque>
# include <iterator>
# include <functional>
# include <bitset>
# include <climits>
# include <ctime>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define odd(x) ((x)&1)
#define sqr(x) ((x)*(x))
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,a,b) for (int i=(a); i<=(b); ++i)
#define per(i,a,b) for (int i=(a); i>=(b); --i)
#define rep_it(it,x) for (__typeof((x).begin()) it=(x).begin(); it!=(x).end(); it++)
#define ____ puts("\n_______________\n\n")
#define debug(x) ____; cout<< #x << " => " << (x) << endl
#define debug_pair(x) cout<<"\n{ "<<(x).fir<<" , "<<(x).sec<<" }\n"
#define debug_arr(x,n) ____; cout<<#x<<":\n"; rep(i,0,n) cout<<#x<<"["<<(i)<<"] => "<<x[i]<<endl
#define debug_arr2(x,n,m) ____; cout<<#x<<":\n"; rep(i,0,n) rep(j,0,m) cout<<#x<<"["<<(i)<<"]["<<(j)<<"]= "<<x[i][j]<<((j==m)?"\n\n":"    ")
#define debug_set(x) ____; cout<<#x<<": \n"; rep_it(it,x) cout<<(*it)<<" "; cout<<endl
#define debug_map(x) ____; cout<<#x<<": \n"; rep_it(it,x) debug_pair(*it)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int oo = /*2 * 1000 * 1000 * 1000*/0x3f3f3f3f;
const ll ooo=9223372036854775807ll;
const int _cnt = 1000 * 1000 + 7;
const int _p = 1000 * 1000 * 1000 + 7;
const int N=301005;
const double PI=acos(-1.0);
const double eps=1e-9;
int o(int x) { return x%_p; }
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
int lcm(int a, int b) { return a / gcd(a, b)*b; }

void file_put() {
    freopen("filename.in", "r", stdin);
    freopen("filename.out", "w", stdout);
}

int a[N],op[N],tmp[N],ans[N],n; ll dp[N];

void U(int k,int x,ll t=0) { if (dp[x]+t<dp[k]) dp[k]=dp[x]+t,op[k]=x; }

int main() {
    //file_put();

    scanf("%d",&n);
	rep(i,1,n) scanf("%d",&a[i]);
	rep(i,0,n+15) dp[i]=oo;
	if (n==1) return 0*printf("%d\n",0); else dp[n]=a[n];
	per(i,n-1,0) {
		if (a[i]==0) { U(i,i+1),U(i,i+2); continue; }
		U(i,i+2,a[i]);
		if (a[i]>=a[i+1]) { U(i,i+3,a[i]); continue; }
		int tt=a[i+1]-a[i];
		if (i+2<=n) {
			if (i+3<=n) U(i,i+3,a[i+1]);
			if (i+4<=n) U(i,i+4,a[i+1]);
		}
	}
	for (int t=op[0]; t; t=op[t]) ans[++ans[0]]=t;
	per(i,ans[0],1) {
		if (a[ans[i]]==0) continue;
		if (i==ans[0] || tmp[tmp[0]]-ans[i]<=2)
			if (a[ans[i]]<=a[ans[i]+1]) {
				a[ans[i]+1]-=a[ans[i]];
				a[ans[i]]=0;
				tmp[++tmp[0]]=ans[i];
			} else {
				if (a[ans[i]+1]) tmp[++tmp[0]]=ans[i];
				tmp[++tmp[0]]=ans[i]-1;
				a[ans[i]]-=a[ans[i]+1];
				a[ans[i]-1]-=a[ans[i]];
				a[ans[i]+1]=a[ans[i]]=0;
			}
		else {
			tmp[++tmp[0]]=ans[i];
			tmp[++tmp[0]]=ans[i]+1;
			a[ans[i]+1]-=a[ans[i]];
			a[ans[i]]=0;
			a[ans[i]+2]-=a[ans[i]+1];
			a[ans[i]+1]=0;
		}
	}
	printf("%d\n",tmp[0]);
	rep(i,1,tmp[0]) printf("%d\n",tmp[i]);

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
const int nax=300*1007;

int n;
ll tab[nax];
ll dp[nax];

vector <int> wyn;

void wrzu(int v)
{
	if (v<n)
		wyn.push_back(v);
}

int main()
{
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
		scanf("%lld", &tab[i]);
	for (int i=n-1; i; i--)
		dp[i]=min(dp[i+2]+tab[i+1], dp[i+3]+tab[i+2]+tab[i]-tab[i-1]);
	for (int i=1; i<n;)
	{
		if (dp[i+2]+tab[i+1]<dp[i+3]+tab[i+2]+tab[i]-tab[i-1])
		{
			wrzu(i+1);
			wrzu(i);
			i+=2;
		}
		else
		{
			wrzu(i+2);
			wrzu(i+1);
			wrzu(i);
			i+=3;
		}
	}
	printf("%d\n", n-1);
	for (int i : wyn)
		printf("%d\n", i);
	return 0;
}

#pragma comment(linker,"/stack:200000000")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
using INT=long long;
const int NN=372727;
int a[NN],val[NN],b[NN];
INT dp[NN][2];
vector <int> ANS;
void sol(int k){
	int mn=min(a[k],a[k+1]);
	if(mn==0) return;
	ANS.push_back(k);
	a[k]-=mn;a[k+1]-=mn;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i),b[i]=a[i];
	dp[0][0]=dp[0][1]=0;
	for(int i=1;i<=n;i++){
		dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i];
		if(i>1) dp[i][0]=min(dp[i][0],min(dp[i-2][1],dp[i-2][1])+max(a[i],a[i-1]));

		dp[i][1]=dp[i-1][0];
	}
	cout<<min(dp[n][0],dp[n][1])<<endl;
	int st=n,fl=0;
	if(dp[n][1]<=dp[n][0]) fl=1;
	while(st){
		if(fl){
			st--;
			fl=0;
		}
		else {
			if(dp[st][0]==dp[st-1][0]+b[st]) sol(st),sol(st-1),st--;
			else if(dp[st][0]==dp[st-1][1]+b[st]) sol(st),sol(st-1),st--,fl=1;
			else if(dp[st][0]==dp[st-2][0]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=0;
			else if(dp[st][0]==dp[st-2][1]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=1;
		}
	}
	for(int v:ANS) printf("%d\n",v);
	return 0;
}

#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

typedef long long ll;

const ll INF = 1e18L + 5;
const int nax = 3e5 + 5;

struct S {
	ll score;
	pair<int,int> last_state;
	vector<int> moves;
};

bool done[nax][2];
S dp[nax][2];
int in[nax], memo[nax];

ll rec(int n, int anything) {
	if(n == 0) return 0;
	assert(n >= 1);
	if(done[n][anything]) return dp[n][anything].score;
	done[n][anything] = true;
	dp[n][anything].score = INF;
	if(anything) {
		rec(n, 0);
		dp[n][anything] = dp[n][0];
	}
	auto consider = [&] (vector<int> moves) {
		int first = n;
		for(int x : moves) first = min(first, x);
		if(first < 1) return;
		for(int i = first; i <= n; ++i) in[i] = memo[i];
		ll cost = 0;
		for(int x : moves) {
			int here = min(in[x], in[x+1]);
			if(here == 0) return;
			cost += here;
			in[x] -= here;
			in[x+1] -= here;
		}
		if(!anything && in[n]) return;
		for(int i = first; i < n; ++i)
			if(in[i] && in[i+1]) return;
		bool ple = in[first] == 0;
		cost += rec(first - 1, ple);
		if(cost < dp[n][anything].score) {
			dp[n][anything].score = cost;
			dp[n][anything].last_state = {first - 1, ple};
			dp[n][anything].moves = moves;
		}
	};
	consider({});
	consider({n-1});
	for(int len = 2; len <= 8; ++len) {
		vector<int> moves(len);
		for(int i = 1; i <= len; ++i)
			moves[i-1] = n - i;
		consider(moves);
		reverse(moves.begin(), moves.end());
		consider(moves);
	}
	debug() << imie(n) imie(anything) imie(dp[n][anything].score);
	if(anything) assert(dp[n][anything].score < INF);
	return dp[n][anything].score;
}

int main() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &in[i]);
		memo[i] = in[i];
	}
	vector<int> moves;
	rec(n, 1);
	pair<int,int> p{n, 1};
	while(p.first) {
		for(int x : dp[p.first][p.second].moves) moves.push_back(x);
		p = dp[p.first][p.second].last_state;
	}
	printf("%d\n", (int) moves.size());
	for(int x : moves) printf("%d\n", x);
}

#include <bits/stdc++.h>
#define NN 300100
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using INT = long long;
using pii = pair<int, int>;

int p[NN];
INT dp[NN][2];
int vst[NN];
vector<int> ans;
int N;
int val, flag = 1;
void calc(int n){
	if(dp[n][0] == val){
		if(n >= 2 && min(dp[n - 2][0], dp[n - 2][1]) == val - max(p[n], p[n - 1])){
			val -= max(p[n], p[n - 1]);
			if(!vst[n - 1]) {
				int V = min(p[n - 1], p[n]);
				if(V) ans.push_back(n - 1);
				vst[n - 1] = 1;
				p[n - 1] -= V; p[n] -= V;
			}
			if(n != N) {
				if(!vst[n]) {
					int V = min(p[n + 1], p[n]);
					if(V) ans.push_back(n);
					vst[n] = 1;
					p[n + 1] -= V; p[n] -= V;
				}
			}
			if(!vst[n - 2]){
				int V = min(p[n - 2], p[n - 1]);
				if(V) ans.push_back(n - 2);
				vst[n - 2] = 1;
				p[n - 2] -= V; p[n - 1] -= V;
			}
			flag = 2;
			return ;
		}
		val -= p[n];
		if(n > 1) {
			if(!vst[n - 1]) {
				int V = min(p[n - 1], p[n]);
				if(V) ans.push_back(n - 1);
				vst[n - 1] = 1;
				p[n - 1] -= V; p[n] -= V;
			}
		}
		if(n != N) {
			if(!vst[n]){
				int V = min(p[n + 1], p[n]);
				if(V)ans.push_back(n);
				vst[n] = 1;
				p[n + 1] -= V; p[n] -= V;
			}
		}
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	ios_base :: sync_with_stdio(0);cin.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> p[i];
	for(int i = 1; i <= n; i++) for(int j = 0; j < 2; j++) dp[i][j] = inf;
	dp[1][0] = p[1], dp[1][1] = 0;
	for(int i = 2; i <= n; i++){
		dp[i][0] = min(dp[i - 1][1], dp[i - 1][0]) + p[i];
		if(i >= 2) dp[i][0] = min(dp[i][0], min(dp[i - 2][0], dp[i - 2][1]) + max(p[i], p[i - 1]));
		dp[i][1] = dp[i - 1][0];
	}
	val = min(dp[n][1], dp[n][0]);
//	for(int i = 1; i <= n; i++) cout << dp[i][0]<<" "<<dp[i][1]<<endl;
	N = n;
	while(val > 0) {
//		cout << val << " "<< n <<endl;
		calc(n);
		n -= flag;
		flag = 1;
	}
	cout << ans.size() <<endl;
	for(int i = 0; i < ans.size(); i++) cout << ans[i] << endl;
	cout << endl;
	return 0;
}

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define forE(i,x) for(int i=head[x];i!=-1;i=ne[i])
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned u32;
typedef pair<int,int> pin;
#define mk(a,b) make_pair(a,b)
#define lowbit(x) ((x)&(-(x)))
#define sqr(a) ((a)*(a))
#define clr(a) (memset((a),0,sizeof(a)))
#define ls ((x)<<1)
#define rs (((x)<<1)|1)
#define mid (((l)+(r))>>1)
#define pb push_back
#define w1 first
#define w2 second
inline void read(int &x){
	x=0;int f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	x*=f;
}
inline void judge(){
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
}
/*******************************head*******************************/
const int maxn=500005;
int a[maxn],n;
i64 f[maxn],g[maxn];
vector<int> ans,tans;
inline void get(int x){
	if(x<0)return;
	if(g[x]==f[x]){
		ans.pb(x-1);
		get(x-2);
		return;
	}
	if(a[x-1]>=a[x]){
		if(g[x]==f[x-1]){
			ans.pb(x-1);
			ans.pb(x-2);
			get(x-3);
		}else assert(0);
	}else{
		if(a[x]<=a[x-1]+a[x+1]){
			if(g[x]==f[x-1]+a[x]-a[x-1]){
				ans.pb(x-1);
				ans.pb(x);
				get(x-3);
			}else{
				assert(0);
			}
		}else{
			assert(0);
		}
	}
}
int main(){
	read(n);
	rep(i,1,n)read(a[i]);
	rep(i,1,n){
		f[i]=((i-2)>=0?(g[i-2]):0)+a[i];
		g[i]=f[i];
		if(a[i-1]>=a[i])g[i]=min(g[i],f[i-1]);
		else{
			if(a[i]<=a[i-1]+a[i+1])
				g[i]=min(g[i],f[i-1]+a[i]-a[i-1]);
		}
	}
	int val=min(g[n],g[n-1]);
	if(val==g[n])get(n);
	else get(n-1);
	int cnt=0;
	for(u32 i=0;i<ans.size();i++){
		int v=ans[i];
		if(v<=0)continue;
		if(a[v]&&a[v+1]){
			cnt++;tans.pb(v);
			int res=min(a[v],a[v+1]);
			a[v]-=res;a[v+1]-=res;
		}
	}printf("%d\n",cnt);
	for(u32 i=0;i<tans.size();i++){
		int v=tans[i];if(v>0)printf("%d\n",v);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=301000;
map<int,ll> dp[N];
map<int,PII> pre[N],way[N];
int n,a[N];
PII cur;
VI ret;
void upd(int p,int v,ll cost,PII ww) {
	if (!dp[p].count(v)||cost<dp[p][v]) {
		dp[p][v]=cost;
		way[p][v]=ww;
		pre[p][v]=cur;
	}
}
void dfs(int n,int v) {
	if (n==1) return;
	if (way[n][v].fi!=-1) ret.pb(way[n][v].fi);
	if (way[n][v].se!=-1) ret.pb(way[n][v].se);
	dfs(pre[n][v].fi,pre[n][v].se);
}
int main() {
	scanf("%d",&n);
	rep(i,1,n+1) scanf("%d",a+i);
	rep(i,1,n+1) dp[i].clear(),pre[i].clear(),way[i].clear();
	dp[1][a[1]]=0;
	rep(i,1,n) {
		ll mv=1ll<<60;
		for (auto p:dp[i]) {
			int v=p.fi; ll w=p.se;
//			if (w>=mv) continue;
			mv=w;
			cur=mp(i,v);
			if (v==0) upd(i+1,a[i+1],w,mp(-1,-1));
			upd(i+1,max(a[i+1]-v,0),w+min(v,a[i+1]),mp(i,-1));
			if (i+2<n&&a[i+1]<=a[i+2]) upd(i+2,a[i+2]-a[i+1],w+a[i+1],mp(i+1,-1));
			if (i+2<n&&a[i+1]>=a[i+2]) upd(i+2,0,w+a[i+2]+min(v,(a[i+1]-a[i+2])),mp(i,i+1));
		}
	}
	int mv=-1; ll w=1ll<<60;
	for (auto p:dp[n]) if (p.se<w) w=p.se,mv=p.fi;
	dfs(n,mv);
	printf("%d\n",SZ(ret));
	reverse(all(ret));
	for (auto p:ret) printf("%d ",p);
	puts("");
}

#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int MAX = 3e5 + 100;
int n,t[MAX];
int dp[MAX][2];
vector<int> res;
bool ruszone[MAX];
int RES = 0;
void rusz(int i){
  if(i < 0 || i >= n-1 || ruszone[i])return;
  ruszone[i] = 1;
  int pom = min(t[i],t[i+1]);
  RES += pom;
  if(pom){
    res.PB(i+1);
    t[i] -= pom;
    t[i+1] -= pom;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> n;
  R(i,n)cin >> t[i];
  R(i,n)dp[i+1][0] = dp[i+1][1] = -1e18;
  R(i,n){
    maxi(dp[i+1][0], max(dp[i][0], dp[i][1]) - t[i]);
    maxi(dp[i+1][1], dp[i][0] + t[i]);
    if(i > 1){
      if(t[i-2] >= t[i-1]){
        maxi(dp[i+1][1], dp[i-2][1] - t[i-2] + t[i] + t[i-1]);
      }
      if(t[i] >= t[i-1]){
        maxi(dp[i+1][0], dp[i-1][1] + t[i-1] - t[i]);
      }
    }
  }
  int ak = 0;
  if(dp[n][1] > dp[n][0]){
    ak = 1;
  }
  debug(dp[n][ak]);
  vector<int> xx;
  for(int i = n - 1; i>0; i--){
    if(ak == 0 && dp[i+1][0] == dp[i][0] - t[i]){
      xx.PB(i - 2);
      xx.PB(i);
    }else{
      if(i > 1){
        if(t[i-2] >= t[i-1] && dp[i+1][1] == dp[i-2][1] - t[i-2] + t[i] + t[i-1]){
          xx.PB(i-2);
          i--;
        }else if(t[i] >= t[i-1] && dp[i+1][0] == dp[i-1][1] + t[i-1] - t[i]){
          xx.PB(i-1);
          i--;
        }
      }
      ak = !ak;
    }
  }
  for(int el:xx){
    rusz(el);
  }
  R(i,n-1){
    rusz(i);
  }
  cout << SZ(res) << "\n";
  for(int el:res){
    cout << el << "\n";
  }
  debug(RES);
}

#include <bits/stdc++.h>
#define NN 300100
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using INT = long long;
using pii = pair<int, int>;

int p[NN];
INT dp[NN][2];
int vst[NN];
vector<int> ans;
int N;
int val, flag = 1;
void calc(int n){
	if(dp[n][0] == val){
		if(n > 2 && min(dp[n - 2][0], dp[n - 2][1]) == val - max(p[n], p[n - 1])){
			if(!vst[n - 1]) {
				int V = min(p[n - 1], p[n]);
				if(V) ans.push_back(n - 1);
				vst[n - 1] = 1;
				val -= V;
				if(!val) return ;
				p[n - 1] -= V; p[n] -= V;
			}
			if(n != N) {
				if(!vst[n]) {
					int V = min(p[n + 1], p[n]);
					if(V) ans.push_back(n);
					vst[n] = 1;
					val -= V;
					if(!val) return ;
					p[n + 1] -= V; p[n] -= V;
				}
			}
			if(!vst[n - 2]){
				int V = min(p[n - 2], p[n - 1]);
				if(V) ans.push_back(n - 2);
				vst[n - 2] = 1;
				val -= V;
				p[n - 2] -= V; p[n - 1] -= V;
			}
			return ;
		}
		if(n > 1) {
			if(!vst[n - 1]) {
				int V = min(p[n - 1], p[n]);
				if(V) ans.push_back(n - 1);
				vst[n - 1] = 1;
				val -= V;
				if(!val) return ;
				p[n - 1] -= V; p[n] -= V;
			}
		}
		if(n != N) {
			if(!vst[n]){
				int V = min(p[n + 1], p[n]);
				if(V)ans.push_back(n);
				vst[n] = 1;
				val -= V;
				p[n + 1] -= V; p[n] -= V;
			}
		}
	}
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	ios_base :: sync_with_stdio(0);cin.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> p[i];
	for(int i = 1; i <= n; i++) for(int j = 0; j < 2; j++) dp[i][j] = inf;
	dp[1][0] = p[1], dp[1][1] = 0;
	for(int i = 2; i <= n; i++){
		dp[i][0] = min(dp[i - 1][1], dp[i - 1][0]) + p[i];
		if(i >= 2) dp[i][0] = min(dp[i][0], min(dp[i - 2][0], dp[i - 2][1]) + max(p[i], p[i - 1]));
		dp[i][1] = dp[i - 1][0];
	}
	val = min(dp[n][1], dp[n][0]);
//	for(int i = 1; i <= n; i++) cout << dp[i][0]<<" "<<dp[i][1]<<endl;
	N = n;
	while(val) {
//		cout << val << " "<< n <<endl;
		if(n < 0) break;
		calc(n);
		n -= flag;
		flag = 1;
	}
	cout << ans.size() <<endl;
	for(int i = 0; i < ans.size(); i++) cout << ans[i] << endl;
	cout << endl;
	return 0;
}

/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
template<class T, class... U> void DEBUG(const T &head, const U &... tail) {
#ifdef HOME
    _W('#'); _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...);
#endif
}
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int n;
int p[SIZE];
LL dp[SIZE];
VI ans;
void go(int x){
    if(!p[x])return;
    if(p[x]<=p[x-1]){
        p[x-1]-=p[x];
        p[x]=0;
        ans.PB(x-1);
    }
    else{
        p[x+1]-=p[x];
        p[x]=0;
        ans.PB(x);
    }
}
void go(int x,int y){
    ans.PB(x);
    int mi=min(p[x],p[y]);
    p[x]-=mi; p[y]-=mi;
    if(p[x]){
        ans.PB(x-1);
        p[x-1]-=p[x];
        p[x]=0;
    }
    if(p[y]){
        ans.PB(y);
        p[y+1]-=p[y];
        p[y]=0;
    }
}
int main(){
    R(n);
    if(n==1){
        W(0);
        return 0;
    }
    else if(n==2){
        W(1);
        W(1);
        return 0;
    }
    FOR(i,1,n){
        R(p[i]);
    }
    dp[2]=p[1];
    FOR(i,3,n+1){
        dp[i]=dp[i-2]+p[i-1];
        dp[i]=min(dp[i],dp[i-3]+max(p[i-1],p[i-2]));
    }
    int now=n+1;
    while(now>1){
        if(dp[now]==dp[now-2]+p[now-1]){
            go(now-1);
            now-=2;
        }
        else{
            go(now-2,now-1);
            now-=3;
        }
    }
    W(SZ(ans));
    W(ans);
    return 0;
}

#pragma GCC optimize ("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge {c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

typedef long long ll;

const ll INF = 1e18L + 5;
const int nax = 3e5 + 5;

struct S {
	ll score;
	pair<int,int> last_state;
	vector<int> moves;
};

bool done[nax][2];
S dp[nax][2];
int in[nax], memo[nax];

ll rec(int n, int anything) {
	if(n == 0) return 0;
	assert(n >= 1);
	if(done[n][anything]) return dp[n][anything].score;
	done[n][anything] = true;
	dp[n][anything].score = INF;
	if(anything) {
		rec(n, 0);
		dp[n][anything] = dp[n][0];
	}
	auto consider = [&] (vector<int> moves) {
		int first = n;
		for(int x : moves) first = min(first, x);
		if(first < 1) return;
		for(int i = first; i <= n; ++i) in[i] = memo[i];
		ll cost = 0;
		for(int x : moves) {
			int here = min(in[x], in[x+1]);
			if(here == 0) return;
			cost += here;
			in[x] -= here;
			in[x+1] -= here;
		}
		if(!anything && in[n]) return;
		for(int i = first; i < n; ++i)
			if(in[i] && in[i+1]) return;
		bool ple = in[first] == 0;
		cost += rec(first - 1, ple);
		if(cost < dp[n][anything].score) {
			dp[n][anything].score = cost;
			dp[n][anything].last_state = {first - 1, ple};
			dp[n][anything].moves = moves;
		}
	};
	consider({});
	consider({n-1});
	for(int len = 2; len <= 100; ++len) {
		vector<int> moves(len);
		for(int i = 1; i <= len; ++i)
			moves[i-1] = n - i;
		consider(moves);
		reverse(moves.begin(), moves.end());
		consider(moves);
	}
	debug() << imie(n) imie(anything) imie(dp[n][anything].score);
	if(anything) assert(dp[n][anything].score < INF);
	return dp[n][anything].score;
}

int main() {
	int n;
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &in[i]);
		memo[i] = in[i];
	}
	vector<int> moves;
	rec(n, 1);
	pair<int,int> p{n, 1};
	while(p.first) {
		for(int x : dp[p.first][p.second].moves) moves.push_back(x);
		p = dp[p.first][p.second].last_state;
	}
	printf("%d\n", (int) moves.size());
	for(int x : moves) printf("%d\n", x);
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,a) for(int i=start[(a)];i;i=e[i].next)
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
#define SZ(A) ((int)A.size())
inline int read()
{
    int sum=0,p=1;char ch=getchar();
    while(!(('0'<=ch && ch<='9') || ch=='-'))ch=getchar();
    if(ch=='-')p=-1,ch=getchar();
    while('0'<=ch && ch<='9')sum=sum*10+ch-48,ch=getchar();
    return sum*p;
}

const int maxn=3e5+20;

int n,a[maxn];

inline void init()
{
    n=read();REP(i,1,n)a[i]=read();
}

int dp[maxn],pre[maxn];
int val[maxn],na[maxn];
vector<int>ans,nans;

inline void work(int x){ int t=min(na[x],na[x+1]);if(t)nans.push_back(x),na[x]-=t,na[x+1]-=t;}

inline void doing()
{
    dp[0]=0;dp[1]=a[1];pre[1]=1; dp[2]=a[2]; pre[2]=1;
    REP(i,3,n)
    {
	int A=dp[i-3]+max(a[i],a[i-1]),B=dp[i-2]+a[i];
	if(A<B)dp[i]=A,pre[i]=2;
	else dp[i]=B,pre[i]=1;
    }
    int now=dp[n-1]<dp[n]?n-1:n;
    while(now>0)
    {
	int t=pre[now];
	if(t==2)
	{
	    if(a[now-1]>a[now])ans.push_back(now-1),ans.push_back(now-2);
	    else ans.push_back(now-1),ans.push_back(now);
	}else
	{
	    if(a[now]<a[now-1])ans.push_back(now-1);
	    else ans.push_back(now);
	}
	now-=t+1;
    }
    REP(i,1,n)na[i]=a[i];
    for(int x:ans)work(x);
    printf("%d\n",SZ(nans));
    for(int x:nans)printf("%d\n",x);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("E.in","r",stdin);
    freopen("E.out","w",stdout);
#endif
    init();
    doing();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using INT=long long;
const int NN=372727;
int a[NN],val[NN],b[NN];
INT dp[NN][2];
vector <int> ANS;
void sol(int k){
	int mn=min(a[k],a[k+1]);
	if(mn==0) return;
	ANS.push_back(k);
	a[k]-=mn;a[k+1]-=mn;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i),b[i]=a[i];
	dp[0][0]=dp[0][1]=0;
	for(int i=1;i<=n;i++){
		dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i];
		if(i>1) dp[i][0]=min(dp[i][0],min(dp[i-2][1],dp[i-2][1])+max(a[i],a[i-1]));

		dp[i][1]=dp[i-1][0];
	}
	cout<<min(dp[n][0],dp[n][1])<<endl;
	int st=n,fl=0;
	if(dp[n][1]<=dp[n][0]) fl=1;
	while(st){
		if(fl){
			st--;
			fl=0;
		}
		else {
			if(dp[st][0]==dp[st-1][0]+b[st]) sol(st-1),sol(st),st--;
			else if(dp[st][0]==dp[st-1][1]+b[st]) sol(st-1),sol(st),st--,fl=1;
			else if(dp[st][0]==dp[st-2][0]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=0;
			else if(dp[st][0]==dp[st-2][1]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=1;
		}
	}
	for(int v:ANS) printf("%d\n",v);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=301000;
map<int,ll> dp[N];
map<int,PII> pre[N],way[N];
int n,a[N];
PII cur;
VI ret;
void upd(int p,int v,ll cost,PII ww) {
	if (!dp[p].count(v)||cost<dp[p][v]) {
		dp[p][v]=cost;
		way[p][v]=ww;
		pre[p][v]=cur;
	}
}
void dfs(int n,int v) {
	if (n==1) return;
	if (way[n][v].fi!=-1) ret.pb(way[n][v].fi);
	if (way[n][v].se!=-1) ret.pb(way[n][v].se);
	dfs(pre[n][v].fi,pre[n][v].se);
}
int main() {
	scanf("%d",&n);
	rep(i,1,n+1) scanf("%d",a+i);
	rep(i,1,n+1) dp[i].clear(),pre[i].clear(),way[i].clear();
	dp[1][a[1]]=0;
	rep(i,1,n) {
		ll mv=1ll<<60;
		for (auto p:dp[i]) {
			int v=p.fi; ll w=p.se;
			if (w>=mv) continue;
			mv=w;
			cur=mp(i,v);
			if (v==0) upd(i+1,a[i+1],w,mp(-1,-1));
			upd(i+1,max(a[i+1]-v,0),w+min(v,a[i+1]),mp(i,-1));
			if (i+2<n&&a[i+1]<=a[i+2]) upd(i+2,a[i+2]-a[i+1],w+a[i+1],mp(i+1,-1));
			if (i+2<n&&a[i+1]>=a[i+2]) upd(i+2,0,w+a[i+2]+min(v,(a[i+1]-a[i+2])),mp(i+1,i));
		}
	}
	int mv=-1; ll w=1ll<<60;
	for (auto p:dp[n]) if (p.se<w) w=p.se,mv=p.fi;
//	fprintf(stderr,"%lld\n",w);
	dfs(n,mv);
	printf("%d\n",SZ(ret));
	reverse(all(ret));
	for (auto p:ret) printf("%d ",p);
	puts("");
}

// So when you feel like hope is gone, Look inside you and be strong, And you'll finally see the truth, That a hero lies in you ...

#include<bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int maxn = 3e5 + 10, mod = 1e9 + 7;
const ll inf = 1e18;

template<typename A, typename B> ostream& operator << (ostream& out, pair<A, B> p){
    out << "{ " << p.F << ", " << p.S <<" }";
    return out;
}
template<typename T> ostream& operator << (ostream& out, vector<T> v){
    out << "[ ";
    for(int i = 0; i < sz(v); i++){
	out << v[i];
	if(i != sz(v)-1)
	    out << " ,";
    }
    out << " ]";
    return out;
}

int a[maxn], dpp[maxn];
ll dp[maxn];
bool is[maxn];

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();

    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
	cin >> a[i];
    }
    dp[1] = a[1];
    dp[2] = a[2];
    for(int i = 3; i <= n; i++){
	dp[i] = min( max(a[i], a[i-1]) + dp[i-3], a[i] + dp[i-2] );
	if(dp[i] == dp[i-3] + max(a[i], a[i-1]))
	    dpp[i] = 2;
	else
	    dpp[i] = 1;
    }
    int tmp = dp[n] < dp[n-1] ? n : (n-1);
    while(tmp > 2){
	if(dpp[tmp] == 2){
	    is[tmp-1] = 1;
	    tmp-=3;
	}
	else{
	    tmp-=2;
	}
    }
    cout << n-1 << "\n";
    for(int i = 1; i <= n; i++){
	if(is[i])
	    cout << i << "\n";
    }
    for(int i = 1; i < n; i++){
	if(is[i] == 0)
	    cout << i << "\n";
    }
    return 0;
}
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include<bits/stdc++.h>
using namespace std;
using INT=long long;
const int NN=372727;
int a[NN],val[NN],b[NN];
INT dp[NN][2];
vector <int> ANS;
void sol(int k){
	int mn=min(a[k],a[k+1]);
	if(mn==0) return;
	ANS.push_back(k);
	a[k]-=mn;a[k+1]-=mn;
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i),b[i]=a[i];
	dp[0][0]=dp[0][1]=0;
	for(int i=1;i<=n;i++){
		dp[i][0]=min(dp[i-1][0],dp[i-1][1])+a[i];
//		if(i>1) dp[i][0]=min(dp[i][0],min(dp[i-2][1],dp[i-2][1])+max(a[i],a[i-1]));

		dp[i][1]=dp[i-1][0];
	}
	int st=n,fl=0;
	if(dp[n][1]<=dp[n][0]) fl=1;
	while(st){
		if(fl){
			st--;
			fl=0;
		}
		else {
			if(dp[st][0]==dp[st-1][0]+b[st]) sol(st-1),sol(st),st--;
			else if(dp[st][0]==dp[st-1][1]+b[st]) sol(st),sol(st-1),st--,fl=1;
//			else if(dp[st][0]==dp[st-2][0]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=0;
//			else if(dp[st][0]==dp[st-2][1]+max(b[st],b[st-1])) sol(st-1),sol(st-2),sol(st),st-=2,fl=1;
		}
	}
	cout<<ANS.size()<<endl;
	for(int v:ANS) printf("%d\n",v);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
vector<int> ans;
int n,f[300010],p[300010],g[300010];
void work(int x)
{
    int vl=min(p[x],p[x+1]);
    if(vl)
    {
        ans.push_back(x);
        p[x]-=vl;p[x+1]-=vl;
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&p[i]);
    f[0]=0;
    for(int i=1;i<=n;i++)
    {
        f[i]=min(f[max(i-2,0)]+p[i],f[max(i-3,0)]+max(p[i-1],p[i]));
        if(f[i]==f[max(i-3,0)]+max(p[i],p[i-1]))g[i]=1;
    }
    for(int i=n-(f[n-1]<=f[n]);i>0;i=i-2-g[i])
    {
        work(i-1);
        if(g[i])work(i-2);
        work(i);
    }
    printf("%d\n",ans.size());
    for(int &x:ans)printf("%d\n",x);
    return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;
#define MAXNUM 333333
using ll = long long;
#define rep(i,start,end) for(int i=start;i<end;i++)
ll dp[MAXNUM],p[MAXNUM];
int before[MAXNUM],a[MAXNUM],anum;
int res[MAXNUM], rnum;
void doit(int k)
{
	int x1 = min(p[k], p[k - 1]);
	if (x1)
		res[rnum++] = k - 1;
	p[k] -= x1, p[k - 1] += x1;
}
int main(void)
{
	int n;
	scanf("%d", &n);
	rep(i, 1, n + 1)
		scanf("%I64d", &p[i]);
	rep(i, 1, n + 1)
	{
		ll k1 = dp[max(0, i - 2)] + p[i];
		ll k2 = dp[max(0, i - 3)] + max(p[i], p[i - 1]);
		if (k1 < k2)
			dp[i] = k1, before[i] = max(0, i - 2);
		else dp[i] = k2, before[i] = max(0, i - 3);
	}
	int x = n;
	if (dp[n - 1] < dp[n])
		x = n - 1;
	for (; x; x = before[x])
		a[++anum] = x;
	reverse(a + 1, a + anum + 1);
	rep(i, 1, anum + 1)
	{
		int k = a[i];
		doit(k);
		doit(k + 1);
		if (a[i] - a[i - 1] == 3)
		{
			doit(k - 1);
		}
	}
	printf("%d\n", rnum);
	rep(i, 0, rnum)
		printf("%d\n", res[i]);
}

#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define R(i,n) REP(i,n)
#define VI vector<int>
#define PII pair<int,int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const int MAX = 3e5 + 100;
int n,t[MAX];
int dp[MAX][2];
vector<int> res;
bool ruszone[MAX];
int RES = 0;
void rusz(int i){
  if(i < 0 || i >= n-1 || ruszone[i])return;
  ruszone[i] = 1;
  int pom = min(t[i],t[i+1]);
  RES += pom;
  if(pom){
    res.PB(i+1);
    t[i] -= pom;
    t[i+1] -= pom;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> n;
  R(i,n)cin >> t[i];
  R(i,n)dp[i+1][0] = dp[i+1][1] = -1e18;
  R(i,n){
    maxi(dp[i+1][0], max(dp[i][0], dp[i][1]) - t[i]);
    maxi(dp[i+1][1], dp[i][0] + t[i]);
    if(i > 1){
      if(t[i-2] >= t[i-1]){
        maxi(dp[i+1][1], dp[i-1][0] + t[i] + t[i-1]);
      }
      if(t[i] >= t[i-1]){
        maxi(dp[i+1][0], dp[i-1][1] + t[i-1] - t[i]);
      }
    }
  }
  int ak = 0;
  if(dp[n][1] > dp[n][0]){
    ak = 1;
  }
  vector<int> xx;
  for(int i = n - 1; i>0; i--){
    if(ak == 0 && dp[i+1][0] == dp[i][0] - t[i]){
      xx.PB(i - 2);
      xx.PB(i);
    }else{
      if(i > 1){
        if(t[i-2] >= t[i-1] && dp[i+1][1] ==  dp[i-1][0] + t[i] + t[i-1]){
          xx.PB(i-2);
          i--;
        }else if(t[i] >= t[i-1] && dp[i+1][0] == dp[i-1][1] + t[i-1] - t[i]){
          xx.PB(i-1);
          i--;
        }
      }
      ak = !ak;
    }
  }
  for(int el:xx){
    rusz(el);
  }
  R(i,n-1){
    rusz(i);
  }
  cout << SZ(res) << "\n";
  for(int el:res){
    cout << el << "\n";
  }
  debug(RES);
}

#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = (l), i##end = (r);i <= i##end;++i)
using std::cin; using std::cout;
const int maxn = 300200;
typedef long long ll;
const int mod = 998244353;
inline ll pow(ll a,int b,int ans = 1) {
	for(;b;b >>= 1,a = a * a % mod) if(b & 1)
		ans = ans * a % mod;
	return ans;
}
inline ll inverse(int x){ return pow(x, mod - 2); }
int n;
std::unordered_map<int, int> dp[maxn][2];
std::vector<int> vector;
int p[maxn];
inline int dfs(int now, int need, int last) {
	if(!now) return 0;
	if(dp[now][need].count(last)) return dp[now][need][last];
	int & ans = dp[now][need][last] = need && last ? 1e9 : dfs(now - 1, bool(last), p[now - 1]);
	int min = std::min(p[now - 1], last);
	last -= min;
	return ans = last && need ? 1e9 : std::min(ans, dfs(now - 1, bool(last), p[now - 1] - min) + min);
}
inline void output(int now, int need, int last) {
	if(!now) return ;
	if((!need || !last) && dfs(now, need, last) == dfs(now - 1, bool(last), p[now - 1])) return output(now - 1, bool(last), p[now - 1]);
	vector.emplace_back(now - 1);
	int min = std::min(p[now - 1], last);
	output(now - 1, bool(last - min), p[now - 1] - min);
}
int main() {
	std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;++i) {
		cin >> p[i];
	}
	dfs(n, 0, p[n]);
	output(n, 0, p[n]);
	cout << vector.size() << '\n';
	for(int i : vector) {
		cout << i << '\n';
	}
}

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int gi(){
	int x=0,w=1;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
	if(ch=='-')w=0,ch=getchar();
	while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
	return w?x:-x;
}
#define ll long long
const int N=3e5+5;
int n,a[N],mrk[N],ans[N],m;ll dp[N];
void work(int x){
	if(x>0&&a[x]&&a[x+1]){
		int d=min(a[x],a[x+1]);ans[++m]=x;
		a[x]-=d;a[x+1]-=d;
	}
}
int main(){
	n=gi()+1;
	for(int i=1;i<=n;++i){
		if(i<n)a[i]=gi();
		ll x=dp[max(i-3,0)]+max(a[i],a[i-1]);
		ll y=dp[max(i-2,0)]+a[i];
		dp[i]=min(x,y);if(dp[i]==x)mrk[i]=1;
	}
	for(int i=n;i;--i){
		work(i-1);
		if(mrk[i])work(i-2);
	}
	printf("%d\n",m);
	for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
	return 0;
}

// luogu-judger-enable-o2
#include<bits/stdc++.h>
//this code is written by Itst
using namespace std;

#define int long long
const int _ = 3e5 + 7;
int dp[_] , pre[_] , N , val[_];

void move(int x){
	if(x == N) return;
	printf("%lld\n" , x); int t = min(val[x] , val[x + 1]);
	val[x] -= t; val[x + 1] -= t;
}

signed main(){
	ios::sync_with_stdio(0); cin >> N;
	for(int i = 1 ; i <= N ; ++i) cin >> val[i];
	dp[1] = 0; dp[2] = val[2];
	for(int i = 3 ; i <= N + 1 ; ++i)
		if(dp[i - 3] + max(val[i - 1] , val[i - 2]) < dp[i - 2] + val[i - 1])
			dp[i] = dp[pre[i] = i - 3] + max(val[i - 1] , val[i - 2]);
		else dp[i] = dp[pre[i] = i - 2] + val[i - 1];
	printf("%lld\n" , min(dp[N] , dp[N + 1]));
	for(int i = dp[N] < dp[N + 1] ? N : N + 1 ; i > 1 ; i = pre[i])
		if(pre[i] == i - 3){move(i - 2); move(i - 1); if(i > 3) move(i - 3);}
		else {move(i - 1); if(i > 2) move(i - 2);}
	return 0;
}