One way to create a task is to learn from life. You can choose some experience in real life, formalize it and then you will get a new task.Let's think about a scene in real life: there are lots of people waiting in front of the elevator, each person wants to go to a certain floor. We can formalize it in the following way. We have n people standing on the first floor, the i-th person wants to go to the fi-th floor. Unfortunately, there is only one elevator and its capacity equal to k (that is at most k people can use it simultaneously). Initially the elevator is located on the first floor. The elevator needs |a - b| seconds to move from the a-th floor to the b-th floor (we don't count the time the people need to get on and off the elevator).What is the minimal number of seconds that is needed to transport all the people to the corresponding floors and then return the elevator to the first floor?

The first line contains two integers n and k (1 ≤ n, k ≤ 2000) — the number of people and the maximal capacity of the elevator. The next line contains n integers: f1, f2, ..., fn (2 ≤ fi ≤ 2000), where fi denotes the target floor of the i-th person.

Output a single integer — the minimal time needed to achieve the goal.

NoteIn first sample, an optimal solution is:   The elevator takes up person #1 and person #2.  It goes to the 2nd floor.  Both people go out of the elevator.  The elevator goes back to the 1st floor.  Then the elevator takes up person #3.  And it goes to the 2nd floor.  It picks up person #2.  Then it goes to the 3rd floor.  Person #2 goes out.  Then it goes to the 4th floor, where person #3 goes out.  The elevator goes back to the 1st floor.

package main

import (
	"fmt"
)

const maxF = 2000

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	num := make([]int, maxF+1)
	for i := 0; i < n; i++ {
		var f int
		fmt.Scan(&f)
		num[f]++
	}
	cur := k
	ans := 0
	for i := maxF; i > 1; i-- {
		for cur+num[i] > k {
			ans += 2 * (i - 1)
			num[i] -= k - cur
			cur = 0
		}
		cur += num[i]
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, k int

	fmt.Scan(&n, &k)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
	ret := 0
	for i := 0; i < n; i += k {
		ret += 2 * (a[i] - 1)
	}
	fmt.Println(ret)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, k := nextInt(), nextInt()
	f := make([]int, 2001)
	lastFloor := 0
	second := 0

	for i := 0; i < n; i++ {
		f[nextInt() - 1]++
	}

	for i := 2000; i >= 0; i-- {
		if f[i] != 0 {
			// println("i:", i," second:", second," f[i]:", f[i], " lastFloor:", lastFloor, " f[lastFloor]:", f[lastFloor])
			if f[i] + f[lastFloor] < k {
				if f[lastFloor] == 0 {
					lastFloor = i
				} else {
					f[lastFloor] += f[i]
				}

			} else {
				if f[lastFloor] > 0 {
					f[i] -= k - f[lastFloor]
					second += lastFloor * 2
					// println(i, f[lastFloor])
				}

				second += (f[i] / k) * i * 2
				f[i] %= k
				lastFloor = i
			}

			// println("i:", i," second:", second," f[i]:", f[i], " lastFloor:", lastFloor, " f[lastFloor]:", f[lastFloor])
		}
	}

	// if f[lastFloor] > 0 {
	// 	second += 2
	// }

	if f[lastFloor] > 0 {
		second += lastFloor * 2
	}

	println(second)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	var n, k int
	if _, err := fmt.Scanf("%d %d\n", &n, &k); err != nil {
		return
	}

	f := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		f[i] = a
	}

	merge_sort(f, 0, n-1)

	time := 0
	for i := n - 1; i >= 0; i -= k {
		time += 2 * (f[i] - 1)
	}
	fmt.Println(time)
}

func merge(buffer []int, start, middle, end int) {
	sorted := make([]int, end-start+1)

	i := start
	j := middle + 1
	k := 0
	for i <= middle && j <= end {
		if buffer[i] <= buffer[j] {
			sorted[k] = buffer[i]
			i++
		} else {
			sorted[k] = buffer[j]
			j++
		}
		k++
	}
	for i <= middle {
		sorted[k] = buffer[i]
		k++
		i++
	}
	for j <= end {
		sorted[k] = buffer[j]
		k++
		j++
	}

	i = start
	k = 0
	for i <= end {
		buffer[i] = sorted[k]
		i++
		k++
	}
}

func merge_sort(buffer []int, start, end int) {
	if end > start {
		middle := (end + start) / 2
		merge_sort(buffer, start, middle)
		merge_sort(buffer, middle+1, end)
		merge(buffer, start, middle, end)
	}
}

package main

import (
	"bufio"
	"bytes"
	"errors"
	"io"
	"math/rand"
	"os"
	"strconv"
	"time"
	"unicode/utf8"
	"sort"
)

// ******************************** CODE STARTS ********************************

const ()

var (
)

func solve() {
	n := readInt()
	k := readInt()
	array := make([]int, n)
	for i := 0; i < n; i++ {
		array[i] = readInt()
	}
	sort.Sort(sort.IntSlice(array))
	dp := make([]int, n + 1)
	for p := n - 1; p >= 0; p-- {
		dp[p] = -1
		for i := 0; i < k && p + i < n; i++ {
			value := 2 * (array[p + i] - 1) + dp[p + i + 1]
			if dp[p] == -1 || dp[p] > value {
				dp[p] = value
			}
		}
	}
	printInt(dp[0])
	printLine()
}

// ********************************* CODE ENDS *********************************

// IO.

var (
	scanner *Scanner
	writer  *bufio.Writer
)

func main() {
	input := os.Stdin
	output := os.Stdout
	//	input, _ := os.Open("")
	//	output, _ := os.Create("")

	defer input.Close()
	defer output.Close()

	scanner = NewScanner(input)
	scanner.Split(bufio.ScanWords)

	writer = bufio.NewWriter(output)
	defer writer.Flush()

	rand.Seed(int64(time.Now().Nanosecond()))
	solve()
}

// Read.

func readString() string {
	scanner.Scan()
	ans := scanner.Text()
	return ans
}

func readLine() string {
	scanner.Split(bufio.ScanLines)
	defer scanner.Split(bufio.ScanWords)
	return readString()
}

func readInt() int {
	ans, _ := strconv.ParseInt(readString(), 10, 0)
	return int(ans)
}

func readInt64() int64 {
	ans, _ := strconv.ParseInt(readString(), 10, 64)
	return ans
}

func readFloat64() float64 {
	ans, _ := strconv.ParseFloat(readString(), 64)
	return ans
}

// Write.

func printString(value string) {
	writer.WriteString(value)
}

func printLine() {
	printString("\n")
}

func printSpace() {
	printString(" ")
}

func printInt(value int) {
	printString(strconv.FormatInt(int64(value), 10))
}

func printInt64(value int64) {
	printString(strconv.FormatInt(value, 10))
}

func printFloat64(value float64) {
	printString(strconv.FormatFloat(value, 'f', 20, 64))
}

// Tuple

type Tuple struct {
	a, b int
}

func (p Tuple) Equals(key Key) bool {
	return p.a == key.(Tuple).a
}

func (p Tuple) Less(key Key) bool {
	return p.a < key.(Tuple).a
}

type TupleSlice []Tuple

func (p TupleSlice) Len() int {
	return len(p)
}

func (p TupleSlice) Less(j, i int) bool {
	return p[j].a < p[i].a
}

func (p TupleSlice) Swap(j, i int) {
	p[j], p[i] = p[i], p[j]
}

// Min, max, abs.

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

func absInt(a int) int {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

func maxInt64(a, b int64) int64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt64(a, b int64) int64 {
	if a < b {
		return a
	} else {
		return b
	}
}

func absInt64(a int64) int64 {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

func maxFloat64(a, b float64) float64 {
	if a > b {
		return a
	} else {
		return b
	}
}

func minFloat64(a, b float64) float64 {
	if a < b {
		return a
	} else {
		return b
	}
}

func absFloat64(a float64) float64 {
	if a > 0 {
		return a
	} else {
		return -a
	}
}

// Set Key interface implementations.

type Int int

func (p Int) Equals(key Key) bool {
	return int(p) == int(key.(Int))
}

func (p Int) Less(key Key) bool {
	return int(p) < int(key.(Int))
}

// Set.

type Key interface {
	Equals(key Key) bool
	Less(key Key) bool
}

type node struct {
	key   Key
	left  *node
	right *node
}

func newNode(key Key) *node {
	return &node{key, nil, nil}
}

type SplayTree struct {
	root *node
	tmp  *node
	len  int
}

func NewSplayTree() *SplayTree {
	return &SplayTree{nil, newNode(nil), 0}
}

func rotateLeft(x, p *node) {
	p.right = x.left
	x.left = p
}

func rotateRight(x, p *node) {
	p.left = x.right
	x.right = p
}

func (p *SplayTree) splay(x *node, key Key) *node {
	if x == nil {
		return nil
	}

	left := p.tmp
	right := p.tmp

	for {
		if key.Less(x.key) {
			y := x.left
			if y == nil {
				break
			}
			if key.Less(y.key) { // zig-zig
				rotateRight(y, x)
				x = y
				if x.left == nil {
					break
				}
			}
			// link right
			right.left = x
			right = x
			// move left
			x = x.left
		} else if x.key.Less(key) {
			y := x.right
			if y == nil {
				break
			}
			if y.key.Less(key) { // zig-zig
				rotateLeft(y, x)
				x = y
				if x.right == nil {
					break
				}
			}
			// link left
			left.right = x
			left = x
			// move right
			x = x.right
		} else {
			break
		}
	}

	left.right = x.left
	right.left = x.right
	x.left = p.tmp.right
	x.right = p.tmp.left

	return x
}

// left <= key < right
func (p *SplayTree) split(key Key) (left, right *node) {
	p.root = p.splay(p.root, key)
	if p.root.key.Equals(key) || p.root.key.Less(key) {
		right := p.root.right
		p.root.right = nil
		return p.root, right
	} else {
		left := p.root.left
		p.root.left = nil
		return left, p.root
	}
}

// keys from left tree must be less then keys from right tree
func (p *SplayTree) join(left, right *node) *node {
	if left == nil {
		return right
	} else if right == nil {
		return left
	}
	left = p.splay(left, right.key)
	left.right = right
	return left
}

type Set interface {
	Len() int
	Insert(key Key) error
	Find(key Key) bool
	Remove(key Key) error
}

func NewSet() Set {
	return Set(NewSplayTree())
}

func (p *SplayTree) Len() int {
	return p.len
}

func (p *SplayTree) Insert(key Key) error {
	if p.root == nil {
		p.root = newNode(key)
		p.len++
	} else {
		p.root = p.splay(p.root, key)
		if p.root.key.Equals(key) {
			return errors.New("Such key already exists")
		} else {
			left, right := p.split(key)
			p.root = newNode(key)
			p.root.left = left
			p.root.right = right
			p.len++
		}
	}
	return nil
}

func (p *SplayTree) Find(key Key) bool {
	if p.root == nil {
		return false
	}
	p.root = p.splay(p.root, key)
	return p.root.key.Equals(key)
}

func (p *SplayTree) Remove(key Key) error {
	p.root = p.splay(p.root, key)
	if p.root == nil || !p.root.key.Equals(key) {
		return errors.New("Such key doesn't exist")
	}
	p.root = p.join(p.split(key))
	return nil
}

// Copied scan.go
// Scanner provides a convenient interface for reading data such as
// a file of newline-delimited lines of text. Successive calls to
// the Scan method will step through the 'tokens' of a file, skipping
// the bytes between the tokens. The specification of a token is
// defined by a split function of type SplitFunc; the default split
// function breaks the input into lines with line termination stripped. Split
// functions are defined in this package for scanning a file into
// lines, bytes, UTF-8-encoded runes, and space-delimited words. The
// client may instead provide a custom split function.
//
// Scanning stops unrecoverably at EOF, the first I/O error, or a token too
// large to fit in the buffer. When a scan stops, the reader may have
// advanced arbitrarily far past the last token. Programs that need more
// control over error handling or large tokens, or must run sequential scans
// on a reader, should use bufio.Reader instead.
//
type Scanner struct {
	r            io.Reader // The reader provided by the client.
	split        SplitFunc // The function to split the tokens.
	maxTokenSize int       // Maximum size of a token; modified by tests.
	token        []byte    // Last token returned by split.
	buf          []byte    // Buffer used as argument to split.
	start        int       // First non-processed byte in buf.
	end          int       // End of data in buf.
	err          error     // Sticky error.
}

// SplitFunc is the signature of the split function used to tokenize the
// input. The arguments are an initial substring of the remaining unprocessed
// data and a flag, atEOF, that reports whether the Reader has no more data
// to give. The return values are the number of bytes to advance the input
// and the next token to return to the user, plus an error, if any. If the
// data does not yet hold a complete token, for instance if it has no newline
// while scanning lines, SplitFunc can return (0, nil, nil) to signal the
// Scanner to read more data into the slice and try again with a longer slice
// starting at the same point in the input.
//
// If the returned error is non-nil, scanning stops and the error
// is returned to the client.
//
// The function is never called with an empty data slice unless atEOF
// is true. If atEOF is true, however, data may be non-empty and,
// as always, holds unprocessed text.
type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)

// Errors returned by Scanner.
var (
	ErrTooLong         = errors.New("bufio.Scanner: token too long")
	ErrNegativeAdvance = errors.New("bufio.Scanner: SplitFunc returns negative advance count")
	ErrAdvanceTooFar   = errors.New("bufio.Scanner: SplitFunc returns advance count beyond input")
)

const (
	// Maximum size used to buffer a token. The actual maximum token size
	// may be smaller as the buffer may need to include, for instance, a newline.
	MaxScanTokenSize = 1 << 20
)

// NewScanner returns a new Scanner to read from r.
// The split function defaults to ScanLines.
func NewScanner(r io.Reader) *Scanner {
	return &Scanner{
		r:            r,
		split:        ScanLines,
		maxTokenSize: MaxScanTokenSize,
		buf:          make([]byte, 4096), // Plausible starting size; needn't be large.
	}
}

// Err returns the first non-EOF error that was encountered by the Scanner.
func (s *Scanner) Err() error {
	if s.err == io.EOF {
		return nil
	}
	return s.err
}

// Bytes returns the most recent token generated by a call to Scan.
// The underlying array may point to data that will be overwritten
// by a subsequent call to Scan. It does no allocation.
func (s *Scanner) Bytes() []byte {
	return s.token
}

// Text returns the most recent token generated by a call to Scan
// as a newly allocated string holding its bytes.
func (s *Scanner) Text() string {
	return string(s.token)
}

// Scan advances the Scanner to the next token, which will then be
// available through the Bytes or Text method. It returns false when the
// scan stops, either by reaching the end of the input or an error.
// After Scan returns false, the Err method will return any error that
// occurred during scanning, except that if it was io.EOF, Err
// will return nil.
func (s *Scanner) Scan() bool {
	// Loop until we have a token.
	for {
		// See if we can get a token with what we already have.
		if s.end > s.start {
			advance, token, err := s.split(s.buf[s.start:s.end], s.err != nil)
			if err != nil {
				s.setErr(err)
				return false
			}
			if !s.advance(advance) {
				return false
			}
			s.token = token
			if token != nil {
				return true
			}
		}
		// We cannot generate a token with what we are holding.
		// If we've already hit EOF or an I/O error, we are done.
		if s.err != nil {
			// Shut it down.
			s.start = 0
			s.end = 0
			return false
		}
		// Must read more data.
		// First, shift data to beginning of buffer if there's lots of empty space
		// or space is neded.
		if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
			copy(s.buf, s.buf[s.start:s.end])
			s.end -= s.start
			s.start = 0
		}
		// Is the buffer full? If so, resize.
		if s.end == len(s.buf) {
			if len(s.buf) >= s.maxTokenSize {
				s.setErr(ErrTooLong)
				return false
			}
			newSize := len(s.buf) * 2
			if newSize > s.maxTokenSize {
				newSize = s.maxTokenSize
			}
			newBuf := make([]byte, newSize)
			copy(newBuf, s.buf[s.start:s.end])
			s.buf = newBuf
			s.end -= s.start
			s.start = 0
			continue
		}
		// Finally we can read some input. Make sure we don't get stuck with
		// a misbehaving Reader. Officially we don't need to do this, but let's
		// be extra careful: Scanner is for safe, simple jobs.
		for loop := 0; ; {
			n, err := s.r.Read(s.buf[s.end:len(s.buf)])
			s.end += n
			if err != nil {
				s.setErr(err)
				break
			}
			if n > 0 {
				break
			}
			loop++
			if loop > 100 {
				s.setErr(io.ErrNoProgress)
				break
			}
		}
	}
}

// advance consumes n bytes of the buffer. It reports whether the advance was legal.
func (s *Scanner) advance(n int) bool {
	if n < 0 {
		s.setErr(ErrNegativeAdvance)
		return false
	}
	if n > s.end-s.start {
		s.setErr(ErrAdvanceTooFar)
		return false
	}
	s.start += n
	return true
}

// setErr records the first error encountered.
func (s *Scanner) setErr(err error) {
	if s.err == nil || s.err == io.EOF {
		s.err = err
	}
}

// Split sets the split function for the Scanner. If called, it must be
// called before Scan. The default split function is ScanLines.
func (s *Scanner) Split(split SplitFunc) {
	s.split = split
}

// Split functions

// ScanBytes is a split function for a Scanner that returns each byte as a token.
func ScanBytes(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	return 1, data[0:1], nil
}

var errorRune = []byte(string(utf8.RuneError))

// ScanRunes is a split function for a Scanner that returns each
// UTF-8-encoded rune as a token. The sequence of runes returned is
// equivalent to that from a range loop over the input as a string, which
// means that erroneous UTF-8 encodings translate to U+FFFD = "\xef\xbf\xbd".
// Because of the Scan interface, this makes it impossible for the client to
// distinguish correctly encoded replacement runes from encoding errors.
func ScanRunes(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}

	// Fast path 1: ASCII.
	if data[0] < utf8.RuneSelf {
		return 1, data[0:1], nil
	}

	// Fast path 2: Correct UTF-8 decode without error.
	_, width := utf8.DecodeRune(data)
	if width > 1 {
		// It's a valid encoding. Width cannot be one for a correctly encoded
		// non-ASCII rune.
		return width, data[0:width], nil
	}

	// We know it's an error: we have width==1 and implicitly r==utf8.RuneError.
	// Is the error because there wasn't a full rune to be decoded?
	// FullRune distinguishes correctly between erroneous and incomplete encodings.
	if !atEOF && !utf8.FullRune(data) {
		// Incomplete; get more bytes.
		return 0, nil, nil
	}

	// We have a real UTF-8 encoding error. Return a properly encoded error rune
	// but advance only one byte. This matches the behavior of a range loop over
	// an incorrectly encoded string.
	return 1, errorRune, nil
}

// dropCR drops a terminal \r from the data.
func dropCR(data []byte) []byte {
	if len(data) > 0 && data[len(data)-1] == '\r' {
		return data[0 : len(data)-1]
	}
	return data
}

// ScanLines is a split function for a Scanner that returns each line of
// text, stripped of any trailing end-of-line marker. The returned line may
// be empty. The end-of-line marker is one optional carriage return followed
// by one mandatory newline. In regular expression notation, it is `\r?\n`.
// The last non-empty line of input will be returned even if it has no
// newline.
func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	if i := bytes.IndexByte(data, '\n'); i >= 0 {
		// We have a full newline-terminated line.
		return i + 1, dropCR(data[0:i]), nil
	}
	// If we're at EOF, we have a final, non-terminated line. Return it.
	if atEOF {
		return len(data), dropCR(data), nil
	}
	// Request more data.
	return 0, nil, nil
}

// isSpace reports whether the character is a Unicode white space character.
// We avoid dependency on the unicode package, but check validity of the implementation
// in the tests.
func isSpace(r rune) bool {
	if r <= '\u00FF' {
		// Obvious ASCII ones: \t through \r plus space. Plus two Latin-1 oddballs.
		switch r {
		case ' ', '\t', '\n', '\v', '\f', '\r':
			return true
		case '\u0085', '\u00A0':
			return true
		}
		return false
	}
	// High-valued ones.
	if '\u2000' <= r && r <= '\u200a' {
		return true
	}
	switch r {
	case '\u1680', '\u180e', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
		return true
	}
	return false
}

// ScanWords is a split function for a Scanner that returns each
// space-separated word of text, with surrounding spaces deleted. It will
// never return an empty string. The definition of space is set by
// unicode.IsSpace.
func ScanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	// Skip leading spaces.
	start := 0
	for width := 0; start < len(data); start += width {
		var r rune
		r, width = utf8.DecodeRune(data[start:])
		if !isSpace(r) {
			break
		}
	}
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}
	// Scan until space, marking end of word.
	for width, i := 0, start; i < len(data); i += width {
		var r rune
		r, width = utf8.DecodeRune(data[i:])
		if isSpace(r) {
			return i + width, data[start:i], nil
		}
	}
	// If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
	if atEOF && len(data) > start {
		return len(data), data[start:], nil
	}
	// Request more data.
	return 0, nil, nil
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	f := make([]int, n)
	for i := 0; i < n; i++ {
		var x int
		fmt.Scan(&x)
		f[i] = x - 1
	}
	sort.Ints(f)
	z, p := 0, n-1
	for p >= 0 {
		z += 2 * f[p]
		p -= k
	}
	fmt.Println(z)
}

package main

import (
    "bufio"
    "os"
    "sort"
    "strconv"
)

var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func ReadInt32() int {
    scanner.Scan()
    ans, _ := strconv.Atoi(scanner.Text())
    return ans
}

func main() {
    defer writer.Flush()
    scanner.Split(bufio.ScanWords)

    n := ReadInt32()
    k := ReadInt32()
    a := make([]int, n)
    ans := 0

    for i := 0; i < n; i++ {
        a[i] = ReadInt32()
    }

    sort.Ints(a)

    for i := n - 1; i >= 0; i -= k {
        ans += (a[i] - 1) * 2
    }
    writer.WriteString(strconv.Itoa(ans) + "\n")
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, k int
	fmt.Scan(&n, &k)

	fn := make([]int, n, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&fn[i])
	}
	sort.Sort(sort.Reverse(sort.IntSlice(fn)))

	ans := 0
	for i := 0; i < n; i += k {
		ans += 2 * (fn[i] - 1)
	}

	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, k := nextInt(), nextInt()
	f := make([]int, 2001)
	lastFloor := 0
	second := 0

	for i := 0; i < n; i++ {
		f[nextInt() - 1]++
	}

	for i := 2000; i >= 0; i-- {
		if f[i] != 0 {
			// println("i:", i," second:", second," f[i]:", f[i], " lastFloor:", lastFloor, " f[lastFloor]:", f[lastFloor])
			if f[i] + f[lastFloor] < k {
				f[i] += f[lastFloor]
			} else {
				if f[lastFloor] > 0 {
					f[i] -= k - f[lastFloor]
					second += lastFloor * 2
				}

				second += (f[i] / k) * i * 2
				f[i] %= k
			}

			lastFloor = i
			// println("i:", i," second:", second," f[i]:", f[i], " lastFloor:", lastFloor, " f[lastFloor]:", f[lastFloor])
		}
	}

	if f[lastFloor] > 0 {
		second += 2
	}

	println(second)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, k := nextInt(), nextInt()
	f := make([]int, 2001)
	lastFloor := 0
	second := 0

	for i := 0; i < n; i++ {
		f[nextInt() - 1]++
	}

	for i := 2000; i >= 0; i-- {
		if f[i] != 0 {
			println("i:", i," second:", second," f[i]:", f[i], " lastFloor:", lastFloor, " f[lastFloor]:", f[lastFloor])
			if f[i] + f[lastFloor] < k {
				if f[lastFloor] == 0 {
					lastFloor = i
				} else {
					f[lastFloor] += f[i]
				}

				println(i)
			} else {
				if f[lastFloor] > 0 {
					f[i] -= k - f[lastFloor]
					second += lastFloor * 2
					println(i, f[lastFloor])
				}

				second += (f[i] / k) * i * 2
				f[i] %= k
				lastFloor = i
			}

			println("i:", i," second:", second," f[i]:", f[i], " lastFloor:", lastFloor, " f[lastFloor]:", f[lastFloor])
		}
	}

	// if f[lastFloor] > 0 {
	// 	second += 2
	// }

	if f[lastFloor] > 0 {
		second += lastFloor * 2
	}

	println(second)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(next())
	if err != nil {
		panic(err)
	}
	return n
}

func nextFloat() float64 {
	n, err := strconv.ParseFloat(next(), 64)
	if err != nil {
		panic(err)
	}
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, k int
	fmt.Scan(&n, &k)

	fn := make([]int, n, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&fn[i])
	}
	sort.Sort(sort.Reverse(sort.IntSlice(fn)))
	fmt.Println(fn)

	ans := 0
	for i := 0; i < n; i += k {
		ans += 2 * (fn[i] - 1)
	}

	fmt.Println(ans)
}


Dmitry has $$$n$$$ segments of different colors on the coordinate axis $$$Ox$$$. Each segment is characterized by three integers $$$l_i$$$, $$$r_i$$$ and $$$c_i$$$ ($$$1 \le l_i \le r_i \le 10^9, 1 \le c_i \le n$$$), where $$$l_i$$$ and $$$r_i$$$ are are the coordinates of the ends of the $$$i$$$-th segment, and $$$c_i$$$ is its color.Dmitry likes to find the minimum distances between segments. However, he considers pairs of segments of the same color uninteresting. Therefore, he wants to know for each segment the distance from this segment to the nearest differently colored segment.The distance between two segments is the minimum of the distances between a point of the first segment and a point of the second segment. In particular, if the segments intersect, then the distance between them is equal to $$$0$$$.For example, Dmitry has $$$5$$$ segments:     The first segment intersects with the second (and these are segments of different colors), so the answers for them are equal to $$$0$$$.  For the $$$3$$$-rd segment, the nearest segment of a different color is the $$$2$$$-nd segment, the distance to which is equal to $$$2$$$.  For the $$$4$$$-th segment, the nearest segment of a different color is the $$$5$$$-th segment, the distance to which is equal to $$$1$$$.  The $$$5$$$-th segment lies inside the $$$2$$$-nd segment (and these are segments of different colors), so the answers for them are equal to $$$0$$$.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of description of each test case contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of segments. The next $$$n$$$ lines contain descriptions of the segments. Each segment is described by three integers $$$l_i$$$, $$$r_i$$$ and $$$c_i$$$ ($$$1 \le l_i \le r_i \le 10^9, 1 \le c_i \le n$$$) — coordinates of the left and right ends of $$$i$$$-th segment, as well as the color of this segment. It is guaranteed that there are at least $$$2$$$ segments of different colors. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, on a separate line print $$$n$$$ integers, where the $$$i$$$-th number is equal to the distance from the $$$i$$$-th segment to the nearest segment of a different color.

NoteIn the first test case of the first sample there is only one segment of color $$$2$$$, and all other segments have color $$$1$$$. Therefore, for segments of color $$$1$$$, the answer is equal to the distance to the $$$3$$$rd segment, and for $$$3$$$rd one, the answer is equal to the minimum of the distances to segments of color $$$1$$$.In the second test case of the first sample there are only $$$2$$$ segments, and for both of them the answer is equal to the distance between them.In the third test case of the first sample, each segment intersects at least one of its ends with a segment of a different color, so all answers are equal to $$$0$$$.The fourth test case of the first sample is described in the problem statement.In the fifth test case of the first sample, one segment lies completely inside the other, and for both of them the answer is $$$0$$$.In the sixth test case of the first sample, all segments are points of different colors.

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type Seg struct {
	l int
	r int
	c int
	i int
}

func min(a, b int) int {
	if a < b { return a }
	return b
}

func bsearch(a []Seg, l, r, v int) int {
	if (l == r) { return l }
	m := (l + r + 1) >> 1
	if a[m].l > v { return bsearch(a, l, m-1, v) }
	return bsearch(a, m, r, v)
}

func solve(a []Seg, dist *[]int) {
	c := make([][]Seg, len(a))

	for i, s := range a {
		if i == 0 {
			c[i] = []Seg{Seg{-2e10, -2e10, -1, -1}, s}
			continue
		}

		c[i] = []Seg{c[i-1][0], c[i-1][1]}
		same_clr := s.c != c[i][1].c
		if s.r >= c[i][1].r {
			if same_clr { c[i][0] = c[i][1] }
			c[i][1] = s
		} else if s.r >= c[i][0].r && same_clr {
			c[i][0] = s
		}
	}

	for _, s := range a {
		j := bsearch(a, 0, len(a)-1, s.r)
		i := 1
		if c[j][1].c == s.c { i = 0 }
		if s.l > c[j][i].r {
			(*dist)[s.i] = min((*dist)[s.i], s.l - c[j][i].r)
		} else {
			(*dist)[s.i] = 0
		}
	}
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		fmt.Fscan(r, &n)

		a := make([]Seg, n)
		b := make([]Seg, n)
		dist := make([]int, n)

		for i := range a {
			var x, y, z int
			fmt.Fscan(r, &x, &y, &z)
			a[i] = Seg{x, y, z, i}
			b[i] = Seg{-y, -x, z, i}
			dist[i] = 1e10 + 7
		}

		sort.Slice(a, func(i, j int) bool { return a[i].l < a[j].l })
		sort.Slice(b, func(i, j int) bool { return b[i].l < b[j].l })

		solve(a, &dist)
		solve(b, &dist)

		for _, d := range dist {
			fmt.Fprintf(w, "%d ", d)
		}
		fmt.Fprintln(w, "")
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type Seg struct {
	l int
	r int
	c int
	i int
}

func min(a, b int) int {
	if a < b { return a }
	return b
}

func bsearch(a []Seg, l, r, v int) int {
	if (l == r) { return l }
	m := (l + r + 1) >> 1
	if a[m].l > v { return bsearch(a, l, m-1, v) }
	return bsearch(a, m, r, v)
}

func solve(a []Seg, dist *[]int) {
	c := make([][]Seg, len(a))

	for i, s := range a {
		if i == 0 {
			c[i] = []Seg{Seg{-2e10, -2e10, -1, -1}, s}
			// fmt.Println("&&&&&&&&&&& i, c ", i, c[i]) // ***
			continue
		}

		c[i] = []Seg{c[i-1][0], c[i-1][1]}
		same_clr := s.c != c[i][1].c
		if s.r >= c[i][1].r {
			if same_clr { c[i][0] = c[i][1] }
			c[i][1] = s
		} else if s.r >= c[i][0].r && same_clr {
			c[i][0] = s
		}
		// fmt.Println("&&&&&&&&&&& i, c ", i, c[i]) // ***
	}

	for _, s := range a {
		j := bsearch(a, 0, len(a)-1, s.r)
		i := 1
		if c[j][1].c == s.c { i = 0 }
		// fmt.Println("---------- c[j] ", c[j]) // ***
		// fmt.Println("---------- s ", s) // ***
		// fmt.Println("---------- j i s.l c[j][i].r ", j, i, s.l, c[j][i].r) // ***
		if s.l > c[j][i].r {
			(*dist)[s.i] = min((*dist)[s.i], s.l - c[j][i].r)
		} else {
			(*dist)[s.i] = 0
		}
	}
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		fmt.Fscan(r, &n)

		a := make([]Seg, n)
		b := make([]Seg, n)
		dist := make([]int, n)

		for i := range a {
			var x, y, z int
			fmt.Fscan(r, &x, &y, &z)
			a[i] = Seg{x, y, z, i}
			b[i] = Seg{-y, -x, z, i}
			dist[i] = 1e10 + 7
		}

		sort.Slice(a, func(i, j int) bool { return a[i].l < a[j].l })
		sort.Slice(b, func(i, j int) bool { return b[i].l < b[j].l })

		/*
		fmt.Fprintln(w, "********** sorted a")
		for _, v := range a {
			fmt.Fprintln(w, "----------- ", v)
		}

		fmt.Fprintln(w, "********** sorted b")
		for _, v := range b {
			fmt.Fprintln(w, "----------- ", v)
		}
		*/

		solve(a, &dist)
		solve(b, &dist)

		for _, d := range dist {
			fmt.Fprintf(w, "%d ", d)
		}
		fmt.Fprintln(w, "")
	}

}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type Seg struct {
	l int
	r int
	c int
	i int
}

func min(a, b int) int {
	if a < b { return a }
	return b
}

func solve(a []Seg, dist *[]int) {
	c := make([]Seg, 2)
	c[0] = a[0]
	for _, s := range a {
		if s.c != c[0].c {
			c[1] = s
			break
		}
	}

	for _, s := range a {
		i := 1
		if s.c == c[1].c { i = 0 }

		if c[i].l <= s.r {
			if s.l > c[i].r {
				(*dist)[s.i] = min((*dist)[s.i], s.l - c[i].r)
			} else {
				(*dist)[s.i] = 0
			}

			if s.r >= c[1].r {
				if i == 1 { c[0] = c[1] }
				c[1] = s
			} else if s.r >= c[0].r && i == 1 {
				c[0] = s
			}
		}

		/*
		fmt.Println("@@@@@@@@@@ c ", c)
		fmt.Println("########## d ", *dist)
		*/
	}
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		fmt.Fscan(r, &n)

		a := make([]Seg, n)
		b := make([]Seg, n)
		dist := make([]int, n)

		for i := range a {
			var x, y, z int
			fmt.Fscan(r, &x, &y, &z)
			a[i] = Seg{x, y, z, i}
			b[i] = Seg{-y, -x, z, i}
			dist[i] = 1e10 + 7
		}

		sort.Slice(a, func(i, j int) bool { return a[i].l < a[j].l })
		sort.Slice(b, func(i, j int) bool { return b[i].l < b[j].l })

		/*
		fmt.Fprintln(w, "********** sorted a")
		for _, v := range a {
			fmt.Fprintln(w, "----------- ", v)
		}

		fmt.Fprintln(w, "********** sorted b")
		for _, v := range b {
			fmt.Fprintln(w, "----------- ", v)
		}
		*/

		solve(a, &dist)
		solve(b, &dist)

		for _, d := range dist {
			fmt.Fprintf(w, "%d ", d)
		}
		fmt.Fprintln(w, "")
	}

}


Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.Consider two infinite sets of numbers. The first set consists of odd positive numbers ($$$1, 3, 5, 7, \ldots$$$), and the second set consists of even positive numbers ($$$2, 4, 6, 8, \ldots$$$). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage — the first two numbers from the second set, on the third stage — the next four numbers from the first set, on the fourth — the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another. The ten first written numbers: $$$1, 2, 4, 3, 5, 7, 9, 6, 8, 10$$$. Let's number the numbers written, starting with one.The task is to find the sum of numbers with numbers from $$$l$$$ to $$$r$$$ for given integers $$$l$$$ and $$$r$$$. The answer may be big, so you need to find the remainder of the division by $$$1000000007$$$ ($$$10^9+7$$$).Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.

The first line contains two integers $$$l$$$ and $$$r$$$ ($$$1 \leq l \leq r \leq 10^{18}$$$) — the range in which you need to find the sum.

Print a single integer — the answer modulo $$$1000000007$$$ ($$$10^9+7$$$).

NoteIn the first example, the answer is the sum of the first three numbers written out ($$$1 + 2 + 4 = 7$$$).In the second example, the numbers with numbers from $$$5$$$ to $$$14$$$: $$$5, 7, 9, 6, 8, 10, 12, 14, 16, 18$$$. Their sum is $$$105$$$.

package main

import (
	"bufio"
	"fmt"
	"os"
)

const (
	mod = uint64(1e9 + 7)
)

type slot struct {
	from uint64
	num  uint64
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	l, r := uint64(0), uint64(0)
	fmt.Fscanf(in, "%d %d\n", &l, &r)
	slots := make([]slot, 0)
	slots = append(slots, slot{1, 1})
	oddStart := uint64(3)
	evenStart := uint64(2)
	until := uint64(1)
	curNum := uint64(1)
	evenTurn := true
	for until < r {
		curNum *= 2
		until += curNum
		if evenTurn {
			evenEnd := evenStart + 2*(curNum-1)
			slots = append(slots, slot{evenStart, curNum})
			evenStart = evenEnd + 2
		} else {
			oddEnd := oddStart + 2*(curNum-1)
			slots = append(slots, slot{oddStart, curNum})
			oddStart = oddEnd + 2
		}
		evenTurn = !evenTurn
	}
	count := uint64(0)
	sum := uint64(0)
	//fmt.Println(slots)
	for i := 0; i < len(slots); i++ {
		// if count >= r {
		// 	fmt.Println("wtf")
		// 	break
		// }
		sumHere := uint64(0)
		// smallID := count + 1
		// bigID := count + slots[i].num
		// from := max(smallID, l)
		// to := min(bigID, r)
		// if from > to {
		// 	continue
		// }
		// num := to - from + 1
		// first := slots[i].from + 2*(from-smallID-1)
		// last := first + 2*(num-1)
		// last %= mod
		// sumd2 := (first + last) / 2
		// num %= mod
		// sumd2 *= num
		// sumd2 %= mod
		// sum += sumd2
		// sum %= mod
		if count+1 >= l {
			if count+slots[i].num <= r {
				first := slots[i].from
				end := first + 2*(slots[i].num-1)
				f1 := (first + end) / 2
				f1 %= mod
				f2 := slots[i].num
				f2 %= mod
				f1 *= f2
				f1 %= mod
				sumHere = f1
			} else {
				num := r - count
				first := slots[i].from
				end := first + 2*(num-1)
				f1 := (first + end) / 2
				f1 %= mod
				f2 := num
				f2 %= mod
				f1 *= f2
				f1 %= mod
				sumHere = f1
			}
		} else {
			if count+slots[i].num >= l {
				if count+slots[i].num <= r {
					num := count + slots[i].num - l + 1
					first := slots[i].from + 2*(l-count-1)
					end := first + 2*(num-1)
					f1 := (first + end) / 2
					f1 %= mod
					num %= mod
					f1 *= num
					f1 %= mod
					sumHere = f1
				} else {
					num := r - l + 1
					first := slots[i].from + 2*(l-count-1)
					end := first + 2*(num-1)
					f1 := (first + end) / 2
					f1 %= mod
					num %= mod
					f1 *= num
					f1 %= mod
					sumHere = f1
				}
			}
		}
		sum += sumHere
		sum %= mod
		count += slots[i].num
	}
	fmt.Fprintf(out, "%d\n", sum)
	return
}

func max(a, b uint64) uint64 {
	if a < b {
		return b
	}
	return a
}

func min(a, b uint64) uint64 {
	if a > b {
		return b
	}
	return a
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func getLine() string {
	s, _ := reader.ReadString('\n')
	if strings.HasSuffix(s, "\r\n") {
		s = s[:len(s)-2]
	} else if strings.HasSuffix(s, "\n") {
		s = s[:len(s)-1]
	}
	return s
}
func getLine2Int() (n int) {
	scanf("%d\n", &n)
	return
}
func getLine2Int2() (a, b int) {
	scanf("%d %d\n", &a, &b)
	return
}
func getLine2Ints(n int) []int {
	ans := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &ans[i])
	}
	scanf("%d\n", &ans[n-1])
	return ans
}

var debug = false

func see(a interface{}) {
	if debug {
		printf("DEBUG:%v\n", a)
	}
}

var mod = int64(1000000007)

func add(a, b int64) int64 {
	return (a%mod + b%mod) % mod
}

type oenode struct {
	index int64
	first int64
	last  int64
}

func (e oenode) Len() int64 {
	return (e.last-e.first)/2 + 1
}

func (e oenode) Sum() int64 {
	return (((e.first + e.last) % (mod * 2)) * (e.Len() % (mod * 2)) / 2) % mod
}

func (e oenode) headSum(last int64) int64 {
	var t oenode
	t.first = e.first
	t.last = last
	return t.Sum()
}

func (e oenode) midSum(first, last int64) int64 {
	var t oenode
	t.first = first
	t.last = last
	return t.Sum()
}

func (e oenode) footSum(first int64) int64 {
	var t oenode
	t.first = first
	t.last = e.last
	return t.Sum()
}

func (e oenode) get(index int64) int64 {
	return e.first + index*2
}

var data = make([]oenode, 0)

func main() {
	defer writer.Flush()

	// init
	isOdd := true
	oddNow := int64(1)
	evenNow := int64(2)
	index := int64(1)
	for i := int64(1); i <= 1000000000000000000; i *= 2 {
		var t oenode
		if isOdd {
			t.first = oddNow
			oddNow += i * 2
			t.last = oddNow - 2
		} else {
			t.first = evenNow
			evenNow += i * 2
			t.last = evenNow - 2
		}
		t.index = index
		data = append(data, t)
		index += i
		isOdd = !isOdd
	}

	// input
	var n, m int64
	scanf("%d %d\n", &n, &m)

	// solve
	status := 0
	ans := int64(0)
	ok := false
	for i := 0; i < len(data) && !ok; i++ {
		t := data[i]
		switch status {
		case 0:
			if n < t.index+t.Len() {
				if m < t.index+t.Len() {
					ans = t.midSum(t.get(n-t.index), t.get(m-t.index))
					ok = true
					break
				}
				ans = add(ans, t.footSum(t.get(n-t.index)))
				status = 1
			}
		case 1:
			if m < t.index+t.Len() {
				ans = add(ans, t.headSum(t.get(m-t.index)))
				ok = true
				break
			}
			ans = add(ans, t.Sum())
		}
	}

	// output
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"math/big"
	"os"
	"strconv"
)

var N = big.NewInt(1000000007)
var Q = big.NewInt(4)
var D = big.NewInt(2)
var B0 = big.NewInt(0)
var B1 = big.NewInt(1)
var B2 = big.NewInt(2)
var B3 = big.NewInt(3)

// calculate the last number of the arithmetic progression
func c_li(f_i, n, d *big.Int) *big.Int {
	return new(big.Int).Add(f_i, new(big.Int).Mul(d, new(big.Int).Sub(n, B1)))
}

// calculate the first number of the arithmetic progression
func c_fi(l_i, n, d *big.Int) *big.Int {
	return new(big.Int).Sub(l_i, new(big.Int).Mul(d, new(big.Int).Sub(n, B1)))
}

// calculate the arithmetic progression sum
func APSum(f_i, l_i, d *big.Int) *big.Int {
	a := new(big.Int).Add(f_i, l_i)
	b := new(big.Int).Div(new(big.Int).Sub(l_i, f_i), d)
	b = new(big.Int).Add(b, B1)
	return new(big.Int).Div(new(big.Int).Mul(a, b), B2)
}

func seqSumEven(x *big.Int) (res *big.Int) {
	if x.Cmp(B1) == 0 {
		return B1
	}
	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	i2_u := new(big.Int).Sub(B2, new(big.Int).Mul(B2, new(big.Int).Exp(Q, lx.Div(lx, B2), nil)))
	i2_d := new(big.Int).Sub(B1, Q)
	i2 := new(big.Int).Mul(i2_u.Div(i2_u, i2_d), B2)
	if i2.Cmp(x) < 0 { // Section B
		n := new(big.Int).Sub(x, i2)
		i2a1 := new(big.Int).Add(i2, B1)
		l_i := c_li(i2a1, n, D)
		res = new(big.Int).Add(APSum(B1, i2, B1), APSum(i2a1, l_i, D))
	} else { // Section A
		n := new(big.Int).Sub(i2, x)
		i2s1 := new(big.Int).Sub(i2, B1)
		f_i := c_fi(i2s1, n, D)
		res = new(big.Int).Sub(APSum(B1, i2, B1), APSum(f_i, i2s1, D))
	}
	return
}

func seqSumOdd(x *big.Int) (res *big.Int) {
	if x.Cmp(B2) == 0 {
		return B3
	}
	if x.Cmp(B3) == 0 {
		return big.NewInt(7)
	}

	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	i1_u := new(big.Int).Sub(B1, new(big.Int).Exp(Q, lx.Div(new(big.Int).Add(lx, B1), B2), nil))
	i1_d := new(big.Int).Sub(B1, Q)
	i1 := new(big.Int).Mul(i1_u.Div(i1_u, i1_d), B2)

	if i1.Cmp(x) < 0 { // Section B
		n := new(big.Int).Sub(x, i1)
		i1a2 := new(big.Int).Add(i1, B2)
		l_i := c_li(i1a2, n, D)
		res = new(big.Int).Add(APSum(B1, i1, B1), APSum(i1a2, l_i, D))
	} else { // Section A
		n := new(big.Int).Sub(i1, x)
		f_i := c_fi(i1, n, D)
		res = new(big.Int).Sub(APSum(B1, i1, B1), APSum(f_i, i1, D))
	}
	return
}

// SeqSum returns the sum of the sequence from the first element to the xth element
func SeqSum(x *big.Int) (res *big.Int) {
	if x.Cmp(B0) == 0 {
		return B0
	}
	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	if lx.Mod(lx, B2).Cmp(B0) == 0 {
		res = seqSumEven(x)
	} else {
		res = seqSumOdd(x)
	}

	return res
}

func solve(l, r int64) {
	rr := SeqSum(big.NewInt(r))
	lr := SeqSum(big.NewInt(l - 1))
	res := new(big.Int).Mod(rr.Sub(rr, lr), N)
	fmt.Println(res)
}

func ScanNum(s *bufio.Scanner) (int64, error) {
	s.Scan()
	// old version atoi default set to int32 instead of int64
	return strconv.ParseInt(s.Text(), 10, 64)
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)

	l, _ := ScanNum(s)
	r, _ := ScanNum(s)

	// l, _ := strconv.Atoi(os.Args[1])
	// r, _ := strconv.Atoi(os.Args[2])

	if l > r {
		panic(fmt.Errorf("Invalid range"))
	}
	solve(int64(l), int64(r))
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1151C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	const mod int64 = 1e9 + 7
	f := func(n int64) (res int64) {
		var s, o, e, st int64
		odd := true
		for i := int64(1); s < n; i <<= 1 {
			if odd {
				st = 2*o + 1
				o = (o + i) % mod
			} else {
				st = 2*e + 2
				e = (e + i) % mod
			}
			if s+i > n {
				i = n - s
			}
			s += i
			res += i % mod * (st + i%mod - 1) % mod
			odd = !odd
		}
		return
	}

	var l, r int64
	Fscan(in, &l, &r)
	Fprint(out, ((f(r)-f(l-1))%mod+mod)%mod)
}

func main() { CF1151C(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"os"
)

const mod = int(1e9) + 7
var L int64
var R int64

func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func oddSum(X int64) int {
	var cnt = (X + 1) / 2
	cnt %= int64(mod)
	return int(cnt * cnt % int64(mod))

}

func evenSum(X int64) int {
	var cnt = X / 2
	var cntp = cnt + 1
	cnt %= int64(mod)
	cntp %= int64(mod)
	return int(cnt * cntp % int64(mod))
}

func oddRange(L int64, R int64) int {
	var tot = (oddSum(R) - oddSum(L - 1)) % mod
	if tot < 0 {
		tot += mod
	}
	return tot
}

func evenRange(L int64, R int64) int {
	var tot = (evenSum(R) - evenSum(L - 1)) % mod
	if tot < 0 {
		tot += mod
	}
	return tot
}

func comp(X int64) int {
	if X == 0 {
		return 0
	}

	var tot = 0
	var lastOdd = int64(-1)
	var lastEven = int64(0)

	var turn = 0

	var sz = int64(1)

	for true {
		sz = min(sz, X)
		X -= sz
		if turn == 0 {
			tot += oddRange(lastOdd + 2, lastOdd + 2 * sz)
			tot %= mod
			lastOdd += 2 * sz
		} else {
			tot += evenRange(lastEven + 2, lastEven + 2 * sz)
			tot %= mod
			lastEven += 2 * sz
		}
		turn = 1 - turn
		sz *= 2

		if X == 0 {
			break
		}
	}
	return tot
}

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	fmt.Fscanf(reader, "%d %d\n", &L, &R)
	var ret = (comp(R) - comp(L - 1)) % mod
	if ret < 0 {
		ret += mod
	}
	fmt.Fprintf(writer, "%d", ret)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const (
	mod = uint64(1e9 + 7)
)

type slot struct {
	from uint64
	num  uint64
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	l, r := uint64(0), uint64(0)
	fmt.Fscanf(in, "%d %d\n", &l, &r)
	slots := make([]slot, 0)
	slots = append(slots, slot{1, 1})
	oddStart := uint64(3)
	evenStart := uint64(2)
	until := uint64(1)
	curNum := uint64(1)
	evenTurn := true
	for until < r {
		curNum *= 2
		until += curNum
		if evenTurn {
			evenEnd := evenStart + 2*(curNum-1)
			slots = append(slots, slot{evenStart, curNum})
			evenStart = evenEnd + 2
		} else {
			oddEnd := oddStart + 2*(curNum-1)
			slots = append(slots, slot{oddStart, curNum})
			oddStart = oddEnd + 2
		}
		evenTurn = !evenTurn
	}
	count := uint64(0)
	sum := uint64(0)
	//fmt.Println(slots)
	for i := 0; i < len(slots); i++ {
		if count >= r {
			break
		}
		sumHere := uint64(0)
		if count+1 >= l {
			if count+slots[i].num <= r {
				first := slots[i].from
				end := first + 2*(slots[i].num-1)
				f1 := (first + end) / 2
				f1 %= mod
				f2 := slots[i].num
				f1 *= f2
				f1 %= mod
				sumHere = f1
			} else {
				num := r - count
				first := slots[i].from
				end := first + 2*(num-1)
				f1 := (first + end) / 2
				f1 %= mod
				f2 := num
				f1 *= f2
				f1 %= mod
				sumHere = f1
			}
		} else {
			if count+slots[i].num >= l {
				if count+slots[i].num <= r {
					num := count + slots[i].num - l + 1
					first := slots[i].from + 2*(l-count-1)
					end := first + 2*(num-1)
					f1 := (first + end) / 2
					f1 %= mod
					f1 *= num
					f1 %= mod
					sumHere = f1
				} else {
					num := r - l + 1
					first := slots[i].from + 2*(l-count-1)
					end := first + 2*(num-1)
					f1 := (first + end) / 2
					f1 %= mod
					f1 *= num
					f1 %= mod
					sumHere = f1
				}
			}
		}
		sum += sumHere
		sum %= mod
		count += slots[i].num
	}
	fmt.Fprintf(out, "%d\n", sum)
	return
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }

func getLine() string {
	s, _ := reader.ReadString('\n')
	if strings.HasSuffix(s, "\r\n") {
		s = s[:len(s)-2]
	} else if strings.HasSuffix(s, "\n") {
		s = s[:len(s)-1]
	}
	return s
}
func getLine2Int() (n int) {
	scanf("%d\n", &n)
	return
}
func getLine2Int2() (a, b int) {
	scanf("%d %d\n", &a, &b)
	return
}
func getLine2Ints(n int) []int {
	ans := make([]int, n)
	for i := 0; i < n; i++ {
		scanf("%d ", &ans[i])
	}
	scanf("%d\n", &ans[n-1])
	return ans
}

var debug = false

func see(a interface{}) {
	if debug {
		printf("DEBUG:%v\n", a)
	}
}

var mod = int64(1000000007)

func add(a, b int64) int64 {
	return (a%mod + b%mod) % mod
}

type oenode struct {
	index int64
	first int64
	last  int64
}

func (e oenode) Len() int64 {
	return (e.last-e.first)/2 + 1
}

func (e oenode) Sum() int64 {
	return (((e.first + e.last) % (mod * 2)) * (e.Len() % (mod * 2)) / 2) % mod
}

func (e oenode) headSum(last int64) int64 {
	var t oenode
	t.first = e.first
	t.last = last
	return t.Sum()
}

func (e oenode) midSum(first, last int64) int64 {
	var t oenode
	t.first = first
	t.last = last
	return t.Sum()
}

func (e oenode) footSum(first int64) int64 {
	var t oenode
	t.first = first
	t.last = e.last
	return t.Sum()
}

func (e oenode) get(index int64) int64 {
	return e.first + index*2
}

var data = make([]oenode, 0)

func main() {
	defer writer.Flush()

	// init
	isOdd := true
	oddNow := int64(1)
	evenNow := int64(2)
	index := int64(1)
	for i := int64(1); i <= 1000000000000000000; i *= 2 {
		var t oenode
		if isOdd {
			t.first = oddNow
			oddNow += i * 2
			t.last = oddNow - 2
		} else {
			t.first = evenNow
			evenNow += i * 2
			t.last = evenNow - 2
		}
		t.index = index
		data = append(data, t)
		index += i
		isOdd = !isOdd
	}

	// input
	var n, m int64
	scanf("%d %d\n", &n, &m)

	// solve
	status := 0
	ans := int64(0)
	ok := false
	for i := 0; i < len(data) && !ok; i++ {
		t := data[i]
		switch status {
		case 0:
			if n <= t.index+t.Len() {
				if m <= t.index+t.Len() {
					ans = t.midSum(t.get(n-t.index), t.get(m-t.index))
					ok = true
					break
				}
				ans = add(ans, t.footSum(t.get(n-t.index)))
				status = 1
			}
		case 1:
			if m <= t.index+t.Len() {
				ans = add(ans, t.headSum(t.get(m-t.index)))
				ok = true
				break
			}
			ans = add(ans, t.Sum())
		}
	}

	// output
	printf("%d\n", ans)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"math/big"
	"os"
	"strconv"
)

var N = big.NewInt(1000000007)
var Q = big.NewInt(4)
var D = big.NewInt(2)
var B0 = big.NewInt(0)
var B1 = big.NewInt(1)
var B2 = big.NewInt(2)
var B3 = big.NewInt(3)

// calculate the last number of the arithmetic progression
func c_li(f_i, n, d *big.Int) *big.Int {
	return new(big.Int).Add(f_i, new(big.Int).Mul(d, new(big.Int).Sub(n, B1)))
}

// calculate the first number of the arithmetic progression
func c_fi(l_i, n, d *big.Int) *big.Int {
	return new(big.Int).Sub(l_i, new(big.Int).Mul(d, new(big.Int).Sub(n, B1)))
}

// calculate the arithmetic progression sum
func APSum(f_i, l_i, d *big.Int) *big.Int {
	a := new(big.Int).Add(f_i, l_i)
	b := new(big.Int).Div(new(big.Int).Sub(l_i, f_i), d)
	b = new(big.Int).Add(b, B1)
	return new(big.Int).Div(new(big.Int).Mul(a, b), B2)
}

func seqSumEven(x *big.Int) (res *big.Int) {
	if x.Cmp(B1) == 0 {
		return B1
	}
	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	i2_u := new(big.Int).Sub(B2, new(big.Int).Mul(B2, new(big.Int).Exp(Q, lx.Div(lx, B2), nil)))
	i2_d := new(big.Int).Sub(B1, Q)
	i2 := new(big.Int).Mul(i2_u.Div(i2_u, i2_d), B2)
	if i2.Cmp(x) < 0 { // Section B
		n := new(big.Int).Sub(x, i2)
		i2a1 := new(big.Int).Add(i2, B1)
		l_i := c_li(i2a1, n, D)
		res = new(big.Int).Add(APSum(B1, i2, B1), APSum(i2a1, l_i, D))
	} else { // Section A
		n := new(big.Int).Sub(i2, x)
		i2s1 := new(big.Int).Sub(i2, B1)
		f_i := c_fi(i2s1, n, D)
		res = new(big.Int).Sub(APSum(B1, i2, B1), APSum(f_i, i2s1, D))
	}
	return
}

func seqSumOdd(x *big.Int) (res *big.Int) {
	if x.Cmp(B2) == 0 {
		return B3
	}
	if x.Cmp(B3) == 0 {
		return big.NewInt(7)
	}

	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	i1_u := new(big.Int).Sub(B1, new(big.Int).Exp(Q, lx.Div(new(big.Int).Add(lx, B1), B2), nil))
	i1_d := new(big.Int).Sub(B1, Q)
	i1 := new(big.Int).Mul(i1_u.Div(i1_u, i1_d), B2)

	if i1.Cmp(x) < 0 { // Section B
		n := new(big.Int).Sub(x, i1)
		i1a2 := new(big.Int).Add(i1, B2)
		l_i := c_li(i1a2, n, D)
		res = new(big.Int).Add(APSum(B1, i1, B1), APSum(i1a2, l_i, D))
	} else { // Section A
		n := new(big.Int).Sub(i1, x)
		f_i := c_fi(i1, n, D)
		res = new(big.Int).Sub(APSum(B1, i1, B1), APSum(f_i, i1, D))
	}
	return
}

// SeqSum returns the sum of the sequence from the first element to the xth element
func SeqSum(x *big.Int) (res *big.Int) {
	if x.Cmp(B0) == 0 {
		return B0
	}
	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	if lx.Mod(lx, B2).Cmp(B0) == 0 {
		res = seqSumEven(x)
	} else {
		res = seqSumOdd(x)
	}

	return res
}

func solve(l, r int64) {
	rr := SeqSum(big.NewInt(r))
	lr := SeqSum(big.NewInt(l - 1))
	res := new(big.Int).Mod(rr.Sub(rr, lr), N)
	fmt.Println(res)
}

func ScanNum(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)

	l, _ := ScanNum(s)
	r, _ := ScanNum(s)
	fmt.Printf("l:%d r:%d\n", l, r)

	// l, _ := strconv.Atoi(os.Args[1])
	// r, _ := strconv.Atoi(os.Args[2])

	if l > r {
		panic(fmt.Errorf("Invalid range"))
	}
	solve(int64(l), int64(r))
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"math/big"
	"os"
	"strconv"
)

var N = big.NewInt(1000000007)
var Q = big.NewInt(4)
var D = big.NewInt(2)
var B0 = big.NewInt(0)
var B1 = big.NewInt(1)
var B2 = big.NewInt(2)
var B3 = big.NewInt(3)

// calculate the last number of the arithmetic progression
func c_li(f_i, n, d *big.Int) *big.Int {
	return new(big.Int).Add(f_i, new(big.Int).Mul(d, new(big.Int).Sub(n, B1)))
}

// calculate the first number of the arithmetic progression
func c_fi(l_i, n, d *big.Int) *big.Int {
	return new(big.Int).Sub(l_i, new(big.Int).Mul(d, new(big.Int).Sub(n, B1)))
}

// calculate the arithmetic progression sum
func APSum(f_i, l_i, d *big.Int) *big.Int {
	a := new(big.Int).Add(f_i, l_i)
	b := new(big.Int).Div(new(big.Int).Sub(l_i, f_i), d)
	b = new(big.Int).Add(b, B1)
	return new(big.Int).Div(new(big.Int).Mul(a, b), B2)
}

func seqSumEven(x *big.Int) (res *big.Int) {
	if x.Cmp(B1) == 0 {
		return B1
	}
	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	i2_u := new(big.Int).Sub(B2, new(big.Int).Mul(B2, new(big.Int).Exp(Q, lx.Div(lx, B2), nil)))
	i2_d := new(big.Int).Sub(B1, Q)
	i2 := new(big.Int).Mul(i2_u.Div(i2_u, i2_d), B2)
	if i2.Cmp(x) < 0 { // Section B
		n := new(big.Int).Sub(x, i2)
		i2a1 := new(big.Int).Add(i2, B1)
		l_i := c_li(i2a1, n, D)
		res = new(big.Int).Add(APSum(B1, i2, B1), APSum(i2a1, l_i, D))
	} else { // Section A
		n := new(big.Int).Sub(i2, x)
		i2s1 := new(big.Int).Sub(i2, B1)
		f_i := c_fi(i2s1, n, D)
		res = new(big.Int).Sub(APSum(B1, i2, B1), APSum(f_i, i2s1, D))
	}
	return
}

func seqSumOdd(x *big.Int) (res *big.Int) {
	if x.Cmp(B2) == 0 {
		return B3
	}
	if x.Cmp(B3) == 0 {
		return big.NewInt(7)
	}

	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	i1_u := new(big.Int).Sub(B1, new(big.Int).Exp(Q, lx.Div(new(big.Int).Add(lx, B1), B2), nil))
	i1_d := new(big.Int).Sub(B1, Q)
	i1 := new(big.Int).Mul(i1_u.Div(i1_u, i1_d), B2)

	if i1.Cmp(x) < 0 { // Section B
		n := new(big.Int).Sub(x, i1)
		i1a2 := new(big.Int).Add(i1, B2)
		l_i := c_li(i1a2, n, D)
		res = new(big.Int).Add(APSum(B1, i1, B1), APSum(i1a2, l_i, D))
	} else { // Section A
		n := new(big.Int).Sub(i1, x)
		f_i := c_fi(i1, n, D)
		res = new(big.Int).Sub(APSum(B1, i1, B1), APSum(f_i, i1, D))
	}
	return
}

// SeqSum returns the sum of the sequence from the first element to the xth element
func SeqSum(x *big.Int) (res *big.Int) {
	if x.Cmp(B0) == 0 {
		return B0
	}
	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	if lx.Mod(lx, B2).Cmp(B0) == 0 {
		res = seqSumEven(x)
	} else {
		res = seqSumOdd(x)
	}

	return res
}

func solve(l, r int64) {
	rr := SeqSum(big.NewInt(r))
	lr := SeqSum(big.NewInt(l - 1))
	res := new(big.Int).Mod(rr.Sub(rr, lr), N)
	fmt.Println(res)
}

func ScanNum(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)

	l, _ := ScanNum(s)
	r, _ := ScanNum(s)

	// l, _ := strconv.Atoi(os.Args[1])
	// r, _ := strconv.Atoi(os.Args[2])

	if l > r {
		panic(fmt.Errorf("Invalid range"))
	}
	solve(int64(l), int64(r))
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

const (
	N = 1000000007
	Q = 4
	D = 2
)

// calculate the last number of the arithmetic progression
func c_li(f_i, n, d float64) float64 {
	return f_i + (n-1)*d
}

// calculate the first number of the arithmetic progression
func c_fi(l_i, n, d float64) float64 {
	return l_i - (n-1)*d
}

// calculate the arithmetic progression sum
func APSum(f_i, l_i, d float64) float64 {
	// res := math.Mod((f_i+l_i)*((l_i-f_i)/d+1)/2, N)
	// fmt.Println(uint(res))
	return (f_i + l_i) * ((l_i-f_i)/d + 1) / 2
}

func seqSumEven(x float64) (res float64) {
	if x == 1 {
		return 1
	}

	lx := int(math.Log2(x))
	i2 := ((2 - 2*math.Pow(Q, float64(lx/2))) / (1 - Q)) * 2
	if i2 < x { // section B
		n := x - i2
		l_i := c_li(i2+1, n, D)
		res = APSum(1, i2, 1) + APSum(i2+1, l_i, D)
	} else { // section A
		n := i2 - x
		f_i := c_fi(i2-1, n, D)
		res = APSum(1, i2, 1) - APSum(f_i, i2-1, D)
	}
	return
}

func seqSumOdd(x float64) (res float64) {
	if x == 2 {
		return 3
	}
	if x == 3 {
		return 7
	}

	lx := int(math.Log2(x))
	i1_1 := ((1 - math.Pow(Q, float64((lx+1)/2))) / (1 - Q)) * 2
	if i1_1 < x { // section B
		n := x - i1_1
		l_i := c_li(i1_1+2, n, D)
		fmt.Println(l_i)
		res = APSum(1, i1_1, 1) + APSum(i1_1+2, l_i, D)
	} else { // section A
		n := i1_1 - x
		f_i := c_fi(i1_1, n, D)
		res = APSum(1, i1_1, 1) - APSum(f_i, i1_1, D)
	}
	return
}

// SeqSum returns the sum of the sequence from the first element to the xth element
func SeqSum(x float64) (res float64) {
	if x == 0 {
		return 0
	}
	lx := int(math.Log2(x))
	if lx%2 == 0 {
		res = seqSumEven(x)
	} else {
		res = seqSumOdd(x)
	}

	return res
}

func solve(l, r int) {
	res := SeqSum(float64(r)) - SeqSum(float64(l-1))
	fmt.Println(int(math.Mod(res, N)))
}

func ScanNum(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)

	l, _ := ScanNum(s)
	r, _ := ScanNum(s)

	if l > r {
		panic(fmt.Errorf("Invalid range"))
	}
	solve(l, r)
	// fmt.Println(seqSumEven(123))
	// fmt.Println(seqSumEven(122))
	// fmt.Println(seqSumEven(12))
	// fmt.Println(seqSumEven(14))
	// fmt.Println(seqSumEven(99999999998))
	// fmt.Println(seqSumEven(99999999999))
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"math/big"
	"os"
	"strconv"
)

var N = big.NewInt(1000000007)
var Q = big.NewInt(4)
var D = big.NewInt(2)
var B0 = big.NewInt(0)
var B1 = big.NewInt(1)
var B2 = big.NewInt(2)
var B3 = big.NewInt(3)

// calculate the last number of the arithmetic progression
func c_li(f_i, n, d *big.Int) *big.Int {
	return new(big.Int).Add(f_i, new(big.Int).Mul(d, new(big.Int).Sub(n, B1)))
}

// calculate the first number of the arithmetic progression
func c_fi(l_i, n, d *big.Int) *big.Int {
	return new(big.Int).Sub(l_i, new(big.Int).Mul(d, new(big.Int).Sub(n, B1)))
}

// calculate the arithmetic progression sum
func APSum(f_i, l_i, d *big.Int) *big.Int {
	a := new(big.Int).Add(f_i, l_i)
	b := new(big.Int).Div(new(big.Int).Sub(l_i, f_i), d)
	b = new(big.Int).Add(b, B1)
	return new(big.Int).Div(new(big.Int).Mul(a, b), B2)
}

func seqSumEven(x *big.Int) (res *big.Int) {
	if x.Cmp(B1) == 0 {
		return B1
	}
	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	i2_u := new(big.Int).Sub(B2, new(big.Int).Mul(B2, new(big.Int).Exp(Q, lx.Div(lx, B2), nil)))
	i2_d := new(big.Int).Sub(B1, Q)
	i2 := new(big.Int).Mul(i2_u.Div(i2_u, i2_d), B2)
	if i2.Cmp(x) < 0 { // Section B
		n := new(big.Int).Sub(x, i2)
		i2a1 := new(big.Int).Add(i2, B1)
		l_i := c_li(i2a1, n, D)
		res = new(big.Int).Add(APSum(B1, i2, B1), APSum(i2a1, l_i, D))
	} else { // Section A
		n := new(big.Int).Sub(i2, x)
		i2s1 := new(big.Int).Sub(i2, B1)
		f_i := c_fi(i2s1, n, D)
		res = new(big.Int).Sub(APSum(B1, i2, B1), APSum(f_i, i2s1, D))
	}
	return
}

func seqSumOdd(x *big.Int) (res *big.Int) {
	if x.Cmp(B2) == 0 {
		return B3
	}
	if x.Cmp(B3) == 0 {
		return big.NewInt(7)
	}

	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	i1_u := new(big.Int).Sub(B1, new(big.Int).Exp(Q, lx.Div(new(big.Int).Add(lx, B1), B2), nil))
	i1_d := new(big.Int).Sub(B1, Q)
	i1 := new(big.Int).Mul(i1_u.Div(i1_u, i1_d), B2)

	if i1.Cmp(x) < 0 { // Section B
		n := new(big.Int).Sub(x, i1)
		i1a2 := new(big.Int).Add(i1, B2)
		l_i := c_li(i1a2, n, D)
		res = new(big.Int).Add(APSum(B1, i1, B1), APSum(i1a2, l_i, D))
	} else { // Section A
		n := new(big.Int).Sub(i1, x)
		f_i := c_fi(i1, n, D)
		res = new(big.Int).Sub(APSum(B1, i1, B1), APSum(f_i, i1, D))
	}
	return
}

// SeqSum returns the sum of the sequence from the first element to the xth element
func SeqSum(x *big.Int) (res *big.Int) {
	if x.Cmp(B0) == 0 {
		return B0
	}
	lx := big.NewInt(int64(math.Log2(float64(x.Int64()))))
	if lx.Mod(lx, B2).Cmp(B0) == 0 {
		res = seqSumEven(x)
	} else {
		res = seqSumOdd(x)
	}

	return res
}

func solve(l, r int64) {
	rr := SeqSum(big.NewInt(r))
	lr := SeqSum(big.NewInt(l - 1))
	res := new(big.Int).Mod(rr.Sub(rr, lr), N)
	fmt.Println(res)
}

func ScanNum(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)

	l, _ := ScanNum(s)
	r, _ := ScanNum(s)
	if l > 123123123 {
		fmt.Printf("l:%d r:%d\n", l, r)
	}

	// l, _ := strconv.Atoi(os.Args[1])
	// r, _ := strconv.Atoi(os.Args[2])

	if l > r {
		panic(fmt.Errorf("Invalid range"))
	}
	solve(int64(l), int64(r))
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

const (
	N = 1000000007
	Q = 4
	D = 2
)

// calculate the last number of the arithmetic progression
func c_li(f_i, n, d float64) float64 {
	return f_i + (n-1)*d
}

// calculate the first number of the arithmetic progression
func c_fi(l_i, n, d float64) float64 {
	return l_i - (n-1)*d
}

// calculate the arithmetic progression sum
func APSum(f_i, l_i, d float64) float64 {
	// res := math.Mod((f_i+l_i)*((l_i-f_i)/d+1)/2, N)
	// fmt.Println(uint(res))
	return (f_i + l_i) * ((l_i-f_i)/d + 1) / 2
}

func seqSumEven(x float64) (res float64) {
	if x == 1 {
		return 1
	}

	lx := int(math.Log2(x))
	i2 := ((2 - 2*math.Pow(Q, float64(lx/2))) / (1 - Q)) * 2
	if i2 < x { // section B
		n := x - i2
		l_i := c_li(i2+1, n, D)
		res = APSum(1, i2, 1) + APSum(i2+1, l_i, D)
	} else { // section A
		n := i2 - x
		f_i := c_fi(i2-1, n, D)
		res = APSum(1, i2, 1) - APSum(f_i, i2-1, D)
	}
	return
}

func seqSumOdd(x float64) (res float64) {
	if x == 2 {
		return 3
	}
	if x == 3 {
		return 7
	}

	lx := int(math.Log2(x))
	i1_1 := ((1 - math.Pow(Q, float64((lx+1)/2))) / (1 - Q)) * 2
	if i1_1 < x { // section B
		n := x - i1_1
		l_i := c_li(i1_1+2, n, D)
		// fmt.Println(l_i)
		res = APSum(1, i1_1, 1) + APSum(i1_1+2, l_i, D)
	} else { // section A
		n := i1_1 - x
		f_i := c_fi(i1_1, n, D)
		res = APSum(1, i1_1, 1) - APSum(f_i, i1_1, D)
	}
	return
}

// SeqSum returns the sum of the sequence from the first element to the xth element
func SeqSum(x float64) (res float64) {
	if x == 0 {
		return 0
	}
	lx := int(math.Log2(x))
	if lx%2 == 0 {
		res = seqSumEven(x)
	} else {
		res = seqSumOdd(x)
	}

	return res
}

func solve(l, r int) {
	res := SeqSum(float64(r)) - SeqSum(float64(l-1))
	fmt.Println(int(math.Mod(res, N)))
}

func ScanNum(s *bufio.Scanner) (int, error) {
	s.Scan()
	return strconv.Atoi(s.Text())
}

func main() {
	s := bufio.NewScanner(bufio.NewReader(os.Stdin))
	s.Split(bufio.ScanWords)

	l, _ := ScanNum(s)
	r, _ := ScanNum(s)

	if l > r {
		panic(fmt.Errorf("Invalid range"))
	}
	solve(l, r)
	// fmt.Println(seqSumEven(123))
	// fmt.Println(seqSumEven(122))
	// fmt.Println(seqSumEven(12))
	// fmt.Println(seqSumEven(14))
	// fmt.Println(seqSumEven(99999999998))
	// fmt.Println(seqSumEven(99999999999))
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const mod = int(1e9) + 7
var L, R int64

func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func oddSum(X int64) int {
	var cnt = (X + 1) / 2
	cnt %= int64(mod)
	return int(cnt * cnt % int64(mod))

}

func evenSum(X int64) int {
	var cnt = X / 2
	var cntp = cnt + 1
	cnt %= int64(mod)
	cntp %= int64(mod)
	return int(cnt * cntp % int64(mod))
}

func oddRange(L int64, R int64) int {
	var tot = (oddSum(R) - oddSum(L - 1)) % mod
	if tot < 0 {
		tot += mod
	}
	return tot
}

func evenRange(L int64, R int64) int {
	var tot = (evenSum(R) - evenSum(L - 1)) % mod
	if tot < 0 {
		tot += mod
	}
	return tot
}

func comp(X int64) int {
	if X == 0 {
		return 0
	}

	var tot = 0
	var lastOdd = int64(-1)
	var lastEven = int64(0)

	var turn = 0

	var sz = int64(1)

	for true {
		sz = min(sz, X)
		X -= sz
		// odd
		if turn == 0 {
			tot += oddRange(lastOdd + 2, lastOdd + 2 * sz)
			//fmt.Println("[", lastOdd + 2, lastOdd + 2 * sz, "]")
			lastOdd += 2 * sz
		} else {
			tot += evenRange(lastEven + 2, lastEven + 2 * sz)
			//fmt.Println("[", lastEven + 2, lastEven + 2 * sz, "]")
			lastEven += 2 * sz
		}
		turn = 1 - turn
		sz *= 2

		if X == 0 {
			break
		}
	}
	return tot
}

func main() {
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	fmt.Fscanf(reader, "%d %d\n", &L, &R)
	var ret = (comp(R) - comp(L - 1)) % mod
	if ret < 0 {
		ret += mod
	}
	fmt.Fprintf(writer, "%d", ret)
}