import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Vector;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Stack;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ProblemBLipshitzSequence solver = new ProblemBLipshitzSequence();
        solver.solve(1, in, out);
        out.close();
    }

    static class ProblemBLipshitzSequence {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.readInt();
            int q = in.readInt();
            long[] a = in.readLongArray(n);
            long[] d = new long[n - 1];
            for (int i = 0; i < n - 1; i++) {
                d[i] = Math.abs(a[i + 1] - a[i]);
            }
            int[] f = new int[n - 1];
            int[] b = new int[n - 1];
            Arrays.fill(f, n - 1);
            Arrays.fill(b, -1);
            Stack<Integer> stack = new Stack<>();
            for (int i = 0; i < n - 1; i++) {
                while (!stack.isEmpty() && d[stack.peek()] < d[i]) {
                    f[stack.pop()] = i;
                }
                stack.push(i);
            }
            stack = new Stack<>();
            for (int i = n - 2; i >= 0; i--) {
                while (!stack.isEmpty() && d[stack.peek()] <= d[i]) {
                    b[stack.pop()] = i;
                }
                stack.push(i);
            }
            while (q-- > 0) {
                int l = in.readInt() - 1;
                int r = in.readInt() - 2;
                long ans = 0;
                for (int i = l; i <= r; i++) {
                    int t1 = Math.min(r + 1, f[i]);
                    int t2 = Math.max(l - 1, b[i]);
                    ans += (t1 - i) * (i - t2) * d[i];
                }
                out.println(ans);
            }
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long readLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public long[] readLongArray(int size) {
            long[] ans = new long[size];
            for (int i = 0; i < size; i++) ans[i] = readLong();
            return ans;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Vector;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Stack;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ProblemBLipshitzSequence solver = new ProblemBLipshitzSequence();
        solver.solve(1, in, out);
        out.close();
    }

    static class ProblemBLipshitzSequence {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.readInt();
            int q = in.readInt();
            long[] a = in.readLongArray(n);
            long[] d = new long[n - 1];
            for (int i = 0; i < n - 1; i++) {
                d[i] = Math.abs(a[i + 1] - a[i]);
            }
            int[] f = new int[n - 1];
            int[] b = new int[n - 1];
            Arrays.fill(f, n - 1);
            Arrays.fill(b, -1);
            Stack<Integer> stack = new Stack<>();
            for (int i = 0; i < n - 1; i++) {
                while (!stack.isEmpty() && d[stack.peek()] < d[i]) {
                    f[stack.pop()] = i;
                }
                stack.push(i);
            }
            stack = new Stack<>();
            for (int i = n - 2; i >= 0; i--) {
                while (!stack.isEmpty() && d[stack.peek()] <= d[i]) {
                    b[stack.pop()] = i;
                }
                stack.push(i);
            }
            while (q-- > 0) {
                int l = in.readInt() - 1;
                int r = in.readInt() - 2;
                long ans = 0;
                for (int i = l; i <= r; i++) {
                    long t1 = Math.min(r + 1, f[i]);
                    long t2 = Math.max(l - 1, b[i]);
                    ans += (t1 - i) * (i - t2) * d[i];
                }
                out.println(ans);
            }
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long readLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public long[] readLongArray(int size) {
            long[] ans = new long[size];
            for (int i = 0; i < size; i++) ans[i] = readLong();
            return ans;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.*;
import java.util.*;

public class R333qB {

	static int N;
	static int a[];
	static int b[];
	static int n,q;
	static RmqSparseTable rmq;

	public static void main(String args[]) {
		InputReader in = new InputReader(System.in);
		PrintWriter w = new PrintWriter(System.out);

		N = in.nextInt();
		q = in.nextInt();

		a = new int[N];
		for(int i=0;i<N;i++)
			a[i] = in.nextInt();

		n = N - 1;
		b = new int[n];
		for(int j=0;j<n;j++)
			b[j] = Math.abs(a[j + 1] - a[j]);

		rmq = new RmqSparseTable(b);

		while(q-- > 0)
			w.println(go(in.nextInt() - 1,in.nextInt() - 1));

		w.close();
	}

	public static long go(int l,int r){
		if(l >= r)
			return 0;
		int idx = rmq.maxPos(l,r-1);
		long left = (idx - l + 1);
		long right = (r - l + 1) - left;
		return go(l,idx) + go(idx + 1,r) + b[idx] * left * right;
	}

	/*
	static class Brute{
		int a[];
		Brute(int b[]){
			a = new int[b.length];
			for(int i=0;i<b.length;i++)
				a[i] = b[i];
		}
		long query(int l,int r){
			long ans = 0;
			for(int i=l;i<=r;i++){
				for(int j=i;j<=r;j++){
					ans += wow(i,j);
				}
			}
			return ans;
		}
		long wow(int l,int r){
			long max = 0;
			for(int i=l;i<=r;i++)
				for(int j=i+1;j<=r;j++)
					max = Math.max (max, (long)(Math.ceil( (Math.abs(a[j] - a[i]) * 1.0 / (j - i) )) + 0.00001) );
			return max;
		}
	}*/

	public static class RmqSparseTable {

		  int[] logTable;
		  int[][] rmq;
		  int[] a;

		  public RmqSparseTable(int[] a) {
		    this.a = a;
		    int n = a.length;

		    logTable = new int[n + 1];
		    for (int i = 2; i <= n; i++)
		      logTable[i] = logTable[i >> 1] + 1;

		    rmq = new int[logTable[n] + 1][n];

		    for (int i = 0; i < n; ++i)
		      rmq[0][i] = i;

		    for (int k = 1; (1 << k) < n; ++k) {
		      for (int i = 0; i + (1 << k) <= n; i++) {
		        int x = rmq[k - 1][i];
		        int y = rmq[k - 1][i + (1 << k - 1)];
		        rmq[k][i] = a[x] >= a[y] ? x : y;
		      }
		    }
		  }

		  public int maxPos(int i, int j) {
		    int k = logTable[j - i];
		    int x = rmq[k][i];
		    int y = rmq[k][j - (1 << k) + 1];
		    return a[x] >= a[y] ? x : y;
		  }
	}

	static class InputReader {

		private InputStream stream;
		private byte[] buf = new byte[8192];
		private int curChar;
		private int snumChars;
		private SpaceCharFilter filter;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int snext() {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c))
				c = snext();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}

			int res = 0;

			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));

			return res * sgn;
		}

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author K0T
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        int n;
        int q;
        int[] a;
        int[] b;
        int[] left;
        int[] right;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            n = in.nextInt();
            q = in.nextInt();
            a = new int[n + 2];
            b = new int[n + 2];
            for (int i = 1; i <= n; i++) {
                a[i] = in.nextInt();
            }
            for (int i = 1; i < n; i++) {
                b[i] = Math.abs(a[i + 1] - a[i]);
            }
            prepare();
            for (int i = 0; i < q; i++) {
                int le = in.nextInt();
                int ri = in.nextInt();
                out.println(solve(le, ri));
            }
        }

        void prepare() {
            left = new int[n + 2];
            right = new int[n + 2];
            int[] stack = new int[n + 2];
            int stackSize = 0;
            for (int i = 1; i < n; i++) {
                while (stackSize > 0 && b[stack[stackSize]] < b[i]) {
                    right[stack[stackSize]] = i - 1;
                    --stackSize;
                }
                stack[++stackSize] = i;
            }
            for (int i = 1; i <= stackSize; i++) {
                right[stack[i]] = n - 1;
            }
            stackSize = 0;
            for (int i = n - 1; i >= 1; --i) {
                while (stackSize > 0 && b[stack[stackSize]] <= b[i]) {
                    left[stack[stackSize]] = i + 1;
                    --stackSize;
                }
                stack[++stackSize] = i;
            }
            for (int i = 1; i <= stackSize; i++) {
                left[stack[i]] = 1;
            }
        }

        long solve(int le, int ri) {
            long res = 0L;
            for (int i = le; i < ri; i++) {
                int pl = Math.max(le, left[i]);
                int pr = Math.min(ri, right[i] + 1);
                res = res + 1L * (i - pl + 1) * (pr - i) * b[i];
            }
            return res;
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    IOS ios;
    public static void main(String[] args) throws Exception {
        (new Main()).main();
    }

    void main() throws Exception {
        //ios = new IOS("main.in","");
        ios = new IOS("","");
        run();
        ios.close();
    }

    int n, q, a[], h[];
    SegmentTree tree;
    void run() throws Exception {
        ios.get();
            n = ios.Int();
            q = ios.Int();
        ios.get();
            a = new int[n+1];
            for (int i = 1; i <= n; ++i) {
                a[i] = ios.Int();
            }
        h = new int[n];
        int u = 0;
        for (int i = 1; i < n; ++i) {
            h[i] = Math.abs(a[i + 1] - a[i]);
            u = Math.max(u, h[i]);
        }
        tree = new SegmentTree(n - 1, h);
        for (int i = 0; i < q; ++i) {
            ios.get();
                int l = ios.Int(), r = ios.Int() - 1;
            ios.wn((u == 0) ? 0 : get(l, r));
        }
    }

    long get(int l, int r) {
        if (l == r) return h[l];
        int m = tree.info(l, r);
        long res = ((long)m - l + 1) * ((long)r - m + 1) * (long)h[m];
        if (l < m) res += get(l, m - 1);
        if (m < r) res += get(m + 1, r);
        return res;
    }
    class SegmentTree {
        int a[], n, d[], p[];
        SegmentTree(int N, int[] A) {
            a = A; n = N;
            d = new int[n << 2 + 1];
            p = new int[n + 1];
            build(1, 1, n);
        }

        void build(int x, int i, int j) {
            if (i == j) {
                p[i] = x; d[x] = i;
            } else {
                int m = (i + j) >> 1;
                    build(x + x, i, m);
                    build(x + x + 1, m + 1, j);
                d[x] = d[x + x + 1];
                    if (a[d[x]] <= a[d[x + x]]) {
                        d[x] = d[x + x];
                    }
            }
        }

        int get(int x, int i, int j, int l, int r) {
            if (i >= l && j <= r) return d[x];
            if (i > r || j < l || i >= j) return 0;
            int m = (i + j) >> 1;
            int u = get(x + x, i, m, l, r);
            int v = get(x + x + 1, m + 1, j, l, r);
            if (a[u] < a[v]) return v;
            return u;
        }
        int info(int l, int r) {
            return get(1, 1, n, l, r);
        }
    }
}

class IOS {
    int buffer = 1 << 16;
    BufferedReader  inp;
    BufferedWriter  out;
    IOS(String Inp, String Out) throws Exception {
        inp = new BufferedReader(Inp != "" ? (new FileReader(Inp)) : (new InputStreamReader(System.in)), buffer);
        out = new BufferedWriter(Out != "" ? (new FileWriter(Out)) : (new OutputStreamWriter(System.out)), buffer);
    }
    void close() throws Exception {
        out.flush(); inp.close(); out.close();
    }
    StringTokenizer i;
    void    get() throws Exception {i = new StringTokenizer(inp.readLine());}
    long    Long() throws Exception {return Long.parseLong(i.nextToken());}
    int     Int() throws Exception {return Integer.parseInt(i.nextToken());}
    String  word() throws Exception {return i.nextToken();}
    String  str() throws Exception {return inp.readLine();}
    <T> void w(T e) throws Exception {out.write(String.valueOf(e));}
    <T> void wn(T e) throws Exception {out.write(String.valueOf(e)); out.newLine();}
    <T> void wa(T... a) throws Exception {for (T e: a) {out.write(String.valueOf(e)); out.write(" ");} out.newLine();}

    long gcd(long u, long v) { long w = 0; while (u != 0) { w = u; u = v % u; v = w;} return v; }
    long lcm(long u, long v) {
        return u / gcd(u, v) * v;
    }
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    IOS ios;
    public static void main(String[] args) throws Exception {
        (new Main()).main();
    }

    void main() throws Exception {
        //ios = new IOS("main.in","");
        ios = new IOS("","");
        run();
        ios.close();
    }

    int n, q, a[], h[];
    SegmentTree tree;
    void run() throws Exception {
        ios.get();
            n = ios.Int();
            q = ios.Int();
        ios.get();
            a = new int[n+1];
            for (int i = 1; i <= n; ++i) {
                a[i] = ios.Int();
            }
        h = new int[n];
        for (int i = 1; i < n; ++i) {
            h[i] = Math.abs(a[i + 1] - a[i]);
        }
        tree = new SegmentTree(n - 1, h);
        for (int i = 0; i < q; ++i) {
            ios.get();
                int l = ios.Int(), r = ios.Int() - 1;
            ios.wn(get(l, r));
        }
    }

    long get(int l, int r) {
        if (l == r) return h[l];
        int m = tree.info(l, r);
        long res = ((long)m - l + 1) * ((long)r - m + 1) * (long)h[m];
        if (l < m) res += get(l, m - 1);
        if (m < r) res += get(m + 1, r);
        return res;
    }
    class SegmentTree {
        int a[], n, d[], p[];
        SegmentTree(int N, int[] A) {
            a = A; n = N;
            d = new int[n << 2 + 1];
            p = new int[n + 1];
            build(1, 1, n);
        }

        void build(int x, int i, int j) {
            if (i == j) {
                p[i] = x; d[x] = i;
            } else {
                int m = (i + j) >> 1;
                    build(x + x, i, m);
                    build(x + x + 1, m + 1, j);
                d[x] = d[x + x + 1];
                    if (a[d[x]] < a[d[x + x]]) {
                        d[x] = d[x + x];
                    }
            }
        }

        int get(int x, int i, int j, int l, int r) {
            if (i >= l && j <= r) return d[x];
            if (i > r || j < l || i >= j) return 0;
            int m = (i + j) >> 1;
            int u = get(x + x, i, m, l, r);
            int v = get(x + x + 1, m + 1, j, l, r);
            if (a[u] < a[v]) return v;
            return u;
        }
        int info(int l, int r) {
            return get(1, 1, n, l, r);
        }
    }
}

class IOS {
    int buffer = 1 << 16;
    BufferedReader  inp;
    BufferedWriter  out;
    IOS(String Inp, String Out) throws Exception {
        inp = new BufferedReader(Inp != "" ? (new FileReader(Inp)) : (new InputStreamReader(System.in)), buffer);
        out = new BufferedWriter(Out != "" ? (new FileWriter(Out)) : (new OutputStreamWriter(System.out)), buffer);
    }
    void close() throws Exception {
        out.flush(); inp.close(); out.close();
    }
    StringTokenizer i;
    void    get() throws Exception {i = new StringTokenizer(inp.readLine());}
    long    Long() throws Exception {return Long.parseLong(i.nextToken());}
    int     Int() throws Exception {return Integer.parseInt(i.nextToken());}
    String  word() throws Exception {return i.nextToken();}
    String  str() throws Exception {return inp.readLine();}
    <T> void w(T e) throws Exception {out.write(String.valueOf(e));}
    <T> void wn(T e) throws Exception {out.write(String.valueOf(e)); out.newLine();}
    <T> void wa(T... a) throws Exception {for (T e: a) {out.write(String.valueOf(e)); out.write(" ");} out.newLine();}

    long gcd(long u, long v) { long w = 0; while (u != 0) { w = u; u = v % u; v = w;} return v; }
    long lcm(long u, long v) {
        return u / gcd(u, v) * v;
    }
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    IOS ios;
    public static void main(String[] args) throws Exception {
        (new Main()).main();
    }

    void main() throws Exception {
        //ios = new IOS("main.in","");
        ios = new IOS("","");
        run();
        ios.close();
    }

    int n, q, a[], h[], L[];
    void run() throws Exception {
        ios.get();
            n = ios.Int();
            q = ios.Int();
        ios.get();
            a = new int[n+1];
            h = new int[n];
            L = new int[n];
            for (int i = 1; i <= n; ++i) {
                a[i] = ios.Int();
                h[i - 1] = Math.abs(a[i] - a[i - 1]);
            }
        for (int i = 0; i < q; ++i) {
            ios.get();
                int l = ios.Int(), r = ios.Int() - 1;
            ios.wn((l > r) ? 0 : get(l, r));
        }
    }

    long get(int l, int r) throws Exception {
        Stack<Integer> s = new Stack<>();
        long res = 0;
        s.add(l - 1);
        for (int i = l; i <= r; ++i) {
            while (s.size() > 1 && h[i] >= h[s.peek()]) s.pop();
            L[i] = s.peek(); s.add(i);
        }
        s = new Stack<>();
        s.add(r + 1);
        for (int i = r; i >= l; --i) {
            while (s.size() > 1 && h[i] > h[s.peek()]) s.pop();
            res += ((long)i - L[i]) * ((long)s.peek() - i) * (long)h[i]; s.add(i);
        }
        return res;
    }
}

class IOS {
    int buffer = 1 << 16;
    BufferedReader  inp;
    BufferedWriter  out;
    IOS(String Inp, String Out) throws Exception {
        inp = new BufferedReader(Inp != "" ? (new FileReader(Inp)) : (new InputStreamReader(System.in)), buffer);
        out = new BufferedWriter(Out != "" ? (new FileWriter(Out)) : (new OutputStreamWriter(System.out)), buffer);
    }
    void close() throws Exception {
        out.flush(); inp.close(); out.close();
    }
    StringTokenizer i;
    void    get() throws Exception {i = new StringTokenizer(inp.readLine());}
    long    Long() throws Exception {return Long.parseLong(i.nextToken());}
    int     Int() throws Exception {return Integer.parseInt(i.nextToken());}
    String  word() throws Exception {return i.nextToken();}
    String  str() throws Exception {return inp.readLine();}
    <T> void w(T e) throws Exception {out.write(String.valueOf(e));}
    <T> void wn(T e) throws Exception {out.write(String.valueOf(e)); out.newLine();}
    <T> void wa(T... a) throws Exception {for (T e: a) {out.write(String.valueOf(e)); out.write(" ");} out.newLine();}

    long gcd(long u, long v) { long w = 0; while (u != 0) { w = u; u = v % u; v = w;} return v; }
    long lcm(long u, long v) {
        return u / gcd(u, v) * v;
    }
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    IOS ios;
    public static void main(String[] args) throws Exception {
        (new Main()).main();
    }

    void main() throws Exception {
        //ios = new IOS("main.in","");
        ios = new IOS("","");
        run();
        ios.close();
    }

    int n, q, a[], h[], L[], s[], N;
    void run() throws Exception {
        ios.get();
            n = ios.Int();
            q = ios.Int();
        ios.get();
            a = new int[n+1];
            h = new int[n];
            L = new int[n];
            s = new int[n];
            for (int i = 1; i <= n; ++i) {
                a[i] = ios.Int();
                h[i - 1] = Math.abs(a[i] - a[i - 1]);
            }
        for (int i = 0; i < q; ++i) {
            ios.get();
                int l = ios.Int(), r = ios.Int() - 1;
            ios.wn((l > r) ? 0 : get(l, r));
        }
    }

    long get(int l, int r) throws Exception {
        long res = 0;
        N = 0;
        s[N] = l - 1;
        for (int i = l; i <= r; ++i) {
            while (N > 0 && h[i] >= h[s[N]]) --N;
            L[i] = s[N]; s[++N] = i;
        }
        N = 0;
        s[N] = r + 1;
        for (int i = r; i >= l; --i) {
            while (N > 0 && h[i] > h[s[N]]) --N;
            res += ((long)i - L[i]) * ((long)s[N] - i) * (long)h[i]; s[++N] = i;
        }
        return res;
    }
}

class IOS {
    int buffer = 1 << 16;
    BufferedReader  inp;
    BufferedWriter  out;
    IOS(String Inp, String Out) throws Exception {
        inp = new BufferedReader(Inp != "" ? (new FileReader(Inp)) : (new InputStreamReader(System.in)), buffer);
        out = new BufferedWriter(Out != "" ? (new FileWriter(Out)) : (new OutputStreamWriter(System.out)), buffer);
    }
    void close() throws Exception {
        out.flush(); inp.close(); out.close();
    }
    StringTokenizer i;
    void    get() throws Exception {i = new StringTokenizer(inp.readLine());}
    long    Long() throws Exception {return Long.parseLong(i.nextToken());}
    int     Int() throws Exception {return Integer.parseInt(i.nextToken());}
    String  word() throws Exception {return i.nextToken();}
    String  str() throws Exception {return inp.readLine();}
    <T> void w(T e) throws Exception {out.write(String.valueOf(e));}
    <T> void wn(T e) throws Exception {out.write(String.valueOf(e)); out.newLine();}
    <T> void wa(T... a) throws Exception {for (T e: a) {out.write(String.valueOf(e)); out.write(" ");} out.newLine();}

    long gcd(long u, long v) { long w = 0; while (u != 0) { w = u; u = v % u; v = w;} return v; }
    long lcm(long u, long v) {
        return u / gcd(u, v) * v;
    }
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    IOS ios;
    public static void main(String[] args) throws Exception {
        (new Main()).main();
    }

    void main() throws Exception {
        //ios = new IOS("main.in","");
        ios = new IOS("","");
        run();
        ios.close();
    }

    int n, q, a[], h[];
    SegmentTree tree;
    void run() throws Exception {
        ios.get();
            n = ios.Int();
            q = ios.Int();
        ios.get();
            a = new int[n+1];
            for (int i = 1; i <= n; ++i) {
                a[i] = ios.Int();
            }
        h = new int[n];
        for (int i = 1; i < n; ++i) {
            h[i] = Math.abs(a[i + 1] - a[i]);
        }
        tree = new SegmentTree(n - 1, h);
        for (int i = 0; i < q; ++i) {
            ios.get();
                int l = ios.Int(), r = ios.Int() - 1;
            ios.wn(get(l, r));
        }
    }

    long get(int l, int r) {
        if (l == r) return h[l];
        int m = tree.info(l, r);
        long res = ((long)m - l + 1) * ((long)r - m + 1) * (long)h[m];
        if (l < m) res += get(l, m - 1);
        if (m < r) res += get(m + 1, r);
        return res;
    }
    class SegmentTree {
        int a[], n, d[], p[];
        SegmentTree(int N, int[] A) {
            a = A; n = N;
            d = new int[n << 2 + 1];
            p = new int[n + 1];
            build(1, 1, n);
        }

        void build(int x, int i, int j) {
            if (i == j) {
                p[i] = x; d[x] = i;
            } else {
                int m = (i + j) >> 1;
                    build(x + x, i, m);
                    build(x + x + 1, m + 1, j);
                d[x] = d[x + x + 1];
                    if (a[d[x]] < a[d[x + x]]) {
                        d[x] = d[x + x];
                    }
            }
        }

        int get(int x, int i, int j, int l, int r) {
            if (i > r || j < l) return 0;
            if (i >= l && j <= r) return d[x];
            int m = (i + j) >> 1;
            int u = get(x + x, i, m, l, r);
            int v = get(x + x + 1, m + 1, j, l, r);
            if (a[u] < a[v]) return v;
            return u;
        }
        int info(int l, int r) {
            return get(1, 1, n, l, r);
        }
    }
}

class IOS {
    int buffer = 1 << 16;
    BufferedReader  inp;
    BufferedWriter  out;
    IOS(String Inp, String Out) throws Exception {
        inp = new BufferedReader(Inp != "" ? (new FileReader(Inp)) : (new InputStreamReader(System.in)), buffer);
        out = new BufferedWriter(Out != "" ? (new FileWriter(Out)) : (new OutputStreamWriter(System.out)), buffer);
    }
    void close() throws Exception {
        out.flush(); inp.close(); out.close();
    }
    StringTokenizer i;
    void    get() throws Exception {i = new StringTokenizer(inp.readLine());}
    long    Long() throws Exception {return Long.parseLong(i.nextToken());}
    int     Int() throws Exception {return Integer.parseInt(i.nextToken());}
    String  word() throws Exception {return i.nextToken();}
    String  str() throws Exception {return inp.readLine();}
    <T> void w(T e) throws Exception {out.write(String.valueOf(e));}
    <T> void wn(T e) throws Exception {out.write(String.valueOf(e)); out.newLine();}
    <T> void wa(T... a) throws Exception {for (T e: a) {out.write(String.valueOf(e)); out.write(" ");} out.newLine();}

    long gcd(long u, long v) { long w = 0; while (u != 0) { w = u; u = v % u; v = w;} return v; }
    long lcm(long u, long v) {
        return u / gcd(u, v) * v;
    }
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    IOS ios;
    public static void main(String[] args) throws Exception {
        (new Main()).main();
    }

    void main() throws Exception {
        //ios = new IOS("main.in","");
        ios = new IOS("","");
        run();
        ios.close();
    }

    int n, q, a[], h[];
    SegmentTree tree;
    void run() throws Exception {
        ios.get();
            n = ios.Int();
            q = ios.Int();
        ios.get();
            a = new int[n+1];
            for (int i = 1; i <= n; ++i) {
                a[i] = ios.Int();
            }
        h = new int[n];
        for (int i = 1; i < n; ++i) {
            h[i] = Math.abs(a[i + 1] - a[i]);
        }
        tree = new SegmentTree(n - 1, h);
        for (int i = 0; i < q; ++i) {
            ios.get();
                int l = ios.Int(), r = ios.Int() - 1;
            ios.wn((l > r) ? 0 : get(l, r));
        }
//        ios.wn(tree.info(2,5));
    }

    long get(int l, int r) throws Exception {
        if (l == r) return h[l];
        if (l > r) return 0;
        int m = tree.info(l, r);
        long res = ((long)m - l + 1L) * ((long)r - m + 1L) * (long)h[m];
        if (l < m) res += get(l, m - 1);
        if (m < r) res += get(m + 1, r);
        return res;
    }
    class SegmentTree {
        int a[], n, d[];
        SegmentTree(int N, int[] A) {
            a = A; n = N;
            d = new int[(n << 2) + 1];
            build(1, 1, n);
        }

        void build(int x, int i, int j) {
            if (i == j) {
                d[x] = i;
            } else {
                int m = (i + j) >> 1;
                    build(x + x, i, m);
                    build(x + x + 1, m + 1, j);
                d[x] = d[x + x + 1];
                    if (a[d[x]] <= a[d[x + x]]) {
                        d[x] = d[x + x];
                    }
            }
        }

        int get(int x, int i, int j, int l, int r) throws Exception {
            if (i >= l && j <= r) return d[x];
            if (i > r  || j < l) return 0;
            int m = (i + j) >> 1;
            int u = get(x + x, i, m, l, r);
            int v = get(x + x + 1, m + 1, j, l, r);
            if (u == 0 || a[u] < a[v]) return v;
            return u;
        }
        int info(int l, int r) throws Exception {
            return get(1, 1, n, l, r);
        }
    }
}

class IOS {
    int buffer = 1 << 16;
    BufferedReader  inp;
    BufferedWriter  out;
    IOS(String Inp, String Out) throws Exception {
        inp = new BufferedReader(Inp != "" ? (new FileReader(Inp)) : (new InputStreamReader(System.in)), buffer);
        out = new BufferedWriter(Out != "" ? (new FileWriter(Out)) : (new OutputStreamWriter(System.out)), buffer);
    }
    void close() throws Exception {
        out.flush(); inp.close(); out.close();
    }
    StringTokenizer i;
    void    get() throws Exception {i = new StringTokenizer(inp.readLine());}
    long    Long() throws Exception {return Long.parseLong(i.nextToken());}
    int     Int() throws Exception {return Integer.parseInt(i.nextToken());}
    String  word() throws Exception {return i.nextToken();}
    String  str() throws Exception {return inp.readLine();}
    <T> void w(T e) throws Exception {out.write(String.valueOf(e));}
    <T> void wn(T e) throws Exception {out.write(String.valueOf(e)); out.newLine();}
    <T> void wa(T... a) throws Exception {for (T e: a) {out.write(String.valueOf(e)); out.write(" ");} out.newLine();}

    long gcd(long u, long v) { long w = 0; while (u != 0) { w = u; u = v % u; v = w;} return v; }
    long lcm(long u, long v) {
        return u / gcd(u, v) * v;
    }
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    IOS ios;
    public static void main(String[] args) throws Exception {
        (new Main()).main();
    }

    void main() throws Exception {
        //ios = new IOS("main.in","");
        ios = new IOS("","");
        run();
        ios.close();
    }

    int n, q, a[], h[];
    SegmentTree tree;
    void run() throws Exception {
        ios.get();
            n = ios.Int();
            q = ios.Int();
        ios.get();
            a = new int[n+1];
            for (int i = 1; i <= n; ++i) {
                a[i] = ios.Int();
            }
        h = new int[n];
        for (int i = 1; i < n; ++i) {
            h[i] = Math.abs(a[i + 1] - a[i]);
        }
        tree = new SegmentTree(n - 1, h);
        for (int i = 0; i < q; ++i) {
            ios.get();
                int l = ios.Int(), r = ios.Int() - 1;
            ios.wn((l > r) ? 0 : get(l, r));
        }
    }

    long get(int l, int r) {
        if (l == r) return h[l];
        int m = tree.info(l, r);
        long res = ((long)m - l + 1) * ((long)r - m + 1) * (long)h[m];
        if (l < m) res += get(l, m - 1);
        if (m < r) res += get(m + 1, r);
        return res;
    }
    class SegmentTree {
        int a[], n, d[];
        SegmentTree(int N, int[] A) {
            a = A; n = N;
            d = new int[(n << 2) + 1];
            build(1, 1, n);
        }

        void build(int x, int i, int j) {
            if (i == j) {
                d[x] = i;
            } else {
                int m = (i + j) >> 1;
                    build(x + x, i, m);
                    build(x + x + 1, m + 1, j);
                d[x] = d[x + x + 1];
                    if (a[d[x]] <= a[d[x + x]]) {
                        d[x] = d[x + x];
                    }
            }
        }

        int get(int x, int i, int j, int l, int r) {
            if (i >= l && j <= r) return d[x];
            if (i > r  || j < l) return 0;
            int m = (i + j) >> 1;
            int u = get(x + x, i, m, l, r);
            int v = get(x + x + 1, m + 1, j, l, r);
            if (a[u] <= a[v]) return v;
            return u;
        }
        int info(int l, int r) {
            return get(1, 1, n, l, r);
        }
    }
}

class IOS {
    int buffer = 1 << 16;
    BufferedReader  inp;
    BufferedWriter  out;
    IOS(String Inp, String Out) throws Exception {
        inp = new BufferedReader(Inp != "" ? (new FileReader(Inp)) : (new InputStreamReader(System.in)), buffer);
        out = new BufferedWriter(Out != "" ? (new FileWriter(Out)) : (new OutputStreamWriter(System.out)), buffer);
    }
    void close() throws Exception {
        out.flush(); inp.close(); out.close();
    }
    StringTokenizer i;
    void    get() throws Exception {i = new StringTokenizer(inp.readLine());}
    long    Long() throws Exception {return Long.parseLong(i.nextToken());}
    int     Int() throws Exception {return Integer.parseInt(i.nextToken());}
    String  word() throws Exception {return i.nextToken();}
    String  str() throws Exception {return inp.readLine();}
    <T> void w(T e) throws Exception {out.write(String.valueOf(e));}
    <T> void wn(T e) throws Exception {out.write(String.valueOf(e)); out.newLine();}
    <T> void wa(T... a) throws Exception {for (T e: a) {out.write(String.valueOf(e)); out.write(" ");} out.newLine();}

    long gcd(long u, long v) { long w = 0; while (u != 0) { w = u; u = v % u; v = w;} return v; }
    long lcm(long u, long v) {
        return u / gcd(u, v) * v;
    }
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    IOS ios;
    public static void main(String[] args) throws Exception {
        (new Main()).main();
    }

    void main() throws Exception {
        //ios = new IOS("main.in","");
        ios = new IOS("","");
        run();
        ios.close();
    }

    int n, q, a[], h[];
    SegmentTree tree;
    void run() throws Exception {
        ios.get();
            n = ios.Int();
            q = ios.Int();
        ios.get();
            a = new int[n+1];
            for (int i = 1; i <= n; ++i) {
                a[i] = ios.Int();
            }
        h = new int[n];
        for (int i = 1; i < n; ++i) {
            h[i] = Math.abs(a[i + 1] - a[i]);
        }
        tree = new SegmentTree(n - 1, h);
        for (int i = 0; i < q; ++i) {
            ios.get();
                int l = ios.Int(), r = ios.Int() - 1;
            ios.wn(get(l, r));
        }
    }

    long get(int l, int r) {
        if (l == r) return h[l];
        int m = tree.info(l, r);
        long res = ((long)m - l + 1) * ((long)r - m + 1) * (long)h[m];
        if (l < m) res += get(l, m - 1);
        if (m < r) res += get(m + 1, r);
        return res;
    }
    class SegmentTree {
        int a[], n, d[], p[];
        SegmentTree(int N, int[] A) {
            a = A; n = N;
            d = new int[n << 2 + 1];
            p = new int[n + 1];
            build(1, 1, n);
        }

        void build(int x, int i, int j) {
            if (i == j) {
                p[i] = x; d[x] = i;
            } else {
                int m = (i + j) >> 1;
                    build(x + x, i, m);
                    build(x + x + 1, m + 1, j);
                d[x] = d[x + x + 1];
                    if (a[d[x]] < a[d[x + x]]) {
                        d[x] = d[x + x];
                    }
            }
        }

        int get(int x, int i, int j, int l, int r) {
            if (i > r || j < l || i > j) return 0;
            if (i >= l && j <= r) return d[x];
            int m = (i + j) >> 1;
            int u = get(x + x, i, m, l, r);
            int v = get(x + x + 1, m + 1, j, l, r);
            if (a[u] < a[v]) return v;
            return u;
        }
        int info(int l, int r) {
            return get(1, 1, n, l, r);
        }
    }
}

class IOS {
    int buffer = 1 << 16;
    BufferedReader  inp;
    BufferedWriter  out;
    IOS(String Inp, String Out) throws Exception {
        inp = new BufferedReader(Inp != "" ? (new FileReader(Inp)) : (new InputStreamReader(System.in)), buffer);
        out = new BufferedWriter(Out != "" ? (new FileWriter(Out)) : (new OutputStreamWriter(System.out)), buffer);
    }
    void close() throws Exception {
        out.flush(); inp.close(); out.close();
    }
    StringTokenizer i;
    void    get() throws Exception {i = new StringTokenizer(inp.readLine());}
    long    Long() throws Exception {return Long.parseLong(i.nextToken());}
    int     Int() throws Exception {return Integer.parseInt(i.nextToken());}
    String  word() throws Exception {return i.nextToken();}
    String  str() throws Exception {return inp.readLine();}
    <T> void w(T e) throws Exception {out.write(String.valueOf(e));}
    <T> void wn(T e) throws Exception {out.write(String.valueOf(e)); out.newLine();}
    <T> void wa(T... a) throws Exception {for (T e: a) {out.write(String.valueOf(e)); out.write(" ");} out.newLine();}

    long gcd(long u, long v) { long w = 0; while (u != 0) { w = u; u = v % u; v = w;} return v; }
    long lcm(long u, long v) {
        return u / gcd(u, v) * v;
    }
}


import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    IOS ios;
    public static void main(String[] args) throws Exception {
        (new Main()).main();
    }

    void main() throws Exception {
        //ios = new IOS("main.in","");
        ios = new IOS("","");
        run();
        ios.close();
    }

    int n, q, a[], h[], L[], R[];
    void run() throws Exception {
        ios.get();
            n = ios.Int();
            q = ios.Int();
        ios.get();
            a = new int[n+1];
            for (int i = 1; i <= n; ++i) {
                a[i] = ios.Int();
            }
        h = new int[n];
        L = new int[n];
        R = new int[n];
        for (int i = 1; i < n; ++i) {
            h[i] = Math.abs(a[i + 1] - a[i]);
        }
        for (int i = 0; i < q; ++i) {
            ios.get();
                int l = ios.Int(), r = ios.Int() - 1;
            ios.wn((l > r) ? 0 : get(l, r));
        }
    }

    long get(int l, int r) throws Exception {
        Stack<Integer> s = new Stack<>();
        long res = 0;
        s.add(l - 1);
        for (int i = l; i <= r; ++i) {
            while (s.size() > 1 && h[i] >= h[s.peek()]) s.pop();
            L[i] = s.peek(); s.add(i);
        }
        s.clear();
        s.add(r + 1);
        for (int i = r; i >= l; --i) {
            while (s.size() > 1 && h[i] > h[s.peek()]) s.pop();
            R[i] = s.peek(); s.add(i);
        }
        for (int i = l; i <= r; ++i) {
            res += ((long)i - L[i]) * ((long)R[i] - i) * (long)h[i];
        }
        return res;
    }
}

class IOS {
    int buffer = 1 << 16;
    BufferedReader  inp;
    BufferedWriter  out;
    IOS(String Inp, String Out) throws Exception {
        inp = new BufferedReader(Inp != "" ? (new FileReader(Inp)) : (new InputStreamReader(System.in)), buffer);
        out = new BufferedWriter(Out != "" ? (new FileWriter(Out)) : (new OutputStreamWriter(System.out)), buffer);
    }
    void close() throws Exception {
        out.flush(); inp.close(); out.close();
    }
    StringTokenizer i;
    void    get() throws Exception {i = new StringTokenizer(inp.readLine());}
    long    Long() throws Exception {return Long.parseLong(i.nextToken());}
    int     Int() throws Exception {return Integer.parseInt(i.nextToken());}
    String  word() throws Exception {return i.nextToken();}
    String  str() throws Exception {return inp.readLine();}
    <T> void w(T e) throws Exception {out.write(String.valueOf(e));}
    <T> void wn(T e) throws Exception {out.write(String.valueOf(e)); out.newLine();}
    <T> void wa(T... a) throws Exception {for (T e: a) {out.write(String.valueOf(e)); out.write(" ");} out.newLine();}

    long gcd(long u, long v) { long w = 0; while (u != 0) { w = u; u = v % u; v = w;} return v; }
    long lcm(long u, long v) {
        return u / gcd(u, v) * v;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Stack;

public class LipshitzSequence {

	public static class node {
		int x;
		int pos;

		node(int x, int p) {
			this.x = x;
			pos = p;
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder qq = new StringBuilder();
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		String y[] = in.readLine().split(" ");
		int n = Integer.parseInt(y[0]);
		int q = Integer.parseInt(y[1]);
		int a[] = new int[n];
		y = in.readLine().split(" ");

		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(y[i]);
		}

		int dp[] = new int[n - 1];
		for (int i = 0; i < n - 1; i++) {
			dp[i] = Math.abs(a[i] - a[i + 1]);
		}

		int maxLeft[] = new int[n - 1];
		int maxright[] = new int[n - 1];

		Stack<node> s = new Stack();
		s.add(new node(dp[0], 0));
		for (int i = 1; i < n - 1; i++) {
			while (!s.isEmpty() && dp[i] > s.peek().x) {
				node e = s.pop();
				maxLeft[e.pos] = i - e.pos;
			}

			s.add(new node(dp[i], i));

		}

		while (!s.isEmpty()) {
			node e = s.pop();
			maxLeft[e.pos] = n - 1 - e.pos;
		}

		s.add(new node(dp[n - 2], n - 2));
		for (int i = n - 3; i >= 0; i--) {
			while (!s.isEmpty() && dp[i] >= s.peek().x) {
				node e = s.pop();
				maxright[e.pos] = e.pos - i;
			}

			s.add(new node(dp[i], i));

		}

		while (!s.isEmpty()) {
			node e = s.pop();
			maxright[e.pos] = e.pos + 1;
		}

		for (int i = 0; i < q; i++) {
			y = in.readLine().split(" ");
			int l = Integer.parseInt(y[0]) - 1;
			int r = Integer.parseInt(y[1]) - 1;
			long ans = 0;
			for (int j = l; j < r; j++) {
				ans += (long) dp[j] * (long) Math.min(maxLeft[j], r - j)
						* (long) Math.min(maxright[j], j - l + 1);
			}
			out.println(ans);
		}

		out.close();

	}

}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class a {
	public static int k;

	public static class node implements Comparable<node> {
		int x;
		int y;

		node(int x, int y) {
			this.x = x;
			this.y = y;

		}

		@Override
		public int compareTo(node o) {
			if (o.y < y)
				return 1;
			else if (o.y > y)
				return -1;
			else if (o.x < x)
				return 1;
			else if (o.x > x)
				return -1;
			return 0;
		}

	}

	public static class point {
		int x;
		int y;

		point(int x, int val) {
			this.x = x;
			this.y = val;
		}

	}

	public static BigInteger gcd(BigInteger a, BigInteger b) {
		if (b.equals(BigInteger.ZERO))
			return a;
		return gcd(b, a.mod(b));
	}

	public static long gcd1(long a, long b) {
		if (b == 0)
			return a;
		return gcd1(b, a % b);
	}

	public static ArrayList<Integer> prime;

	public static void sieve(int n) {
		boolean vis[] = new boolean[n + 5];
		vis[1] = true;
		vis[0] = true;
		for (int i = 2; i <= n; i++) {
			if (!vis[i]) {
				prime.add(i);
				for (long j = (long) i * (long) i; j <= n; j += i) {
					vis[(int) j] = true;
				}
			}
		}
	}

	public static int tree[][];
	public static boolean end[][];
	public static int counter;

	public static void trie(String a) {
		int st = 0;

		for (int i = 0; i < a.length(); i++) {
			if (tree[a.charAt(i) - 'a'][st] == 0) {
				tree[a.charAt(i) - 'a'][st] = counter++;
			}

			st = tree[a.charAt(i) - 'a'][st];
		}
		end[a.charAt(a.length() - 1) - 'a'][st] = true;

	}

	public static int seg[][];

	public static int get(int s, int e, int p, int from, int to, int seg[]) {
		if (s >= from && e <= to)
			return seg[p];
		if (s > to || e < from)
			return 0;
		int mid = (s + e) / 2;
		int one = get(s, mid, p * 2, from, to, seg);
		int two = get(mid + 1, e, p * 2 + 1, from, to, seg);
		return Math.max(one, two);
	}

	public static long comp(int n, int r) {
		long ans = 1;
		r = Math.min(r, n - r);
		for (int i = 1; i <= r; i++) {
			ans = ans * ((long) n - (long) i + 1L) / (long) i;
		}
		return ans;
	}

	public static ArrayList<Integer> arr[];
	public static boolean vis[];
	public static Stack<Integer> S;

	public static void bfs(int node) {
		for (int i = 0; i < arr[node].size(); i++) {
			int k = arr[node].get(i);
			if (vis[k] == false) {
				vis[k] = true;
				S.push(k);
				bfs(k);
			}
		}
		S.push(node);
	}

	public static void dfs(int node) {

		for (int i = 0; i < arr[node].size(); i++) {
			int k = arr[node].get(i);
			if (vis[k] == false) {
				vis[k] = true;
				dfs(k);
			}
		}
	}

	public static boolean chPrime(int r) {

		for (int i = 2; i * i <= r; i++) {
			if (r % i == 0)
				return false;
		}
		return true;
	}

	public static int len(long ai) {
		String temp = "" + ai;

		return temp.length();
	}

	public static int f_digit(long ai) {
		String temp = "" + ai;

		return (temp.charAt(0) - '0');

	}

	public static double cross(node a, node b) {

		return a.x * b.y - b.x * a.y;
	}

	public static double dot(node a, node b) {

		return a.x * b.x + a.y * b.y;
	}

	public static double sqrt(node a) {
		double e = Math.sqrt((double) a.x * (double) a.x + (double) a.y
				* (double) a.y);
		return e;
	}

	public static double theta(node a, node b) {

		// double theta = Math.atan2((double) Math.abs(cross(a, b)),
		// (double) dot(a, b));

		double theta = (double) Math.acos((double) dot(a, b)
				/ ((sqrt(a)) * sqrt(b)));
		return theta;
	}

	public static int binary(int a[], int val) {

		int low = 0;
		int high = a.length;

		int mid = (low + high) / 2;
		while (high - low > 1) {
			if (a[mid] <= val) {
				low = mid;
			} else if (a[mid] > val)
				high = mid;

			mid = (low + high) / 2;
		}
		if (a[high] == val)
			return high;
		return low;
	}

	public static long mod = (long) Math.pow(10, 9) + 7;

	public static int bin(int a[], int val) {

		int low = 0;
		int high = a.length - 1;

		int mid = (low + high) / 2;

		while (high - low > 1) {
			if (a[mid] > val) {
				high = mid;
			} else
				low = mid;

			mid = (low + high) / 2;
		}
		if (a[low] > val)
			return -1;
		else if (a[high] > val)
			return low;
		return high;

	}

	public static int dig(boolean a[][], boolean f, int n) {
		int dp[] = new int[n + 1];
		Arrays.fill(dp, Integer.MAX_VALUE);
		PriorityQueue<node> q = new PriorityQueue<>();
		dp[1] = 0;
		q.add(new node(1, 0));
		boolean ok[] = new boolean[n + 1];
		while (!q.isEmpty()) {
			node e = q.poll();
			if (ok[e.x])
				continue;
			if (e.x == n)
				return dp[n];
			ok[e.x] = true;

			for (int i = 1; i <= n; i++) {
				if (a[e.x][i] == f && !ok[i] && dp[i] > dp[e.x] + 1) {
					dp[i] = dp[e.x] + 1;
					q.add(new node(i, dp[i]));
				}
			}

		}

		return -1;

	}

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		// BufferedReader in = new BufferedReader(new FileReader("cubes.in"));
		// System.out.println(-70 % 3);
		StringBuilder qq = new StringBuilder();
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		System.out.println(8+3+21+8*8+8+8);

		String y[] = in.readLine().split(" ");
		int n = Integer.parseInt(y[0]);
		int m = Integer.parseInt(y[1]);

		boolean a[][] = new boolean[n + 1][n + 1];
		boolean ok = true;
		for (int i = 0; i < m; i++) {
			y = in.readLine().split(" ");
			int one = Integer.parseInt(y[0]);
			int two = Integer.parseInt(y[1]);
			a[one][two] = true;
			a[two][one] = true;
			if ((one == 1 && two == n) || (one == n && two == 1))
				ok = false;
		}

		int ans = dig(a, ok, n);

			System.out.println(ans);

		out.close();

	}
}


/**
 * @author nellex
 * Created at: 26-Aug-2016 1:52:23 PM
 */

import java.util.Scanner;
import java.util.Stack;

public class CF601B {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int q = in.nextInt();

		int v[] = new int[n+1];

		for( int i = 0; i < n; i++ )
			v[i] = in.nextInt();

		int diff[] = new int[n+1];

		for( int i = 1; i < n; i++ )
			diff[i] = Math.abs(v[i]-v[i-1]);

		int R[] = new int[n+1];
		int L[] = new int[n+1];

		Stack<Integer> st = new Stack<Integer>();

		diff[0] = diff[n] = Integer.MAX_VALUE;

		for( int i = 1; i <= n; i++ ) {
			while( !st.empty() && diff[st.peek()] < diff[i] ) {
				R[st.peek()] = i-1;
				st.pop();
			}
			st.push(i);
		}

		st.clear();

		for( int i = n-1; i >= 0; i-- ) {
			while( !st.empty() && diff[st.peek()] <= diff[i] ) {
				L[st.peek()] = i+1;
				st.pop();
			}
			st.push(i);
		}

		while( q-- > 0 ) {
			int l = in.nextInt();
			int r = in.nextInt();

			int ans = 0;

			for( int i = l; i < r; i++ )
				ans += diff[i] * ( i - Math.max(l, L[i]) + 1 ) * ( Math.min(r-1, R[i]) - i + 1);

			System.out.println(ans);
		}

	}

}


/**
 * @author nellex
 * Created at: 26-Aug-2016 1:52:23 PM
 */

import java.util.Scanner;
import java.util.Stack;

public class CF601B {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int q = in.nextInt();

		int v[] = new int[n+1];

		for( int i = 0; i < n; i++ )
			v[i] = in.nextInt();

		int diff[] = new int[n+1];

		for( int i = 1; i < n; i++ )
			diff[i] = Math.abs(v[i]-v[i-1]);

		int R[] = new int[n+1];
		int L[] = new int[n+1];

		Stack<Integer> st = new Stack<Integer>();

		diff[0] = diff[n] = Integer.MAX_VALUE;

		for( int i = 1; i <= n; i++ ) {
			while( !st.empty() && diff[st.peek()] < diff[i] ) {
				R[st.peek()] = i-1;
				st.pop();
			}
			st.push(i);
		}

		st.clear();

		for( int i = n-1; i >= 0; i-- ) {
			while( !st.empty() && diff[st.peek()] <= diff[i] ) {
				L[st.peek()] = i+1;
				st.pop();
			}
			st.push(i);
		}

		while( q-- > 0 ) {
			int l = in.nextInt();
			int r = in.nextInt();

			long ans = 0;

			for( int i = l; i < r; i++ )
				ans += diff[i] * (long)( i - Math.max(l, L[i]) + 1 ) * (long)( Math.min(r-1, R[i]) - i + 1);

			System.out.println(ans);
		}

	}

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.util.NoSuchElementException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Ruins He
 */
public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    OutputWriter out = new OutputWriter(outputStream);
    TaskB solver = new TaskB();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskB {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
      int n = in.nextInt();
      int m = in.nextInt();
      int[] h = in.nextInts(n);
      int[] left = new int[n];
      int[] right = new int[n];
      for (int i = n - 1; i > 0; i--) {
        h[i] -= h[i - 1];
        if (h[i] < 0) h[i] = -h[i];
      }
      for (int i = 1; i < n; i++) {
        left[i] = i;
        while (left[i] > 1 && h[i] > h[left[i] - 1]) left[i] = left[left[i] - 1];
      }
      for (int i = n - 1; i > 0; i--) {
        right[i] = i;
        while (right[i] < n - 1 && h[i] >= h[right[i] + 1]) right[i] = right[right[i] + 1];
      }
      for (int i = 0; i < m; i++) {
        int l = in.nextInt() - 1;
        int r = in.nextInt() - 1;
        if (r - l + 1 < 2) {
          out.println(0);
          continue;
        }
        long sum = 0;
        for (int j = l + 1; j <= r; j++) {
          sum += (long) (Math.min(r, right[j]) - j + 1) * (j - Math.max(l + 1, left[j]) + 1) * h[j];
        }
        out.println(sum);
      }
    }

  }

  static class OutputWriter {
    private final PrintWriter writer;

    private OutputWriter(Writer writer, boolean autoFlush) {
      this.writer = new PrintWriter(new BufferedWriter(writer, 32767), autoFlush);
    }

    public OutputWriter(Writer writer) {
      this(writer, true);
    }

    public OutputWriter(OutputStream outputStream) {
      this(new OutputStreamWriter(outputStream), false);
    }

    public OutputWriter println(int value) {
      writer.println(value);
      return this;
    }

    public OutputWriter println(long value) {
      writer.println(value);
      return this;
    }

    public void close() {
      writer.close();
    }

  }

  static class InputReader {
    public final BufferedReader reader;
    public StringTokenizer tokenizer = null;

    public InputReader(Reader reader) {
      this.reader = new BufferedReader(reader, 32767);
    }

    public InputReader(InputStream inputStream) {
      this(new InputStreamReader(inputStream));
    }

    public String next() {
      if (hasNext()) return tokenizer.nextToken();
      else throw new NoSuchElementException();
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public int[] nextInts(int n) {
      int[] res = new int[n];
      for (int i = 0; i < n; i++) res[i] = nextInt();
      return res;
    }

    public boolean hasNext() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          String nextLine = reader.readLine();
          if (nextLine == null) return false;
          tokenizer = new StringTokenizer(nextLine);
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.hasMoreTokens();
    }

  }
}


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

#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define mp make_pair

int main()
{
  int n;
  cin>>n;
  vector< pair<int,int> > v(n);
  for(int i=0;i<n;i++)
  {
      int x;
      cin>>x;
      v[i]={x,i};
  }
  ll sum=0;
  sort(v.rbegin(),v.rend());
  int p[n];
  for(int i=0;i<n;i++)
  {
     sum+=v[i].ff*i+1;
     p[i]=v[i].ss+1;
  }
  cout<<sum<<endl;
  for(int i=0;i<n;i++)
      cout<<p[i]<<" ";;
}


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

#define ll long long int
#define pb push_back
#define ff first
#define ss second
#define mp make_pair

int main()
{
  int n;
  cin>>n;
  vector< pair<int,int> > v(n);
  for(int i=0;i<n;i++)
  {
      int x;
      cin>>x;
      v[i]={x,i};
  }
  ll sum=0;
  sort(v.begin(),v.end(),greater<pair<int,int> > ());
  int p[n];
  for(int i=0;i<n;i++)
  {
     sum+=v[i].ff*i+1;
     p[v[i].ss]=i+1;
  }
  cout<<sum<<endl;
  for(int i=0;i<n;i++)
      cout<<p[i]<<" ";;
}


#include <bits/stdc++.h>
using namespace std;
struct NODE{
	int key, ind;
} a[1005];
int n, ans = 0;
bool cmp(NODE b, NODE a)
{
	if (a.key < b.key) return 1;
	return 0;
}
int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i].key);
		a[i].ind = i;
	}
	sort(a+1, a+1+n, cmp);
	ans = n;
	for(int i = 1; i <= n; i++)
	{
		ans += (a[i].key * (i-1));
	}
	cout << ans << endl;
	for(int i = 1; i <= n; i++)
		cout << a[i].ind << ' ';
	cout << endl;
	return 0;
}


/*******************************************************************************
NEVER LIE SOMEONE WHO TRUSTS YOU
*******************************************************************************/
#include<stdio.h>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define MAX 200005
#define MOD 1000000007
#define ll long long

pair<ll,ll> v[1009];
ll n, ans;
bool cmp(pair<ll,ll> a, pair<ll,ll> b)
{
    if(a.first != b.first)
        return a.first > b.first;
    return a.second < b.second;
}
int main()
{
    cin >> n;
    for(ll i = 1 ; i <= n; ++i)
    {
        cin >> v[i].first;
        v[i].second = i;
    }
    sort(v + 1, v + n + 1,cmp);
    for(ll i = 1; i <= n; ++i)
        ans += v[i].first * (i - 1) + 1;
    cout << ans << '\n';
    for(ll i = 1; i <= n; ++i)
        cout << v[i].second << ' ';
    return 0;
}


/*******************************************************************************
NEVER LIE SOMEONE WHO TRUSTS YOU
*******************************************************************************/
#include<stdio.h>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define MAX 200005
#define MOD 1000000007

bool comp(pair<int, int> a, pair<int, int> b)
{
	if(a.first >= b.first) return true;
	return false;
}
int main()
{
    int N, ans = 0;
    vector<pair<int, int> > v;
    scanf("%d", &N);
    for(int i=0; i<N; i++)
    {
    	scanf("%d", &a);
    	v.push_back(make_pair(a, i+1));
    }
    sort(v.begin(), v.end(), comp);

    for(int i=0; i<N; i++)
    {
    	ans += ((i * v[i].first) + 1);

    }
    printf("%d\n", ans);
    for(int i=0; i<N; i++)
    {
    	printf("%d ", v[i].second);
    }
    printf("\n");
    return(0);
}


/*******************************************************************************
NEVER LIE SOMEONE WHO TRUSTS YOU
*******************************************************************************/
#include<stdio.h>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define MAX 200005
#define MOD 1000000007

bool comp(pair<int, int> a, pair<int, int> b)
{
    if(a.first >= b.first) return true;
    return false;
}
int main()
{
    int N, ans = 0, a;
    vector<pair<int, int> > v;
    scanf("%d", &N);
    for(int i=0; i<N; i++)
    {
        scanf("%d", &a);
        v.push_back(make_pair(a, i+1));
    }
    sort(v.begin(), v.end(), comp);

    for(int i=0; i<N; i++)
    {
        ans += ((i * v[i].first) + 1);

    }
    printf("%d\n", ans);
    for(int i=0; i<N; i++)
    {
        printf("%d ", v[i].second);
    }
    printf("\n");
    return(0);
}


/*******************************************************************************
NEVER LIE SOMEONE WHO TRUSTS YOU
*******************************************************************************/
#include<stdio.h>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define MAX 200005
#define MOD 1000000007

bool comp(pair<long long int, long long int> a, pair<long long int, long long int> b)
{
    if(a.first >= b.first) return true;
    return false;
}
int main()
{
    long long int N, ans = 0, a;
    vector<pair<long long int, long long int> > v;
    scanf("%lld", &N);
    for(int i=0; i<N; i++)
    {
        scanf("%lld", &a);
        v.push_back(make_pair(a, i+1));
    }
    sort(v.begin(), v.end(), comp);

    for(int i=0; i<N; i++)
    {
        ans += ((i * v[i].first) + 1);

    }
    printf("%lld\n", ans);
    for(int i=0; i<N; i++)
    {
        printf("%lld ", v[i].second);
    }
    printf("\n");
    return(0);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define deb(x) cout<<#x<<" : "<<x<<"\n";
#define debug(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\n";
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define all(a) (a).begin(),(a).end()
#define bs binary_search
#define SZ(x) (int)(x).size()
#define vi vector<int>
#define pii pair<int,int>
#define ld long double
//#define clr fflush(stdout)
#define clr cout.flush()
#define N 100001
#define fpr(i,a,b) for(int i=a;i<b;i++)
#define print_mat(dist,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cout<<dist[i][j]<<" ";}cout<<'\n';}
#define LLMIN LLONG_MIN
#define LLMAX LLONG_MAX
#define AKASH_PATEL ios_base::sync_with_stdio(false);
#define SVNIT_SURAT cin.tie(NULL);cout.tie(NULL);
int main()
{
    AKASH_PATEL;
    SVNIT_SURAT;
    int n;
    cin>>n;
    vector <int> v;
    int a[n];
    multiset <int> s;
    for(int i=0;i<n;i++)
    {

        cin>>a[i];
        s.insert(a[i]);
        v.pb(a[i]);
    }
    sort(v.rbegin(),v.rend());
    ll ans=0;
    for(int i=0;i<n;i++)
    {
        ans+=(i*v[i])+1;
    }
    cout<<ans<<'\n';
    map <int,int> m;
    int res[n];
    int curr=-1,prev=-1;
    int idx=0;

    for(auto it:s)
    {
        if(it==prev)
        {

        }
        else
        {
//            cout<<idx<<'\n';
            prev=it;
            m[it]=idx;
        }
        idx++;
    }

    for(int i=n-1;i>=0;i--)
    {
        res[i]=n-m[a[i]];
        m[a[i]]++;
//        if(m[a[i]])
//        {
//            m[a[i]]++;
////            cout<<m[a[i]]<<" ";
//
//        } else {
//            auto it = distance(s.begin(), s.upper_bound(a[i]));
//            m[a[i]]=it;
////            cout<<m[a[i]]<<" ";
//            res[i]=n+1-m[a[i]];
//        }
    }
    for(int i=0;i<n;i++)
    {
        cout<<res[i]<<" ";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define deb(x) cout<<#x<<" : "<<x<<"\n";
#define debug(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\n";
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define all(a) (a).begin(),(a).end()
#define bs binary_search
#define SZ(x) (int)(x).size()
#define vi vector<int>
#define pii pair<int,int>
#define ld long double
//#define clr fflush(stdout)
#define clr cout.flush()
#define N 100001
#define fpr(i,a,b) for(int i=a;i<b;i++)
#define print_mat(dist,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cout<<dist[i][j]<<" ";}cout<<'\n';}
#define LLMIN LLONG_MIN
#define LLMAX LLONG_MAX
#define AKASH_PATEL ios_base::sync_with_stdio(false);
#define SVNIT_SURAT cin.tie(NULL);cout.tie(NULL);
int main()
{
    AKASH_PATEL;
    SVNIT_SURAT;
    int n;
    cin>>n;
    pair <int,int> p[n];
    for(int i=0;i<n;i++)
    {
        int ch;
        cin>>ch;
        p[i]=mp(ch,i+1);
    }
    sort(p,p+n);
    reverse(p,p+n);

    ll ans=0;
    for(int i=0;i<n;i++)
    {
        ans+=p[i].ff*i+1;
    }
    cout<<ans<<'\n';
    for(int i=0;i<n;i++)
    {
        cout<<p[i].ss<<' ';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define deb(x) cout<<#x<<" : "<<x<<"\n";
#define debug(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\n";
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define all(a) (a).begin(),(a).end()
#define bs binary_search
#define SZ(x) (int)(x).size()
#define vi vector<int>
#define pii pair<int,int>
#define ld long double
//#define clr fflush(stdout)
#define clr cout.flush()
#define N 100001
#define fpr(i,a,b) for(int i=a;i<b;i++)
#define print_mat(dist,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cout<<dist[i][j]<<" ";}cout<<'\n';}
#define LLMIN LLONG_MIN
#define LLMAX LLONG_MAX
#define AKASH_PATEL ios_base::sync_with_stdio(false);
#define SVNIT_SURAT cin.tie(NULL);cout.tie(NULL);
int main()
{
    AKASH_PATEL;
    SVNIT_SURAT;
    int n;
    cin>>n;
    vector <int> v;
    int a[n];
    set <int> s;
    for(int i=0;i<n;i++)
    {

        cin>>a[i];
        s.insert(a[i]);
        v.pb(a[i]);
    }
    sort(v.rbegin(),v.rend());
    ll ans=0;
    for(int i=0;i<n;i++)
    {
        ans+=(i*v[i])+1;
    }
    cout<<ans<<'\n';
    map <int,int> m;
    for(int i=0;i<n;i++)
    {
        if(m[a[i]])
        {
            m[a[i]]++;
            cout<<n+1-m[a[i]]<<" ";
        } else {
            auto it = distance(s.begin(), s.find(a[i]));
            m[a[i]]=it+1;
            cout<<n+1-m[a[i]]<<" ";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define deb(x) cout<<#x<<" : "<<x<<"\n";
#define debug(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\n";
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define all(a) (a).begin(),(a).end()
#define bs binary_search
#define SZ(x) (int)(x).size()
#define vi vector<int>
#define pii pair<int,int>
#define ld long double
//#define clr fflush(stdout)
#define clr cout.flush()
#define N 100001
#define fpr(i,a,b) for(int i=a;i<b;i++)
#define print_mat(dist,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cout<<dist[i][j]<<" ";}cout<<'\n';}
#define LLMIN LLONG_MIN
#define LLMAX LLONG_MAX
#define AKASH_PATEL ios_base::sync_with_stdio(false);
#define SVNIT_SURAT cin.tie(NULL);cout.tie(NULL);
int main()
{
    AKASH_PATEL;
    SVNIT_SURAT;
    int n;
    cin>>n;
    vector <int> v;
    int a[n];
    set <int> s;
    for(int i=0;i<n;i++)
    {

        cin>>a[i];
        s.insert(a[i]);
        v.pb(a[i]);
    }
    sort(v.rbegin(),v.rend());
    ll ans=0;
    for(int i=0;i<n;i++)
    {
        ans+=(i*v[i])+1;
    }
    cout<<ans<<'\n';
    map <int,int> m;
    for(int i=0;i<n;i++)
    {
        if(m[a[i]])
        {
            m[a[i]]++;
            cout<<m[a[i]]<<" ";
        } else {
            auto it = distance(s.begin(), s.find(a[i]));
            m[a[i]]=it+1;
            cout<<m[a[i]]<<" ";
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define deb(x) cout<<#x<<" : "<<x<<"\n";
#define debug(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\n";
#define ff first
#define ss second
#define ub upper_bound
#define lb lower_bound
#define all(a) (a).begin(),(a).end()
#define bs binary_search
#define SZ(x) (int)(x).size()
#define vi vector<int>
#define pii pair<int,int>
#define ld long double
//#define clr fflush(stdout)
#define clr cout.flush()
#define N 100001
#define fpr(i,a,b) for(int i=a;i<b;i++)
#define print_mat(dist,n,m) for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cout<<dist[i][j]<<" ";}cout<<'\n';}
#define LLMIN LLONG_MIN
#define LLMAX LLONG_MAX
#define AKASH_PATEL ios_base::sync_with_stdio(false);
#define SVNIT_SURAT cin.tie(NULL);cout.tie(NULL);
int main()
{
    AKASH_PATEL;
    SVNIT_SURAT;
    int n;
    cin>>n;
    vector <int> v;
    int a[n];
    set <int> s;
    for(int i=0;i<n;i++)
    {

        cin>>a[i];
        s.insert(a[i]);
        v.pb(a[i]);
    }
    sort(v.rbegin(),v.rend());
    ll ans=0;
    for(int i=0;i<n;i++)
    {
        ans+=(i*v[i])+1;
    }
    cout<<ans<<'\n';
    map <int,int> m;
    int res[n];
    for(int i=n-1;i>=0;i--)
    {
        if(m[a[i]])
        {
            m[a[i]]++;
//            cout<<m[a[i]]<<" ";
            res[i]=n+1-m[a[i]];
        } else {
            auto it = distance(s.begin(), s.upper_bound(a[i]));
            m[a[i]]=it;
//            cout<<m[a[i]]<<" ";
            res[i]=n+1-m[a[i]];
        }
    }
    for(int i=0;i<n;i++)
    {
        cout<<res[i]<<" ";
    }
    return 0;
}


//
// Created by Akash on 21-09-2019.
//

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;

int main()
{
    ios
    int n ;
    cin>>n ;
    vector<pair<ll,ll>> vp ;
    ll ans[n] ;
    ll x ;
    for( int i = 0 ; i < n ; i++ )
    {
        cin>>x ;
        vp.push_back(make_pair(x,i)) ;
    }
    sort(vp.begin(),vp.end()) ;
    ll sum = 0 , hit = 0;
    for( int i = n-1 ; i >= 0 ; i-- )
    {
        //cout<<vp[i].first<<" "<<vp[i].second<<"\n" ;
        sum += ((vp[i].first * hit) + 1) ;
        ans[vp[i].second] = hit+1 ;
        hit+=1 ;
    }
    cout<<sum<<"\n" ;
    for( int i = 0 ; i < n ; i++ )
    {
        cout<<ans[i]<<" ";
    }
    return 0;
}


//
// Created by Akash on 21-09-2019.
//

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;

int main()
{
    ios
    int n ;
    cin>>n ;
    vector<pair<ll,ll>> vp ;
    ll ans[n] ;
    ll x ;
    for( int i = 0 ; i < n ; i++ )
    {
        cin>>x ;
        vp.push_back(make_pair(x,i)) ;
    }
    sort(vp.begin(),vp.end()) ;
    ll sum = 0 , hit = 0;
    for( int i = n-1 ; i >= 0 ; i-- )
    {
        cout<<vp[i].first<<" "<<vp[i].second<<"\n" ;
        sum += ((vp[i].first * hit) + 1) ;
        ans[vp[i].second] = hit+1 ;
        hit+=1 ;
    }
    cout<<sum<<"\n" ;
    for( int i = 0 ; i < n ; i++ )
    {
        cout<<ans[i]<<" ";
    }
    return 0;
}


//
// Created by Akash on 21-09-2019.
//

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
    return (a.first > b.first);
}

int main()
{
    ios
    ll n ;
    cin>>n ;
    vector<pair<ll,ll>> vp ;
    ll ans[n] ;
    ll x ;
    for( int i = 0 ; i < n ; i++ )
    {
        cin>>x ;
        vp.push_back(make_pair(x,i)) ;
    }
    sort(vp.begin(),vp.end(),sortinrev ) ;
    ll sum = 0 , hit = 0;
    for( ll i = 0 ; i < n ; i++ )
    {
        //cout<<vp[i].first<<" "<<vp[i].second<<"\n" ;
        sum += ((vp[i].first * i) + 1) ;
        ans[vp[i].second] = i+1 ;
        hit+=1 ;
    }
    cout<<sum<<"\n" ;
    for( ll i = 0 ; i < n ; i++ )
    {
        cout<<ans[i]<<" ";
    }
    return 0;
}


//
// Created by Akash on 21-09-2019.
//

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
    return (a.first > b.first);
}

int main()
{
    ios
    ll n ;
    cin>>n ;
    vector<pair<ll,ll>> vp ;
    ll ans[n] ;
    ll x ;
    for( int i = 0 ; i < n ; i++ )
    {
        cin>>x ;
        vp.push_back(make_pair(x,i)) ;
    }
    sort(vp.begin(),vp.end(),sortinrev ) ;
    ll sum = 0 , hit = 0;
    for( ll i = 0 ; i < n ; i++ )
    {
        //cout<<vp[i].first<<" "<<vp[i].second<<"\n" ;
        sum += ((vp[i].first * hit) + 1) ;
        ans[i] = vp[i].second + 1 ;
        hit+=1 ;
    }
    cout<<sum<<"\n" ;
    for( ll i = 0 ; i < n ; i++ )
    {
        cout<<ans[i]<<" ";
    }
    return 0;
}


//
// Created by Akash on 21-09-2019.
//

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;

int main()
{
    ios
    int n ;
    cin>>n ;
    vector<pair<ll,ll>> vp ;
    ll ans[n] ;
    ll x ;
    for( int i = 0 ; i < n ; i++ )
    {
        cin>>x ;
        vp.push_back(make_pair(x,i)) ;
    }
    sort(vp.begin(),vp.end()) ;
    ll sum = 0 , hit = 0;
    for( int i = n-1 ; i >= 0 ; i-- )
    {
        sum += (vp[i].first * hit) + 1 ;
        ans[vp[i].second] = i+1 ;
        hit+=1 ;
    }
    cout<<sum<<"\n" ;
    for( int i = 0 ; i < n ; i++ )
    {
        cout<<ans[i]<<" ";
    }
    return 0;
}


//
// Created by Akash on 21-09-2019.
//

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;

bool sortinrev(const pair<int,int> &a,
               const pair<int,int> &b)
{
    return (a.first > b.first);
}

int main()
{
    ios
    ll n ;
    cin>>n ;
    vector<pair<ll,ll>> vp ;
    ll ans[n] ;
    ll x ;
    for( int i = 0 ; i < n ; i++ )
    {
        cin>>x ;
        vp.push_back(make_pair(x,i)) ;
    }
    sort(vp.begin(),vp.end(),sortinrev ) ;
    ll sum = 0 , hit = 0;
    for( ll i = 0 ; i < n ; i++ )
    {
        //cout<<vp[i].first<<" "<<vp[i].second<<"\n" ;
        sum += ((vp[i].first * hit) + 1) ;
        ans[vp[i].second] = hit+1 ;
        hit+=1 ;
    }
    cout<<sum<<"\n" ;
    for( ll i = 0 ; i < n ; i++ )
    {
        cout<<ans[i]<<" ";
    }
    return 0;
}


/* ****Ragnar7**** */
/* It's Going to be Hard, but hard does not mean Impossible. */
#include<bits/stdc++.h>
#define ll          long long
#define hell        1000000007
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll n;cin>>n;
	vector<ll> v;
	vector<ll> ans;
	ll x,y;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		v.push_back(x);
		ans.push_back(x);
	}
	sort(v.begin(),v.end());
	cout<<n<<'\n';
	reverse(v.begin(),v.end());
	std::vector<ll> bb[10000];
	for(ll i=0;i<n;i++)
		bb[v[i]].push_back(i+1);
	for(ll i=0;i<10000;i++)
		reverse(bb[i].begin(),bb[i].end());
	for(int i=0;i<n;i++)
	{
		ll temp=ans[i];
		ans[i]=bb[temp][bb[temp].size()-1];
		bb[temp].pop_back();
		cout<<ans[i]<<" ";
	}

	return 0;
}


/* ****Ragnar7**** */
/* It's Going to be Hard, but hard does not mean Impossible. */
#include<bits/stdc++.h>
#define ll          long long
#define hell        1000000007
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll n;cin>>n;
	vector<pair<ll,ll> > v;
	ll x,y;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		v.push_back(make_pair(x,i+1));
	}
	sort(v.begin(), v.end());
	reverse(v.begin(), v.end());
	ll ans=0;
	for(int i=0;i<n;i++)
		ans+=v[i].first*i+1;
	cout<<ans<<'\n';
	for(int i=0;i<v.size();i++)
		cout<<v[i].second<<" ";

	return 0;
}


/* ****Ragnar7**** */
/* It's Going to be Hard, but hard does not mean Impossible. */
#include<bits/stdc++.h>
#define ll          long long
#define hell        1000000007
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll n;cin>>n;
	vector<ll> v[10005];
	std::vector<ll> ans;
	ll x,y;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		v[x].push_back(i+1);
		ans.push_back(x);
	}
	sort(ans.begin(), ans.end());
	cout<<n<<'\n';
	for(int i=0;i<n;i++)
	{
		ll temp=ans[i];
		ans[i]=v[temp][v[temp].size()-1];
		v[temp].pop_back();
		cout<<ans[i]<<" ";
	}

	return 0;
}


/* ****Ragnar7**** */
/* It's Going to be Hard, but hard does not mean Impossible. */
#include<bits/stdc++.h>
#define ll          long long
#define hell        1000000007
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll n;cin>>n;
	vector<ll> v;
	vector<ll> ans;
	ll x,y;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		v.push_back(x);
		ans.push_back(x);
	}
	sort(v.begin(),v.end());
	cout<<n<<'\n';
	reverse(v.begin(),v.end());
	std::vector<ll> bb[10000];
	for(ll i=0;i<n;i++)
		bb[v[i]].push_back(i+1);
	for(int i=0;i<n;i++)
	{
		ll temp=ans[i];
		ans[i]=bb[temp][bb[temp].size()-1];
		bb[temp].pop_back();
		cout<<ans[i]<<" ";
	}

	return 0;
}


/* ****Ragnar7**** */
/* It's Going to be Hard, but hard does not mean Impossible. */
#include<bits/stdc++.h>
#define ll          long long
#define hell        1000000007
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll n;cin>>n;
	vector<ll> v;
	vector<ll> ans;
	ll x,y;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		v.push_back(x);
		ans.push_back(x);
	}
	sort(v.begin(),v.end());

	reverse(v.begin(),v.end());
	std::vector<ll> bb[10000];ll cnt=0;
	for(ll i=0;i<n;i++)
	{
		cnt+=v[i]*(i)+1;
		bb[v[i]].push_back(i+1);
	}
	cout<<cnt<<'\n';
	for(ll i=0;i<10000;i++)
		reverse(bb[i].begin(),bb[i].end());
	for(int i=0;i<n;i++)
	{
		ll temp=ans[i];
		ans[i]=bb[temp][bb[temp].size()-1];
		bb[temp].pop_back();
		cout<<ans[i]<<" ";
	}

	return 0;
}


#include<bits/stdc++.h>
#define hell                         1000000007
#define M                            1000000007
#define Maxi                      10000000000000000
#define lcm(a,b)                     (a*b)/__gcd(a,b)
#define ll                           long long

#define vll                          vector<ll>
#define vi                           vector<long long>
#define pll                          vector< pair<ll,ll> >
#define pb                           push_back
//#define mp                           make_pair
#define all(v)                       v.begin(),v.end()
#define lbnd                         lower_bound
#define ubnd                         upper_bound
#define bs                           binary_search
#define F                            first
#define S                            second
#define rep(i,a,b)                   for(i=a;i<b;i++)
#define parr(a,n)                    for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define pcont(a)                     for(auto i:a) cout<<i<<" ";cout<<endl;
#define ret(x)                       return cout<<x,0;
#define dbg(x)                       cout << #x << " is " << x << endl;
#define endl                           '\n'
 //const ll infl=0x3f3f3f3f3f3f3f3fLL;

 #define pi                           3.141592653589793238
// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

using namespace std;
ll prime[1000006];
ll k;
void fun()
{
  for(ll i=2;i<=1000000;i++)
  {
    if(prime[i]==0)
    {
      for(ll j=i;j<=1000000;j+=i)
      {
        if(prime[j]==0)
        prime[j]=i;
      }
    }
  }
}

// bool divisors(long long n)
// {
//     long long i;
//     vector<ll>v;
//     for(i=2;i*i<=n;i++)
//     {
//         if(n%i==0)
//         {
//             if(n/i==i)
//                 v.pb(i);
//             else
//             {
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     ll x=v.size();
//     if(x==0)
//         return true;
//     else
//         return false;
// }

// ll fact(ll n)
// {
//     ll i,p=1;
//     for(i=1;i<=n;i++)
//     {
//         p*=i;
//         p%=hell;
//     }
//     return p;
// }

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    ll n,i;
    cin>>n;
    vector<pair<ll,ll>>v;
    for(i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        v.pb({x,i});
    }
    sort(all(v));
    reverse(all(v));
    ll ans[n];
    for(i=0;i<n;i++)
    {
        ll pos=v[i].S;
        ans[pos]=i+1;
    }
    for(i=0;i<n;i++)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}


#include<bits/stdc++.h>
#define hell                         1000000007
#define M                            1000000007
#define Maxi                      10000000000000000
#define lcm(a,b)                     (a*b)/__gcd(a,b)
#define ll                           long long

#define vll                          vector<ll>
#define vi                           vector<long long>
#define pll                          vector< pair<ll,ll> >
#define pb                           push_back
//#define mp                           make_pair
#define all(v)                       v.begin(),v.end()
#define lbnd                         lower_bound
#define ubnd                         upper_bound
#define bs                           binary_search
#define F                            first
#define S                            second
#define rep(i,a,b)                   for(i=a;i<b;i++)
#define parr(a,n)                    for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define pcont(a)                     for(auto i:a) cout<<i<<" ";cout<<endl;
#define ret(x)                       return cout<<x,0;
#define dbg(x)                       cout << #x << " is " << x << endl;
#define endl                           '\n'
 //const ll infl=0x3f3f3f3f3f3f3f3fLL;

 #define pi                           3.141592653589793238
// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

using namespace std;
ll prime[1000006];
ll k;
void fun()
{
  for(ll i=2;i<=1000000;i++)
  {
    if(prime[i]==0)
    {
      for(ll j=i;j<=1000000;j+=i)
      {
        if(prime[j]==0)
        prime[j]=i;
      }
    }
  }
}

// bool divisors(long long n)
// {
//     long long i;
//     vector<ll>v;
//     for(i=2;i*i<=n;i++)
//     {
//         if(n%i==0)
//         {
//             if(n/i==i)
//                 v.pb(i);
//             else
//             {
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     ll x=v.size();
//     if(x==0)
//         return true;
//     else
//         return false;
// }

// ll fact(ll n)
// {
//     ll i,p=1;
//     for(i=1;i<=n;i++)
//     {
//         p*=i;
//         p%=hell;
//     }
//     return p;
// }

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    ll n,i;
    cin>>n;
    vector<pair<ll,ll>>v;
    for(i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        v.pb({x,-i});
    }
    sort(all(v));
    reverse(all(v));
    ll ans[n];
    ll x=0,p=0;
    for(i=0;i<n;i++)
    {
        p+=((v[i].F*x)+1);
        x++;
        ll pos=-v[i].S;
        ans[pos]=i+1;
    }
    cout<<p<<endl;
    for(i=0;i<n;i++)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}


#include<bits/stdc++.h>
#define hell                         1000000007
#define M                            1000000007
#define Maxi                      10000000000000000
#define lcm(a,b)                     (a*b)/__gcd(a,b)
#define ll                           long long

#define vll                          vector<ll>
#define vi                           vector<long long>
#define pll                          vector< pair<ll,ll> >
#define pb                           push_back
//#define mp                           make_pair
#define all(v)                       v.begin(),v.end()
#define lbnd                         lower_bound
#define ubnd                         upper_bound
#define bs                           binary_search
#define F                            first
#define S                            second
#define rep(i,a,b)                   for(i=a;i<b;i++)
#define parr(a,n)                    for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define pcont(a)                     for(auto i:a) cout<<i<<" ";cout<<endl;
#define ret(x)                       return cout<<x,0;
#define dbg(x)                       cout << #x << " is " << x << endl;
#define endl                           '\n'
 //const ll infl=0x3f3f3f3f3f3f3f3fLL;

 #define pi                           3.141592653589793238
// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

using namespace std;
ll prime[1000006];
ll k;
void fun()
{
  for(ll i=2;i<=1000000;i++)
  {
    if(prime[i]==0)
    {
      for(ll j=i;j<=1000000;j+=i)
      {
        if(prime[j]==0)
        prime[j]=i;
      }
    }
  }
}

// bool divisors(long long n)
// {
//     long long i;
//     vector<ll>v;
//     for(i=2;i*i<=n;i++)
//     {
//         if(n%i==0)
//         {
//             if(n/i==i)
//                 v.pb(i);
//             else
//             {
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     ll x=v.size();
//     if(x==0)
//         return true;
//     else
//         return false;
// }

// ll fact(ll n)
// {
//     ll i,p=1;
//     for(i=1;i<=n;i++)
//     {
//         p*=i;
//         p%=hell;
//     }
//     return p;
// }

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    ll n,i;
    cin>>n;
    vector<pair<ll,ll>>v;
    for(i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        v.pb({x,i});
    }
    sort(all(v));
    reverse(all(v));
    ll ans[n];
    ll x=0,p=0;
    for(i=0;i<n;i++)
    {
        p+=((v[i].F*x)+1);
        x++;
        ll pos=v[i].S;
        ans[i]=pos+1;
    }
    cout<<p<<endl;
    for(i=0;i<n;i++)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}


#include<bits/stdc++.h>
#define hell                         1000000007
#define M                            1000000007
#define Maxi                      10000000000000000
#define lcm(a,b)                     (a*b)/__gcd(a,b)
#define ll                           long long

#define vll                          vector<ll>
#define vi                           vector<long long>
#define pll                          vector< pair<ll,ll> >
#define pb                           push_back
//#define mp                           make_pair
#define all(v)                       v.begin(),v.end()
#define lbnd                         lower_bound
#define ubnd                         upper_bound
#define bs                           binary_search
#define F                            first
#define S                            second
#define rep(i,a,b)                   for(i=a;i<b;i++)
#define parr(a,n)                    for(i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define pcont(a)                     for(auto i:a) cout<<i<<" ";cout<<endl;
#define ret(x)                       return cout<<x,0;
#define dbg(x)                       cout << #x << " is " << x << endl;
#define endl                           '\n'
 //const ll infl=0x3f3f3f3f3f3f3f3fLL;

 #define pi                           3.141592653589793238
// Debug //

#define trace(x)                 cerr << #x << ": " << x << endl;
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

using namespace std;
ll prime[1000006];
ll k;
void fun()
{
  for(ll i=2;i<=1000000;i++)
  {
    if(prime[i]==0)
    {
      for(ll j=i;j<=1000000;j+=i)
      {
        if(prime[j]==0)
        prime[j]=i;
      }
    }
  }
}

// bool divisors(long long n)
// {
//     long long i;
//     vector<ll>v;
//     for(i=2;i*i<=n;i++)
//     {
//         if(n%i==0)
//         {
//             if(n/i==i)
//                 v.pb(i);
//             else
//             {
//                 v.pb(i);
//                 v.pb(n/i);
//             }
//         }
//     }
//     ll x=v.size();
//     if(x==0)
//         return true;
//     else
//         return false;
// }

// ll fact(ll n)
// {
//     ll i,p=1;
//     for(i=1;i<=n;i++)
//     {
//         p*=i;
//         p%=hell;
//     }
//     return p;
// }

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    ll n,i;
    cin>>n;
    vector<pair<ll,ll>>v;
    for(i=0;i<n;i++)
    {
        ll x;
        cin>>x;
        v.pb({x,i});
    }
    sort(all(v));
    reverse(all(v));
    ll ans[n];
    ll x=0,p=0;
    for(i=0;i<n;i++)
    {
        p+=((v[i].F*x)+1);
        x++;
        ll pos=v[i].S;
        ans[pos]=i+1;
    }
    cout<<p<<endl;
    for(i=0;i<n;i++)
    {
        cout<<ans[i]<<" ";
    }
    cout<<endl;
}