Go: package main

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

func main() {
	io := NewIo()
	defer io.Flush()
	seq := make([]int, 360)
	N := io.NextInt()
	for i := 0; i < N; i++ {
		M, D, V, S := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
		M = (M - 1) * 30
		D--
		for j := M + D; j < M+D+V; j++ {
			seq[j%360] = intMax(S, seq[j%360])
		}
		for j := 0; j < S; j++ {
			seq[(M+D-j+360)%360] = intMax(S-j, seq[(M+D-j+360)%360])
			seq[(M+D+V+j-1)%360] = intMax(S-j, seq[(M+D+V+j-1)%360])
		}
	}
	ans := 360
	for _, val := range seq {
		ans = intMin(val, ans)
	}
	io.Println(ans)
}

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

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

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <bits/stdc++.h>
using ll = long long;
// #define int ll
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, int(n) + 1)
#define rrep(i, n) RFOR(i, 0, n)
#define rrep1(i, n) RFOR(i, 1, int(n) + 1)
#define all(c) begin(c), end(c)
// const int MOD = 1000000007;

template <typename T>
void __print__(std::ostream &os, const char *, const char *tail, const T &fst) {
    os << fst << tail;
}
template <typename Fst, typename... Rst>
void __print__(std::ostream &os, const char *del, const char *tail, const Fst &fst,
               const Rst &... rst) {
    os << fst << del;
    __print__(os, del, tail, rst...);
}

#ifdef LOCAL
#define dump(...)                                         \
    do {                                                  \
        std::ostringstream os;                            \
        os << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; \
        __print__(os, ", ", "\n", __VA_ARGS__);           \
        std::cerr << os.str();                            \
    } while (0)
#else
#define dump(...)
#endif

template <typename Fst, typename... Rst>
void println(const Fst &fst, const Rst &... rst) {
    __print__(std::cout, "\n", "\n", fst, rst...);
}
template <typename Fst, typename... Rst>
void print(const Fst &fst, const Rst &... rst) {
    __print__(std::cout, " ", "\n", fst, rst...);
}

template <typename iter>
void println_(iter bgn, iter end) {
    while (bgn != end) println(*bgn++);
}

template <typename iter>
void print_(iter bgn, iter end) {
    while (bgn != end) {
        std::cout << *bgn++;
        std::cout << (bgn == end ? "\n" : " ");
    }
}

int _ = (std::cout.precision(10), std::cout.setf(std::ios::fixed), std::cin.tie(0),
         std::ios::sync_with_stdio(0), 0);

template <typename T>
std::vector<T> ndarray(int n, T v) {
    return std::vector<T>(n, v);
}
template <typename... Args>
auto ndarray(int n, Args... args) {
    auto val = ndarray(args...);
    return std::vector<decltype(val)>(n, move(val));
}

template <typename T>
bool umax(T &a, const T &b) {
    return a < b ? a = b, true : false;
}

template <typename T>
bool umin(T &a, const T &b) {
    return a > b ? a = b, true : false;
}

using namespace std;

int main() {
    int n;
    while (cin >> n) {
        vector<int> start(n), end(n), power(n);
        rep(i, n) {
            int m, d, v, s;
            cin >> m >> d >> v >> s;
            --m;
            --d;
            start[i] = m*30 + d;
            end[i] = start[i] + v - 1;
            power[i] = s;
        }

        const int Y = 30*12;
        int ans = 1e9;
        rep(d, Y) {
            int maxi = 0;
            rep(i, n) {
                int cand;
                int s = start[i], e = end[i];
                int s2 = s + Y, e2 = e - Y;
                if (s <= d && d <= e) {
                    // cout << "i";
                    cand = power[i];
                } else if (e + 1 <= d && d <= s2 - 1) {
                    // cout << "a";
                    cand = max(0, power[i] - min(d - e, s2 - d));
                } else {
                    // cout << "b";
                    cand = max(0, power[i] - min(d - e2, s - d));
                }
                maxi = max(maxi, cand);
            }
            // cout << maxi << ' ';
            ans = min(ans, maxi);
        }
        // cout << endl;
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int Start[100];
int s[100];
int End[100];
int v[100];

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

	int N;
	cin >> N;
	for(int i = 0; i < N; i++) {
		int M, D;
		cin >> M >> D >> v[i] >> s[i];
		Start[i] = 30 * (M - 1) + D;
		End[i] = Start[i] + v[i] - 1;
	}

	int ans = 1e9;
	for(int i = 0; i < 360; i++) {
		int x = 0;
		for(int j = 0; j < N; j++) {
			if(Start[j] <= i && i <= End[j]) {
				x = max(x, s[j]);
			}
			else if(Start[j] <= i + 360 && i + 360 <= End[j]) {
				x = max(x, s[j]);
			}
			else {
				int t1 = s[j] - min({ abs(Start[j] - i), abs(End[j] - i) });
				int t2 = s[j] - min({ abs(Start[j] + 360 - i), abs(End[j] + 360 - i) });
				x = max({ x, t1, t2 });
				/*if(i < Start[j]) {
					int t = s[j] - min(Start[j] - i, i + 360 - End[j]);
					x = max(x, t);
					}
					else {
					int t = s[j] - min(i - End[j], Start[j] + 360 - i);
					x = max(x, t);
					}*/
			}
		}

		ans = min(ans, x);
	}
	cout << ans << endl;
}

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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

int N;

int ma[360];

signed main(){
    cin>>N;
    while(N--){
        int m,d,v,s;
        cin>>m>>d>>v>>s;
        m--;d--;

        int l=m*30+d;
        int r=l+v-1;

        for(int i=-360*10;i<=360*10;i++){
            int tmp;
            if(i<l)tmp=s-(l-i);
            else if(l<=i&&i<=r)tmp=s;
            else tmp=s-(i-r);
            chmax(tmp,0ll);
            chmax(ma[(i+360*114)%360],tmp);
        }
    }

    cout<<*min_element(ma,ma+360)<<endl;
    return 0;
}