The only difference between easy and hard versions is a number of elements in the array.

You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$.

You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$.

You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$.

You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible.

Note that you can choose the empty set.

If there are multiple answers, you can print any.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.


-----Input-----

The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) — the length of the array $a$ and the number of segments, respectively.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$.

The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment.


-----Output-----

In the first line of the output print one integer $d$ — the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$.

In the second line of the output print one integer $q$ ($0 \le q \le m$) — the number of segments you apply.

In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) — indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible.

If there are multiple answers, you can print any.


-----Examples-----
Input
5 4
2 -2 3 1 2
1 3
4 5
2 5
1 3

Output
6
2
1 4 

Input
5 4
2 -2 3 1 4
3 5
3 4
2 4
2 5

Output
7
2
3 2 

Input
1 0
1000000

Output
0
0




-----Note-----

In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$.

In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$.

In the third example you cannot do anything so the answer is $0$.

Difficulty: introductory

Starter code:

Solutions:
from copy import deepcopy
n,m = map(int,input().split())
a = list(map(int,input().split()))
b = [list(map(int,input().split())) for i in range(m)]
best1 = -1
best2 = -1
best3 = []
for i in range(n):
    tmp = 0
    tmp2 = 0
    tmp3 = []
    c = deepcopy(a)
    for j in range(m):
        x,y = b[j]
        x-=1;y-=1
        if x<= i and i <= y:
            continue
        for k in range(x,y+1):
            c[k] -= 1
        tmp3.append(j+1)
        tmp2 += 1
    kon = max(c)-min(c)
    if best1 < kon:
        best1 = kon
        best2 = tmp2
        best3 = tmp3
print(best1)
print(best2)
print(*best3)

n, m = map(int, input().split())
mass = list(map(int, input().split()))
lr = []
for t in range(m):
    l, r = map(int, input().split())
    lr.append([l, r])
mq = 0
mdel = []
mitog = 0
for a in range(len(mass)):
    ma = mass[a]
    for b in range(len(mass)):
        mb = mass[b]
        q = 0
        delete = []
        itog = 0
        for x in range(len(lr)):
            l, r = lr[x][0], lr[x][1]
            if l <= b+1 <= r and (a+1 < l or a+1 > r):
                q += 1
                delete.append(x+1)
        itog = ma + q - mb
        if mitog < itog:
            mitog = itog
            mq = q
            mdel = delete
print(mitog)
print(mq)
if len(mdel):
    print(' '.join(list(map(str, mdel))))
else:
    print('')

n, m = list(map(int, input().split()))
mas = list(map(int, input().split()))
otmas = []
for i in range(m):
    otmas.append(list(map(int, input().split())))
for i in range(m):
    otmas[i][0] -= 1
    otmas[i][1] -= 1
ans = -1
masotv = []
for i in range(n):
    mascop = mas.copy()
    masotvizc = []
    for j in range(m):
        if not (i >= otmas[j][0] and i <= otmas[j][1]):
            masotvizc.append(j + 1)
            for k in range(otmas[j][0], otmas[j][1] + 1):
                mascop[k] -= 1
    if max(mascop) - min(mascop) > ans:
        masotv = masotvizc
    ans = max(ans, max(mascop) - min(mascop))
print(ans)
print(len(masotv))
print(*masotv)


n, m = map(int, input().split())
arr = [int(x) for x in input().split()]
seg = []
el = []
el2 = []
h = []

def cont(a, b, c):
    return a <= c <= b

def count_delta():
    return max(h) - min(h)

def decrement(l, r):
    for i in range(l - 1, r):
        h[i] -= 1

for i in range(m):
    seg.append([int(x) for x in input().split()])

for i in range(len(arr)):
    h = arr[:]
    tmp = []
    for j in range(len(seg)):
        if cont(seg[j][0], seg[j][1], i + 1):
            decrement(seg[j][0], seg[j][1])
            tmp.append(j + 1)
    el.append(count_delta())
    el2.append(tmp)
    
print(max(el))
k = el.index(max(el))
print(len(el2[k]))
print(*el2[k])

from sys import stdin,stdout
from collections import defaultdict
import math
#stdin = open('input.txt','r')
I = stdin.readline
P = stdout.write

n,m = map(int,I().split())
arr = [int(x) for x in I().split()]

indi = defaultdict(lambda : set())
for j in range(m):
	l,r = map(int,I().split())
	for i in range(l-1,r):
		
		indi[i].add(j+1)

ans = 0
a,b = 0,0
inter = set()
#print(indi)
for i in range(n):
	for j in range(n):
		nowint = indi[j].intersection(indi[i])
		now = arr[i]-arr[j]+len(indi[j])-len(nowint)

		
		if(now>ans):
			ans = now
			a = i
			b = j
			inter = nowint

print(ans)
#print(indi[b],nowint)
print(len(indi[b]))
for i in indi[b]:
	
	print(i,end = " ")























        
       
    






n,m=list(map(int,input().split()))
b=list(map(int,input().split()))
se=[]
for i in range(m):
    se.append(list(map(int,input().split())))
k=max(b)-min(b)
ans=[]
for i in range(n):
    l=[]
    r=b[:]
    for j in range(m):
        c,d=se[j][0],se[j][1]
        if i not in list(range(c-1,d)):
            for x in range(c-1,d):
                r[x]-=1
            l.append(j+1)
    x=max(r)-min(r)
    if x>k:
        k=x
        ans=l
print(k)
print(len(ans))
print(*ans)
        
                



n, m = list(map(int,input().split()))
a = list(map(int,input().split()))
b = []
for i in range(m):
    b.append(list(map(int,input().split())))
ans = -1000000000

for i in range(n):
    ac = a.copy()
    for j in range(m):
        if i+1 < b[j][0] or i+1 > b[j][1]:
            for k in range(b[j][0]-1, b[j][1]):
                ac[k] -= 1
    if a[i] - min(ac) > ans:
        ans = a[i] - min(ac)
        ansi = i

ansh = []
i = ansi
ac = a.copy()
q = 0
for j in range(m):
    if i+1 < b[j][0] or i+1 > b[j][1]:
        q += 1
        ansh.append(j+1)

print(ans)
print(q)
print(*ansh)



def read_nums():
    return [int(x) for x in input().split()]


def calculate_diff(i1, i2, nums, all_segments):
    cur_diff = nums[i2] - nums[i1]
    used_segments = []
    for index, segment in enumerate(all_segments):
        left, right = segment
        if left <= i1 <= right and not (left <= i2 <= right):
            used_segments.append(index)
            cur_diff += 1
    return cur_diff, used_segments


def main():
    n, m = read_nums()
    nums = read_nums()
    segments = [[x-1 for x in read_nums()] for _ in range(m)]

    if n == 1:
        print(0)
        print(0)
        return

    candidates = []
    for i1, num1 in enumerate(nums):
        for i2, num2 in enumerate(nums):
            if i1 == i2:
                continue
            diff, used_segments = calculate_diff(i1, i2, nums, segments)
            candidates.append((diff, used_segments))

    res_diff, segment_indexes = max(candidates, key=lambda x: x[0])
    print(res_diff)
    print(len(segment_indexes))
    if len(segment_indexes) > 0:
        print(' '.join([str(x + 1) for x in segment_indexes]))


def __starting_point():
    main()

__starting_point()

import sys
input=sys.stdin.readline

n,m=map(int,input().split())
x=[*map(int,input().split())]
seg=[]
for i in range(m):
    a=[*map(int,input().split())]
    seg.append(a)

ans=-1
ansi=[]
for i in range(len(x)):
    tx=x[:]
    oi=[]
    for n,j in enumerate(seg):
        a,b=j
        if not (a-1<=i<=b-1):
            oi.append(n+1)
            for k in range(a-1,b):
                tx[k]-=1
    m=max(tx)-min(tx)
    if m>ans:
        ans=m
        ansi=oi[:]

print(ans)
print(len(ansi))
for i in ansi:
    print(i,end=" ")

def __starting_point():
    n, subset = map(int, input().split(" "))
    a = list(map(int, input().split(" ")))
    f = []
    t = []

    for i in range(subset):
        x, y = map(int, input().split(" "))
        f.append(x - 1)
        t.append(y - 1)

    diff = 0
    answer = []

    for ma in range(n):
        for mi in range(n):
            inside = 0
            cur_answer = []
            for sub in range(subset):
                if f[sub] <= mi <= t[sub] and not (f[sub] <= ma <= t[sub]):
                    inside += 1
                    cur_answer.append(sub)
            if a[ma] - (a[mi] - inside) > diff:
                diff = a[ma] - (a[mi] - inside)
                answer = cur_answer

    print(diff)
    print(len(answer))
    print(' '.join(str(x + 1) for x in answer))
__starting_point()

n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
segments = []
for i in range(m):
    l, r = list(map(int, input().split()))
    segments.append([l, r])

ans = 0
flag = 0
for i in range(n):
    for j in range(i, n):
        ai = a[i]
        aj = a[j]
        seglist1 = []
        seglist2 = []
        for k in range(m):
            if segments[k][0]-1 <= i <= segments[k][1]-1 and segments[k][1]-1 < j:
                ai -= 1
                seglist1.append(k+1)
            if segments[k][0]-1 > i and segments[k][0]-1 <= j <= segments[k][1]-1:
                aj -= 1
                seglist2.append(k+1)

        if ans < a[j] - ai:
            anslist = seglist1
            ans = a[j] - ai
            flag = 1
        if ans < a[i] - aj:
            anslist = seglist2
            ans = a[i] - aj
            flag = 1
if flag == 1 and len(anslist) >= 1:
    ansseg = str(anslist[0])
    for i in range(1, len(anslist)):
        ansseg += " " + str(anslist[i])
else:
    anslist = []
    ansseg = ""

print(ans)
print(len(anslist))
print(ansseg)


#      
import collections, atexit, math, sys, bisect 

sys.setrecursionlimit(1000000)
def getIntList():
    return list(map(int, input().split()))    

try :
    #raise ModuleNotFoundError
    import numpy
    def dprint(*args, **kwargs):
        #print(*args, **kwargs, file=sys.stderr)
        # in python 3.4 **kwargs is invalid???
        print(*args,  file=sys.stderr)
    dprint('debug mode')
except Exception:
    def dprint(*args, **kwargs):
        pass



inId = 0
outId = 0
if inId>0:
    dprint('use input', inId)
    try:
        f = open('input'+ str(inId) + '.txt', 'r')
        sys.stdin = f #标准输出重定向至文件
    except Exception:
        dprint('invalid input file')
if outId>0:
    dprint('use output', outId)
    try:
        f = open('stdout'+ str(outId) + '.txt', 'w')
        sys.stdout = f #标准输出重定向至文件
    except Exception:
        dprint('invalid output file')
        
    atexit.register(lambda :sys.stdout.close())     #idle 中不会执行 atexit
    
N, M= getIntList()
#print(N)
za =  getIntList()
omin = min(za)
sg = [0 for i in range(N*4) ]
de = sg.copy()
def built_sg(root, left, right):
    if left== right:
        sg[root] = za[left]
        return sg[root]
    mid = (left+right)//2
    a = built_sg(root*2+1, left, mid)
    b = built_sg(root*2+2, mid+1, right)
    sg[root] = min(a,b)
    return sg[root]

def update(root, left, right, qleft, qright, dif):
    if qright<left or right< qleft : return sg[root]
 
    if qleft<= left and right <= qright:
        de[root] += dif
        sg[root] += dif
        return sg[root]
    if de[root] !=0:
        t = de[root]
        sg[root*2+1] += t
        sg[root*2+2] += t
        de[root*2+1] +=t
        de[root*2+2] += t
        de[root] =0
    mid = (left+right)//2
    a = update(root*2+1, left, mid, qleft, qright, dif)
    b = update(root*2+2, mid+1,right, qleft, qright, dif)
    sg[root] = min(a,b)
    return sg[root]

built_sg(0,0,N-1)
zm = []
zz = [0,N]
for i in range(M):
    l,r = getIntList()
    l-=1
    r-=1
    zm.append((l,r) )
    zz.append(l)
    zz.append(r+1)
zz.sort()
r = max(za) - sg[0]
zr = []
for i in range( len(zz) -1):
    a = zz[i]
    b = zz[i+1]
    if a==b: continue
    t = max(za[a:b])
    tzr = []
    for j in range(M):
        x = zm[j]
        if x[0] <= a <= x[1]: continue
        tzr.append(j+1)
        update(0,0,N-1, x[0],x[1],-1)
    t1 = sg[0]
    if r < t - t1:
        r = t - t1
        zr = tzr
    for x in zm:
        if x[0] <= a <= x[1]:  continue
        update(0,0,N-1, x[0],x[1],1)
    assert( omin == sg[0]) 


    
print(r)
print(len(zr))
for x in zr:
    print(x, end = ' ')




n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = [list(map(int, input().split())) for i in range(m)]
gl_ans = -1
gl_mas = []

for num_i in range(n):
    ma = -1e7
    ans_m = []
    o = []
    for i in range(m):
        if b[i][0] - 1 > num_i or b[i][1] - 1 < num_i:
            o.append([b[i][0] - 1, -1])
            o.append([b[i][1], 0])
            ans_m.append(i + 1)
    for i in range(n):
        o.append([i, 1])

    o.sort()
    cnt = 0
    mi = 1e7
    for u in range(len(o)):
        if o[u][1] == -1:
            cnt -= 1
        elif o[u][1] == 0:
            cnt += 1
        else:
            if a[o[u][0]] + cnt < mi:
                mi = a[o[u][0]] + cnt
            # if a[o[u][0]] + cnt > ma:
            #             #     ma = a[o[u][0]] + cnt
            #             #     print(cnt)
    ma = a[num_i]
    # print(mi, ma, ans_m, o)

    if ma - mi > gl_ans:
        gl_ans = ma - mi
        gl_mas = ans_m

print(gl_ans)
print(len(gl_mas))
print(*gl_mas)

n, m = list(map(int, input().split()))
A = [int(a) for a in input().split()]
L = [0] * m
R = [0] * m
for i in range(m):
    l, r = list(map(int, input().split()))
    L[i] = l-1
    R[i] = r-1
    
    

ma = -1
mai = -1
for i in range(n):
    B = [a for a in A] # Copy of A
    for j in range(m):
        if L[j]>i or R[j] < i:
            for k in range(L[j], R[j]+1):
                B[k] -= 1
                
    d = B[i] - min(B)
    # print(d, B)
    
    if d > ma:
        ma = d
        mai = i

print(ma)
B = [a for a in A] # Copy of A
X = []
for j in range(m):
    if L[j]>mai or R[j] < mai:
        X.append(str(j+1))

print(len(X))
print(" ".join(X))


n,m=map(int,input().split())
a=list(map(int,input().split()))
ans=[]
pair=[]
for i in range(m):
    x=list(map(int,input().split()))
    #print(x)
    pair.append(x)
for i in range(n):
    for j in range(n):
        d=[]
        b=0
        for k in range(m):
            if j+1>=pair[k][0] and j+1<=pair[k][1] and (i+1<pair[k][0] or i+1>pair[k][1]) :
                b+=1
                d.append(k+1)
        d=[a[i]-a[j]+b]+d
        ans.append(d) 
        #print(*d)      
ans.sort()
print(ans[-1][0])
sz=len(ans[-1])-1
print(sz)
print(*ans[-1][1:])

n,m=map(int,input().split())
a=list(map(int,input().split()))
ans=[]
pair=[]
for i in range(m):
    x=list(map(int,input().split()))
    #print(x)
    pair.append(x)
for i in range(n):
    for j in range(n):
        d=[]
        b=0
        for k in range(m):
            if j+1>=pair[k][0] and j+1<=pair[k][1] and (i+1<pair[k][0] or i+1>pair[k][1]) :
                b+=1
                d.append(k+1)
        d=[a[i]-a[j]+b]+d
        ans.append(d) 
        #print(*d)      
ans.sort()
print(ans[-1][0])
sz=len(ans[-1])-1
print(sz)
print(*ans[-1][1:])

n,m = [int(x) for x in input().split()]

L = [int(x) for x in input().split()]

large = max(L)
R = []
used = 0
Pairs = []
best = 0
lol = []
for i in range(n):
    if L[i] == large:
        R.append(i)


for T in range(m):
    l,j = [int(x) for x in input().split()]
    Pairs.append((l,j))

for j in range(0,n):
    S = L[:]
    tu = 0
    tuu = []
    for i in range(m):
        if (Pairs[i][0]-1 > j) or (Pairs[i][1] <= j):
            tu += 1
            tuu.append(i+1)
            for k in range(Pairs[i][0]-1,Pairs[i][1]):
                S[k] -= 1
    t = max(S)-min(S)
    if t > best:
        best = t
        used = tu
        lol = tuu
        
print(best)
print(used)
print(' '.join(str(i) for i in lol))





dat = list(map(int, input().split()))
l = list(map(int, input().split()))


d = {}
i = 1
while i < dat[1] + 1:
  inter = list(map(int, input().split()))
  j = inter[0]
  while j < inter[1] + 1:
    if j not in d:
      d[j] = set()
    d[j].add(i)
    j+=1
  i+=1


max_res = -1 
ans = None

i = 0
while i  < len (l):
  j = 0
  while j < len (l):
    ii = i + 1
    jj = j + 1
    res_i = l[i]
    res_j = l[j]
    if ii in d:
      res_i = l[i] - len(d[ii])
      
      if jj in d:
        for dd in d[ii]:
          if dd in d[jj]:
            res_j -= 1

    res = res_j  - res_i
    if res > max_res:
      max_res = res
      ans = ii
    j += 1
  i += 1
  
if max_res == -1:
  print(0)
  print(0)
else:
  print(max_res)
  if ans in d:
    print(len(d[ans]))
    print(' '.join(map(str, d[ans])))
  else:
    print(0)



n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
lst = []
for i in range(m):
    a, b = list(map(int, input().split()))
    lst.append([a, b])
answer = 0
answer_1 = []
for i in range(n):
    B = A.copy()
    kek = []
    for j in range(m):
        a, b = lst[j][0], lst[j][1]
        if a <= i + 1 <= b:
            kek.append(j + 1)
            for q in range(a - 1, b):
                B[q] -= 1
    elem = max(B)
    if answer < elem - B[i]:
        answer = elem - B[i]
        answer_1 = kek.copy()
print(answer)
print(len(answer_1))
print(' '.join(map(str, answer_1)))

import sys

# a very nice implementation of a minimum segment tree with 
# some inspiration taken from https://codeforces.com/blog/entry/18051
# this implementation should be able to be modified to do pretty
# much anything one would want to do with segment trees apart from
# persistance.
# note that especially in python this implementation is much much better
# than most other approches because how slow python can be with function
# calls.

# currently it allows for two operations, both running in o(log n),
# 'add(l,r,value)' adds value to [l,r)
# 'find_min(l,r)' finds the index with the smallest value

class super_seg:
    def __init__(self,data):
        n = len(data)
        m = 1
        while m<n: m *= 2
        
        self.n = n
        self.m = m
        self.data = [0]*(2*m)
        for i in range(n):
            self.data[i+m] = data[i]
        for i in reversed(list(range(m))):
            self.data[i] = min(self.data[2*i], self.data[2*i+1])
        self.query = [0]*(2*m)
    
    # push the query on seg_ind to its children
    def push(self,seg_ind):
        # let the children know of the queries
        q = self.query[seg_ind]

        self.query[2*seg_ind]   += q
        self.query[2*seg_ind+1] += q
        
        self.data[2*seg_ind]   += q
        self.data[2*seg_ind+1] += q

        # remove queries from seg_ind
        self.data[seg_ind] = min(self.data[2*seg_ind],self.data[2*seg_ind+1])
        self.query[seg_ind] = 0

    # updates the node seg_ind to know of all queries
    # applied to it via its ancestors
    def update(self,seg_ind):
        # find all indecies to be updated
        seg_ind //= 2
        inds = []
        while seg_ind>0:
            inds.append(seg_ind)
            seg_ind//=2
       
        # push the queries down the segment tree
        for ind in reversed(inds):
            self.push(ind)

    # make the changes to seg_ind be known to its ancestors
    def build(self,seg_ind):
        seg_ind//=2
        while seg_ind>0:
            self.data[seg_ind] = min(self.data[2*seg_ind], self.data[2*seg_ind+1]) + self.query[seg_ind]
            seg_ind //= 2

    # lazily add value to [l,r)
    def add(self,l,r,value):
        l += self.m
        r += self.m
        
        l0 = l
        r0 = r

        while l<r:
            if l%2==1:
                self.query[l]+= value
                self.data[l] += value
                l+=1
            if r%2==1:
                r-=1
                self.query[r]+= value
                self.data[r] += value
            l//=2
            r//=2

        # tell all nodes above of the updated
        # area of the updates
        self.build(l0)
        self.build(r0-1)
    
    # min of data[l,r)
    def min(self,l,r):
        l += self.m
        r += self.m

        # apply all the lazily stored queries
        self.update(l)
        self.update(r-1)
        
        segs = []
        while l<r:
            if l%2==1:
                segs.append(l)
                l+=1
            if r%2==1:
                r-=1
                segs.append(r)
            l//=2
            r//=2

        return min(self.data[ind] for ind in segs)


    # find index of smallest value in data[l,r)
    def find_min(self,l,r):
        l += self.m
        r += self.m
        
        # apply all the lazily stored queries
        self.update(l)
        self.update(r-1)

        segs = []
        while l<r:
            if l%2==1:
                segs.append(l)
                l+=1
            if r%2==1:
                r-=1
                segs.append(r)
            l//=2
            r//=2

        ind = min(segs, key=lambda i:self.data[i])
        mini = self.data[ind]
       
        # dig down in search of mini
        while ind<self.m:
            self.push(ind)

            if self.data[2*ind]==mini:
                ind *= 2
            else:
                ind = 2*ind+1
        
        return ind-self.m,mini


n,m = [int(x) for x in input().split()]

A = [int(x) for x in input().split()]

inter = []
inter2 = []
for _ in range(m):
    l,r = [int(x) for x in input().split()]
    l -= 1
    inter.append((l,r))
    inter2.append((r,l))

inter_copy = inter[:]

inter.sort()
inter2.sort()

Aneg = super_seg([-a for a in A])

besta = -1
besta_ind = -1

j1 = 0
j2 = 0
for i in range(n):
    # Only segments containing i should be active
    # Activate
    while j1<m and inter[j1][0]<=i:
        l,r = inter[j1]
        Aneg.add(l,r,1)
        j1 += 1
    # Deactivate
    while j2<m and inter2[j2][0]<=i:
        r,l = inter2[j2]
        Aneg.add(l,r,-1)
        j2 += 1
    Amax = -Aneg.min(0,n)
    Ai = -Aneg.min(i,i+1)
    if Amax-Ai>besta:
        besta = Amax-Ai
        besta_ind = i

ints = [j for j in range(m) if inter_copy[j][0]<=besta_ind<inter_copy[j][1]]

print(besta)
print(len(ints))
print(*[x+1 for x in ints])


import sys

# a very nice implementation of a minimum segment tree with 
# some inspiration taken from https://codeforces.com/blog/entry/18051
# this implementation should be able to be modified to do pretty
# much anything one would want to do with segment trees apart from
# persistance.
# note that especially in python this implementation is much much better
# than most other approches because how slow python can be with function
# calls.

# currently it allows for two operations, both running in o(log n),
# 'add(l,r,value)' adds value to [l,r)
# 'find_min(l,r)' finds the index with the smallest value

class super_seg:
    def __init__(self,data):
        n = len(data)
        m = 1
        while m<n: m *= 2
        
        self.n = n
        self.m = m
        self.data = [0]*(2*m)
        for i in range(n):
            self.data[i+m] = data[i]
        for i in reversed(list(range(m))):
            self.data[i] = min(self.data[2*i], self.data[2*i+1])
        self.query = [0]*(2*m)
    
    # push the query on seg_ind to its children
    def push(self,seg_ind):
        # let the children know of the queries
        q = self.query[seg_ind]

        self.query[2*seg_ind]   += q
        self.query[2*seg_ind+1] += q
        
        self.data[2*seg_ind]   += q
        self.data[2*seg_ind+1] += q

        # remove queries from seg_ind
        self.data[seg_ind] = min(self.data[2*seg_ind],self.data[2*seg_ind+1])
        self.query[seg_ind] = 0

    # updates the node seg_ind to know of all queries
    # applied to it via its ancestors
    def update(self,seg_ind):
        # find all indecies to be updated
        seg_ind //= 2
        inds = []
        while seg_ind>0:
            inds.append(seg_ind)
            seg_ind//=2
       
        # push the queries down the segment tree
        for ind in reversed(inds):
            self.push(ind)

    # make the changes to seg_ind be known to its ancestors
    def build(self,seg_ind):
        seg_ind//=2
        while seg_ind>0:
            self.data[seg_ind] = min(self.data[2*seg_ind], self.data[2*seg_ind+1]) + self.query[seg_ind]
            seg_ind //= 2

    # lazily add value to [l,r)
    def add(self,l,r,value):
        l += self.m
        r += self.m
        
        l0 = l
        r0 = r

        while l<r:
            if l%2==1:
                self.query[l]+= value
                self.data[l] += value
                l+=1
            if r%2==1:
                r-=1
                self.query[r]+= value
                self.data[r] += value
            l//=2
            r//=2

        # tell all nodes above of the updated
        # area of the updates
        self.build(l0)
        self.build(r0-1)
    
    # min of data[l,r)
    def min(self,l,r):
        l += self.m
        r += self.m

        # apply all the lazily stored queries
        self.update(l)
        self.update(r-1)
        
        segs = []
        while l<r:
            if l%2==1:
                segs.append(l)
                l+=1
            if r%2==1:
                r-=1
                segs.append(r)
            l//=2
            r//=2

        return min(self.data[ind] for ind in segs)


    # find index of smallest value in data[l,r)
    def find_min(self,l,r):
        l += self.m
        r += self.m
        
        # apply all the lazily stored queries
        self.update(l)
        self.update(r-1)

        segs = []
        while l<r:
            if l%2==1:
                segs.append(l)
                l+=1
            if r%2==1:
                r-=1
                segs.append(r)
            l//=2
            r//=2

        ind = min(segs, key=lambda i:self.data[i])
        mini = self.data[ind]
       
        # dig down in search of mini
        while ind<self.m:
            self.push(ind)

            if self.data[2*ind]==mini:
                ind *= 2
            else:
                ind = 2*ind+1
        
        return ind-self.m,mini


n,m = [int(x) for x in input().split()]

A = [int(x) for x in input().split()]

inter = []
inter2 = []
for _ in range(m):
    l,r = [int(x) for x in input().split()]
    l -= 1
    inter.append((l,r))
    inter2.append((r,l))

inter_copy = inter[:]

inter.sort()
inter2.sort()

Aneg = super_seg([-a for a in A])

besta = -1
besta_ind = -1

j1 = 0
j2 = 0
for i in range(n):
    # Only segments containing i should be active
    # Activate
    while j1<m and inter[j1][0]<=i:
        l,r = inter[j1]
        Aneg.add(l,r,1)
        j1 += 1
    # Deactivate
    while j2<m and inter2[j2][0]<=i:
        r,l = inter2[j2]
        Aneg.add(l,r,-1)
        j2 += 1
    Amax = -Aneg.min(0,n)
    Ai = -Aneg.min(i,i+1)
    if Amax-Ai>besta:
        besta = Amax-Ai
        besta_ind = i

ints = [j for j in range(m) if inter_copy[j][0]<=besta_ind<inter_copy[j][1]]

print(besta)
print(len(ints))
print(*[x+1 for x in ints])


import sys

# a very nice implementation of a minimum segment tree with 
# some inspiration taken from https://codeforces.com/blog/entry/18051
# this implementation should be able to be modified to do pretty
# much anything one would want to do with segment trees apart from
# persistance.
# note that especially in python this implementation is much much better
# than most other approches because how slow python can be with function
# calls.

# currently it allows for two operations, both running in o(log n),
# 'add(l,r,value)' adds value to [l,r)
# 'find_min(l,r)' finds the index with the smallest value

big = 10**9

class super_seg:
    def __init__(self,data):
        n = len(data)
        m = 1
        while m<n: m *= 2
        
        self.n = n
        self.m = m
        self.data = [big]*(2*m)
        for i in range(n):
            self.data[i+m] = data[i]
        for i in reversed(list(range(m))):
            self.data[i] = min(self.data[2*i], self.data[2*i+1])
        self.query = [0]*(2*m)
    
    # push the query on seg_ind to its children
    def push(self,seg_ind):
        # let the children know of the queries
        q = self.query[seg_ind]

        self.query[2*seg_ind]   += q
        self.query[2*seg_ind+1] += q
        
        self.data[2*seg_ind]   += q
        self.data[2*seg_ind+1] += q

        # remove queries from seg_ind
        self.data[seg_ind] = min(self.data[2*seg_ind],self.data[2*seg_ind+1])
        self.query[seg_ind] = 0

    # updates the node seg_ind to know of all queries
    # applied to it via its ancestors
    def update(self,seg_ind):
        # find all indecies to be updated
        seg_ind //= 2
        inds = []
        while seg_ind>0:
            inds.append(seg_ind)
            seg_ind//=2
       
        # push the queries down the segment tree
        for ind in reversed(inds):
            self.push(ind)

    # make the changes to seg_ind be known to its ancestors
    def build(self,seg_ind):
        seg_ind//=2
        while seg_ind>0:
            self.data[seg_ind] = min(self.data[2*seg_ind], self.data[2*seg_ind+1]) + self.query[seg_ind]
            seg_ind //= 2

    # lazily add value to [l,r)
    def add(self,l,r,value):
        l += self.m
        r += self.m
        
        l0 = l
        r0 = r

        while l<r:
            if l%2==1:
                self.query[l]+= value
                self.data[l] += value
                l+=1
            if r%2==1:
                r-=1
                self.query[r]+= value
                self.data[r] += value
            l//=2
            r//=2

        # tell all nodes above of the updated
        # area of the updates
        self.build(l0)
        self.build(r0-1)
    
    # min of data[l,r)
    def min(self,l,r):
        l += self.m
        r += self.m

        # apply all the lazily stored queries
        self.update(l)
        self.update(r-1)
        
        segs = []
        while l<r:
            if l%2==1:
                segs.append(l)
                l+=1
            if r%2==1:
                r-=1
                segs.append(r)
            l//=2
            r//=2

        return min(self.data[ind] for ind in segs)


    # find index of smallest value in data[l,r)
    def find_min(self,l,r):
        l += self.m
        r += self.m
        
        # apply all the lazily stored queries
        self.update(l)
        self.update(r-1)

        segs = []
        while l<r:
            if l%2==1:
                segs.append(l)
                l+=1
            if r%2==1:
                r-=1
                segs.append(r)
            l//=2
            r//=2

        ind = min(segs, key=lambda i:self.data[i])
        mini = self.data[ind]
       
        # dig down in search of mini
        while ind<self.m:
            self.push(ind)

            if self.data[2*ind]==mini:
                ind *= 2
            else:
                ind = 2*ind+1
        
        return ind-self.m,mini


n,m = [int(x) for x in input().split()]

A = [int(x) for x in input().split()]

inter = []
inter2 = []
for _ in range(m):
    l,r = [int(x) for x in input().split()]
    l -= 1
    inter.append((l,r))
    inter2.append((r,l))

inter_copy = inter[:]

inter.sort()
inter2.sort()

Aneg = super_seg([-a for a in A])

besta = -1
besta_ind = -1

j1 = 0
j2 = 0
for i in range(n):
    # Only segments containing i should be active
    # Activate
    while j1<m and inter[j1][0]<=i:
        l,r = inter[j1]
        Aneg.add(l,r,1)
        j1 += 1
    # Deactivate
    while j2<m and inter2[j2][0]<=i:
        r,l = inter2[j2]
        Aneg.add(l,r,-1)
        j2 += 1
    Amax = -Aneg.data[1]
    Ai = A[i]-(j1-j2)
    if Amax-Ai>besta:
        besta = Amax-Ai
        besta_ind = i

ints = [i for i in range(m) if inter_copy[i][0]<=besta_ind<inter_copy[i][1]]

print(besta)
print(len(ints))
print(*[x+1 for x in ints])


import sys

# a very nice implementation of a minimum segment tree with 
# some inspiration taken from https://codeforces.com/blog/entry/18051
# this implementation should be able to be modified to do pretty
# much anything one would want to do with segment trees apart from
# persistance.
# note that especially in python this implementation is much much better
# than most other approches because how slow python can be with function
# calls.

# currently it allows for two operations, both running in o(log n),
# 'add(l,r,value)' adds value to [l,r)
# 'find_min(l,r)' finds the index with the smallest value

big = 10**9

class super_seg:
    def __init__(self,data):
        n = len(data)
        m = 1
        while m<n: m *= 2
        
        self.n = n
        self.m = m
        self.data = [big]*(2*m)
        for i in range(n):
            self.data[i+m] = data[i]
        for i in reversed(list(range(m))):
            self.data[i] = min(self.data[2*i], self.data[2*i+1])
        self.query = [0]*(2*m)
    
    # push the query on seg_ind to its children
    def push(self,seg_ind):
        # let the children know of the queries
        q = self.query[seg_ind]

        self.query[2*seg_ind]   += q
        self.query[2*seg_ind+1] += q
        
        self.data[2*seg_ind]   += q
        self.data[2*seg_ind+1] += q

        # remove queries from seg_ind
        self.data[seg_ind] = min(self.data[2*seg_ind],self.data[2*seg_ind+1])
        self.query[seg_ind] = 0

    # updates the node seg_ind to know of all queries
    # applied to it via its ancestors
    def update(self,seg_ind):
        # find all indecies to be updated
        seg_ind //= 2
        inds = []
        while seg_ind>0:
            inds.append(seg_ind)
            seg_ind//=2
       
        # push the queries down the segment tree
        for ind in reversed(inds):
            self.push(ind)

    # make the changes to seg_ind be known to its ancestors
    def build(self,seg_ind):
        seg_ind//=2
        while seg_ind>0:
            self.data[seg_ind] = min(self.data[2*seg_ind], self.data[2*seg_ind+1]) + self.query[seg_ind]
            seg_ind //= 2

    # lazily add value to [l,r)
    def add(self,l,r,value):
        l += self.m
        r += self.m
        
        l0 = l
        r0 = r

        while l<r:
            if l%2==1:
                self.query[l]+= value
                self.data[l] += value
                l+=1
            if r%2==1:
                r-=1
                self.query[r]+= value
                self.data[r] += value
            l//=2
            r//=2

        # tell all nodes above of the updated
        # area of the updates
        self.build(l0)
        self.build(r0-1)
    
    # min of data[l,r)
    def min(self,l,r):
        l += self.m
        r += self.m

        # apply all the lazily stored queries
        self.update(l)
        self.update(r-1)
        
        segs = []
        while l<r:
            if l%2==1:
                segs.append(l)
                l+=1
            if r%2==1:
                r-=1
                segs.append(r)
            l//=2
            r//=2

        return min(self.data[ind] for ind in segs)


    # find index of smallest value in data[l,r)
    def find_min(self,l,r):
        l += self.m
        r += self.m
        
        # apply all the lazily stored queries
        self.update(l)
        self.update(r-1)

        segs = []
        while l<r:
            if l%2==1:
                segs.append(l)
                l+=1
            if r%2==1:
                r-=1
                segs.append(r)
            l//=2
            r//=2

        ind = min(segs, key=lambda i:self.data[i])
        mini = self.data[ind]
       
        # dig down in search of mini
        while ind<self.m:
            self.push(ind)

            if self.data[2*ind]==mini:
                ind *= 2
            else:
                ind = 2*ind+1
        
        return ind-self.m,mini


n,m = [int(x) for x in input().split()]

A = [int(x) for x in input().split()]

inter = []
inter2 = []
for _ in range(m):
    l,r = [int(x) for x in input().split()]
    l -= 1
    inter.append((l,r))
    inter2.append((r,l))

inter_copy = inter[:]

inter.sort()
inter2.sort()

Aneg = super_seg([-a for a in A])

besta = -1
besta_ind = -1

j1 = 0
j2 = 0
for i in range(n):
    # Only segments containing i should be active
    # Activate
    while j1<m and inter[j1][0]<=i:
        l,r = inter[j1]
        Aneg.add(l,r,1)
        j1 += 1
    # Deactivate
    while j2<m and inter2[j2][0]<=i:
        r,l = inter2[j2]
        Aneg.add(l,r,-1)
        j2 += 1
    Amax = -Aneg.data[1]
    Ai = A[i]-(j1-j2)
    if Amax-Ai>besta:
        besta = Amax-Ai
        besta_ind = i

ints = [i for i in range(m) if inter_copy[i][0]<=besta_ind<inter_copy[i][1]]

print(besta)
print(len(ints))
print(*[x+1 for x in ints])


n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
if n > 300:
    lst = []
    for i in range(m):
        a, b = list(map(int, input().split()))
        lst.append([a, b])
    answer = 0
    answer_1 = []
    m300 = [[-1, -10**6]]
    for i in range(max(0, m-1)):
        m300.append([-1, -10**6])
    for u in range(n):
        if A[u] > min(m300)[1]:
            m300[m300.index(min(m300))] = [u, A[u]]
    for i, mi in m300:
        B = A.copy()
        kek = []
        for j in range(m):
            a, b = lst[j][0], lst[j][1]
            if a <= i + 1 <= b:
                kek.append(j + 1)
                for q in range(a - 1, b):
                    B[q] -= 1
        elem = max(B)
        if answer < elem - mi:
            answer = elem - mi
            answer_1 = kek.copy()
    print(answer)
    print(len(answer_1))
    print(' '.join(map(str, answer_1)))
else:
    lst = []
    for i in range(m):
        a, b = list(map(int, input().split()))
        lst.append([a, b])
    answer = 0
    answer_1 = []
    for i in range(n):
        B = A.copy()
        kek = []
        for j in range(m):
            a, b = lst[j][0], lst[j][1]
            if a <= i + 1 <= b:
                kek.append(j + 1)
                for q in range(a - 1, b):
                    B[q] -= 1
        elem = max(B)
        if answer < elem - B[i]:
            answer = elem - B[i]
            answer_1 = kek.copy()
    print(answer)
    print(len(answer_1))
    print(' '.join(map(str, answer_1)))